Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 | 6 |
| 7 import re | 7 import re |
| 8 import subprocess | 8 import subprocess |
| 9 | 9 |
| 10 | 10 |
| 11 # Show more information about the commands being executed. | 11 # Show more information about the commands being executed. |
| 12 VERBOSE = False | 12 VERBOSE = False |
| 13 | 13 |
| 14 | 14 |
| 15 def GetStatusOutput(cmd): | 15 def GetStatusOutput(cmd, cwd=None): |
| 16 """Return (status, output) of executing cmd in a shell.""" | 16 """Return (status, output) of executing cmd in a shell.""" |
| 17 if VERBOSE: | 17 if VERBOSE: |
| 18 print '' | 18 print '' |
| 19 print '[DEBUG] Running "%s"' % cmd | 19 print '[DEBUG] Running "%s"' % cmd |
| 20 proc = subprocess.Popen(cmd, shell=True, universal_newlines=True, | 20 proc = subprocess.Popen(cmd, shell=True, universal_newlines=True, cwd=cwd, |
| 21 stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | 21 stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 22 output = ''.join(proc.stdout.readlines()) | 22 output = ''.join(proc.stdout.readlines()) |
| 23 status = proc.wait() | 23 status = proc.wait() |
| 24 if status is None: | 24 if status is None: |
| 25 status = 0 | 25 status = 0 |
| 26 | 26 |
| 27 if VERBOSE: | 27 if VERBOSE: |
| 28 short_output = ' '.join(output.splitlines()) | 28 short_output = ' '.join(output.splitlines()) |
| 29 short_output = short_output.strip(' \t\n\r') | 29 short_output = short_output.strip(' \t\n\r') |
| 30 print '[DEBUG] Output: %d, %-60s' % (status, short_output) | 30 print '[DEBUG] Output: %d, %-60s' % (status, short_output) |
| 31 | 31 |
| 32 return (status, output) | 32 return (status, output) |
| 33 | 33 |
| 34 | 34 |
| 35 def Git(git_repo, command): | 35 def Git(git_repo, command, mirror=False): |
| 36 """Execute a git command within a local git repo.""" | 36 """Execute a git command within a local git repo.""" |
| 37 cmd = 'git --git-dir=%s %s' % (git_repo, command) | 37 if mirror: |
| 38 cmd = 'git --git-dir=%s %s' % (git_repo, command) | |
| 39 cwd = None | |
| 40 else: | |
| 41 cmd = 'git %s' % command | |
| 42 cwd = git_repo | |
| 43 (status, output) = GetStatusOutput(cmd, cwd) | |
| 44 if status != 0: | |
| 45 raise Exception('Failed to run %s. error %d. output %s' % (cmd, status, | |
| 46 output)) | |
| 47 return (status, output) | |
| 48 | |
| 49 | |
| 50 def Clone(git_url, git_repo, mirror): | |
| 51 """Clone a repository.""" | |
| 52 if mirror: | |
| 53 return Git(git_repo, 'clone --mirror %s %s' % (git_url, git_repo)) | |
| 54 cmd = 'git clone %s %s' % (git_url, git_repo) | |
|
nsylvain
2012/10/10 17:33:28
Why can't you do Git('/', 'clone %s %s' % ..) ?
szager
2012/10/10 18:09:14
The gotcha is that the directory git_repo might no
| |
| 38 (status, output) = GetStatusOutput(cmd) | 55 (status, output) = GetStatusOutput(cmd) |
| 39 if status != 0: | 56 if status != 0: |
| 40 raise Exception('Failed to run %s. error %d. output %s' % (cmd, status, | 57 raise Exception('Failed to run %s. error %d. output %s' % (cmd, status, |
| 41 output)) | 58 output)) |
| 42 return (status, output) | 59 return (status, output) |
| 43 | 60 |
| 44 | 61 |
| 45 def Clone(git_url, git_repo): | 62 def Fetch(git_repo, mirror): |
| 46 """Clone a repository.""" | |
| 47 Git(git_repo, 'clone --mirror %s %s' % (git_url, git_repo)) | |
| 48 | |
| 49 | |
| 50 def Fetch(git_repo): | |
| 51 """Fetch the latest objects for a given git repository.""" | 63 """Fetch the latest objects for a given git repository.""" |
| 52 Git(git_repo, 'fetch') | 64 Git(git_repo, 'fetch', mirror) |
| 53 | 65 |
| 54 | 66 |
| 55 def Ping(git_repo): | 67 def Ping(git_repo): |
| 56 """Confirm that a remote repository URL is valid.""" | 68 """Confirm that a remote repository URL is valid.""" |
| 57 status, output = GetStatusOutput('git ls-remote ' + git_repo) | 69 status, output = GetStatusOutput('git ls-remote ' + git_repo) |
| 58 return status == 0 | 70 return status == 0 |
| 59 | 71 |
| 60 | 72 |
| 61 def CreateLessThanOrEqualRegex(number): | 73 def CreateLessThanOrEqualRegex(number): |
| 62 """ Return a regular expression to test whether an integer less than or equal | 74 """ Return a regular expression to test whether an integer less than or equal |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 116 # Part 3: We add all the full ranges to match all numbers that are at least | 128 # Part 3: We add all the full ranges to match all numbers that are at least |
| 117 # one order of magnitude smaller than the original numbers. | 129 # one order of magnitude smaller than the original numbers. |
| 118 for index in range(1, num_len): | 130 for index in range(1, num_len): |
| 119 expressions.append('[0-9]'*index) | 131 expressions.append('[0-9]'*index) |
| 120 | 132 |
| 121 # All done. We now have our final regular expression. | 133 # All done. We now have our final regular expression. |
| 122 regex = '(%s)' % ('|'.join(expressions)) | 134 regex = '(%s)' % ('|'.join(expressions)) |
| 123 return regex | 135 return regex |
| 124 | 136 |
| 125 | 137 |
| 126 def Search(git_repo, svn_rev): | 138 def Search(git_repo, svn_rev, mirror): |
|
nsylvain
2012/10/10 17:33:28
Again i'm going to nitpick about the naming. It to
szager
2012/10/10 18:09:14
I think 'use_chrome_checkout' is overly-specific.
| |
| 127 """Return the Git commit id matching the given SVN revision.""" | 139 """Return the Git commit id matching the given SVN revision.""" |
| 128 regex = CreateLessThanOrEqualRegex(svn_rev) | 140 regex = CreateLessThanOrEqualRegex(svn_rev) |
| 129 (status, output) = Git(git_repo, ('log -E --grep=".*git-svn-id:.*@%s " ' | 141 (status, output) = Git(git_repo, ('log -E --grep=".*git-svn-id:.*@%s " ' |
| 130 '-1 --format=%%H FETCH_HEAD') % regex) | 142 '-1 --format=%%H FETCH_HEAD') % regex, |
| 143 mirror) | |
| 131 if output != '': | 144 if output != '': |
| 132 output = output.splitlines()[0] | 145 output = output.splitlines()[0] |
| 133 | 146 |
| 134 print '%s: %s <-> %s' % (git_repo, output, svn_rev) | 147 print '%s: %s <-> %s' % (git_repo, output, svn_rev) |
| 135 if re.match('^[0-9a-fA-F]{40}$', output): | 148 if re.match('^[0-9a-fA-F]{40}$', output): |
| 136 return output | 149 return output |
| 137 raise Exception('Cannot find revision %s in %s' % (svn_rev, git_repo)) | 150 raise Exception('Cannot find revision %s in %s' % (svn_rev, git_repo)) |
| OLD | NEW |