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