21207: Remove Python slow/short tests support
[arvados.git] / sdk / cwl / 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 re
16 import runpy
17 import subprocess
18 import sys
19
20 from pathlib import Path
21
22 # These maps explain the relationships between different Python modules in
23 # the arvados repository. We use these to help generate setup.py.
24 PACKAGE_DEPENDENCY_MAP = {
25     'arvados-cwl-runner': ['arvados-python-client', 'crunchstat_summary'],
26     'arvados-user-activity': ['arvados-python-client'],
27     'arvados_fuse': ['arvados-python-client'],
28     'crunchstat_summary': ['arvados-python-client'],
29 }
30 PACKAGE_MODULE_MAP = {
31     'arvados-cwl-runner': 'arvados_cwl',
32     'arvados-docker-cleaner': 'arvados_docker',
33     'arvados-python-client': 'arvados',
34     'arvados-user-activity': 'arvados_user_activity',
35     'arvados_fuse': 'arvados_fuse',
36     'crunchstat_summary': 'crunchstat_summary',
37 }
38 PACKAGE_SRCPATH_MAP = {
39     'arvados-cwl-runner': Path('sdk', 'cwl'),
40     'arvados-docker-cleaner': Path('services', 'dockercleaner'),
41     'arvados-python-client': Path('sdk', 'python'),
42     'arvados-user-activity': Path('tools', 'user-activity'),
43     'arvados_fuse': Path('services', 'fuse'),
44     'crunchstat_summary': Path('tools', 'crunchstat-summary'),
45 }
46
47 ENV_VERSION = os.environ.get("ARVADOS_BUILDING_VERSION")
48 SETUP_DIR = Path(__file__).absolute().parent
49 try:
50     REPO_PATH = Path(subprocess.check_output(
51         ['git', '-C', str(SETUP_DIR), 'rev-parse', '--show-toplevel'],
52         stderr=subprocess.DEVNULL,
53         text=True,
54     ).rstrip('\n'))
55 except (subprocess.CalledProcessError, OSError):
56     REPO_PATH = None
57 else:
58     # Verify this is the arvados monorepo
59     if all((REPO_PATH / path).exists() for path in PACKAGE_SRCPATH_MAP.values()):
60         PACKAGE_NAME, = (
61             pkg_name for pkg_name, path in PACKAGE_SRCPATH_MAP.items()
62             if (REPO_PATH / path) == SETUP_DIR
63         )
64         MODULE_NAME = PACKAGE_MODULE_MAP[PACKAGE_NAME]
65         VERSION_SCRIPT_PATH = Path(REPO_PATH, 'build', 'version-at-commit.sh')
66     else:
67         REPO_PATH = None
68 if REPO_PATH is None:
69     (PACKAGE_NAME, MODULE_NAME), = (
70         (pkg_name, mod_name)
71         for pkg_name, mod_name in PACKAGE_MODULE_MAP.items()
72         if (SETUP_DIR / mod_name).is_dir()
73     )
74
75 def git_log_output(path, *args):
76     return subprocess.check_output(
77         ['git', '-C', str(REPO_PATH),
78          'log', '--first-parent', '--max-count=1',
79          *args, str(path)],
80         text=True,
81     ).rstrip('\n')
82
83 def choose_version_from():
84     ver_paths = [SETUP_DIR, VERSION_SCRIPT_PATH, *(
85         PACKAGE_SRCPATH_MAP[pkg]
86         for pkg in PACKAGE_DEPENDENCY_MAP.get(PACKAGE_NAME, ())
87     )]
88     getver = max(ver_paths, key=lambda path: git_log_output(path, '--format=format:%ct'))
89     print(f"Using {getver} for version number calculation of {SETUP_DIR}", file=sys.stderr)
90     return getver
91
92 def git_version_at_commit():
93     curdir = choose_version_from()
94     myhash = git_log_output(curdir, '--format=%H')
95     return subprocess.check_output(
96         [str(VERSION_SCRIPT_PATH), myhash],
97         text=True,
98     ).rstrip('\n')
99
100 def save_version(setup_dir, module, v):
101     with Path(setup_dir, module, '_version.py').open('w') as fp:
102         print(f"__version__ = {v!r}", file=fp)
103
104 def read_version(setup_dir, module):
105     file_vars = runpy.run_path(Path(setup_dir, module, '_version.py'))
106     return file_vars['__version__']
107
108 def get_version(setup_dir=SETUP_DIR, module=MODULE_NAME):
109     if ENV_VERSION:
110         version = ENV_VERSION
111     elif REPO_PATH is None:
112         return read_version(setup_dir, module)
113     else:
114         version = git_version_at_commit()
115     version = version.replace("~dev", ".dev").replace("~rc", "rc")
116     save_version(setup_dir, module, version)
117     return version
118
119 def iter_dependencies(version=None):
120     if version is None:
121         version = get_version()
122     # A packaged development release should be installed with other
123     # development packages built from the same source, but those
124     # dependencies may have earlier "dev" versions (read: less recent
125     # Git commit timestamps). This compatible version dependency
126     # expresses that as closely as possible. Allowing versions
127     # compatible with .dev0 allows any development release.
128     # Regular expression borrowed partially from
129     # <https://packaging.python.org/en/latest/specifications/version-specifiers/#version-specifiers-regex>
130     dep_ver, match_count = re.subn(r'\.dev(0|[1-9][0-9]*)$', '.dev0', version, 1)
131     dep_op = '~=' if match_count else '=='
132     for dep_pkg in PACKAGE_DEPENDENCY_MAP.get(PACKAGE_NAME, ()):
133         yield f'{dep_pkg}{dep_op}{dep_ver}'
134
135 # Called from calculate_python_sdk_cwl_package_versions() in run-library.sh
136 if __name__ == '__main__':
137     print(get_version())