Skip to content

Commit 88fd4d9

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

4 files changed

Lines changed: 33 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: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@
2727
as_lists.sort()
2828

2929

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

3332
print("Joined all files into", joined_file)
3433

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

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
1-
from shared_subclass_functions import wrap_in_template
1+
from shared_subclass_functions import write_data_extension
22
import sys
3-
import yaml
43
from pathlib import Path
54

65
py_version = sys.version.split()[0]
76
VERSION = f"process-builtin-exceptions 0.0.1; Python {py_version}"
87

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

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)
1510

1611
builtin_names = dir(__builtins__)
1712
builtin_dict = {x: getattr(__builtins__,x) for x in builtin_names}
@@ -28,4 +23,4 @@ def write_data(data, path: Path):
2823
row = [f"builtins.{basename}~Subclass", f"builtins.{subname}", ""]
2924
data.append(row)
3025

31-
write_data(wrap_in_template(data), builtins_model_path)
26+
write_data_extension(data, VERSION, builtins_model_path)
Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from typing import Dict
2-
import yaml
2+
import json
3+
import json5
34
from pathlib import Path
45
import glob
56
from collections import defaultdict
@@ -11,45 +12,48 @@
1112

1213
subclass_capture_path = mad_path / "subclass-capture"
1314

14-
joined_file = subclass_capture_path / "ALL.model.yml"
15+
joined_file = subclass_capture_path / "ALL.model.json"
1516

1617
def parse_from_file(path: Path) -> set:
1718
if not path.exists():
1819
return set()
1920

2021
f = path.open("r")
21-
assert f.readline().startswith(f"# {VERSION}\n"), path
22+
assert f.readline().startswith(f"// {VERSION}\n"), path
2223

23-
raw_data = yaml.load(f, Loader=yaml.CBaseLoader)
24+
raw_data = json5.load(f)
2425
assert len(raw_data["extensions"]) == 1, path
2526
assert raw_data["extensions"][0]["addsTo"]["extensible"] == "typeModel", path
2627

2728
return set(tuple(x) for x in raw_data["extensions"][0]["data"])
2829

2930

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

4953

5054
def gather_from_existing():
5155
package_data = defaultdict(set)
52-
for f in glob.glob(f"{subclass_capture_path}/auto-*.model.yml", recursive=True):
56+
for f in glob.glob(f"{subclass_capture_path}/auto-*.model.json", recursive=True):
5357
print(f"Processing {f}")
5458

5559
all_data = parse_from_file(Path(f))
@@ -64,7 +68,7 @@ def write_all_package_data_to_files(package_data: Dict[str, set]):
6468
print(f"Skipping {repr(pkg)}")
6569
continue
6670

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

6973
print(f"Writing {pkg_path}")
7074

@@ -74,6 +78,4 @@ def write_all_package_data_to_files(package_data: Dict[str, set]):
7478
as_lists = [list(t) for t in all_data]
7579
as_lists.sort()
7680

77-
data_for_yaml = wrap_in_template(as_lists)
78-
79-
write_data(data_for_yaml, pkg_path)
81+
write_data_extension(as_lists, VERSION, pkg_path)
File renamed without changes.

0 commit comments

Comments
 (0)