Skip to content

Commit 68bd8fb

Browse files
committed
Fix add CI for dev_scripts
1 parent 39130fc commit 68bd8fb

File tree

6 files changed

+251
-1
lines changed

6 files changed

+251
-1
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: CI-for-dev-scripts
2+
3+
on:
4+
push:
5+
branches: [ master, main ]
6+
tags: [ "**" ]
7+
paths:
8+
- dev_scripts/**
9+
pull_request:
10+
branches: [ "**" ]
11+
paths:
12+
- dev_scripts/**
13+
14+
jobs:
15+
Execute-continuous-integration:
16+
runs-on: ubuntu-latest
17+
strategy:
18+
matrix:
19+
python-version: ['3.10']
20+
21+
steps:
22+
- uses: actions/checkout@master
23+
24+
- name: Set up Python ${{ matrix.python-version }}
25+
uses: actions/setup-python@v2
26+
with:
27+
python-version: ${{ matrix.python-version }}
28+
29+
- name: Install dependencies
30+
run: |
31+
python3 -m pip install --upgrade pip
32+
pip3 install --group dev_scripts/pyproject.toml:dev -e dev_scripts
33+
34+
- name: Run checks
35+
run: |
36+
python3 dev_scripts/continuous_integration_of_dev_scripts/precommit.py

dev_scripts/continuous_integration_of_dev_scripts/__init__.py

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[mypy]
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
#!/usr/bin/env python3
2+
3+
"""Run pre-commit checks on the repository."""
4+
5+
import argparse
6+
import enum
7+
import os
8+
import pathlib
9+
import shlex
10+
import subprocess
11+
import sys
12+
from typing import Optional, Mapping, Sequence
13+
14+
15+
# pylint: disable=unnecessary-comprehension
16+
17+
18+
class Step(enum.Enum):
19+
"""Enumerate different pre-commit steps."""
20+
21+
REFORMAT = "reformat"
22+
MYPY = "mypy"
23+
PYLINT = "pylint"
24+
TEST = "test"
25+
DOCTEST = "doctest"
26+
27+
28+
def call_and_report(
29+
verb: str,
30+
cmd: Sequence[str],
31+
cwd: Optional[pathlib.Path] = None,
32+
env: Optional[Mapping[str, str]] = None,
33+
) -> int:
34+
"""
35+
Wrap a subprocess call with the reporting to STDERR if it failed.
36+
37+
Return 1 if there is an error and 0 otherwise.
38+
"""
39+
cmd_str = " ".join(shlex.quote(part) for part in cmd)
40+
41+
if cwd is not None:
42+
print(f"Executing from {cwd}: {cmd_str}")
43+
else:
44+
print(f"Executing: {cmd_str}")
45+
46+
exit_code = subprocess.call(cmd, cwd=str(cwd) if cwd is not None else None, env=env)
47+
48+
if exit_code != 0:
49+
print(
50+
f"Failed to {verb} with exit code {exit_code}: {cmd_str}", file=sys.stderr
51+
)
52+
53+
return exit_code
54+
55+
56+
def main() -> int:
57+
"""Execute entry_point routine."""
58+
parser = argparse.ArgumentParser(description=__doc__)
59+
parser.add_argument(
60+
"--overwrite",
61+
help="Try to automatically fix the offending files (e.g., by re-formatting).",
62+
action="store_true",
63+
)
64+
parser.add_argument(
65+
"--select",
66+
help=(
67+
"If set, only the selected steps are executed. "
68+
"This is practical if some of the steps failed and you want to "
69+
"fix them in isolation. "
70+
"The steps are given as a space-separated list of: "
71+
+ " ".join(value.value for value in Step)
72+
),
73+
metavar="",
74+
nargs="+",
75+
choices=[value.value for value in Step],
76+
)
77+
parser.add_argument(
78+
"--skip",
79+
help=(
80+
"If set, skips the specified steps. "
81+
"This is practical if some of the steps passed and "
82+
"you want to fix the remainder in isolation. "
83+
"The steps are given as a space-separated list of: "
84+
+ " ".join(value.value for value in Step)
85+
),
86+
metavar="",
87+
nargs="+",
88+
choices=[value.value for value in Step],
89+
)
90+
91+
args = parser.parse_args()
92+
93+
overwrite = bool(args.overwrite)
94+
95+
selects = (
96+
[Step(value) for value in args.select]
97+
if args.select is not None
98+
else [value for value in Step]
99+
)
100+
skips = [Step(value) for value in args.skip] if args.skip is not None else []
101+
102+
src_root = pathlib.Path(os.path.realpath(__file__)).parent.parent
103+
104+
if Step.REFORMAT in selects and Step.REFORMAT not in skips:
105+
print("Re-formatting...")
106+
reformat_targets = [
107+
"codegen/codegen.py",
108+
"codegen/download_aas_core_meta_model.py",
109+
"continuous_integration_of_dev_scripts",
110+
"update_to_aas_core_meta_codegen.py",
111+
]
112+
if overwrite:
113+
exit_code = call_and_report(
114+
verb="black",
115+
cmd=["black"] + reformat_targets,
116+
cwd=src_root,
117+
)
118+
if exit_code != 0:
119+
return 1
120+
else:
121+
exit_code = call_and_report(
122+
verb="check with black",
123+
cmd=["black", "--check"] + reformat_targets,
124+
cwd=src_root,
125+
)
126+
if exit_code != 0:
127+
return 1
128+
else:
129+
print("Skipped re-formatting.")
130+
131+
if Step.MYPY in selects and Step.MYPY not in skips:
132+
print("Mypy'ing...")
133+
mypy_targets = [
134+
"codegen/codegen.py",
135+
"codegen/download_aas_core_meta_model.py",
136+
"continuous_integration_of_dev_scripts",
137+
"update_to_aas_core_meta_codegen.py",
138+
]
139+
config_file = pathlib.Path("continuous_integration_of_dev_scripts") / "mypy.ini"
140+
141+
exit_code = call_and_report(
142+
verb="mypy",
143+
cmd=["mypy", "--strict", "--config-file", str(config_file)] + mypy_targets,
144+
cwd=src_root,
145+
)
146+
if exit_code != 0:
147+
return 1
148+
else:
149+
print("Skipped mypy'ing.")
150+
151+
if Step.PYLINT in selects and Step.PYLINT not in skips:
152+
print("Pylint'ing...")
153+
pylint_targets = [
154+
"codegen/codegen.py",
155+
"codegen/download_aas_core_meta_model.py",
156+
"continuous_integration_of_dev_scripts",
157+
"update_to_aas_core_meta_codegen.py",
158+
]
159+
rcfile = pathlib.Path("continuous_integration_of_dev_scripts") / "pylint.rc"
160+
161+
exit_code = call_and_report(
162+
verb="pylint",
163+
cmd=["pylint", f"--rcfile={rcfile}"] + pylint_targets,
164+
cwd=src_root,
165+
)
166+
if exit_code != 0:
167+
return 1
168+
else:
169+
print("Skipped pylint'ing.")
170+
171+
if Step.DOCTEST in selects and Step.DOCTEST not in skips:
172+
print("Doctest'ing...")
173+
174+
for module_name in [
175+
"continuous_integration_of_dev_scripts",
176+
]:
177+
for pth in (src_root / module_name).glob("**/*.py"):
178+
if pth.name == "__main__.py":
179+
continue
180+
181+
# NOTE (mristin, 2022-12-08):
182+
# The subprocess calls are expensive, call only if there is an actual
183+
# doctest
184+
text = pth.read_text(encoding="utf-8")
185+
if ">>>" in text:
186+
exit_code = call_and_report(
187+
verb="doctest",
188+
cmd=[sys.executable, "-m", "doctest", str(pth)],
189+
cwd=src_root,
190+
)
191+
if exit_code != 0:
192+
return 1
193+
else:
194+
print("Skipped doctest'ing.")
195+
196+
return 0
197+
198+
199+
if __name__ == "__main__":
200+
sys.exit(main())
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[TYPECHECK]
2+
ignored-modules = numpy
3+
ignored-classes = numpy,PurePath
4+
generated-members=bottle\.request\.forms\.decode,bottle\.request\.query\.decode
5+
6+
[FORMAT]
7+
max-line-length=120
8+
9+
[MESSAGES CONTROL]
10+
disable=too-few-public-methods,len-as-condition,duplicate-code,no-else-raise,no-else-return,too-many-locals,too-many-branches,too-many-nested-blocks,too-many-return-statements,unsubscriptable-object,not-an-iterable,broad-except,too-many-statements,protected-access,unnecessary-pass,too-many-statements,too-many-arguments,no-member,too-many-instance-attributes,too-many-lines,undefined-variable,unnecessary-lambda,assignment-from-none,useless-return,unused-argument,too-many-boolean-expressions,consider-using-f-string,use-dict-literal,invalid-name,no-else-continue,no-else-break,unneeded-not,too-many-public-methods,line-too-long

dev_scripts/pyproject.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,11 @@ dependencies = [
2626

2727
[dependency-groups]
2828
dev = [
29+
"black==25.9.0",
30+
"mypy==1.18.2",
31+
"pylint==3.3.8",
2932
"types-requests>=2.32.4",
3033
]
3134

3235
[tool.setuptools.packages.find]
33-
exclude = ["tests", "continuous_integration", "dev_scripts"]
36+
exclude = ["continuous_integration_of_dev_scripts"]

0 commit comments

Comments
 (0)