1*8975f5c5SAndroid Build Coastguard Worker# Copyright 2024 The Chromium Authors 2*8975f5c5SAndroid Build Coastguard Worker# Use of this source code is governed by a BSD-style license that can be 3*8975f5c5SAndroid Build Coastguard Worker# found in the LICENSE file. 4*8975f5c5SAndroid Build Coastguard Worker""" Reads version files from the chromium source code. """ 5*8975f5c5SAndroid Build Coastguard Worker 6*8975f5c5SAndroid Build Coastguard Workerimport argparse 7*8975f5c5SAndroid Build Coastguard Workerimport logging 8*8975f5c5SAndroid Build Coastguard Workerimport os.path 9*8975f5c5SAndroid Build Coastguard Worker 10*8975f5c5SAndroid Build Coastguard Workerfrom typing import Dict 11*8975f5c5SAndroid Build Coastguard Worker 12*8975f5c5SAndroid Build Coastguard Workerfrom common import DIR_SRC_ROOT 13*8975f5c5SAndroid Build Coastguard Worker 14*8975f5c5SAndroid Build Coastguard Worker 15*8975f5c5SAndroid Build Coastguard Workerdef chrome_version() -> Dict[str, int]: 16*8975f5c5SAndroid Build Coastguard Worker """ Returns a replica of //chrome/VERSION, crashes if the file does not 17*8975f5c5SAndroid Build Coastguard Worker exist. This function does not assume the existence of all the fields, but 18*8975f5c5SAndroid Build Coastguard Worker treats missing ones as 0; on the other hand, the unexpected fields would be 19*8975f5c5SAndroid Build Coastguard Worker also ignored.""" 20*8975f5c5SAndroid Build Coastguard Worker file = os.path.join(DIR_SRC_ROOT, 'chrome', 'VERSION') 21*8975f5c5SAndroid Build Coastguard Worker assert os.path.exists(file) 22*8975f5c5SAndroid Build Coastguard Worker result = {} 23*8975f5c5SAndroid Build Coastguard Worker 24*8975f5c5SAndroid Build Coastguard Worker def parse_line(field: str, line: str) -> bool: 25*8975f5c5SAndroid Build Coastguard Worker if line.startswith(field.upper() + '='): 26*8975f5c5SAndroid Build Coastguard Worker result[field] = int(line[len(field.upper()) + 1:].rstrip()) 27*8975f5c5SAndroid Build Coastguard Worker return True 28*8975f5c5SAndroid Build Coastguard Worker return False 29*8975f5c5SAndroid Build Coastguard Worker 30*8975f5c5SAndroid Build Coastguard Worker with open(file, 'r') as reader: 31*8975f5c5SAndroid Build Coastguard Worker for line in reader: 32*8975f5c5SAndroid Build Coastguard Worker if (not parse_line('major', line) 33*8975f5c5SAndroid Build Coastguard Worker and not parse_line('minor', line) 34*8975f5c5SAndroid Build Coastguard Worker and not parse_line('build', line) 35*8975f5c5SAndroid Build Coastguard Worker and not parse_line('patch', line)): 36*8975f5c5SAndroid Build Coastguard Worker logging.warning('Unexpected line %s in the VERSION file', line) 37*8975f5c5SAndroid Build Coastguard Worker return result 38*8975f5c5SAndroid Build Coastguard Worker 39*8975f5c5SAndroid Build Coastguard Worker 40*8975f5c5SAndroid Build Coastguard Workerdef chrome_version_str() -> str: 41*8975f5c5SAndroid Build Coastguard Worker """ Returns the chrome_version in a string representation. """ 42*8975f5c5SAndroid Build Coastguard Worker version = chrome_version() 43*8975f5c5SAndroid Build Coastguard Worker return (f'{version["major"]}.{version["minor"]}.' 44*8975f5c5SAndroid Build Coastguard Worker f'{version["build"]}.{version["patch"]}') 45*8975f5c5SAndroid Build Coastguard Worker 46*8975f5c5SAndroid Build Coastguard Worker 47*8975f5c5SAndroid Build Coastguard Workerdef _load_git_args() -> argparse.Namespace: 48*8975f5c5SAndroid Build Coastguard Worker parser = argparse.ArgumentParser() 49*8975f5c5SAndroid Build Coastguard Worker parser.add_argument('--git-revision', default=None) 50*8975f5c5SAndroid Build Coastguard Worker parser.add_argument('--gerrit-issue', type=int, default=None) 51*8975f5c5SAndroid Build Coastguard Worker parser.add_argument('--gerrit-patchset', type=int, default=None) 52*8975f5c5SAndroid Build Coastguard Worker parser.add_argument('--buildbucket-id', type=int, default=None) 53*8975f5c5SAndroid Build Coastguard Worker args, _ = parser.parse_known_args() 54*8975f5c5SAndroid Build Coastguard Worker # The args look like 55*8975f5c5SAndroid Build Coastguard Worker # '--git-revision=e98127af84bf5b33a6e657c90dfd3f3a731eb28c' 56*8975f5c5SAndroid Build Coastguard Worker # '--gerrit-issue=5009604' 57*8975f5c5SAndroid Build Coastguard Worker # '--gerrit-patchset=16' 58*8975f5c5SAndroid Build Coastguard Worker # '--buildbucket-id=8756180599882888289' 59*8975f5c5SAndroid Build Coastguard Worker # on a try build. CI builds have only git-revision. 60*8975f5c5SAndroid Build Coastguard Worker return args 61*8975f5c5SAndroid Build Coastguard Worker 62*8975f5c5SAndroid Build Coastguard Worker 63*8975f5c5SAndroid Build Coastguard Worker_GIT_ARGS: argparse.Namespace = _load_git_args() 64*8975f5c5SAndroid Build Coastguard Worker 65*8975f5c5SAndroid Build Coastguard Worker 66*8975f5c5SAndroid Build Coastguard Workerdef is_try_build() -> bool: 67*8975f5c5SAndroid Build Coastguard Worker """ Returns whether current build is running as a try-build, or unmerged 68*8975f5c5SAndroid Build Coastguard Worker change. This function crashes if the info cannot be retrieved. """ 69*8975f5c5SAndroid Build Coastguard Worker assert _GIT_ARGS.git_revision 70*8975f5c5SAndroid Build Coastguard Worker return _GIT_ARGS.gerrit_issue is not None 71*8975f5c5SAndroid Build Coastguard Worker 72*8975f5c5SAndroid Build Coastguard Worker 73*8975f5c5SAndroid Build Coastguard Workerdef git_revision() -> str: 74*8975f5c5SAndroid Build Coastguard Worker """ Returns the git revision to identify the current change list or the 75*8975f5c5SAndroid Build Coastguard Worker commit info of the CI build. This function crashes if the info cannot be 76*8975f5c5SAndroid Build Coastguard Worker retrieved. """ 77*8975f5c5SAndroid Build Coastguard Worker assert _GIT_ARGS.git_revision 78*8975f5c5SAndroid Build Coastguard Worker if not is_try_build(): 79*8975f5c5SAndroid Build Coastguard Worker return _GIT_ARGS.git_revision 80*8975f5c5SAndroid Build Coastguard Worker return (f'{_GIT_ARGS.git_revision}/{_GIT_ARGS.gerrit_issue}/' 81*8975f5c5SAndroid Build Coastguard Worker f'{_GIT_ARGS.gerrit_patchset}') 82