Skip to content

Commit 6f1cfcd

Browse files
cawo-odooKangOl
andcommitted
[IMP] tools/compile23.py
Recent systems don't have python2 installed. Let the pre-commit hook pass, but warns, when it must verify scripts that should be compatible with python2. closes #280 Signed-off-by: Christophe Simonis (chs) <[email protected]> Co-authored-by: Christophe Simonis (chs) <[email protected]>
1 parent 30ea7c7 commit 6f1cfcd

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ repos:
55
name: Compile python files using the expected runtime version
66
entry: ./tools/compile23.py
77
language: script
8+
require_serial: true
9+
verbose: true
810
- id: bad-import-000
911
name: Incompatible import with old versions in tests and `0.0.0` scripts
1012
language: pygrep

tools/compile23.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import subprocess
44
import sys
55
from pathlib import PurePath
6+
from shutil import which
67

78
py2_only_patterns = []
89
py2_files = []
@@ -41,9 +42,20 @@
4142

4243

4344
if py2_files:
44-
s = subprocess.run(["python2", "-m", "compileall", "-f", "-q", *py2_files], check=False)
45-
if s.returncode:
46-
rc = 1
45+
if which("python2"):
46+
s = subprocess.run(["python2", "-m", "compileall", "-f", "-q", *py2_files], check=False)
47+
if s.returncode:
48+
rc = 1
49+
else:
50+
lines = [
51+
"WARNING: `python2` hasn't been found in $PATH",
52+
"You must ensure the following files are compatible with python2:",
53+
*[f" - {f}" for f in py2_files],
54+
]
55+
width = max(map(len, lines))
56+
message = "\n".join(f"@ {line: <{width}s} @" for line in lines)
57+
extra = "@" * (width + 4)
58+
print(f"{extra}\n{message}\n{extra}", file=sys.stderr)
4759

4860
if py3_files:
4961
s = subprocess.run(["python3", "-m", "compileall", "-f", "-q", *py3_files], check=False)

0 commit comments

Comments
 (0)