1#!/usr/bin/env python
2
3# Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
4#
5# Use of this source code is governed by a BSD-style license
6# that can be found in the LICENSE file in the root of the source
7# tree. An additional intellectual property rights grant can be found
8# in the file PATENTS.  All contributing project authors may
9# be found in the AUTHORS file in the root of the source tree.
10
11"""Get svn revision of working copy
12
13This script tries to get the svn revision as much as it can. It supports
14both git-svn and svn. It will fail if not in a git-svn or svn repository;
15in this case the script will return "n/a".
16This script is a simplified version of lastchange.py which is in Chromium's
17src/build/util folder.
18"""
19
20import os
21import subprocess
22import sys
23
24def popen_cmd_and_get_output(cmd, directory):
25  """Return (status, output) of executing cmd in a shell."""
26  try:
27    proc = subprocess.Popen(cmd,
28                            stdout=subprocess.PIPE,
29                            stderr=subprocess.PIPE,
30                            cwd=directory,
31                            shell=(sys.platform=='win32'))
32  except OSError:
33    # command is apparently either not installed or not executable.
34    return None
35  if not proc:
36    return None
37
38  for line in proc.stdout:
39    line = line.strip()
40    if not line:
41      continue
42    words = line.split()
43    for index, word in enumerate(words):
44      if word == "Revision:":
45        return words[index+1]
46  # return None if cannot find keyword Revision
47  return None
48
49def main():
50  directory = os.path.dirname(sys.argv[0]);
51  version = popen_cmd_and_get_output(['git', 'svn', 'info'], directory)
52  if version == None:
53    version = popen_cmd_and_get_output(['svn', 'info'], directory)
54    if version == None:
55      print "n/a"
56      return 0
57  print version
58  return 0
59
60if __name__ == '__main__':
61  sys.exit(main())
62