Skip to content

Commit 896f6f5

Browse files
author
Puneet Singhwaiya
committed
2 parents fac7127 + 5a0540b commit 896f6f5

File tree

72 files changed

+11545
-861
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+11545
-861
lines changed
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from pathlib import Path
2+
3+
from generate_workflows_lib import (
4+
generate_test_workflow,
5+
generate_lint_workflow,
6+
generate_misc_workflow
7+
)
8+
9+
tox_ini_path = Path(__file__).parent.parent.parent.joinpath("tox.ini")
10+
workflows_directory_path = Path(__file__).parent
11+
12+
generate_test_workflow(tox_ini_path, workflows_directory_path, "ubuntu-latest")
13+
generate_lint_workflow(tox_ini_path, workflows_directory_path)
14+
generate_misc_workflow(tox_ini_path, workflows_directory_path)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from hatchling.builders.hooks.plugin.interface import BuildHookInterface
2+
from pathlib import Path
3+
4+
5+
class CustomBuildHook(BuildHookInterface):
6+
7+
def initialize(self, version, build_data):
8+
9+
with open(
10+
Path(__file__).parent.parent.parent.parent.joinpath("tox.ini")
11+
) as tox_ini_file_0:
12+
with open(
13+
Path(__file__).parent.joinpath("src/generate_workflows_lib/tox.ini"), "w"
14+
) as tox_ini_file_1:
15+
tox_ini_file_1.write(tox_ini_file_0.read())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
[build-system]
2+
requires = ["hatchling"]
3+
build-backend = "hatchling.build"
4+
5+
[project]
6+
name = "generate-workflows-lib"
7+
dynamic = ["version"]
8+
description = "A library to generate workflows"
9+
license = "Apache-2.0"
10+
requires-python = ">=3.8"
11+
authors = [
12+
{ name = "OpenTelemetry Authors", email = "[email protected]" },
13+
]
14+
classifiers = [
15+
"Development Status :: 4 - Beta",
16+
"Intended Audience :: Developers",
17+
"License :: OSI Approved :: Apache Software License",
18+
"Programming Language :: Python",
19+
"Programming Language :: Python :: 3",
20+
"Programming Language :: Python :: 3.8",
21+
"Programming Language :: Python :: 3.9",
22+
"Programming Language :: Python :: 3.10",
23+
"Programming Language :: Python :: 3.11",
24+
"Programming Language :: Python :: 3.12",
25+
"Typing :: Typed",
26+
]
27+
dependencies = ["Jinja2", "tox"]
28+
29+
[tool.hatch.version]
30+
path = "src/generate_workflows_lib/version.py"
31+
32+
[tool.hatch.build.targets.wheel.hooks.custom]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
tox.ini
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,267 @@
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+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Do not edit this file.
2+
# This file is generated automatically by executing tox -e generate-workflows
3+
4+
name: Contrib {{ file_number }}
5+
6+
on:
7+
push:
8+
branches-ignore:
9+
- 'release/*'
10+
pull_request:
11+
12+
env:
13+
CORE_REPO_SHA: ${% raw %}{{ github.sha }}{% endraw %}
14+
CONTRIB_REPO_SHA: main
15+
PIP_EXISTS_ACTION: w
16+
17+
jobs:
18+
{%- for job_data in job_datas %}
19+
20+
{{ job_data.tox_env }}:
21+
name: {{ job_data.ui_name }}
22+
runs-on: ubuntu-latest
23+
steps:
24+
- name: Checkout contrib repo @ SHA - ${% raw %}{{ env.CONTRIB_REPO_SHA }}{% endraw %}
25+
uses: actions/checkout@v4
26+
with:
27+
repository: open-telemetry/opentelemetry-python-contrib
28+
ref: ${% raw %}{{ env.CONTRIB_REPO_SHA }}{% endraw %}
29+
30+
- name: Checkout core repo @ SHA - ${% raw %}{{ github.sha }}{% endraw %}
31+
uses: actions/checkout@v4
32+
with:
33+
repository: open-telemetry/opentelemetry-python
34+
path: opentelemetry-python-core
35+
36+
- name: Set up Python 3.8
37+
uses: actions/setup-python@v5
38+
with:
39+
python-version: "3.8"
40+
architecture: "x64"
41+
42+
- name: Install tox
43+
run: pip install tox
44+
45+
- name: Run tests
46+
run: tox -e {{ job_data.tox_env }} -- -ra
47+
{%- endfor %}

0 commit comments

Comments
 (0)