Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / native_client_sdk / src / build_tools / build_version.py
blobced1a2540d5cb0e85c113207a622edd069ef796c
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 """Small utility library of python functions used during SDK building.
6 """
8 import os
9 import re
10 import sys
12 # pylint: disable=E0602
14 # Reuse last change utility code.
15 SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
16 SRC_DIR = os.path.dirname(os.path.dirname(os.path.dirname(SCRIPT_DIR)))
17 sys.path.append(os.path.join(SRC_DIR, 'build/util'))
18 import lastchange
21 # Location of chrome's version file.
22 VERSION_PATH = os.path.join(SRC_DIR, 'chrome', 'VERSION')
25 def ChromeVersion():
26 '''Extract chrome version from src/chrome/VERSION + svn.
28 Returns:
29 Chrome version string or trunk + svn rev.
30 '''
31 info = FetchVersionInfo()
32 if info.url == 'refs/heads/master':
33 return 'trunk.%s' % info.revision
34 else:
35 return ChromeVersionNoTrunk()
38 def ChromeVersionNoTrunk():
39 '''Extract the chrome version from src/chrome/VERSION.
40 Ignore whether this is a trunk build.
42 Returns:
43 Chrome version string.
44 '''
45 exec(open(VERSION_PATH).read())
46 return '%s.%s.%s.%s' % (MAJOR, MINOR, BUILD, PATCH)
49 def ChromeMajorVersion():
50 '''Extract chrome major version from src/chrome/VERSION.
52 Returns:
53 Chrome major version.
54 '''
55 exec(open(VERSION_PATH, 'r').read())
56 return str(MAJOR)
59 def ChromeRevision():
60 '''Extract chrome revision from svn.
62 Now that the Chrome source-of-truth is git, this will return the
63 Cr-Commit-Position instead. fortunately, this value is equal to the SVN
64 revision if one exists.
66 Returns:
67 The Chrome revision as a string. e.g. "12345"
68 '''
69 return FetchVersionInfo().revision
72 def NaClRevision():
73 '''Extract NaCl revision from svn.
75 Returns:
76 The NaCl revision as a string. e.g. "12345"
77 '''
78 nacl_dir = os.path.join(SRC_DIR, 'native_client')
79 return FetchVersionInfo(nacl_dir, 'native_client').revision
82 def FetchVersionInfo(directory=None,
83 directory_regex_prior_to_src_url='chrome|blink|svn'):
84 """
85 Returns the last change (in the form of a branch, revision tuple),
86 from some appropriate revision control system.
88 TODO(binji): This is copied from lastchange.py. Remove this function and use
89 lastchange.py directly when the dust settles. (see crbug.com/406783)
90 """
91 svn_url_regex = re.compile(
92 r'.*/(' + directory_regex_prior_to_src_url + r')(/.*)')
94 version_info = (lastchange.FetchSVNRevision(directory, svn_url_regex) or
95 lastchange.FetchGitSVNRevision(directory, svn_url_regex) or
96 FetchGitCommitPosition(directory))
97 if not version_info:
98 version_info = lastchange.VersionInfo(None, None)
99 return version_info
102 def FetchGitCommitPosition(directory=None):
104 Return the "commit-position" of the Chromium git repo. This should be
105 equivalent to the SVN revision if one eixsts.
107 This is a copy of the (recently reverted) change in lastchange.py.
108 TODO(binji): Move this logic to lastchange.py when the dust settles.
109 (see crbug.com/406783)
111 proc = lastchange.RunGitCommand(directory,
112 ['show', '-s', '--format=%B', 'HEAD'])
113 if proc:
114 output = proc.communicate()[0]
115 if proc.returncode == 0 and output:
116 for line in reversed(output.splitlines()):
117 match = re.search('Cr-Commit-Position: (.*)@{#(\d+)}', line)
118 if match:
119 return lastchange.VersionInfo(match.group(1), match.group(2))
120 return lastchange.VersionInfo(None, None)