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 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
161 FetchGitSVNRevision(directory, svn_url_regex) or | 161 FetchGitSVNRevision(directory, svn_url_regex) or |
162 FetchGitRevision(directory)) | 162 FetchGitRevision(directory)) |
163 if not version_info: | 163 if not version_info: |
164 if default_lastchange and os.path.exists(default_lastchange): | 164 if default_lastchange and os.path.exists(default_lastchange): |
165 revision = open(default_lastchange, 'r').read().strip() | 165 revision = open(default_lastchange, 'r').read().strip() |
166 version_info = VersionInfo(None, revision) | 166 version_info = VersionInfo(None, revision) |
167 else: | 167 else: |
168 version_info = VersionInfo(None, None) | 168 version_info = VersionInfo(None, None) |
169 return version_info | 169 return version_info |
170 | 170 |
171 def GetHeaderGuard(path): | |
172 """ | |
173 Returns the header #define guard for the given file path. | |
174 This treats everything after the last instance of "src/" as being a | |
175 relevant part of the guard. If there is no "src/", then the entire path | |
176 is used. | |
177 """ | |
178 src_index = path.rfind('src/') | |
179 if src_index != -1: | |
180 guard = path[src_index + 4:] | |
181 else: | |
182 guard = path | |
183 guard = guard.upper() | |
184 return guard.replace('/', '_').replace('.', '_').replace('\\', '_') + '_' | |
viettrungluu
2013/11/06 18:39:22
Maybe it'd be better to just replace all non-alpha
brettw
2013/11/07 18:47:33
I didn't do this because it doesn't come up, it's
| |
185 | |
186 def GetHeaderContents(path, define, version): | |
187 """ | |
188 Returns what the contents of the header file should be that indicate the given | |
189 revision. Note that the #define is specified as a string, even though it's | |
190 currently always a SVN revision number, in case we need to move to git hashes. | |
191 """ | |
192 header_guard = GetHeaderGuard(path) | |
193 | |
194 header_contents = """/* Generated by lastchange.py, do not edit.*/ | |
195 | |
196 #ifndef %(header_guard)s | |
197 #define %(header_guard)s | |
198 | |
199 #define %(define)s "%(version)s" | |
200 | |
201 #endif // %(header_guard)s | |
202 """ | |
203 header_contents = header_contents % { 'header_guard': header_guard, | |
204 'define': define, | |
205 'version': version } | |
206 return header_contents | |
171 | 207 |
172 def WriteIfChanged(file_name, contents): | 208 def WriteIfChanged(file_name, contents): |
173 """ | 209 """ |
174 Writes the specified contents to the specified file_name | 210 Writes the specified contents to the specified file_name |
175 iff the contents are different than the current contents. | 211 iff the contents are different than the current contents. |
176 """ | 212 """ |
177 try: | 213 try: |
178 old_contents = open(file_name, 'r').read() | 214 old_contents = open(file_name, 'r').read() |
179 except EnvironmentError: | 215 except EnvironmentError: |
180 pass | 216 pass |
181 else: | 217 else: |
182 if contents == old_contents: | 218 if contents == old_contents: |
183 return | 219 return |
184 os.unlink(file_name) | 220 os.unlink(file_name) |
185 open(file_name, 'w').write(contents) | 221 open(file_name, 'w').write(contents) |
186 | 222 |
187 | 223 |
188 def main(argv=None): | 224 def main(argv=None): |
189 if argv is None: | 225 if argv is None: |
190 argv = sys.argv | 226 argv = sys.argv |
191 | 227 |
192 parser = optparse.OptionParser(usage="lastchange.py [options]") | 228 parser = optparse.OptionParser(usage="lastchange.py [options]") |
193 parser.add_option("-d", "--default-lastchange", metavar="FILE", | 229 parser.add_option("-d", "--default-lastchange", metavar="FILE", |
194 help="default last change input FILE") | 230 help="Default last change input FILE.") |
231 parser.add_option("", "--define", | |
viettrungluu
2013/11/06 18:39:22
Maybe this should be "--version-macro" or "--versi
| |
232 help="Name of C #define when using --header. Defaults to " + | |
233 "LASTCHANGE.", | |
234 default="LASTCHANGE") | |
viettrungluu
2013/11/06 18:39:22
Why is your default ("LASTCHANGE") something other
| |
195 parser.add_option("-o", "--output", metavar="FILE", | 235 parser.add_option("-o", "--output", metavar="FILE", |
196 help="write last change to FILE") | 236 help="Write last change to FILE. " + |
237 "Can be combined with --header to write both files.") | |
238 parser.add_option("", "--header", metavar="FILE", | |
239 help="Write last change to FILE as a C/C++ header. " + | |
240 "Can be combined with --output to write both files.") | |
197 parser.add_option("--revision-only", action='store_true', | 241 parser.add_option("--revision-only", action='store_true', |
198 help="just print the SVN revision number") | 242 help="Just print the SVN revision number. Overrides any " + |
243 "file-output-related options.") | |
199 parser.add_option("-s", "--source-dir", metavar="DIR", | 244 parser.add_option("-s", "--source-dir", metavar="DIR", |
200 help="use repository in the given directory") | 245 help="Use repository in the given directory.") |
201 opts, args = parser.parse_args(argv[1:]) | 246 opts, args = parser.parse_args(argv[1:]) |
202 | 247 |
203 out_file = opts.output | 248 out_file = opts.output |
249 header = opts.header | |
204 | 250 |
205 while len(args) and out_file is None: | 251 while len(args) and out_file is None: |
206 if out_file is None: | 252 if out_file is None: |
207 out_file = args.pop(0) | 253 out_file = args.pop(0) |
208 if args: | 254 if args: |
209 sys.stderr.write('Unexpected arguments: %r\n\n' % args) | 255 sys.stderr.write('Unexpected arguments: %r\n\n' % args) |
210 parser.print_help() | 256 parser.print_help() |
211 sys.exit(2) | 257 sys.exit(2) |
212 | 258 |
213 if opts.source_dir: | 259 if opts.source_dir: |
214 src_dir = opts.source_dir | 260 src_dir = opts.source_dir |
215 else: | 261 else: |
216 src_dir = os.path.dirname(os.path.abspath(__file__)) | 262 src_dir = os.path.dirname(os.path.abspath(__file__)) |
217 | 263 |
218 version_info = FetchVersionInfo(opts.default_lastchange, src_dir) | 264 version_info = FetchVersionInfo(opts.default_lastchange, src_dir) |
219 | 265 |
220 if version_info.revision == None: | 266 if version_info.revision == None: |
221 version_info.revision = '0' | 267 version_info.revision = '0' |
222 | 268 |
223 if opts.revision_only: | 269 if opts.revision_only: |
224 print version_info.revision | 270 print version_info.revision |
225 else: | 271 else: |
226 contents = "LASTCHANGE=%s\n" % version_info.revision | 272 contents = "LASTCHANGE=%s\n" % version_info.revision |
227 if out_file: | 273 if not out_file and not opts.header: |
228 WriteIfChanged(out_file, contents) | 274 sys.stdout.write(contents) |
229 else: | 275 else: |
230 sys.stdout.write(contents) | 276 if out_file: |
277 WriteIfChanged(out_file, contents) | |
278 if header: | |
279 WriteIfChanged(header, | |
280 GetHeaderContents(header, opts.define, | |
281 version_info.revision)) | |
231 | 282 |
232 return 0 | 283 return 0 |
233 | 284 |
234 | 285 |
235 if __name__ == '__main__': | 286 if __name__ == '__main__': |
236 sys.exit(main()) | 287 sys.exit(main()) |
OLD | NEW |