OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 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 |
| 4 # found in the LICENSE file. |
| 5 |
| 6 """ |
| 7 lastchange.py -- Chromium revision fetching utility. |
| 8 """ |
| 9 |
| 10 import optparse |
| 11 import os |
| 12 import re |
| 13 import subprocess |
| 14 import sys |
| 15 |
| 16 |
| 17 def svn_fetch_revision(): |
| 18 """ |
| 19 Fetch the Subversion revision for the local tree. |
| 20 |
| 21 Errors are swallowed. |
| 22 """ |
| 23 try: |
| 24 p = subprocess.Popen(['svn', 'info'], |
| 25 stdout=subprocess.PIPE, |
| 26 stderr=subprocess.PIPE) |
| 27 except OSError: |
| 28 # 'svn' is apparently either not installed or not executable. |
| 29 return None |
| 30 revision = None |
| 31 if p: |
| 32 svn_re = re.compile('^Revision:\s+(\S+)$', re.M) |
| 33 m = svn_re.search(p.stdout.read()) |
| 34 if m: |
| 35 revision = m.group(1) |
| 36 return revision |
| 37 |
| 38 |
| 39 def git_fetch_id(): |
| 40 """ |
| 41 Fetch the GIT identifier for the local tree. |
| 42 |
| 43 Errors are swallowed. |
| 44 """ |
| 45 try: |
| 46 p = subprocess.Popen(['git', 'log', '-1'], |
| 47 stdout=subprocess.PIPE, |
| 48 stderr=subprocess.PIPE) |
| 49 except OSError: |
| 50 # 'git' is apparently either not installed or not executable. |
| 51 return None |
| 52 id = None |
| 53 if p: |
| 54 git_re = re.compile('^\s*git-svn-id:\s+(\S+)@(\d+)', re.M) |
| 55 m = git_re.search(p.stdout.read()) |
| 56 if m: |
| 57 id = m.group(1) |
| 58 return id |
| 59 |
| 60 |
| 61 def fetch_change(): |
| 62 """ |
| 63 Returns the last change, from some appropriate revision control system. |
| 64 """ |
| 65 change = svn_fetch_revision() |
| 66 if not change and sys.platform in ('linux2',): |
| 67 change = git_fetch_id() |
| 68 if not change: |
| 69 change = '0' |
| 70 return change |
| 71 |
| 72 |
| 73 def write_if_changed(file_name, contents): |
| 74 """ |
| 75 Writes the specified contents to the specified file_name |
| 76 iff the contents are different than the current contents. |
| 77 """ |
| 78 try: |
| 79 old_contents = open(file_name, 'r').read() |
| 80 except EnvironmentError: |
| 81 pass |
| 82 else: |
| 83 if contents == old_contents: |
| 84 return |
| 85 os.unlink(file_name) |
| 86 open(file_name, 'w').write(contents) |
| 87 |
| 88 |
| 89 def main(argv=None): |
| 90 if argv is None: |
| 91 argv = sys.argv |
| 92 |
| 93 parser = optparse.OptionParser(usage="lastchange.py [-h] [[-o] FILE]") |
| 94 parser.add_option("-o", "--output", metavar="FILE", |
| 95 help="write last change to FILE") |
| 96 opts, args = parser.parse_args(argv[1:]) |
| 97 |
| 98 out_file = opts.output |
| 99 |
| 100 while len(args) and out_file is None: |
| 101 if out_file is None: |
| 102 out_file = args.pop(0) |
| 103 if args: |
| 104 sys.stderr.write('Unexpected arguments: %r\n\n' % args) |
| 105 parser.print_help() |
| 106 sys.exit(2) |
| 107 |
| 108 change = fetch_change() |
| 109 |
| 110 contents = "LASTCHANGE=%s\n" % change |
| 111 |
| 112 if out_file: |
| 113 write_if_changed(out_file, contents) |
| 114 else: |
| 115 sys.stdout.write(contents) |
| 116 |
| 117 return 0 |
| 118 |
| 119 |
| 120 if __name__ == '__main__': |
| 121 sys.exit(main()) |
OLD | NEW |