|
1 |
| -from configparser import ConfigParser |
2 |
| -from sys import argv |
3 |
| -from os.path import join |
4 | 1 | from re import compile as re_compile
|
5 | 2 | from jinja2 import Environment, FileSystemLoader
|
6 | 3 | from pathlib import Path
|
|
11 | 8 | from collections import defaultdict
|
12 | 9 |
|
13 | 10 |
|
14 |
| -long_test_env_regex = re_compile( |
15 |
| - r"py(?P<pypy>py)?3(\{(?P<cpython_versions>[\w,]+)\})?-test-" |
16 |
| - r"(?P<name>[-\w]+\w)-?(\{(?P<test_versions>[\d,]+)\})?" |
17 |
| -) |
18 |
| -short_test_env_regex = re_compile(r"-test-") |
19 |
| - |
20 |
| -tox_ini_path = join(str(Path(__file__).parents[2]), "tox.ini") |
21 |
| - |
22 |
| - |
23 |
| -def _get_os_alias(os): |
24 |
| - return os.replace("-latest", "") |
25 |
| - |
26 |
| - |
27 |
| -def _get_python_version_alias(python_version): |
28 |
| - if python_version == "py3": |
29 |
| - return "pypy-3.8" |
30 |
| - |
31 |
| - return f"3.{python_version.replace('3', '')}" |
32 |
| - |
33 |
| - |
34 | 11 | def get_tox_envs(tox_ini_path: Path) -> list:
|
35 | 12 |
|
36 | 13 | tox_ini = ToxIni(tox_ini_path)
|
@@ -60,147 +37,92 @@ def get_tox_envs(tox_ini_path: Path) -> list:
|
60 | 37 | return core_config_set.load("env_list")
|
61 | 38 |
|
62 | 39 |
|
63 |
| -def get_test_tox_envs(): |
| 40 | +def get_test_jobs_data(tox_envs: list, oss: list) -> list: |
64 | 41 |
|
| 42 | + os_alias = { |
| 43 | + "ubuntu-latest": "Ubuntu", |
| 44 | + "windows-latest": "Windows" |
| 45 | + } |
65 | 46 |
|
| 47 | + python_version_alias = { |
| 48 | + "pypy3": "pypy-3.8", |
| 49 | + "py38": "3.8", |
| 50 | + "py39": "3.9", |
| 51 | + "py310": "3.10", |
| 52 | + "py311": "3.11", |
| 53 | + "py312": "3.12", |
| 54 | + } |
66 | 55 |
|
| 56 | + test_jobs_data = [] |
67 | 57 |
|
68 |
| -def get_test_jobs(tox_ini_path: Path) -> list: |
| 58 | + tox_test_env_regex = re_compile( |
| 59 | + r"(?P<python_version>py\w+)-test-" |
| 60 | + r"(?P<name>[-\w]+\w)-?(?P<test_requirements>\d+)?" |
| 61 | + ) |
69 | 62 |
|
70 |
| - config_parser = ConfigParser() |
71 |
| - config_parser.read(tox_ini_path) |
| 63 | + for tox_env in tox_envs: |
72 | 64 |
|
73 |
| - long_test_env_regex_counter = 0 |
74 |
| - short_test_env_regex_counter = 0 |
| 65 | + tox_test_env_match = tox_test_env_regex.match(tox_env) |
75 | 66 |
|
76 |
| - envs = {} |
| 67 | + if tox_test_env_match is None: |
| 68 | + continue |
77 | 69 |
|
78 |
| - for env in config_parser["tox"]["envlist"].split(): |
79 |
| - env = env.strip() |
| 70 | + groups = tox_test_env_match.groupdict() |
| 71 | + |
| 72 | + aliased_python_version = python_version_alias(groups["python_version"]) |
| 73 | + tox_env = tox_test_env_match.groups(0) |
| 74 | + os = groups["os"] |
| 75 | + |
| 76 | + test_jobs_data.append( |
| 77 | + { |
| 78 | + "name": f"{tox_env}_{os}", |
| 79 | + "ui_name": ( |
| 80 | + f"{groups['name']}-" |
| 81 | + f"{groups['test-requirements']} " |
| 82 | + f"{aliased_python_version}" |
| 83 | + f"{os_alias[os]}" |
| 84 | + ), |
| 85 | + "python_version": aliased_python_version, |
| 86 | + "tox_env": tox_test_env_match.groups(0), |
| 87 | + "os": os |
| 88 | + } |
| 89 | + |
| 90 | + ) |
| 91 | + |
| 92 | + return test_jobs_data |
80 | 93 |
|
81 |
| - if env.startswith(";"): |
82 |
| - continue |
83 | 94 |
|
84 |
| - long_test_env_regex_match = long_test_env_regex.match(env) |
85 |
| - short_test_env_regex_match = short_test_env_regex.search(env) |
86 |
| - |
87 |
| - if long_test_env_regex_match is not None: |
88 |
| - long_test_env_regex_counter += 1 |
89 |
| - |
90 |
| - env_dict = long_test_env_regex_match.groupdict() |
91 |
| - env_dict_name = env_dict["name"] |
92 |
| - |
93 |
| - if env_dict_name not in envs.keys(): |
94 |
| - envs[env_dict_name] = [] |
95 |
| - |
96 |
| - python_test_versions = {"python_versions": [], "test_versions": []} |
| 95 | +def get_lint_jobs_data(tox_envs: list) -> list: |
| 96 | + pass |
97 | 97 |
|
98 |
| - if env_dict["pypy"] is not None: |
99 |
| - python_test_versions["python_versions"].append("py3") |
100 |
| - |
101 |
| - if env_dict["cpython_versions"] is not None: |
102 |
| - ( |
103 |
| - python_test_versions["python_versions"]. |
104 |
| - extend( |
105 |
| - [ |
106 |
| - f"3{cpython_version}" |
107 |
| - for cpython_version |
108 |
| - in env_dict["cpython_versions"].split(",") |
109 |
| - ] |
110 |
| - ) |
111 |
| - ) |
112 |
| - |
113 |
| - if env_dict["test_versions"] is not None: |
114 |
| - ( |
115 |
| - python_test_versions["test_versions"]. |
116 |
| - extend(env_dict["test_versions"].split(",")) |
117 |
| - ) |
118 |
| - |
119 |
| - envs[env_dict_name].append(python_test_versions) |
120 |
| - |
121 |
| - if short_test_env_regex_match is not None: |
122 |
| - short_test_env_regex_counter += 1 |
123 |
| - |
124 |
| - assert short_test_env_regex_counter == long_test_env_regex_counter |
125 |
| - |
126 |
| - sorted_envs = [] |
127 |
| - |
128 |
| - for key, value in envs.items(): |
129 |
| - sorted_envs.append([key, value]) |
130 |
| - |
131 |
| - sorted_envs = sorted(sorted_envs) |
132 |
| - |
133 |
| - jobs = [] |
134 |
| - |
135 |
| - for os in ["ubuntu-latest"]: |
136 |
| - |
137 |
| - for env_name, python_test_versions in sorted_envs: |
138 |
| - |
139 |
| - for python_test_version in python_test_versions: |
140 |
| - |
141 |
| - for python_version in python_test_version["python_versions"]: |
142 |
| - |
143 |
| - tox_env = f"py{python_version}-test-{env_name}" |
144 |
| - |
145 |
| - if python_test_version["test_versions"]: |
146 |
| - for test_version in python_test_version["test_versions"]: |
147 |
| - |
148 |
| - jobs.append( |
149 |
| - { |
150 |
| - "tox_env": f"{tox_env}-{test_version}", |
151 |
| - "python_version": ( |
152 |
| - f"{_get_python_version_alias(python_version)}" |
153 |
| - ), |
154 |
| - "os": os, |
155 |
| - "ui_name": ( |
156 |
| - f"{env_name}-{test_version} " |
157 |
| - f"{_get_python_version_alias(python_version)} " |
158 |
| - f"{_get_os_alias(os)}" |
159 |
| - ), |
160 |
| - "name": ( |
161 |
| - f"{env_name}_" |
162 |
| - f"{test_version}_" |
163 |
| - f"{python_version}_" |
164 |
| - f"{os}" |
165 |
| - ) |
166 |
| - } |
167 |
| - ) |
168 |
| - |
169 |
| - else: |
170 |
| - jobs.append( |
171 |
| - { |
172 |
| - "tox_env": f"{tox_env}", |
173 |
| - "python_version": ( |
174 |
| - f"{_get_python_version_alias(python_version)}" |
175 |
| - ), |
176 |
| - "os": os, |
177 |
| - "ui_name": ( |
178 |
| - f"{env_name} " |
179 |
| - f"{_get_python_version_alias(python_version)} " |
180 |
| - f"{_get_os_alias(os)}" |
181 |
| - ), |
182 |
| - "name": ( |
183 |
| - f"{env_name}_" |
184 |
| - f"{python_version}_" |
185 |
| - f"{os}" |
186 |
| - ) |
187 |
| - } |
188 |
| - ) |
189 |
| - |
190 |
| - return jobs |
191 |
| - |
192 |
| - |
193 |
| -def generate_contrib_workflows(): |
194 |
| - |
195 |
| - tox_ini_path = Path(argv[1]) |
196 |
| - workflows_path = Path(argv[2]) |
197 |
| - |
198 |
| - with open(workflows_path.joinpath("test.yml"), "w") as test_yml_file: |
| 98 | + |
| 99 | +def generate_test_workflow(tox_ini_path: Path, workflow_directory_path: Path): |
| 100 | + |
| 101 | + with ( |
| 102 | + open(workflow_directory_path.joinpath("test.yml"), "w") as |
| 103 | + test_yml_file |
| 104 | + ): |
199 | 105 | test_yml_file.write(
|
200 | 106 | Environment(
|
201 |
| - loader=FileSystemLoader(workflows_path) |
| 107 | + loader=FileSystemLoader(Path(__file__).parent) |
202 | 108 | ).get_template("test.yml.j2").render(
|
203 |
| - jobs=get_test_jobs(tox_ini_path) |
| 109 | + test_jobs_data=get_test_jobs_data(tox_ini_path) |
204 | 110 | )
|
205 | 111 | )
|
206 | 112 | test_yml_file.write("\n")
|
| 113 | + |
| 114 | + |
| 115 | +def generate_lint_workflow(tox_ini_path: Path, workflow_directory_path: Path): |
| 116 | + |
| 117 | + with ( |
| 118 | + open(workflow_directory_path.joinpath("lint.yml"), "w") as |
| 119 | + lint_yml_file |
| 120 | + ): |
| 121 | + lint_yml_file.write( |
| 122 | + Environment( |
| 123 | + loader=FileSystemLoader(Path(__file__).parent) |
| 124 | + ).get_template("lint.yml.j2").render( |
| 125 | + lint_jobs_data=get_lint_jobs_data(tox_ini_path) |
| 126 | + ) |
| 127 | + ) |
| 128 | + lint_yml_file.write("\n") |
0 commit comments