Commit b13d29ee authored by evan@chromium.org's avatar evan@chromium.org

lastchange.py: fix style, unify git codepath

1) Change style to match style guide.
2) Use "git svn info" so we can use the same parsing code for
   both git and svn.

TEST=ran manually

Review URL: http://codereview.chromium.org/6263009

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@72074 0039d316-1c4b-4281-b951-d872f2087c98
parent c1590348
...@@ -14,61 +14,35 @@ import subprocess ...@@ -14,61 +14,35 @@ import subprocess
import sys import sys
def svn_fetch_revision(): def FetchSVNRevision(command):
""" """
Fetch the Subversion revision for the local tree. Fetch the Subversion revision for the local tree.
Errors are swallowed. Errors are swallowed.
""" """
try: try:
p = subprocess.Popen(['svn', 'info'], proc = subprocess.Popen(command,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, stderr=subprocess.PIPE,
shell=(sys.platform=='win32')) shell=(sys.platform=='win32'))
except OSError, e: except OSError:
# 'svn' is apparently either not installed or not executable. # command is apparently either not installed or not executable.
return None return None
revision = None if proc:
if p:
svn_re = re.compile('^Revision:\s+(\d+)', re.M) svn_re = re.compile('^Revision:\s+(\d+)', re.M)
m = svn_re.search(p.stdout.read()) match = svn_re.search(proc.stdout.read())
if m:
revision = m.group(1)
return revision
def git_fetch_id():
"""
Fetch the GIT identifier for the local tree.
Errors are swallowed.
"""
git_re = re.compile('^\s*git-svn-id:\s+(\S+)@(\d+)', re.M)
try:
proc = subprocess.Popen(['git', 'log', '-999'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=(sys.platform=='win32'))
for line in proc.stdout:
match = git_re.search(line)
if match: if match:
id = match.group(2) return match.group(1)
if id:
proc.stdout.close() # Cut pipe.
return id
except OSError:
# 'git' is apparently either not installed or not executable.
pass
return None return None
def fetch_change(default_lastchange): def FetchChange(default_lastchange):
""" """
Returns the last change, from some appropriate revision control system. Returns the last change, from some appropriate revision control system.
""" """
change = svn_fetch_revision() change = FetchSVNRevision(['svn', 'info'])
if not change and sys.platform in ('linux2',): if not change and sys.platform in ('linux2',):
change = git_fetch_id() change = FetchSVNRevision(['git', 'svn', 'info'])
if not change: if not change:
if default_lastchange and os.path.exists(default_lastchange): if default_lastchange and os.path.exists(default_lastchange):
change = open(default_lastchange, 'r').read().strip() change = open(default_lastchange, 'r').read().strip()
...@@ -77,7 +51,7 @@ def fetch_change(default_lastchange): ...@@ -77,7 +51,7 @@ def fetch_change(default_lastchange):
return change return change
def write_if_changed(file_name, contents): def WriteIfChanged(file_name, contents):
""" """
Writes the specified contents to the specified file_name Writes the specified contents to the specified file_name
iff the contents are different than the current contents. iff the contents are different than the current contents.
...@@ -114,12 +88,12 @@ def main(argv=None): ...@@ -114,12 +88,12 @@ def main(argv=None):
parser.print_help() parser.print_help()
sys.exit(2) sys.exit(2)
change = fetch_change(opts.default_lastchange) change = FetchChange(opts.default_lastchange)
contents = "LASTCHANGE=%s\n" % change contents = "LASTCHANGE=%s\n" % change
if out_file: if out_file:
write_if_changed(out_file, contents) WriteIfChanged(out_file, contents)
else: else:
sys.stdout.write(contents) sys.stdout.write(contents)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment