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