xref: /aosp_15_r20/external/cronet/build/3pp_common/print_cipd_version.py (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1#!/usr/bin/env python3
2# Copyright 2023 The Chromium Authors
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6import argparse
7import pathlib
8import re
9import subprocess
10
11_DIR_SOURCE_ROOT = str(pathlib.Path(__file__).absolute().parents[2])
12
13
14def main():
15    parser = argparse.ArgumentParser()
16    # Hide args set by wrappers so that using --help with the wrappers does not
17    # show them.
18    parser.add_argument('--subdir', required=True, help=argparse.SUPPRESS)
19    parser.add_argument('--cipd-package',
20                        required=True,
21                        help=argparse.SUPPRESS)
22    parser.add_argument('--git-log-url', help=argparse.SUPPRESS)
23    parser.add_argument('--cipd-instance',
24                        help='Uses value from DEPS by default')
25    args = parser.parse_args()
26
27    if not args.cipd_instance:
28        cmd = [
29            'gclient', 'getdep', '-r', f'src/{args.subdir}:{args.cipd_package}'
30        ]
31        args.cipd_instance = subprocess.check_output(cmd,
32                                                     cwd=_DIR_SOURCE_ROOT,
33                                                     text=True)
34
35    cmd = [
36        'cipd', 'describe', args.cipd_package, '-version', args.cipd_instance
37    ]
38    print(' '.join(cmd))
39    output = subprocess.check_output(cmd, text=True)
40    print(output, end='')
41    if args.git_log_url:
42        git_hashes = re.findall(r'version:.*?@(\w+)', output)
43        if not git_hashes:
44            print('Could not find git hash from output.')
45        else:
46            # Multiple version tags exist when multiple versions have the same sha1.
47            last_version = git_hashes[-1]
48            print()
49            print('Recent commits:', args.git_log_url.format(last_version))
50
51
52if __name__ == '__main__':
53    main()
54