OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env 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 lastchange.py -- Chromium revision fetching utility. | 7 lastchange.py -- Chromium revision fetching utility. |
8 """ | 8 """ |
9 | 9 |
10 import re | 10 import re |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 if proc.returncode == 0 and output: | 75 if proc.returncode == 0 and output: |
76 for line in reversed(output.splitlines()): | 76 for line in reversed(output.splitlines()): |
77 if line.startswith('Cr-Commit-Position:'): | 77 if line.startswith('Cr-Commit-Position:'): |
78 pos = line.rsplit()[-1].strip() | 78 pos = line.rsplit()[-1].strip() |
79 break | 79 break |
80 if hash_only or not pos: | 80 if hash_only or not pos: |
81 return VersionInfo('git', hsh) | 81 return VersionInfo('git', hsh) |
82 return VersionInfo('git', '%s-%s' % (hsh, pos)) | 82 return VersionInfo('git', '%s-%s' % (hsh, pos)) |
83 | 83 |
84 | 84 |
85 def FetchVersionInfo(default_lastchange, directory=None, | 85 def FetchVersionInfo(directory=None, |
86 directory_regex_prior_to_src_url='chrome|blink|svn', | 86 directory_regex_prior_to_src_url='chrome|blink|svn', |
87 go_deeper=False, hash_only=False): | 87 hash_only=False): |
88 """ | 88 """ |
89 Returns the last change (in the form of a branch, revision tuple), | 89 Returns the last change (in the form of a branch, revision tuple), |
90 from some appropriate revision control system. | 90 from some appropriate revision control system. |
91 """ | 91 """ |
92 svn_url_regex = re.compile( | 92 svn_url_regex = re.compile( |
93 r'.*/(' + directory_regex_prior_to_src_url + r')(/.*)') | 93 r'.*/(' + directory_regex_prior_to_src_url + r')(/.*)') |
94 | 94 |
95 version_info = FetchGitRevision(directory, hash_only) | 95 version_info = FetchGitRevision(directory, hash_only) |
96 if not version_info: | 96 if not version_info: |
97 if default_lastchange and os.path.exists(default_lastchange): | 97 version_info = VersionInfo(None, None) |
98 revision = open(default_lastchange, 'r').read().strip() | |
99 version_info = VersionInfo(None, revision) | |
100 else: | |
101 version_info = VersionInfo(None, None) | |
102 return version_info | 98 return version_info |
103 | 99 |
104 | 100 |
105 def GetHeaderGuard(path): | 101 def GetHeaderGuard(path): |
106 """ | 102 """ |
107 Returns the header #define guard for the given file path. | 103 Returns the header #define guard for the given file path. |
108 This treats everything after the last instance of "src/" as being a | 104 This treats everything after the last instance of "src/" as being a |
109 relevant part of the guard. If there is no "src/", then the entire path | 105 relevant part of the guard. If there is no "src/", then the entire path |
110 is used. | 106 is used. |
111 """ | 107 """ |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 return | 151 return |
156 os.unlink(file_name) | 152 os.unlink(file_name) |
157 open(file_name, 'w').write(contents) | 153 open(file_name, 'w').write(contents) |
158 | 154 |
159 | 155 |
160 def main(argv=None): | 156 def main(argv=None): |
161 if argv is None: | 157 if argv is None: |
162 argv = sys.argv | 158 argv = sys.argv |
163 | 159 |
164 parser = optparse.OptionParser(usage="lastchange.py [options]") | 160 parser = optparse.OptionParser(usage="lastchange.py [options]") |
165 parser.add_option("-d", "--default-lastchange", metavar="FILE", | |
166 help="Default last change input FILE.") | |
167 parser.add_option("-m", "--version-macro", | 161 parser.add_option("-m", "--version-macro", |
168 help="Name of C #define when using --header. Defaults to " + | 162 help="Name of C #define when using --header. Defaults to " + |
169 "LAST_CHANGE.", | 163 "LAST_CHANGE.", |
170 default="LAST_CHANGE") | 164 default="LAST_CHANGE") |
171 parser.add_option("-o", "--output", metavar="FILE", | 165 parser.add_option("-o", "--output", metavar="FILE", |
172 help="Write last change to FILE. " + | 166 help="Write last change to FILE. " + |
173 "Can be combined with --header to write both files.") | 167 "Can be combined with --header to write both files.") |
174 parser.add_option("", "--header", metavar="FILE", | 168 parser.add_option("", "--header", metavar="FILE", |
175 help="Write last change to FILE as a C/C++ header. " + | 169 help="Write last change to FILE as a C/C++ header. " + |
176 "Can be combined with --output to write both files.") | 170 "Can be combined with --output to write both files.") |
177 parser.add_option("--revision-only", action='store_true', | 171 parser.add_option("--revision-only", action='store_true', |
178 help="Just print the SVN revision number. Overrides any " + | 172 help="Just print the SVN revision number. Overrides any " + |
179 "file-output-related options.") | 173 "file-output-related options.") |
180 parser.add_option("-s", "--source-dir", metavar="DIR", | 174 parser.add_option("-s", "--source-dir", metavar="DIR", |
181 help="Use repository in the given directory.") | 175 help="Use repository in the given directory.") |
182 parser.add_option("--git-svn-go-deeper", action='store_true', | |
183 help="In a Git-SVN repo, dig down to the last committed " + | |
184 "SVN change (historic behaviour).") | |
185 parser.add_option("--git-hash-only", action="store_true", | 176 parser.add_option("--git-hash-only", action="store_true", |
186 help="In a Git repo with commit positions, report only " + | 177 help="In a Git repo with commit positions, report only " + |
187 "the hash of the latest commit with a position.") | 178 "the hash of the latest commit with a position.") |
188 opts, args = parser.parse_args(argv[1:]) | 179 opts, args = parser.parse_args(argv[1:]) |
189 | 180 |
190 out_file = opts.output | 181 out_file = opts.output |
191 header = opts.header | 182 header = opts.header |
192 | 183 |
193 while len(args) and out_file is None: | 184 while len(args) and out_file is None: |
194 if out_file is None: | 185 if out_file is None: |
195 out_file = args.pop(0) | 186 out_file = args.pop(0) |
196 if args: | 187 if args: |
197 sys.stderr.write('Unexpected arguments: %r\n\n' % args) | 188 sys.stderr.write('Unexpected arguments: %r\n\n' % args) |
198 parser.print_help() | 189 parser.print_help() |
199 sys.exit(2) | 190 sys.exit(2) |
200 | 191 |
201 if opts.source_dir: | 192 if opts.source_dir: |
202 src_dir = opts.source_dir | 193 src_dir = opts.source_dir |
203 else: | 194 else: |
204 src_dir = os.path.dirname(os.path.abspath(__file__)) | 195 src_dir = os.path.dirname(os.path.abspath(__file__)) |
205 | 196 |
206 version_info = FetchVersionInfo(opts.default_lastchange, | 197 version_info = FetchVersionInfo(directory=src_dir, |
207 directory=src_dir, | |
208 go_deeper=opts.git_svn_go_deeper, | |
209 hash_only=opts.git_hash_only) | 198 hash_only=opts.git_hash_only) |
210 | 199 |
211 if version_info.revision == None: | 200 if version_info.revision == None: |
212 version_info.revision = '0' | 201 version_info.revision = '0' |
213 | 202 |
214 if opts.revision_only: | 203 if opts.revision_only: |
215 print version_info.revision | 204 print version_info.revision |
216 else: | 205 else: |
217 contents = "LASTCHANGE=%s\n" % version_info.revision | 206 contents = "LASTCHANGE=%s\n" % version_info.revision |
218 if not out_file and not opts.header: | 207 if not out_file and not opts.header: |
219 sys.stdout.write(contents) | 208 sys.stdout.write(contents) |
220 else: | 209 else: |
221 if out_file: | 210 if out_file: |
222 WriteIfChanged(out_file, contents) | 211 WriteIfChanged(out_file, contents) |
223 if header: | 212 if header: |
224 WriteIfChanged(header, | 213 WriteIfChanged(header, |
225 GetHeaderContents(header, opts.version_macro, | 214 GetHeaderContents(header, opts.version_macro, |
226 version_info.revision)) | 215 version_info.revision)) |
227 | 216 |
228 return 0 | 217 return 0 |
229 | 218 |
230 | 219 |
231 if __name__ == '__main__': | 220 if __name__ == '__main__': |
232 sys.exit(main()) | 221 sys.exit(main()) |
OLD | NEW |