Skip to content

Commit cc48290

Browse files
committed
Python: update model processing scripts to use JSON
1 parent 497b803 commit cc48290

5 files changed

Lines changed: 53 additions & 37 deletions

File tree

python/ql/src/meta/ClassHierarchy/join-yml-files.py renamed to python/ql/src/meta/ClassHierarchy/join-model-files.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#!/usr/bin/env python3
22

3+
# /// script
4+
# dependencies = ["json5"]
5+
# ///
6+
37
"""Concerns were raised about performance on Windows with having 2.5 k files for modeling, and it was recommended we join them all together when shipping.
48
59
This script does that.
@@ -27,10 +31,9 @@
2731
as_lists.sort()
2832

2933

30-
to_write = wrap_in_template(as_lists)
31-
write_data(to_write, joined_file)
34+
write_data_extension(as_lists, VERSION, joined_file)
3235

3336
print("Joined all files into", joined_file)
3437

35-
for f in glob.glob(f"{subclass_capture_path}/auto-*.model.yml", recursive=True):
38+
for f in glob.glob(f"{subclass_capture_path}/auto-*.model.json", recursive=True):
3639
os.unlink(f)

‎python/ql/src/meta/ClassHierarchy/process-builtin-exceptions.py‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
from shared_subclass_functions import wrap_in_template
1+
# /// script
2+
# dependencies = ["json5"]
3+
# ///
4+
5+
from shared_subclass_functions import write_data_extension
26
import sys
3-
import yaml
47
from pathlib import Path
58

69
py_version = sys.version.split()[0]
710
VERSION = f"process-builtin-exceptions 0.0.1; Python {py_version}"
811

9-
builtins_model_path = Path(__file__).parent.parent.parent.parent / "lib/semmle/python/frameworks/builtins.model.yml"
12+
builtins_model_path = Path(__file__).parent.parent.parent.parent / "lib/semmle/python/frameworks/builtins.model.json"
1013

11-
def write_data(data, path: Path):
12-
f = path.open("w+")
13-
f.write(f"# {VERSION}\n")
14-
yaml.dump(data, indent=2, stream=f, Dumper=yaml.CDumper)
1514

1615
builtin_names = dir(__builtins__)
1716
builtin_dict = {x: getattr(__builtins__,x) for x in builtin_names}
@@ -28,4 +27,4 @@ def write_data(data, path: Path):
2827
row = [f"builtins.{basename}~Subclass", f"builtins.{subname}", ""]
2928
data.append(row)
3029

31-
write_data(wrap_in_template(data), builtins_model_path)
30+
write_data_extension(data, VERSION, builtins_model_path)

‎python/ql/src/meta/ClassHierarchy/process-mrva-results.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#!/usr/bin/env python3
22

3+
# /// script
4+
# dependencies = ["json5"]
5+
# ///
6+
37
import sys
48
import glob
59
import json
Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
# /// script
2+
# dependencies = ["json5"]
3+
# ///
4+
15
from typing import Dict
2-
import yaml
6+
import json
7+
import json5
38
from pathlib import Path
49
import glob
510
from collections import defaultdict
@@ -11,45 +16,48 @@
1116

1217
subclass_capture_path = mad_path / "subclass-capture"
1318

14-
joined_file = subclass_capture_path / "ALL.model.yml"
19+
joined_file = subclass_capture_path / "ALL.model.json"
1520

1621
def parse_from_file(path: Path) -> set:
1722
if not path.exists():
1823
return set()
1924

2025
f = path.open("r")
21-
assert f.readline().startswith(f"# {VERSION}\n"), path
26+
assert f.readline().startswith(f"// {VERSION}\n"), path
2227

23-
raw_data = yaml.load(f, Loader=yaml.CBaseLoader)
28+
raw_data = json5.load(f)
2429
assert len(raw_data["extensions"]) == 1, path
2530
assert raw_data["extensions"][0]["addsTo"]["extensible"] == "typeModel", path
2631

2732
return set(tuple(x) for x in raw_data["extensions"][0]["data"])
2833

2934

30-
def wrap_in_template(data):
31-
return {
32-
"extensions": [
33-
{
34-
"addsTo": {
35-
"pack": "codeql/python-all",
36-
"extensible": "typeModel",
37-
},
38-
"data": data,
39-
}
40-
]
41-
}
42-
43-
44-
def write_data(data, path: Path):
35+
def write_data_extension(data, header_comment, path: Path):
4536
f = path.open("w+")
46-
f.write(f"# {VERSION}\n")
47-
yaml.dump(data, indent=2, stream=f, Dumper=yaml.CDumper)
37+
f.write(f"// {header_comment}\n")
38+
# Write most of the JSON manually, for finer control over formatting
39+
f.write("{\n")
40+
f.write(' "extensions": [\n')
41+
f.write(' {\n')
42+
f.write(' "addsTo": {\n')
43+
f.write(' "pack": "codeql/python-all",\n')
44+
f.write(' "extensible": "typeModel"\n')
45+
f.write(' },\n')
46+
f.write(' "data": [\n')
47+
for i, row in enumerate(data):
48+
if i > 0:
49+
f.write(",\n")
50+
f.write(f" {json.dumps(row)}")
51+
f.write('\n')
52+
f.write(' ]\n')
53+
f.write(' }\n')
54+
f.write(" ]\n")
55+
f.write("}")
4856

4957

5058
def gather_from_existing():
5159
package_data = defaultdict(set)
52-
for f in glob.glob(f"{subclass_capture_path}/auto-*.model.yml", recursive=True):
60+
for f in glob.glob(f"{subclass_capture_path}/auto-*.model.json", recursive=True):
5361
print(f"Processing {f}")
5462

5563
all_data = parse_from_file(Path(f))
@@ -64,7 +72,7 @@ def write_all_package_data_to_files(package_data: Dict[str, set]):
6472
print(f"Skipping {repr(pkg)}")
6573
continue
6674

67-
pkg_path = subclass_capture_path / f"auto-{pkg}.model.yml"
75+
pkg_path = subclass_capture_path / f"auto-{pkg}.model.json"
6876

6977
print(f"Writing {pkg_path}")
7078

@@ -74,6 +82,4 @@ def write_all_package_data_to_files(package_data: Dict[str, set]):
7482
as_lists = [list(t) for t in all_data]
7583
as_lists.sort()
7684

77-
data_for_yaml = wrap_in_template(as_lists)
78-
79-
write_data(data_for_yaml, pkg_path)
85+
write_data_extension(as_lists, VERSION, pkg_path)

python/ql/src/meta/ClassHierarchy/split-yml-files.py renamed to python/ql/src/meta/ClassHierarchy/split-model-files.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#!/usr/bin/env python3
22

3+
# /// script
4+
# dependencies = ["json5"]
5+
# ///
6+
37
"""Concerns were raised about performance on Windows with having 2.5 k files for modeling, and it was recommended we join them all together when shipping.
48
59
This script does the opposite, so it's easier to work with locally.

0 commit comments

Comments
 (0)