Diana Picus | 052b7d3 | 2017-11-24 16:19:41 +0100 | [diff] [blame] | 1 | from subprocess import CalledProcessError |
| 2 | from subprocess import check_output |
| 3 | from subprocess import STDOUT |
| 4 | |
Diana Picus | 9a68e7b | 2017-12-19 18:00:02 +0100 | [diff] [blame] | 5 | import re |
| 6 | |
| 7 | from linaropy.git.worktree import Worktree |
Diana Picus | 052b7d3 | 2017-11-24 16:19:41 +0100 | [diff] [blame] | 8 | |
| 9 | class CommandPrinter(object): |
| 10 | """Command consumer that just prints the commands that it receives.""" |
| 11 | |
| 12 | def consume(self, command, directory): |
| 13 | print("{}$ {}".format(directory, ' '.join(command))) |
| 14 | |
| 15 | |
| 16 | class CommandRunner(object): |
| 17 | """Command consumer that runs the commands that it receives.""" |
| 18 | |
| 19 | def consume(self, command, directory): |
| 20 | """ |
| 21 | Run the given command in the given directory and print the stdout and |
| 22 | stderr. If an exception is thrown while running the command, it will be |
| 23 | rethrown as a RuntimeError. |
| 24 | """ |
| 25 | # FIXME: This prints the results after the command has finished running. |
| 26 | # For long-running commands (e.g. an LLVM build) we'll want live |
| 27 | # output. |
| 28 | try: |
| 29 | print(str(check_output(command, stderr=STDOUT, cwd=directory), 'utf-8')) |
| 30 | except CalledProcessError as exc: |
| 31 | raise RuntimeError( |
| 32 | "Error while running command\n{}".format(str(exc.output, 'utf-8'))) from exc |
Diana Picus | 5ad5542 | 2017-12-14 17:57:10 +0100 | [diff] [blame] | 33 | |
| 34 | |
| 35 | # FIXME: repo.pushToBranch doesn't work, because it doesn't handle remote |
| 36 | # branches properly. Furthermore, there's no support for getting the remote URL, |
| 37 | # so we need to resort to raw git commands. We may also consider moving the |
| 38 | # functionality for parsing info out of the remote URL into the repo object. |
| 39 | from sh import git |
| 40 | from linaropy.cd import cd |
| 41 | |
| 42 | |
| 43 | def get_user_from_remote(url): |
| 44 | """Get the username used as part of the remote URL, or None. |
| 45 | |
| 46 | The remote URLs that we expect to see look like $protocol://$user@$location. |
| 47 | If they look any different, we won't be able to parse them. |
| 48 | """ |
| 49 | pattern = re.compile("(.*://)?(?P<user>.*)@(.*)\n?") |
| 50 | match = pattern.match(str(url)) |
| 51 | if match is None: |
| 52 | return None |
| 53 | return match.group('user') |
| 54 | |
| 55 | |
| 56 | def get_remote_branch(repo, local_branch): |
| 57 | """ |
| 58 | Get the name of the remote branch corresponding to the given local branch. |
| 59 | """ |
| 60 | with cd(repo.repodir): |
| 61 | remote = git("remote", "get-url", "--push", "origin").strip() |
| 62 | user = get_user_from_remote(remote) |
| 63 | if not user: |
| 64 | raise EnvironmentError("Couldn't parse user from {}.".format(remote)) |
| 65 | |
| 66 | remote_branch = "linaro-local/{}/{}".format(user, local_branch) |
| 67 | if not repo.is_valid_branch_name(remote_branch): |
| 68 | raise EnvironmentError( |
| 69 | "{} is not a valid branch name.".format(remote_branch)) |
| 70 | |
| 71 | return remote_branch |
| 72 | |
| 73 | |
| 74 | def push_branch(proj, local_branch, remote_branch, pathToRepo): |
| 75 | """Push the given local branch into origin's given remote branch.""" |
| 76 | repo = Worktree(proj, pathToRepo) |
| 77 | with cd(repo.repodir): |
| 78 | git("push", "-u", "origin", "+{}:{}".format(local_branch, remote_branch)) |