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 svn_fetch_revision(): | 17 def FetchSVNRevision(command): |
18 """ | 18 """ |
19 Fetch the Subversion revision for the local tree. | 19 Fetch the Subversion revision for the local tree. |
20 | 20 |
21 Errors are swallowed. | 21 Errors are swallowed. |
22 """ | 22 """ |
23 try: | 23 try: |
24 p = subprocess.Popen(['svn', 'info'], | 24 proc = subprocess.Popen(command, |
25 stdout=subprocess.PIPE, | |
26 stderr=subprocess.PIPE, | |
27 shell=(sys.platform=='win32')) | |
28 except OSError, e: | |
29 # 'svn' is apparently either not installed or not executable. | |
30 return None | |
31 revision = None | |
32 if p: | |
33 svn_re = re.compile('^Revision:\s+(\d+)', re.M) | |
34 m = svn_re.search(p.stdout.read()) | |
35 if m: | |
36 revision = m.group(1) | |
37 return revision | |
38 | |
39 | |
40 def git_fetch_id(): | |
41 """ | |
42 Fetch the GIT identifier for the local tree. | |
43 | |
44 Errors are swallowed. | |
45 """ | |
46 git_re = re.compile('^\s*git-svn-id:\s+(\S+)@(\d+)', re.M) | |
47 try: | |
48 proc = subprocess.Popen(['git', 'log', '-999'], | |
49 stdout=subprocess.PIPE, | 25 stdout=subprocess.PIPE, |
50 stderr=subprocess.PIPE, | 26 stderr=subprocess.PIPE, |
51 shell=(sys.platform=='win32')) | 27 shell=(sys.platform=='win32')) |
52 for line in proc.stdout: | |
53 match = git_re.search(line) | |
54 if match: | |
55 id = match.group(2) | |
56 if id: | |
57 proc.stdout.close() # Cut pipe. | |
58 return id | |
59 except OSError: | 28 except OSError: |
60 # 'git' is apparently either not installed or not executable. | 29 # command is apparently either not installed or not executable. |
61 pass | 30 return None |
| 31 if proc: |
| 32 svn_re = re.compile('^Revision:\s+(\d+)', re.M) |
| 33 match = svn_re.search(proc.stdout.read()) |
| 34 if match: |
| 35 return match.group(1) |
62 return None | 36 return None |
63 | 37 |
64 | 38 |
65 def fetch_change(default_lastchange): | 39 def FetchChange(default_lastchange): |
66 """ | 40 """ |
67 Returns the last change, from some appropriate revision control system. | 41 Returns the last change, from some appropriate revision control system. |
68 """ | 42 """ |
69 change = svn_fetch_revision() | 43 change = FetchSVNRevision(['svn', 'info']) |
70 if not change and sys.platform in ('linux2',): | 44 if not change and sys.platform in ('linux2',): |
71 change = git_fetch_id() | 45 change = FetchSVNRevision(['git', 'svn', 'info']) |
72 if not change: | 46 if not change: |
73 if default_lastchange and os.path.exists(default_lastchange): | 47 if default_lastchange and os.path.exists(default_lastchange): |
74 change = open(default_lastchange, 'r').read().strip() | 48 change = open(default_lastchange, 'r').read().strip() |
75 else: | 49 else: |
76 change = '0' | 50 change = '0' |
77 return change | 51 return change |
78 | 52 |
79 | 53 |
80 def write_if_changed(file_name, contents): | 54 def WriteIfChanged(file_name, contents): |
81 """ | 55 """ |
82 Writes the specified contents to the specified file_name | 56 Writes the specified contents to the specified file_name |
83 iff the contents are different than the current contents. | 57 iff the contents are different than the current contents. |
84 """ | 58 """ |
85 try: | 59 try: |
86 old_contents = open(file_name, 'r').read() | 60 old_contents = open(file_name, 'r').read() |
87 except EnvironmentError: | 61 except EnvironmentError: |
88 pass | 62 pass |
89 else: | 63 else: |
90 if contents == old_contents: | 64 if contents == old_contents: |
(...skipping 16 matching lines...) Expand all Loading... |
107 out_file = opts.output | 81 out_file = opts.output |
108 | 82 |
109 while len(args) and out_file is None: | 83 while len(args) and out_file is None: |
110 if out_file is None: | 84 if out_file is None: |
111 out_file = args.pop(0) | 85 out_file = args.pop(0) |
112 if args: | 86 if args: |
113 sys.stderr.write('Unexpected arguments: %r\n\n' % args) | 87 sys.stderr.write('Unexpected arguments: %r\n\n' % args) |
114 parser.print_help() | 88 parser.print_help() |
115 sys.exit(2) | 89 sys.exit(2) |
116 | 90 |
117 change = fetch_change(opts.default_lastchange) | 91 change = FetchChange(opts.default_lastchange) |
118 | 92 |
119 contents = "LASTCHANGE=%s\n" % change | 93 contents = "LASTCHANGE=%s\n" % change |
120 | 94 |
121 if out_file: | 95 if out_file: |
122 write_if_changed(out_file, contents) | 96 WriteIfChanged(out_file, contents) |
123 else: | 97 else: |
124 sys.stdout.write(contents) | 98 sys.stdout.write(contents) |
125 | 99 |
126 return 0 | 100 return 0 |
127 | 101 |
128 | 102 |
129 if __name__ == '__main__': | 103 if __name__ == '__main__': |
130 sys.exit(main()) | 104 sys.exit(main()) |
OLD | NEW |