OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2011 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 Wrapper script around Rietveld's upload.py that simplifies working with groups | 7 Wrapper script around Rietveld's upload.py that simplifies working with groups |
8 of files. | 8 of files. |
9 """ | 9 """ |
10 | 10 |
(...skipping 20 matching lines...) Expand all Loading... |
31 import simplejson as json # pylint: disable=F0401 | 31 import simplejson as json # pylint: disable=F0401 |
32 | 32 |
33 import fix_encoding | 33 import fix_encoding |
34 import gclient_utils | 34 import gclient_utils |
35 import presubmit_support | 35 import presubmit_support |
36 import rietveld | 36 import rietveld |
37 from scm import SVN | 37 from scm import SVN |
38 import subprocess2 | 38 import subprocess2 |
39 from third_party import upload | 39 from third_party import upload |
40 | 40 |
41 __version__ = '1.2.1' | 41 __version__ = '1.2.2' |
42 | 42 |
43 | 43 |
44 CODEREVIEW_SETTINGS = { | 44 CODEREVIEW_SETTINGS = { |
45 # To make gcl send reviews to a server, check in a file named | 45 # To make gcl send reviews to a server, check in a file named |
46 # "codereview.settings" (see |CODEREVIEW_SETTINGS_FILE| below) to your | 46 # "codereview.settings" (see |CODEREVIEW_SETTINGS_FILE| below) to your |
47 # project's base directory and add the following line to codereview.settings: | 47 # project's base directory and add the following line to codereview.settings: |
48 # CODE_REVIEW_SERVER: codereview.yourserver.org | 48 # CODE_REVIEW_SERVER: codereview.yourserver.org |
49 } | 49 } |
50 | 50 |
51 # globals that store the root of the current repository and the directory where | 51 # globals that store the root of the current repository and the directory where |
(...skipping 10 matching lines...) Expand all Loading... |
62 | 62 |
63 # Global cache of files cached in GetCacheDir(). | 63 # Global cache of files cached in GetCacheDir(). |
64 FILES_CACHE = {} | 64 FILES_CACHE = {} |
65 | 65 |
66 # Valid extensions for files we want to lint. | 66 # Valid extensions for files we want to lint. |
67 DEFAULT_LINT_REGEX = r"(.*\.cpp|.*\.cc|.*\.h)" | 67 DEFAULT_LINT_REGEX = r"(.*\.cpp|.*\.cc|.*\.h)" |
68 DEFAULT_LINT_IGNORE_REGEX = r"$^" | 68 DEFAULT_LINT_IGNORE_REGEX = r"$^" |
69 | 69 |
70 REVIEWERS_REGEX = r'\s*R=(.+)' | 70 REVIEWERS_REGEX = r'\s*R=(.+)' |
71 | 71 |
| 72 |
72 def CheckHomeForFile(filename): | 73 def CheckHomeForFile(filename): |
73 """Checks the users home dir for the existence of the given file. Returns | 74 """Checks the users home dir for the existence of the given file. Returns |
74 the path to the file if it's there, or None if it is not. | 75 the path to the file if it's there, or None if it is not. |
75 """ | 76 """ |
76 home_vars = ['HOME'] | 77 home_vars = ['HOME'] |
77 if sys.platform in ('cygwin', 'win32'): | 78 if sys.platform in ('cygwin', 'win32'): |
78 home_vars.append('USERPROFILE') | 79 home_vars.append('USERPROFILE') |
79 for home_var in home_vars: | 80 for home_var in home_vars: |
80 home = os.getenv(home_var) | 81 home = os.getenv(home_var) |
81 if home != None: | 82 if home != None: |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 | 221 |
221 def Warn(msg): | 222 def Warn(msg): |
222 print >> sys.stderr, msg | 223 print >> sys.stderr, msg |
223 | 224 |
224 | 225 |
225 def ErrorExit(msg): | 226 def ErrorExit(msg): |
226 print >> sys.stderr, msg | 227 print >> sys.stderr, msg |
227 sys.exit(1) | 228 sys.exit(1) |
228 | 229 |
229 | 230 |
230 def RunShellWithReturnCode(command, print_output=False): | |
231 """Executes a command and returns the output and the return code.""" | |
232 p = subprocess2.Popen( | |
233 command, stdout=subprocess2.PIPE, | |
234 stderr=subprocess2.STDOUT, universal_newlines=True) | |
235 if print_output: | |
236 output_array = [] | |
237 while True: | |
238 line = p.stdout.readline() | |
239 if not line: | |
240 break | |
241 if print_output: | |
242 print line.strip('\n') | |
243 output_array.append(line) | |
244 output = "".join(output_array) | |
245 else: | |
246 output = p.stdout.read() | |
247 p.wait() | |
248 p.stdout.close() | |
249 return output, p.returncode | |
250 | |
251 | |
252 def RunShell(command, print_output=False): | |
253 """Executes a command and returns the output.""" | |
254 return RunShellWithReturnCode(command, print_output)[0] | |
255 | |
256 | |
257 def FilterFlag(args, flag): | 231 def FilterFlag(args, flag): |
258 """Returns True if the flag is present in args list. | 232 """Returns True if the flag is present in args list. |
259 | 233 |
260 The flag is removed from args if present. | 234 The flag is removed from args if present. |
261 """ | 235 """ |
262 if flag in args: | 236 if flag in args: |
263 args.remove(flag) | 237 args.remove(flag) |
264 return True | 238 return True |
265 return False | 239 return False |
266 | 240 |
(...skipping 740 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1007 commit_message = change_info.description.replace('\r\n', '\n') | 981 commit_message = change_info.description.replace('\r\n', '\n') |
1008 if change_info.issue: | 982 if change_info.issue: |
1009 server = change_info.rietveld | 983 server = change_info.rietveld |
1010 if not server.startswith("http://") and not server.startswith("https://"): | 984 if not server.startswith("http://") and not server.startswith("https://"): |
1011 server = "http://" + server | 985 server = "http://" + server |
1012 commit_message += ('\nReview URL: %s/%d' % (server, change_info.issue)) | 986 commit_message += ('\nReview URL: %s/%d' % (server, change_info.issue)) |
1013 | 987 |
1014 handle, commit_filename = tempfile.mkstemp(text=True) | 988 handle, commit_filename = tempfile.mkstemp(text=True) |
1015 os.write(handle, commit_message) | 989 os.write(handle, commit_message) |
1016 os.close(handle) | 990 os.close(handle) |
1017 | 991 try: |
1018 handle, targets_filename = tempfile.mkstemp(text=True) | 992 handle, targets_filename = tempfile.mkstemp(text=True) |
1019 os.write(handle, "\n".join(change_info.GetFileNames())) | 993 os.write(handle, "\n".join(change_info.GetFileNames())) |
1020 os.close(handle) | 994 os.close(handle) |
1021 | 995 try: |
1022 commit_cmd += ['--file=' + commit_filename] | 996 commit_cmd += ['--file=' + commit_filename] |
1023 commit_cmd += ['--targets=' + targets_filename] | 997 commit_cmd += ['--targets=' + targets_filename] |
1024 # Change the current working directory before calling commit. | 998 # Change the current working directory before calling commit. |
1025 previous_cwd = os.getcwd() | 999 previous_cwd = os.getcwd() |
1026 os.chdir(change_info.GetLocalRoot()) | 1000 os.chdir(change_info.GetLocalRoot()) |
1027 output = RunShell(commit_cmd, True) | 1001 output = '' |
1028 os.remove(commit_filename) | 1002 try: |
1029 os.remove(targets_filename) | 1003 output = subprocess2.check_output(commit_cmd) |
| 1004 except subprocess2.CalledProcessError, e: |
| 1005 ErrorExit('Commit failed.\n%s' % e) |
| 1006 finally: |
| 1007 os.remove(commit_filename) |
| 1008 finally: |
| 1009 os.remove(targets_filename) |
1030 if output.find("Committed revision") != -1: | 1010 if output.find("Committed revision") != -1: |
1031 change_info.Delete() | 1011 change_info.Delete() |
1032 | 1012 |
1033 if change_info.issue: | 1013 if change_info.issue: |
1034 revision = re.compile(".*?\nCommitted revision (\d+)", | 1014 revision = re.compile(".*?\nCommitted revision (\d+)", |
1035 re.DOTALL).match(output).group(1) | 1015 re.DOTALL).match(output).group(1) |
1036 viewvc_url = GetCodeReviewSetting("VIEW_VC") | 1016 viewvc_url = GetCodeReviewSetting("VIEW_VC") |
1037 change_info.description += '\n' | 1017 change_info.description += '\n' |
1038 if viewvc_url: | 1018 if viewvc_url: |
1039 change_info.description += "\nCommitted: " + viewvc_url + revision | 1019 change_info.description += "\nCommitted: " + viewvc_url + revision |
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1297 if args: | 1277 if args: |
1298 change_info = ChangeInfo.Load(args.pop(0), GetRepositoryRoot(), True, True) | 1278 change_info = ChangeInfo.Load(args.pop(0), GetRepositoryRoot(), True, True) |
1299 files = change_info.GetFileNames() | 1279 files = change_info.GetFileNames() |
1300 else: | 1280 else: |
1301 files = [f[1] for f in GetFilesNotInCL()] | 1281 files = [f[1] for f in GetFilesNotInCL()] |
1302 | 1282 |
1303 root = GetRepositoryRoot() | 1283 root = GetRepositoryRoot() |
1304 cmd = ['svn', 'diff'] | 1284 cmd = ['svn', 'diff'] |
1305 cmd.extend([os.path.join(root, x) for x in files]) | 1285 cmd.extend([os.path.join(root, x) for x in files]) |
1306 cmd.extend(args) | 1286 cmd.extend(args) |
1307 return RunShellWithReturnCode(cmd, print_output=True)[1] | 1287 return subprocess2.call(cmd) |
1308 | 1288 |
1309 | 1289 |
1310 @no_args | 1290 @no_args |
1311 def CMDsettings(): | 1291 def CMDsettings(): |
1312 """Prints code review settings for this checkout.""" | 1292 """Prints code review settings for this checkout.""" |
1313 # Force load settings | 1293 # Force load settings |
1314 GetCodeReviewSetting("UNKNOWN") | 1294 GetCodeReviewSetting("UNKNOWN") |
1315 del CODEREVIEW_SETTINGS['__just_initialized'] | 1295 del CODEREVIEW_SETTINGS['__just_initialized'] |
1316 print '\n'.join(("%s: %s" % (str(k), str(v)) | 1296 print '\n'.join(("%s: %s" % (str(k), str(v)) |
1317 for (k,v) in CODEREVIEW_SETTINGS.iteritems())) | 1297 for (k,v) in CODEREVIEW_SETTINGS.iteritems())) |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1380 def CMDpassthru(args): | 1360 def CMDpassthru(args): |
1381 """Everything else that is passed into gcl we redirect to svn. | 1361 """Everything else that is passed into gcl we redirect to svn. |
1382 | 1362 |
1383 It assumes a change list name is passed and is converted with the files names. | 1363 It assumes a change list name is passed and is converted with the files names. |
1384 """ | 1364 """ |
1385 args = ["svn", args[0]] | 1365 args = ["svn", args[0]] |
1386 if len(args) > 1: | 1366 if len(args) > 1: |
1387 root = GetRepositoryRoot() | 1367 root = GetRepositoryRoot() |
1388 change_info = ChangeInfo.Load(args[1], root, True, True) | 1368 change_info = ChangeInfo.Load(args[1], root, True, True) |
1389 args.extend([os.path.join(root, x) for x in change_info.GetFileNames()]) | 1369 args.extend([os.path.join(root, x) for x in change_info.GetFileNames()]) |
1390 return RunShellWithReturnCode(args, print_output=True)[1] | 1370 return subprocess2.call(args) |
1391 | 1371 |
1392 | 1372 |
1393 def Command(name): | 1373 def Command(name): |
1394 return getattr(sys.modules[__name__], 'CMD' + name, None) | 1374 return getattr(sys.modules[__name__], 'CMD' + name, None) |
1395 | 1375 |
1396 | 1376 |
1397 def GenUsage(command): | 1377 def GenUsage(command): |
1398 """Modify an OptParse object with the function's documentation.""" | 1378 """Modify an OptParse object with the function's documentation.""" |
1399 obj = Command(command) | 1379 obj = Command(command) |
1400 display = command | 1380 display = command |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1477 raise | 1457 raise |
1478 print >> sys.stderr, ( | 1458 print >> sys.stderr, ( |
1479 'AppEngine is misbehaving and returned HTTP %d, again. Keep faith ' | 1459 'AppEngine is misbehaving and returned HTTP %d, again. Keep faith ' |
1480 'and retry or visit go/isgaeup.\n%s') % (e.code, str(e)) | 1460 'and retry or visit go/isgaeup.\n%s') % (e.code, str(e)) |
1481 return 1 | 1461 return 1 |
1482 | 1462 |
1483 | 1463 |
1484 if __name__ == "__main__": | 1464 if __name__ == "__main__": |
1485 fix_encoding.fix_encoding() | 1465 fix_encoding.fix_encoding() |
1486 sys.exit(main(sys.argv[1:])) | 1466 sys.exit(main(sys.argv[1:])) |
OLD | NEW |