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 os |
7 import re | 8 import re |
8 import subprocess | 9 import subprocess |
9 | 10 |
10 | 11 |
11 # Show more information about the commands being executed. | 12 # Show more information about the commands being executed. |
12 VERBOSE = False | 13 VERBOSE = False |
13 | 14 |
14 | 15 |
15 def GetStatusOutput(cmd): | 16 def GetStatusOutput(cmd, cwd=None): |
16 """Return (status, output) of executing cmd in a shell.""" | 17 """Return (status, output) of executing cmd in a shell.""" |
17 if VERBOSE: | 18 if VERBOSE: |
18 print '' | 19 print '' |
19 print '[DEBUG] Running "%s"' % cmd | 20 print '[DEBUG] Running "%s"' % cmd |
20 proc = subprocess.Popen(cmd, shell=True, universal_newlines=True, | 21 proc = subprocess.Popen(cmd, shell=True, universal_newlines=True, cwd=cwd, |
21 stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | 22 stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
22 output = ''.join(proc.stdout.readlines()) | 23 output = ''.join(proc.stdout.readlines()) |
23 status = proc.wait() | 24 status = proc.wait() |
24 if status is None: | 25 if status is None: |
25 status = 0 | 26 status = 0 |
26 | 27 |
27 if VERBOSE: | 28 if VERBOSE: |
28 short_output = ' '.join(output.splitlines()) | 29 short_output = ' '.join(output.splitlines()) |
29 short_output = short_output.strip(' \t\n\r') | 30 short_output = short_output.strip(' \t\n\r') |
30 print '[DEBUG] Output: %d, %-60s' % (status, short_output) | 31 print '[DEBUG] Output: %d, %-60s' % (status, short_output) |
31 | 32 |
32 return (status, output) | 33 return (status, output) |
33 | 34 |
34 | 35 |
35 def Git(git_repo, command): | 36 def Git(git_repo, command, is_mirror=False): |
36 """Execute a git command within a local git repo.""" | 37 """Execute a git command within a local git repo.""" |
37 cmd = 'git --git-dir=%s %s' % (git_repo, command) | 38 if is_mirror: |
38 (status, output) = GetStatusOutput(cmd) | 39 cmd = 'git --git-dir=%s %s' % (git_repo, command) |
| 40 cwd = None |
| 41 else: |
| 42 cmd = 'git %s' % command |
| 43 cwd = git_repo |
| 44 (status, output) = GetStatusOutput(cmd, cwd) |
39 if status != 0: | 45 if status != 0: |
40 raise Exception('Failed to run %s. error %d. output %s' % (cmd, status, | 46 raise Exception('Failed to run %s. error %d. output %s' % (cmd, status, |
41 output)) | 47 output)) |
42 return (status, output) | 48 return (status, output) |
43 | 49 |
44 | 50 |
45 def Clone(git_url, git_repo): | 51 def Clone(git_url, git_repo, is_mirror): |
46 """Clone a repository.""" | 52 """Clone a repository.""" |
47 Git(git_repo, 'clone --mirror %s %s' % (git_url, git_repo)) | 53 cmd = 'clone%s %s %s' % (' --mirror' if is_mirror else '', git_url, git_repo) |
| 54 if not is_mirror and not os.path.exists(git_repo): |
| 55 os.mkdir(git_repo) |
| 56 return Git(git_repo, cmd, is_mirror) |
48 | 57 |
49 | 58 |
50 def Fetch(git_repo): | 59 def Fetch(git_repo, is_mirror): |
51 """Fetch the latest objects for a given git repository.""" | 60 """Fetch the latest objects for a given git repository.""" |
52 Git(git_repo, 'fetch') | 61 Git(git_repo, 'fetch', is_mirror) |
53 | 62 |
54 | 63 |
55 def Ping(git_repo): | 64 def Ping(git_repo): |
56 """Confirm that a remote repository URL is valid.""" | 65 """Confirm that a remote repository URL is valid.""" |
57 status, output = GetStatusOutput('git ls-remote ' + git_repo) | 66 status, output = GetStatusOutput('git ls-remote ' + git_repo) |
58 return status == 0 | 67 return status == 0 |
59 | 68 |
60 | 69 |
61 def CreateLessThanOrEqualRegex(number): | 70 def CreateLessThanOrEqualRegex(number): |
62 """ Return a regular expression to test whether an integer less than or equal | 71 """ 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 | 125 # 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. | 126 # one order of magnitude smaller than the original numbers. |
118 for index in range(1, num_len): | 127 for index in range(1, num_len): |
119 expressions.append('[0-9]'*index) | 128 expressions.append('[0-9]'*index) |
120 | 129 |
121 # All done. We now have our final regular expression. | 130 # All done. We now have our final regular expression. |
122 regex = '(%s)' % ('|'.join(expressions)) | 131 regex = '(%s)' % ('|'.join(expressions)) |
123 return regex | 132 return regex |
124 | 133 |
125 | 134 |
126 def Search(git_repo, svn_rev): | 135 def Search(git_repo, svn_rev, is_mirror): |
127 """Return the Git commit id matching the given SVN revision.""" | 136 """Return the Git commit id matching the given SVN revision.""" |
128 regex = CreateLessThanOrEqualRegex(svn_rev) | 137 regex = CreateLessThanOrEqualRegex(svn_rev) |
129 (status, output) = Git(git_repo, ('log -E --grep=".*git-svn-id:.*@%s " ' | 138 (status, output) = Git(git_repo, ('log -E --grep=".*git-svn-id:.*@%s " ' |
130 '-1 --format=%%H FETCH_HEAD') % regex) | 139 '-1 --format=%%H FETCH_HEAD') % regex, |
| 140 is_mirror) |
131 if output != '': | 141 if output != '': |
132 output = output.splitlines()[0] | 142 output = output.splitlines()[0] |
133 | 143 |
134 print '%s: %s <-> %s' % (git_repo, output, svn_rev) | 144 print '%s: %s <-> %s' % (git_repo, output, svn_rev) |
135 if re.match('^[0-9a-fA-F]{40}$', output): | 145 if re.match('^[0-9a-fA-F]{40}$', output): |
136 return output | 146 return output |
137 raise Exception('Cannot find revision %s in %s' % (svn_rev, git_repo)) | 147 raise Exception('Cannot find revision %s in %s' % (svn_rev, git_repo)) |
OLD | NEW |