Repository navigation
Expand file tree
/
Copy pathbuild_java.py
More file actions
89 lines (74 loc) · 2.78 KB
/
Copy pathbuild_java.py
File metadata and controls
89 lines (74 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
"""Compile the Java bridge and safely update the bundled ProLeap JAR."""
from __future__ import annotations
import os
import shutil
import subprocess
import tempfile
import zipfile
from pathlib import Path
ROOT = Path(__file__).resolve().parent
JAVA_SOURCES = sorted((ROOT / "java").rglob("*.java"))
JAR_PATH = ROOT / "cobolscope" / "lib" / "proleap-cobol-parser.jar"
def require_javac() -> str:
executable = shutil.which("javac")
if executable is None:
raise SystemExit("Required JDK tool 'javac' was not found on PATH.")
return executable
def update_jar(classes_dir: Path) -> None:
compiled = {
path.relative_to(classes_dir).as_posix(): path
for path in classes_dir.rglob("*.class")
}
temp_jar = JAR_PATH.with_suffix(".jar.tmp")
seen: set[str] = set()
try:
with zipfile.ZipFile(JAR_PATH, "r") as source, zipfile.ZipFile(
temp_jar, "w"
) as destination:
for info in source.infolist():
if info.filename in compiled or info.filename in seen:
continue
seen.add(info.filename)
destination.writestr(info, source.read(info.filename))
for archive_name, class_file in compiled.items():
destination.write(
class_file, archive_name, compress_type=zipfile.ZIP_DEFLATED
)
os.replace(temp_jar, JAR_PATH)
finally:
temp_jar.unlink(missing_ok=True)
def main() -> int:
if not JAR_PATH.exists():
raise SystemExit(f"Bundled ProLeap JAR not found: {JAR_PATH}")
if not JAVA_SOURCES:
raise SystemExit(f"No Java sources found under {ROOT / 'java'}")
javac = require_javac()
with tempfile.TemporaryDirectory(prefix="cobolscope-java-") as temp_dir:
classes_dir = Path(temp_dir)
print(f"Compiling {len(JAVA_SOURCES)} Java source files with Java 17...")
subprocess.run(
[
javac,
"--release",
"17",
"-cp",
str(JAR_PATH),
"-d",
str(classes_dir),
*(str(source) for source in JAVA_SOURCES),
],
check=True,
)
update_jar(classes_dir)
# If lib/ exists, also synchronize classes to avoid stale classpath overrides
lib_dir = ROOT / "lib"
if lib_dir.exists():
for class_file in classes_dir.rglob("*.class"):
rel_path = class_file.relative_to(classes_dir)
dest = lib_dir / rel_path
dest.parent.mkdir(parents=True, exist_ok=True)
shutil.copy2(class_file, dest)
print(f"Updated Java bridge classes in {JAR_PATH}")
return 0
if __name__ == "__main__":
raise SystemExit(main())