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 | |
13 import subprocess | 12 import subprocess |
14 import sys | 13 import sys |
15 | 14 |
15 class VersionInfo(object): | |
16 def __init__(self, url, root, revision): | |
17 self.url = url | |
18 self.root = root | |
19 self.revision = revision | |
16 | 20 |
17 def FetchSVNRevision(command): | 21 |
22 def FetchSVNRevision(command, directory): | |
18 """ | 23 """ |
19 Fetch the Subversion revision for the local tree. | 24 Fetch the Subversion branch and revision for the a given directory |
25 by running the given command (e.g. "svn info"). | |
20 | 26 |
21 Errors are swallowed. | 27 Errors are swallowed. |
28 | |
29 Returns: | |
30 a tuple of (url, revision) or None on error. | |
tony
2011/01/21 22:59:58
Nit: comment is out of date
| |
22 """ | 31 """ |
23 try: | 32 try: |
24 proc = subprocess.Popen(command, | 33 proc = subprocess.Popen(command, |
25 stdout=subprocess.PIPE, | 34 stdout=subprocess.PIPE, |
26 stderr=subprocess.PIPE, | 35 stderr=subprocess.PIPE, |
36 cwd=directory, | |
27 shell=(sys.platform=='win32')) | 37 shell=(sys.platform=='win32')) |
28 except OSError: | 38 except OSError: |
29 # command is apparently either not installed or not executable. | 39 # command is apparently either not installed or not executable. |
30 return None | 40 return None |
31 if proc: | 41 if not proc: |
32 svn_re = re.compile('^Revision:\s+(\d+)', re.M) | 42 return None |
33 match = svn_re.search(proc.stdout.read()) | 43 |
34 if match: | 44 attrs = {} |
35 return match.group(1) | 45 for line in proc.stdout.read().strip().split('\n'): |
36 return None | 46 if not line: |
47 continue | |
48 key, val = line.split(': ', 1) | |
49 attrs[key] = val | |
50 | |
51 try: | |
52 url = attrs['URL'] | |
53 root = attrs['Repository Root'] | |
54 revision = attrs['Revision'] | |
55 except KeyError: | |
56 return None | |
57 | |
58 return VersionInfo(url, root, revision) | |
37 | 59 |
38 | 60 |
39 def FetchChange(default_lastchange): | 61 def FetchVersionInfo(default_lastchange, directory=None): |
40 """ | 62 """ |
41 Returns the last change, from some appropriate revision control system. | 63 Returns the last change (in the form of a branch, revision tuple), |
64 from some appropriate revision control system. | |
42 """ | 65 """ |
43 change = FetchSVNRevision(['svn', 'info']) | 66 version_info = FetchSVNRevision(['svn', 'info'], directory) |
44 if not change and sys.platform in ('linux2',): | 67 if not version_info and sys.platform in ('linux2',): |
45 change = FetchSVNRevision(['git', 'svn', 'info']) | 68 version_info = FetchSVNRevision(['git', 'svn', 'info'], directory) |
46 if not change: | 69 if not version_info: |
47 if default_lastchange and os.path.exists(default_lastchange): | 70 if default_lastchange and os.path.exists(default_lastchange): |
48 change = open(default_lastchange, 'r').read().strip() | 71 revision = open(default_lastchange, 'r').read().strip() |
72 version_info = VersionInfo(None, None, revision) | |
49 else: | 73 else: |
50 change = '0' | 74 version_info = VersionInfo('', '', '0') |
51 return change | 75 return version_info |
52 | 76 |
53 | 77 |
54 def WriteIfChanged(file_name, contents): | 78 def WriteIfChanged(file_name, contents): |
55 """ | 79 """ |
56 Writes the specified contents to the specified file_name | 80 Writes the specified contents to the specified file_name |
57 iff the contents are different than the current contents. | 81 iff the contents are different than the current contents. |
58 """ | 82 """ |
59 try: | 83 try: |
60 old_contents = open(file_name, 'r').read() | 84 old_contents = open(file_name, 'r').read() |
61 except EnvironmentError: | 85 except EnvironmentError: |
(...skipping 21 matching lines...) Expand all Loading... | |
83 out_file = opts.output | 107 out_file = opts.output |
84 | 108 |
85 while len(args) and out_file is None: | 109 while len(args) and out_file is None: |
86 if out_file is None: | 110 if out_file is None: |
87 out_file = args.pop(0) | 111 out_file = args.pop(0) |
88 if args: | 112 if args: |
89 sys.stderr.write('Unexpected arguments: %r\n\n' % args) | 113 sys.stderr.write('Unexpected arguments: %r\n\n' % args) |
90 parser.print_help() | 114 parser.print_help() |
91 sys.exit(2) | 115 sys.exit(2) |
92 | 116 |
93 change = FetchChange(opts.default_lastchange) | 117 version_info = FetchVersionInfo(opts.default_lastchange) |
94 | 118 |
95 if opts.revision_only: | 119 if opts.revision_only: |
96 print change | 120 print version_info.revision |
97 else: | 121 else: |
98 contents = "LASTCHANGE=%s\n" % change | 122 contents = "LASTCHANGE=%s\n" % version_info.revision |
99 if out_file: | 123 if out_file: |
100 WriteIfChanged(out_file, contents) | 124 WriteIfChanged(out_file, contents) |
101 else: | 125 else: |
102 sys.stdout.write(contents) | 126 sys.stdout.write(contents) |
103 | 127 |
104 return 0 | 128 return 0 |
105 | 129 |
106 | 130 |
107 if __name__ == '__main__': | 131 if __name__ == '__main__': |
108 sys.exit(main()) | 132 sys.exit(main()) |
OLD | NEW |