21601: Move --short-tests-only check to arvados_version
[arvados.git] / tools / user-activity / arvados_version.py
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: Apache-2.0
4 #
5 # This file runs in one of three modes:
6 #
7 # 1. If the ARVADOS_BUILDING_VERSION environment variable is set, it writes
8 #    _version.py and generates dependencies based on that value.
9 # 2. If running from an arvados Git checkout, it writes _version.py
10 #    and generates dependencies from Git.
11 # 3. Otherwise, we expect this is source previously generated from Git, and
12 #    it reads _version.py and generates dependencies from it.
13
14 import os
15 import runpy
16 import subprocess
17 import sys
18
19 from pathlib import Path
20
21 # These maps explain the relationships between different Python modules in
22 # the arvados repository. We use these to help generate setup.py.
23 PACKAGE_DEPENDENCY_MAP = {
24     'arvados-cwl-runner': ['arvados-python-client', 'crunchstat_summary'],
25     'arvados-user-activity': ['arvados-python-client'],
26     'arvados_fuse': ['arvados-python-client'],
27     'crunchstat_summary': ['arvados-python-client'],
28 }
29 PACKAGE_MODULE_MAP = {
30     'arvados-cwl-runner': 'arvados_cwl',
31     'arvados-docker-cleaner': 'arvados_docker',
32     'arvados-python-client': 'arvados',
33     'arvados-user-activity': 'arvados_user_activity',
34     'arvados_fuse': 'arvados_fuse',
35     'crunchstat_summary': 'crunchstat_summary',
36 }
37 PACKAGE_SRCPATH_MAP = {
38     'arvados-cwl-runner': Path('sdk', 'cwl'),
39     'arvados-docker-cleaner': Path('services', 'dockercleaner'),
40     'arvados-python-client': Path('sdk', 'python'),
41     'arvados-user-activity': Path('tools', 'user-activity'),
42     'arvados_fuse': Path('services', 'fuse'),
43     'crunchstat_summary': Path('tools', 'crunchstat-summary'),
44 }
45
46 ENV_VERSION = os.environ.get("ARVADOS_BUILDING_VERSION")
47 SETUP_DIR = Path(__file__).absolute().parent
48 try:
49     REPO_PATH = Path(subprocess.check_output(
50         ['git', '-C', str(SETUP_DIR), 'rev-parse', '--show-toplevel'],
51         stderr=subprocess.DEVNULL,
52         text=True,
53     ).rstrip('\n'))
54 except (subprocess.CalledProcessError, OSError):
55     REPO_PATH = None
56 else:
57     # Verify this is the arvados monorepo
58     if all((REPO_PATH / path).exists() for path in PACKAGE_SRCPATH_MAP.values()):
59         PACKAGE_NAME, = (
60             pkg_name for pkg_name, path in PACKAGE_SRCPATH_MAP.items()
61             if (REPO_PATH / path) == SETUP_DIR
62         )
63         MODULE_NAME = PACKAGE_MODULE_MAP[PACKAGE_NAME]
64         VERSION_SCRIPT_PATH = Path(REPO_PATH, 'build', 'version-at-commit.sh')
65     else:
66         REPO_PATH = None
67 if REPO_PATH is None:
68     (PACKAGE_NAME, MODULE_NAME), = (
69         (pkg_name, mod_name)
70         for pkg_name, mod_name in PACKAGE_MODULE_MAP.items()
71         if (SETUP_DIR / mod_name).is_dir()
72     )
73
74 def short_tests_only(arglist=sys.argv):
75     try:
76         arglist.remove('--short-tests-only')
77     except ValueError:
78         return False
79     else:
80         return True
81
82 def git_log_output(path, *args):
83     return subprocess.check_output(
84         ['git', '-C', str(REPO_PATH),
85          'log', '--first-parent', '--max-count=1',
86          *args, str(path)],
87         text=True,
88     ).rstrip('\n')
89
90 def choose_version_from():
91     ver_paths = [SETUP_DIR, VERSION_SCRIPT_PATH, *(
92         PACKAGE_SRCPATH_MAP[pkg]
93         for pkg in PACKAGE_DEPENDENCY_MAP.get(PACKAGE_NAME, ())
94     )]
95     getver = max(ver_paths, key=lambda path: git_log_output(path, '--format=format:%ct'))
96     print(f"Using {getver} for version number calculation of {SETUP_DIR}", file=sys.stderr)
97     return getver
98
99 def git_version_at_commit():
100     curdir = choose_version_from()
101     myhash = git_log_output(curdir, '--format=%H')
102     return subprocess.check_output(
103         [str(VERSION_SCRIPT_PATH), myhash],
104         text=True,
105     ).rstrip('\n')
106
107 def save_version(setup_dir, module, v):
108     with Path(setup_dir, module, '_version.py').open('w') as fp:
109         print(f"__version__ = {v!r}", file=fp)
110
111 def read_version(setup_dir, module):
112     file_vars = runpy.run_path(Path(setup_dir, module, '_version.py'))
113     return file_vars['__version__']
114
115 def get_version(setup_dir=SETUP_DIR, module=MODULE_NAME):
116     if ENV_VERSION:
117         version = ENV_VERSION
118     elif REPO_PATH is None:
119         return read_version(setup_dir, module)
120     else:
121         version = git_version_at_commit()
122     version = version.replace("~dev", ".dev").replace("~rc", "rc")
123     save_version(setup_dir, module, version)
124     return version
125
126 # Called from calculate_python_sdk_cwl_package_versions() in run-library.sh
127 if __name__ == '__main__':
128     print(get_version())