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.
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'))
21 # Location of chrome's version file.
22 VERSION_PATH
= os
.path
.join(SRC_DIR
, 'chrome', 'VERSION')
26 '''Extract chrome version from src/chrome/VERSION + svn.
29 Chrome version string or trunk + svn rev.
31 info
= FetchGitCommitPosition()
33 _
, ref
, revision
= ParseCommitPosition(info
.revision
)
34 if ref
== 'refs/heads/master':
35 return 'trunk.%s' % revision
36 return ChromeVersionNoTrunk()
39 def ChromeVersionNoTrunk():
40 '''Extract the chrome version from src/chrome/VERSION.
41 Ignore whether this is a trunk build.
44 Chrome version string.
46 exec(open(VERSION_PATH
).read())
47 return '%s.%s.%s.%s' % (MAJOR
, MINOR
, BUILD
, PATCH
)
50 def ChromeMajorVersion():
51 '''Extract chrome major version from src/chrome/VERSION.
56 exec(open(VERSION_PATH
, 'r').read())
61 '''Extract chrome revision from svn.
63 Now that the Chrome source-of-truth is git, this will return the
64 Cr-Commit-Position instead. Fortunately, this value is equal to the SVN
65 revision if one exists.
68 The Chrome revision as a string. e.g. "12345"
70 version
= FetchGitCommitPosition()
71 return ParseCommitPosition(version
.revision
)[2]
74 def ChromeCommitPosition():
75 '''Return the full git sha and commit position.
79 0178d4831bd36b5fb9ff477f03dc43b11626a6dc-refs/heads/master@{#292238}
81 return FetchGitCommitPosition().revision
85 '''Extract NaCl revision from svn.
88 The NaCl revision as a string. e.g. "12345"
90 nacl_dir
= os
.path
.join(SRC_DIR
, 'native_client')
91 return lastchange
.FetchVersionInfo(None, nacl_dir
, 'native_client').revision
94 def FetchGitCommitPosition(directory
=None):
95 '''Return the "commit-position" of the Chromium git repo. This should be
96 equivalent to the SVN revision if one exists.
99 for i
in xrange(SEARCH_LIMIT
):
100 cmd
= ['show', '-s', '--format=%H%n%B', 'HEAD~%d' % i
]
101 proc
= lastchange
.RunGitCommand(directory
, cmd
)
105 output
= proc
.communicate()[0]
106 if not (proc
.returncode
== 0 and output
):
109 lines
= output
.splitlines()
111 # First line is the hash.
113 if not re
.match(r
'[0-9a-fA-F]+', hsh
):
116 for line
in reversed(lines
):
117 if line
.startswith('Cr-Commit-Position:'):
118 pos
= line
.rsplit()[-1].strip()
119 return lastchange
.VersionInfo('git', '%s-%s' % (hsh
, pos
))
121 raise Exception('Unable to fetch a Git Commit Position.')
125 def ParseCommitPosition(commit_position
):
126 '''Parse a Chrome commit position into its components.
128 Given a commit position like:
129 0178d4831bd36b5fb9ff477f03dc43b11626a6dc-refs/heads/master@{#292238}
131 ("0178d4831bd36b5fb9ff477f03dc43b11626a6dc", "refs/heads/master", "292238")
133 m
= re
.match(r
'([0-9a-fA-F]+)(?:-([^@]+)@{#(\d+)})?', commit_position
)