media/webrtc/trunk/build/util/lastchange.py

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rwxr-xr-x

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 #!/usr/bin/env python
michael@0 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
michael@0 3 # Use of this source code is governed by a BSD-style license that can be
michael@0 4 # found in the LICENSE file.
michael@0 5
michael@0 6 """
michael@0 7 lastchange.py -- Chromium revision fetching utility.
michael@0 8 """
michael@0 9
michael@0 10 import re
michael@0 11 import optparse
michael@0 12 import os
michael@0 13 import subprocess
michael@0 14 import sys
michael@0 15
michael@0 16 _GIT_SVN_ID_REGEX = re.compile(r'.*git-svn-id:\s*([^@]*)@([0-9]+)', re.DOTALL)
michael@0 17
michael@0 18 class VersionInfo(object):
michael@0 19 def __init__(self, url, revision):
michael@0 20 self.url = url
michael@0 21 self.revision = revision
michael@0 22
michael@0 23
michael@0 24 def FetchSVNRevision(directory, svn_url_regex):
michael@0 25 """
michael@0 26 Fetch the Subversion branch and revision for a given directory.
michael@0 27
michael@0 28 Errors are swallowed.
michael@0 29
michael@0 30 Returns:
michael@0 31 A VersionInfo object or None on error.
michael@0 32 """
michael@0 33 try:
michael@0 34 proc = subprocess.Popen(['svn', 'info'],
michael@0 35 stdout=subprocess.PIPE,
michael@0 36 stderr=subprocess.PIPE,
michael@0 37 cwd=directory,
michael@0 38 shell=(sys.platform=='win32'))
michael@0 39 except OSError:
michael@0 40 # command is apparently either not installed or not executable.
michael@0 41 return None
michael@0 42 if not proc:
michael@0 43 return None
michael@0 44
michael@0 45 attrs = {}
michael@0 46 for line in proc.stdout:
michael@0 47 line = line.strip()
michael@0 48 if not line:
michael@0 49 continue
michael@0 50 key, val = line.split(': ', 1)
michael@0 51 attrs[key] = val
michael@0 52
michael@0 53 try:
michael@0 54 match = svn_url_regex.search(attrs['URL'])
michael@0 55 if match:
michael@0 56 url = match.group(2)
michael@0 57 else:
michael@0 58 url = ''
michael@0 59 revision = attrs['Revision']
michael@0 60 except KeyError:
michael@0 61 return None
michael@0 62
michael@0 63 return VersionInfo(url, revision)
michael@0 64
michael@0 65
michael@0 66 def RunGitCommand(directory, command):
michael@0 67 """
michael@0 68 Launches git subcommand.
michael@0 69
michael@0 70 Errors are swallowed.
michael@0 71
michael@0 72 Returns:
michael@0 73 A process object or None.
michael@0 74 """
michael@0 75 command = ['git'] + command
michael@0 76 # Force shell usage under cygwin. This is a workaround for
michael@0 77 # mysterious loss of cwd while invoking cygwin's git.
michael@0 78 # We can't just pass shell=True to Popen, as under win32 this will
michael@0 79 # cause CMD to be used, while we explicitly want a cygwin shell.
michael@0 80 if sys.platform == 'cygwin':
michael@0 81 command = ['sh', '-c', ' '.join(command)]
michael@0 82 try:
michael@0 83 proc = subprocess.Popen(command,
michael@0 84 stdout=subprocess.PIPE,
michael@0 85 stderr=subprocess.PIPE,
michael@0 86 cwd=directory,
michael@0 87 shell=(sys.platform=='win32'))
michael@0 88 return proc
michael@0 89 except OSError:
michael@0 90 return None
michael@0 91
michael@0 92
michael@0 93 def FetchGitRevision(directory):
michael@0 94 """
michael@0 95 Fetch the Git hash for a given directory.
michael@0 96
michael@0 97 Errors are swallowed.
michael@0 98
michael@0 99 Returns:
michael@0 100 A VersionInfo object or None on error.
michael@0 101 """
michael@0 102 proc = RunGitCommand(directory, ['rev-parse', 'HEAD'])
michael@0 103 if proc:
michael@0 104 output = proc.communicate()[0].strip()
michael@0 105 if proc.returncode == 0 and output:
michael@0 106 return VersionInfo('git', output[:7])
michael@0 107 return None
michael@0 108
michael@0 109
michael@0 110 def FetchGitSVNURLAndRevision(directory, svn_url_regex):
michael@0 111 """
michael@0 112 Fetch the Subversion URL and revision through Git.
michael@0 113
michael@0 114 Errors are swallowed.
michael@0 115
michael@0 116 Returns:
michael@0 117 A tuple containing the Subversion URL and revision.
michael@0 118 """
michael@0 119 proc = RunGitCommand(directory, ['log', '-1',
michael@0 120 '--grep=git-svn-id', '--format=%b'])
michael@0 121 if proc:
michael@0 122 output = proc.communicate()[0].strip()
michael@0 123 if proc.returncode == 0 and output:
michael@0 124 # Extract the latest SVN revision and the SVN URL.
michael@0 125 # The target line is the last "git-svn-id: ..." line like this:
michael@0 126 # git-svn-id: svn://svn.chromium.org/chrome/trunk/src@85528 0039d316....
michael@0 127 match = _GIT_SVN_ID_REGEX.search(output)
michael@0 128 if match:
michael@0 129 revision = match.group(2)
michael@0 130 url_match = svn_url_regex.search(match.group(1))
michael@0 131 if url_match:
michael@0 132 url = url_match.group(2)
michael@0 133 else:
michael@0 134 url = ''
michael@0 135 return url, revision
michael@0 136 return None, None
michael@0 137
michael@0 138
michael@0 139 def FetchGitSVNRevision(directory, svn_url_regex):
michael@0 140 """
michael@0 141 Fetch the Git-SVN identifier for the local tree.
michael@0 142
michael@0 143 Errors are swallowed.
michael@0 144 """
michael@0 145 url, revision = FetchGitSVNURLAndRevision(directory, svn_url_regex)
michael@0 146 if url and revision:
michael@0 147 return VersionInfo(url, revision)
michael@0 148 return None
michael@0 149
michael@0 150
michael@0 151 def FetchVersionInfo(default_lastchange, directory=None,
michael@0 152 directory_regex_prior_to_src_url='chrome|svn'):
michael@0 153 """
michael@0 154 Returns the last change (in the form of a branch, revision tuple),
michael@0 155 from some appropriate revision control system.
michael@0 156 """
michael@0 157 svn_url_regex = re.compile(
michael@0 158 r'.*/(' + directory_regex_prior_to_src_url + r')(/.*)')
michael@0 159
michael@0 160 version_info = (FetchSVNRevision(directory, svn_url_regex) or
michael@0 161 FetchGitSVNRevision(directory, svn_url_regex) or
michael@0 162 FetchGitRevision(directory))
michael@0 163 if not version_info:
michael@0 164 if default_lastchange and os.path.exists(default_lastchange):
michael@0 165 revision = open(default_lastchange, 'r').read().strip()
michael@0 166 version_info = VersionInfo(None, revision)
michael@0 167 else:
michael@0 168 version_info = VersionInfo(None, None)
michael@0 169 return version_info
michael@0 170
michael@0 171
michael@0 172 def WriteIfChanged(file_name, contents):
michael@0 173 """
michael@0 174 Writes the specified contents to the specified file_name
michael@0 175 iff the contents are different than the current contents.
michael@0 176 """
michael@0 177 try:
michael@0 178 old_contents = open(file_name, 'r').read()
michael@0 179 except EnvironmentError:
michael@0 180 pass
michael@0 181 else:
michael@0 182 if contents == old_contents:
michael@0 183 return
michael@0 184 os.unlink(file_name)
michael@0 185 open(file_name, 'w').write(contents)
michael@0 186
michael@0 187
michael@0 188 def main(argv=None):
michael@0 189 if argv is None:
michael@0 190 argv = sys.argv
michael@0 191
michael@0 192 parser = optparse.OptionParser(usage="lastchange.py [options]")
michael@0 193 parser.add_option("-d", "--default-lastchange", metavar="FILE",
michael@0 194 help="default last change input FILE")
michael@0 195 parser.add_option("-o", "--output", metavar="FILE",
michael@0 196 help="write last change to FILE")
michael@0 197 parser.add_option("--revision-only", action='store_true',
michael@0 198 help="just print the SVN revision number")
michael@0 199 opts, args = parser.parse_args(argv[1:])
michael@0 200
michael@0 201 out_file = opts.output
michael@0 202
michael@0 203 while len(args) and out_file is None:
michael@0 204 if out_file is None:
michael@0 205 out_file = args.pop(0)
michael@0 206 if args:
michael@0 207 sys.stderr.write('Unexpected arguments: %r\n\n' % args)
michael@0 208 parser.print_help()
michael@0 209 sys.exit(2)
michael@0 210
michael@0 211 version_info = FetchVersionInfo(opts.default_lastchange,
michael@0 212 os.path.dirname(sys.argv[0]))
michael@0 213
michael@0 214 if version_info.revision == None:
michael@0 215 version_info.revision = '0'
michael@0 216
michael@0 217 if opts.revision_only:
michael@0 218 print version_info.revision
michael@0 219 else:
michael@0 220 contents = "LASTCHANGE=%s\n" % version_info.revision
michael@0 221 if out_file:
michael@0 222 WriteIfChanged(out_file, contents)
michael@0 223 else:
michael@0 224 sys.stdout.write(contents)
michael@0 225
michael@0 226 return 0
michael@0 227
michael@0 228
michael@0 229 if __name__ == '__main__':
michael@0 230 sys.exit(main())

mercurial