| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2009 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2009 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 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 return None | 53 return None |
| 54 id = None | 54 id = None |
| 55 if p: | 55 if p: |
| 56 git_re = re.compile('^\s*git-svn-id:\s+(\S+)@(\d+)', re.M) | 56 git_re = re.compile('^\s*git-svn-id:\s+(\S+)@(\d+)', re.M) |
| 57 m = git_re.search(p.stdout.read()) | 57 m = git_re.search(p.stdout.read()) |
| 58 if m: | 58 if m: |
| 59 id = m.group(2) | 59 id = m.group(2) |
| 60 return id | 60 return id |
| 61 | 61 |
| 62 | 62 |
| 63 def fetch_change(): | 63 def fetch_change(default_lastchange): |
| 64 """ | 64 """ |
| 65 Returns the last change, from some appropriate revision control system. | 65 Returns the last change, from some appropriate revision control system. |
| 66 """ | 66 """ |
| 67 change = svn_fetch_revision() | 67 change = svn_fetch_revision() |
| 68 if not change and sys.platform in ('linux2',): | 68 if not change and sys.platform in ('linux2',): |
| 69 change = git_fetch_id() | 69 change = git_fetch_id() |
| 70 if not change: | 70 if not change: |
| 71 change = '0' | 71 if default_lastchange and os.path.exists(default_lastchange): |
| 72 change = open(default_lastchange, 'r').read().strip() |
| 73 else: |
| 74 change = '0' |
| 72 return change | 75 return change |
| 73 | 76 |
| 74 | 77 |
| 75 def write_if_changed(file_name, contents): | 78 def write_if_changed(file_name, contents): |
| 76 """ | 79 """ |
| 77 Writes the specified contents to the specified file_name | 80 Writes the specified contents to the specified file_name |
| 78 iff the contents are different than the current contents. | 81 iff the contents are different than the current contents. |
| 79 """ | 82 """ |
| 80 try: | 83 try: |
| 81 old_contents = open(file_name, 'r').read() | 84 old_contents = open(file_name, 'r').read() |
| 82 except EnvironmentError: | 85 except EnvironmentError: |
| 83 pass | 86 pass |
| 84 else: | 87 else: |
| 85 if contents == old_contents: | 88 if contents == old_contents: |
| 86 return | 89 return |
| 87 os.unlink(file_name) | 90 os.unlink(file_name) |
| 88 open(file_name, 'w').write(contents) | 91 open(file_name, 'w').write(contents) |
| 89 | 92 |
| 90 | 93 |
| 91 def main(argv=None): | 94 def main(argv=None): |
| 92 if argv is None: | 95 if argv is None: |
| 93 argv = sys.argv | 96 argv = sys.argv |
| 94 | 97 |
| 95 parser = optparse.OptionParser(usage="lastchange.py [-h] [[-o] FILE]") | 98 parser = optparse.OptionParser(usage="lastchange.py [-h] [[-o] FILE]") |
| 99 parser.add_option("-d", "--default-lastchange", metavar="FILE", |
| 100 help="default last change input FILE") |
| 96 parser.add_option("-o", "--output", metavar="FILE", | 101 parser.add_option("-o", "--output", metavar="FILE", |
| 97 help="write last change to FILE") | 102 help="write last change to FILE") |
| 98 opts, args = parser.parse_args(argv[1:]) | 103 opts, args = parser.parse_args(argv[1:]) |
| 99 | 104 |
| 100 out_file = opts.output | 105 out_file = opts.output |
| 101 | 106 |
| 102 while len(args) and out_file is None: | 107 while len(args) and out_file is None: |
| 103 if out_file is None: | 108 if out_file is None: |
| 104 out_file = args.pop(0) | 109 out_file = args.pop(0) |
| 105 if args: | 110 if args: |
| 106 sys.stderr.write('Unexpected arguments: %r\n\n' % args) | 111 sys.stderr.write('Unexpected arguments: %r\n\n' % args) |
| 107 parser.print_help() | 112 parser.print_help() |
| 108 sys.exit(2) | 113 sys.exit(2) |
| 109 | 114 |
| 110 change = fetch_change() | 115 change = fetch_change(opts.default_lastchange) |
| 111 | 116 |
| 112 contents = "LASTCHANGE=%s\n" % change | 117 contents = "LASTCHANGE=%s\n" % change |
| 113 | 118 |
| 114 if out_file: | 119 if out_file: |
| 115 write_if_changed(out_file, contents) | 120 write_if_changed(out_file, contents) |
| 116 else: | 121 else: |
| 117 sys.stdout.write(contents) | 122 sys.stdout.write(contents) |
| 118 | 123 |
| 119 return 0 | 124 return 0 |
| 120 | 125 |
| 121 | 126 |
| 122 if __name__ == '__main__': | 127 if __name__ == '__main__': |
| 123 sys.exit(main()) | 128 sys.exit(main()) |
| OLD | NEW |