|
| 1 | +from re import compile as re_compile |
| 2 | +from jinja2 import Environment, FileSystemLoader |
| 3 | +from pathlib import Path |
| 4 | +from tox.config.cli.parse import get_options |
| 5 | +from tox.session.state import State |
| 6 | +from tox.config.sets import CoreConfigSet |
| 7 | +from tox.config.source.tox_ini import ToxIni |
| 8 | +from collections import defaultdict |
| 9 | + |
| 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+)?" |
| 18 | +) |
| 19 | + |
| 20 | + |
| 21 | +def get_tox_envs(tox_ini_path: Path) -> list: |
| 22 | + |
| 23 | + tox_ini = ToxIni(tox_ini_path) |
| 24 | + |
| 25 | + conf = State(get_options(), []).conf |
| 26 | + |
| 27 | + tox_section = next(tox_ini.sections()) |
| 28 | + |
| 29 | + core_config_set = ( |
| 30 | + CoreConfigSet(conf, tox_section, tox_ini_path.parent, tox_ini_path) |
| 31 | + ) |
| 32 | + |
| 33 | + ( |
| 34 | + core_config_set. |
| 35 | + loaders. |
| 36 | + extend( |
| 37 | + tox_ini. |
| 38 | + get_loaders( |
| 39 | + tox_section, |
| 40 | + base=[], |
| 41 | + override_map=defaultdict(list, {}), |
| 42 | + conf=core_config_set |
| 43 | + ) |
| 44 | + ) |
| 45 | + ) |
| 46 | + |
| 47 | + return core_config_set.load("env_list") |
| 48 | + |
| 49 | + |
| 50 | +def get_test_job_datas(tox_envs: list, operating_systems: list) -> list: |
| 51 | + |
| 52 | + os_alias = { |
| 53 | + "ubuntu-latest": "Ubuntu", |
| 54 | + "windows-latest": "Windows" |
| 55 | + } |
| 56 | + |
| 57 | + python_version_alias = { |
| 58 | + "pypy3": "pypy-3.8", |
| 59 | + "py38": "3.8", |
| 60 | + "py39": "3.9", |
| 61 | + "py310": "3.10", |
| 62 | + "py311": "3.11", |
| 63 | + "py312": "3.12", |
| 64 | + } |
| 65 | + |
| 66 | + test_job_datas = [] |
| 67 | + |
| 68 | + for operating_system in operating_systems: |
| 69 | + for tox_env in tox_envs: |
| 70 | + |
| 71 | + tox_test_env_match = _tox_test_env_regex.match(tox_env) |
| 72 | + |
| 73 | + if tox_test_env_match is None: |
| 74 | + continue |
| 75 | + |
| 76 | + groups = tox_test_env_match.groupdict() |
| 77 | + |
| 78 | + aliased_python_version = ( |
| 79 | + python_version_alias[groups["python_version"]] |
| 80 | + ) |
| 81 | + tox_env = tox_test_env_match.string |
| 82 | + |
| 83 | + test_requirements = groups["test_requirements"] |
| 84 | + |
| 85 | + if test_requirements is None: |
| 86 | + test_requirements = " " |
| 87 | + |
| 88 | + else: |
| 89 | + test_requirements = f"-{test_requirements} " |
| 90 | + |
| 91 | + test_job_datas.append( |
| 92 | + { |
| 93 | + "name": f"{tox_env}_{operating_system}", |
| 94 | + "ui_name": ( |
| 95 | + f"{groups['name']}" |
| 96 | + f"{test_requirements}" |
| 97 | + f"{aliased_python_version} " |
| 98 | + f"{os_alias[operating_system]}" |
| 99 | + ), |
| 100 | + "python_version": aliased_python_version, |
| 101 | + "tox_env": tox_env, |
| 102 | + "os": operating_system |
| 103 | + } |
| 104 | + |
| 105 | + ) |
| 106 | + |
| 107 | + return test_job_datas |
| 108 | + |
| 109 | + |
| 110 | +def get_lint_job_datas(tox_envs: list) -> list: |
| 111 | + |
| 112 | + lint_job_datas = [] |
| 113 | + |
| 114 | + for tox_env in tox_envs: |
| 115 | + |
| 116 | + tox_lint_env_match = _tox_lint_env_regex.match(tox_env) |
| 117 | + |
| 118 | + if tox_lint_env_match is None: |
| 119 | + continue |
| 120 | + |
| 121 | + tox_env = tox_lint_env_match.string |
| 122 | + |
| 123 | + lint_job_datas.append( |
| 124 | + { |
| 125 | + "name": f"{tox_env}", |
| 126 | + "ui_name": f"{tox_lint_env_match.groupdict()['name']}", |
| 127 | + "tox_env": tox_env, |
| 128 | + } |
| 129 | + |
| 130 | + ) |
| 131 | + |
| 132 | + return lint_job_datas |
| 133 | + |
| 134 | + |
| 135 | +def get_contrib_job_datas(tox_envs: list) -> list: |
| 136 | + |
| 137 | + contrib_job_datas = [] |
| 138 | + |
| 139 | + for tox_env in tox_envs: |
| 140 | + |
| 141 | + tox_contrib_env_match = _tox_contrib_env_regex.match(tox_env) |
| 142 | + |
| 143 | + if tox_contrib_env_match is None: |
| 144 | + continue |
| 145 | + |
| 146 | + groups = tox_contrib_env_match.groupdict() |
| 147 | + |
| 148 | + tox_env = tox_contrib_env_match.string |
| 149 | + |
| 150 | + contrib_requirements = groups["contrib_requirements"] |
| 151 | + |
| 152 | + if contrib_requirements is None: |
| 153 | + contrib_requirements = " " |
| 154 | + |
| 155 | + else: |
| 156 | + contrib_requirements = f"-{contrib_requirements} " |
| 157 | + |
| 158 | + contrib_job_datas.append( |
| 159 | + { |
| 160 | + "ui_name": ( |
| 161 | + f"{groups['name']}" |
| 162 | + f"{contrib_requirements}" |
| 163 | + ), |
| 164 | + "tox_env": tox_env, |
| 165 | + } |
| 166 | + |
| 167 | + ) |
| 168 | + |
| 169 | + return contrib_job_datas |
| 170 | + |
| 171 | + |
| 172 | +def get_misc_job_datas(tox_envs: list) -> list: |
| 173 | + |
| 174 | + misc_job_datas = [] |
| 175 | + |
| 176 | + _tox_benchmark_env_regex = re_compile(r"benchmark.+") |
| 177 | + |
| 178 | + for tox_env in tox_envs: |
| 179 | + if ( |
| 180 | + _tox_test_env_regex.match(tox_env) is not None or |
| 181 | + _tox_lint_env_regex.match(tox_env) is not None or |
| 182 | + _tox_contrib_env_regex.match(tox_env) is not None or |
| 183 | + _tox_benchmark_env_regex.match(tox_env) is not None |
| 184 | + ): |
| 185 | + continue |
| 186 | + |
| 187 | + misc_job_datas.append(tox_env) |
| 188 | + |
| 189 | + return misc_job_datas |
| 190 | + |
| 191 | + |
| 192 | +def _generate_workflow( |
| 193 | + job_datas: list, name: str, workflow_directory_path: Path |
| 194 | +): |
| 195 | + |
| 196 | + # Github seems to limit the amount of jobs in a workflow file, that is why |
| 197 | + # they are split in groups of 250 per workflow file. |
| 198 | + for file_number, job_datas in enumerate( |
| 199 | + [ |
| 200 | + job_datas[index:index + 250] |
| 201 | + for index in range(0, len(job_datas), 250) |
| 202 | + ] |
| 203 | + ): |
| 204 | + |
| 205 | + with open( |
| 206 | + workflow_directory_path.joinpath(f"{name}_{file_number}.yml"), |
| 207 | + "w" |
| 208 | + ) as test_yml_file: |
| 209 | + |
| 210 | + test_yml_file.write( |
| 211 | + Environment( |
| 212 | + loader=FileSystemLoader(Path(__file__).parent) |
| 213 | + ).get_template(f"{name}.yml.j2").render( |
| 214 | + job_datas=job_datas, file_number=file_number |
| 215 | + ) |
| 216 | + ) |
| 217 | + test_yml_file.write("\n") |
| 218 | + |
| 219 | + |
| 220 | +def generate_test_workflow( |
| 221 | + tox_ini_path: Path, |
| 222 | + workflow_directory_path: Path, |
| 223 | + *operating_systems |
| 224 | +) -> None: |
| 225 | + |
| 226 | + _generate_workflow( |
| 227 | + get_test_job_datas(get_tox_envs(tox_ini_path), operating_systems), |
| 228 | + "test", |
| 229 | + workflow_directory_path |
| 230 | + ) |
| 231 | + |
| 232 | + |
| 233 | +def generate_lint_workflow( |
| 234 | + tox_ini_path: Path, |
| 235 | + workflow_directory_path: Path, |
| 236 | +) -> None: |
| 237 | + |
| 238 | + _generate_workflow( |
| 239 | + get_lint_job_datas(get_tox_envs(tox_ini_path)), |
| 240 | + "lint", |
| 241 | + workflow_directory_path |
| 242 | + ) |
| 243 | + |
| 244 | + |
| 245 | +def generate_contrib_workflow( |
| 246 | + workflow_directory_path: Path, |
| 247 | +) -> None: |
| 248 | + |
| 249 | + _generate_workflow( |
| 250 | + get_contrib_job_datas( |
| 251 | + get_tox_envs(Path(__file__).parent.joinpath("tox.ini")) |
| 252 | + ), |
| 253 | + "contrib", |
| 254 | + workflow_directory_path |
| 255 | + ) |
| 256 | + |
| 257 | + |
| 258 | +def generate_misc_workflow( |
| 259 | + tox_ini_path: Path, |
| 260 | + workflow_directory_path: Path, |
| 261 | +) -> None: |
| 262 | + |
| 263 | + _generate_workflow( |
| 264 | + get_misc_job_datas(get_tox_envs(tox_ini_path)), |
| 265 | + "misc", |
| 266 | + workflow_directory_path |
| 267 | + ) |
0 commit comments