|
| 1 | +from collections import defaultdict |
1 | 2 | from pathlib import Path
|
| 3 | +from re import compile as re_compile |
2 | 4 |
|
3 |
| -from generate_workflows_lib import ( |
4 |
| - generate_lint_workflow, |
5 |
| - generate_misc_workflow, |
6 |
| - generate_test_workflow, |
| 5 | +from jinja2 import Environment, FileSystemLoader |
| 6 | +from tox.config.cli.parse import get_options |
| 7 | +from tox.config.sets import CoreConfigSet |
| 8 | +from tox.config.source.tox_ini import ToxIni |
| 9 | +from tox.session.state import State |
| 10 | + |
| 11 | +_tox_test_env_regex = re_compile( |
| 12 | + r"(?P<python_version>py\w+)-test-" |
| 13 | + r"(?P<name>[-\w]+\w)-?(?P<test_requirements>\d+)?" |
| 14 | +) |
| 15 | +_tox_lint_env_regex = re_compile(r"lint-(?P<name>[-\w]+)") |
| 16 | +_tox_contrib_env_regex = re_compile( |
| 17 | + r"py38-test-(?P<name>[-\w]+\w)-?(?P<contrib_requirements>\d+)?" |
7 | 18 | )
|
8 | 19 |
|
9 |
| -tox_ini_path = Path(__file__).parent.parent.parent.joinpath("tox.ini") |
10 |
| -workflows_directory_path = Path(__file__).parent |
11 | 20 |
|
12 |
| -generate_test_workflow( |
13 |
| - tox_ini_path, |
14 |
| - workflows_directory_path, |
15 |
| - "ubuntu-latest", |
16 |
| - "windows-latest", |
17 |
| -) |
18 |
| -generate_lint_workflow(tox_ini_path, workflows_directory_path) |
19 |
| -generate_misc_workflow(tox_ini_path, workflows_directory_path) |
| 21 | +def get_tox_envs(tox_ini_path: Path) -> list: |
| 22 | + tox_ini = ToxIni(tox_ini_path) |
| 23 | + |
| 24 | + conf = State(get_options(), []).conf |
| 25 | + |
| 26 | + tox_section = next(tox_ini.sections()) |
| 27 | + |
| 28 | + core_config_set = CoreConfigSet( |
| 29 | + conf, tox_section, tox_ini_path.parent, tox_ini_path |
| 30 | + ) |
| 31 | + |
| 32 | + ( |
| 33 | + core_config_set.loaders.extend( |
| 34 | + tox_ini.get_loaders( |
| 35 | + tox_section, |
| 36 | + base=[], |
| 37 | + override_map=defaultdict(list, {}), |
| 38 | + conf=core_config_set, |
| 39 | + ) |
| 40 | + ) |
| 41 | + ) |
| 42 | + |
| 43 | + return core_config_set.load("env_list") |
| 44 | + |
| 45 | + |
| 46 | +def get_test_job_datas(tox_envs: list, operating_systems: list) -> list: |
| 47 | + os_alias = {"ubuntu-latest": "Ubuntu", "windows-latest": "Windows"} |
| 48 | + |
| 49 | + python_version_alias = { |
| 50 | + "pypy3": "pypy-3.8", |
| 51 | + "py38": "3.8", |
| 52 | + "py39": "3.9", |
| 53 | + "py310": "3.10", |
| 54 | + "py311": "3.11", |
| 55 | + "py312": "3.12", |
| 56 | + } |
| 57 | + |
| 58 | + test_job_datas = [] |
| 59 | + |
| 60 | + for operating_system in operating_systems: |
| 61 | + for tox_env in tox_envs: |
| 62 | + tox_test_env_match = _tox_test_env_regex.match(tox_env) |
| 63 | + |
| 64 | + if tox_test_env_match is None: |
| 65 | + continue |
| 66 | + |
| 67 | + groups = tox_test_env_match.groupdict() |
| 68 | + |
| 69 | + aliased_python_version = python_version_alias[ |
| 70 | + groups["python_version"] |
| 71 | + ] |
| 72 | + tox_env = tox_test_env_match.string |
| 73 | + |
| 74 | + test_requirements = groups["test_requirements"] |
| 75 | + |
| 76 | + if test_requirements is None: |
| 77 | + test_requirements = " " |
| 78 | + |
| 79 | + else: |
| 80 | + test_requirements = f"-{test_requirements} " |
| 81 | + |
| 82 | + test_job_datas.append( |
| 83 | + { |
| 84 | + "name": f"{tox_env}_{operating_system}", |
| 85 | + "ui_name": ( |
| 86 | + f"{groups['name']}" |
| 87 | + f"{test_requirements}" |
| 88 | + f"{aliased_python_version} " |
| 89 | + f"{os_alias[operating_system]}" |
| 90 | + ), |
| 91 | + "python_version": aliased_python_version, |
| 92 | + "tox_env": tox_env, |
| 93 | + "os": operating_system, |
| 94 | + } |
| 95 | + ) |
| 96 | + |
| 97 | + return test_job_datas |
| 98 | + |
| 99 | + |
| 100 | +def get_lint_job_datas(tox_envs: list) -> list: |
| 101 | + lint_job_datas = [] |
| 102 | + |
| 103 | + for tox_env in tox_envs: |
| 104 | + tox_lint_env_match = _tox_lint_env_regex.match(tox_env) |
| 105 | + |
| 106 | + if tox_lint_env_match is None: |
| 107 | + continue |
| 108 | + |
| 109 | + tox_env = tox_lint_env_match.string |
| 110 | + |
| 111 | + lint_job_datas.append( |
| 112 | + { |
| 113 | + "name": f"{tox_env}", |
| 114 | + "ui_name": f"{tox_lint_env_match.groupdict()['name']}", |
| 115 | + "tox_env": tox_env, |
| 116 | + } |
| 117 | + ) |
| 118 | + |
| 119 | + return lint_job_datas |
| 120 | + |
| 121 | + |
| 122 | +def get_misc_job_datas(tox_envs: list) -> list: |
| 123 | + regex_patterns = [ |
| 124 | + _tox_test_env_regex, |
| 125 | + _tox_lint_env_regex, |
| 126 | + _tox_contrib_env_regex, |
| 127 | + re_compile(r"benchmark.+"), |
| 128 | + ] |
| 129 | + |
| 130 | + return [ |
| 131 | + tox_env |
| 132 | + for tox_env in tox_envs |
| 133 | + if not any(pattern.match(tox_env) for pattern in regex_patterns) |
| 134 | + ] |
| 135 | + |
| 136 | + |
| 137 | +def _generate_workflow( |
| 138 | + job_datas: list, |
| 139 | + template_name: str, |
| 140 | + output_dir: Path, |
| 141 | + max_jobs: int = 250, |
| 142 | +): |
| 143 | + # Github seems to limit the amount of jobs in a workflow file, that is why |
| 144 | + # they are split in groups of 250 per workflow file. |
| 145 | + for file_number, job_datas in enumerate( |
| 146 | + [ |
| 147 | + job_datas[index : index + max_jobs] |
| 148 | + for index in range(0, len(job_datas), max_jobs) |
| 149 | + ] |
| 150 | + ): |
| 151 | + with open( |
| 152 | + output_dir.joinpath(f"{template_name}_{file_number}.yml"), "w" |
| 153 | + ) as test_yml_file: |
| 154 | + test_yml_file.write( |
| 155 | + Environment( |
| 156 | + loader=FileSystemLoader( |
| 157 | + Path(__file__).parent.joinpath("templates") |
| 158 | + ) |
| 159 | + ) |
| 160 | + .get_template(f"{template_name}.yml.j2") |
| 161 | + .render(job_datas=job_datas, file_number=file_number) |
| 162 | + ) |
| 163 | + test_yml_file.write("\n") |
| 164 | + |
| 165 | + |
| 166 | +def generate_test_workflow( |
| 167 | + tox_ini_path: Path, workflow_directory_path: Path, operating_systems |
| 168 | +) -> None: |
| 169 | + _generate_workflow( |
| 170 | + get_test_job_datas(get_tox_envs(tox_ini_path), operating_systems), |
| 171 | + "test", |
| 172 | + workflow_directory_path, |
| 173 | + ) |
| 174 | + |
| 175 | + |
| 176 | +def generate_lint_workflow( |
| 177 | + tox_ini_path: Path, |
| 178 | + workflow_directory_path: Path, |
| 179 | +) -> None: |
| 180 | + _generate_workflow( |
| 181 | + get_lint_job_datas(get_tox_envs(tox_ini_path)), |
| 182 | + "lint", |
| 183 | + workflow_directory_path, |
| 184 | + ) |
| 185 | + |
| 186 | + |
| 187 | +def generate_misc_workflow( |
| 188 | + tox_ini_path: Path, |
| 189 | + workflow_directory_path: Path, |
| 190 | +) -> None: |
| 191 | + _generate_workflow( |
| 192 | + get_misc_job_datas(get_tox_envs(tox_ini_path)), |
| 193 | + "misc", |
| 194 | + workflow_directory_path, |
| 195 | + ) |
| 196 | + |
| 197 | + |
| 198 | +if __name__ == "__main__": |
| 199 | + tox_ini_path = Path(__file__).parent.parent.parent.joinpath("tox.ini") |
| 200 | + output_dir = Path(__file__).parent |
| 201 | + generate_test_workflow( |
| 202 | + tox_ini_path, output_dir, ["ubuntu-latest", "windows-latest"] |
| 203 | + ) |
| 204 | + generate_lint_workflow(tox_ini_path, output_dir) |
| 205 | + generate_misc_workflow(tox_ini_path, output_dir) |
0 commit comments