OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2010 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2010 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 lastchange.py -- Chromium revision fetching utility. | 7 lastchange.py -- Chromium revision fetching utility. |
8 """ | 8 """ |
9 | 9 |
10 import optparse | 10 import optparse |
11 import os | 11 import os |
12 import re | 12 import re |
13 import subprocess | 13 import subprocess |
14 import sys | 14 import sys |
15 | 15 |
16 | 16 |
17 def FetchSVNRevision(command): | 17 def FetchSVNRevision(command, directory): |
18 """ | 18 """ |
19 Fetch the Subversion revision for the local tree. | 19 Fetch the Subversion branch and revision for the a given directory |
20 by running the given command (e.g. "svn info"). | |
20 | 21 |
21 Errors are swallowed. | 22 Errors are swallowed. |
23 | |
24 Returns: | |
25 a tuple of (url, revision) or None on error. | |
22 """ | 26 """ |
23 try: | 27 try: |
24 proc = subprocess.Popen(command, | 28 proc = subprocess.Popen(command, |
25 stdout=subprocess.PIPE, | 29 stdout=subprocess.PIPE, |
26 stderr=subprocess.PIPE, | 30 stderr=subprocess.PIPE, |
31 cwd=directory, | |
27 shell=(sys.platform=='win32')) | 32 shell=(sys.platform=='win32')) |
28 except OSError: | 33 except OSError: |
29 # command is apparently either not installed or not executable. | 34 # command is apparently either not installed or not executable. |
30 return None | 35 return None |
31 if proc: | 36 if proc: |
32 svn_re = re.compile('^Revision:\s+(\d+)', re.M) | 37 output = proc.stdout.read() |
33 match = svn_re.search(proc.stdout.read()) | 38 match = re.search('^Revision:\s+(\d+)', output, re.M) |
34 if match: | 39 if not match: |
35 return match.group(1) | 40 return None |
41 revision = match.group(1) | |
42 | |
43 match = re.search('^Repository Root:\s+(\S+)', output, re.M) | |
44 if not match: | |
45 return None | |
46 url = match.group(1) | |
47 | |
48 return (url, revision) | |
tony
2011/01/21 21:18:24
Down with returning tuples! Can you make this a s
| |
49 | |
36 return None | 50 return None |
37 | 51 |
38 | 52 |
39 def FetchChange(default_lastchange): | 53 def FetchChange(default_lastchange, directory=None): |
40 """ | 54 """ |
41 Returns the last change, from some appropriate revision control system. | 55 Returns the last change (in the form of a branch, revision tuple), |
56 from some appropriate revision control system. | |
42 """ | 57 """ |
43 change = FetchSVNRevision(['svn', 'info']) | 58 change = FetchSVNRevision(['svn', 'info'], directory) |
44 if not change and sys.platform in ('linux2',): | 59 if not change and sys.platform in ('linux2',): |
45 change = FetchSVNRevision(['git', 'svn', 'info']) | 60 change = FetchSVNRevision(['git', 'svn', 'info'], directory) |
46 if not change: | 61 if not change: |
47 if default_lastchange and os.path.exists(default_lastchange): | 62 if default_lastchange and os.path.exists(default_lastchange): |
48 change = open(default_lastchange, 'r').read().strip() | 63 revision = open(default_lastchange, 'r').read().strip() |
64 change = (None, revision) | |
49 else: | 65 else: |
50 change = '0' | 66 change = ('', '0') |
51 return change | 67 return change |
52 | 68 |
53 | 69 |
54 def WriteIfChanged(file_name, contents): | 70 def WriteIfChanged(file_name, contents): |
55 """ | 71 """ |
56 Writes the specified contents to the specified file_name | 72 Writes the specified contents to the specified file_name |
57 iff the contents are different than the current contents. | 73 iff the contents are different than the current contents. |
58 """ | 74 """ |
59 try: | 75 try: |
60 old_contents = open(file_name, 'r').read() | 76 old_contents = open(file_name, 'r').read() |
(...skipping 22 matching lines...) Expand all Loading... | |
83 out_file = opts.output | 99 out_file = opts.output |
84 | 100 |
85 while len(args) and out_file is None: | 101 while len(args) and out_file is None: |
86 if out_file is None: | 102 if out_file is None: |
87 out_file = args.pop(0) | 103 out_file = args.pop(0) |
88 if args: | 104 if args: |
89 sys.stderr.write('Unexpected arguments: %r\n\n' % args) | 105 sys.stderr.write('Unexpected arguments: %r\n\n' % args) |
90 parser.print_help() | 106 parser.print_help() |
91 sys.exit(2) | 107 sys.exit(2) |
92 | 108 |
93 change = FetchChange(opts.default_lastchange) | 109 url, revision = FetchChange(opts.default_lastchange) |
94 | 110 |
95 if opts.revision_only: | 111 if opts.revision_only: |
96 print change | 112 print revision |
97 else: | 113 else: |
98 contents = "LASTCHANGE=%s\n" % change | 114 contents = "LASTCHANGE=%s\n" % revision |
99 if out_file: | 115 if out_file: |
100 WriteIfChanged(out_file, contents) | 116 WriteIfChanged(out_file, contents) |
101 else: | 117 else: |
102 sys.stdout.write(contents) | 118 sys.stdout.write(contents) |
103 | 119 |
104 return 0 | 120 return 0 |
105 | 121 |
106 | 122 |
107 if __name__ == '__main__': | 123 if __name__ == '__main__': |
108 sys.exit(main()) | 124 sys.exit(main()) |
OLD | NEW |