Skip to content

Commit 75b16f0

Browse files
committed
1 central run_pylint script.
1 parent 87764de commit 75b16f0

File tree

7 files changed

+108
-114
lines changed

7 files changed

+108
-114
lines changed

.github/actions/test-pylint/action.yml

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ runs:
1313
sudo apt-get -qq update
1414
sudo apt-get -qq -y install python3-pip gnupg
1515
python -m pip install --upgrade pip
16+
# Pylint runs against the prebuilt wheels in pycode/wheelhouse/*cp311*.
17+
# So, use current latest supported Python version.
1618
- name: Set up Python 3.11
1719
uses: actions/setup-python@v5
1820
with:
@@ -30,14 +32,8 @@ runs:
3032
- name: Run pylint
3133
shell: bash
3234
run: |
33-
cd pycode/memilio-${{ inputs.package }}
34-
mkdir -p build_pylint
35-
if [ -f tools/run_pylint.py ]; then
36-
python tools/run_pylint.py
37-
else
38-
python -m pylint --rcfile=../pylintrc --load-plugins=pylint_json2html --output-format=jsonextended memilio/ > build_pylint/pylint_extended.json || true
39-
fi
40-
pylint-json2html -f jsonextended -o build_pylint/pylint.html < build_pylint/pylint_extended.json
35+
python pycode/tools/run_pylint.py --package-dir memilio-${{ inputs.package }}
36+
pylint-json2html -f jsonextended -o pycode/memilio-${{ inputs.package }}/build_pylint/pylint.html < pycode/memilio-${{ inputs.package }}/build_pylint/pylint_extended.json
4137
- name: Upload Pylint Report
4238
uses: actions/upload-artifact@v4
4339
with:

docs/source/python/python_packages.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,11 @@ Run pylint with the commands in the package folder
171171

172172
.. code-block:: console
173173
174-
python tools/run_pylint.py
174+
python ../tools/run_pylint.py
175175
pylint-json2html -f jsonextended -o build_pylint/pylint.html < build_pylint/pylint_extended.json
176176
177-
Packages without the helper script can run pylint directly, for example ``python -m pylint memilio``.
177+
From the repository root you can also target a package explicitly, for example
178+
``python pycode/tools/run_pylint.py --package-dir memilio-plot``.
178179

179180
Pylint report for actual master:
180181

pycode/memilio-epidata/README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ Run pylint with the commands
116116

117117
.. code:: sh
118118
119-
python tools/run_pylint.py
119+
python ../tools/run_pylint.py
120120
pylint-json2html -f jsonextended -o build_pylint/pylint.html < build_pylint/pylint_extended.json
121121
122122
Pylint report for actual master:

pycode/memilio-epidata/tools/run_pylint.py

Lines changed: 0 additions & 51 deletions
This file was deleted.

pycode/memilio-plot/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ Run pylint with the commands
104104

105105
.. code:: sh
106106

107-
python tools/run_pylint.py
107+
python ../tools/run_pylint.py
108108
pylint-json2html -f jsonextended -o build_pylint/pylint.html < build_pylint/pylint_extended.json
109109

110110
Pylint report for actual master:

pycode/memilio-plot/tools/run_pylint.py

Lines changed: 0 additions & 51 deletions
This file was deleted.

pycode/tools/run_pylint.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#!/usr/bin/env python3
2+
"""Run pylint for a given memilio package and write the jsonextended report."""
3+
4+
from __future__ import annotations
5+
6+
import argparse
7+
import subprocess
8+
import sys
9+
from pathlib import Path
10+
11+
12+
def parse_args(pycode_root: Path) -> argparse.Namespace:
13+
parser = argparse.ArgumentParser(
14+
description="Run pylint for a specific memilio pycode package."
15+
)
16+
parser.add_argument(
17+
"--package-dir",
18+
help="Directory name under pycode/ (e.g. memilio-epidata).",
19+
)
20+
args = parser.parse_args()
21+
22+
# if package_dir is given, use it directly
23+
if args.package_dir:
24+
return args
25+
26+
# If no package dir is provided, try to infer it from the current working directory
27+
# Used, when the script is called from inside pycode/<package>/...
28+
try:
29+
relative = Path.cwd().resolve().relative_to(pycode_root)
30+
except ValueError:
31+
parser.error(
32+
"--package-dir is required when not running from inside pycode/<package>"
33+
)
34+
35+
if not relative.parts:
36+
parser.error(
37+
"--package-dir is required when not running from inside pycode/<package>"
38+
)
39+
40+
args.package_dir = relative.parts[0]
41+
return args
42+
43+
44+
def main() -> int:
45+
repo_root = Path(__file__).resolve().parents[2]
46+
pycode_root = repo_root / "pycode"
47+
args = parse_args(pycode_root)
48+
package_dir = pycode_root / args.package_dir
49+
50+
if not package_dir.is_dir():
51+
sys.stderr.write(f"Package directory not found: {package_dir}\n")
52+
return 1
53+
54+
rcfile = pycode_root / "pylintrc"
55+
if not rcfile.is_file():
56+
sys.stderr.write(f"Cannot find pylintrc at {rcfile}\n")
57+
return 1
58+
59+
build_dir = package_dir / "build_pylint"
60+
build_dir.mkdir(exist_ok=True)
61+
output_file = build_dir / "pylint_extended.json"
62+
63+
cmd = [
64+
sys.executable,
65+
"-m",
66+
"pylint",
67+
"--rcfile",
68+
str(rcfile),
69+
"--load-plugins",
70+
"pylint_json2html",
71+
"--output-format=jsonextended",
72+
"memilio/",
73+
]
74+
75+
with output_file.open("w", encoding="utf-8") as stream:
76+
result = subprocess.run(
77+
cmd,
78+
cwd=package_dir,
79+
stdout=stream,
80+
stderr=subprocess.PIPE,
81+
text=True,
82+
check=False,
83+
)
84+
85+
if result.stderr:
86+
sys.stderr.write(result.stderr)
87+
88+
if result.returncode:
89+
sys.stderr.write(
90+
f"pylint exited with {result.returncode}; "
91+
f"see {output_file} for details.\n"
92+
)
93+
94+
# Always exit 0 so CI can upload and publish the report even when pylint fails.
95+
return 0
96+
97+
98+
if __name__ == "__main__":
99+
sys.exit(main())

0 commit comments

Comments
 (0)