-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathranges.py
49 lines (34 loc) · 1.17 KB
/
ranges.py
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
from functools import reduce
import json
import os
import sys
if len(sys.argv) < 2:
raise RuntimeError("AST filename argument is required")
if len(sys.argv) < 3:
raise RuntimeError("JS filename argument is required")
if len(sys.argv) > 3:
raise RuntimeError("Exactly 2 arguments are required")
with open(sys.argv[1]) as fd:
ast = json.load(fd)
assert ast["type"] == "Program"
assert ast["body"][1]["expression"]["type"] == "SequenceExpression"
with open(sys.argv[2], encoding="utf-8") as fd:
source = fd.read()
for chunk in ast["body"][1:]:
for expr in chunk["expression"]["expressions"]:
assert expr["type"] == "CallExpression"
assert expr["callee"]["name"] == "define"
start = expr["start"]
end = expr["end"]
name = expr["arguments"][0]["value"]
print(name)
parts = name.split("/")
def reducer(acc, part):
path = f"{acc}/{part}"
if not os.path.exists(path):
os.makedirs(path)
return path
reduce(reducer, parts[:-1], ".")
filename = f"./{name}.js"
with open(filename, "w") as fd:
fd.write(source[start:end])