-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
72 lines (47 loc) · 1.57 KB
/
Copy pathmain.py
File metadata and controls
72 lines (47 loc) · 1.57 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
import sys
import json
import os
from CodeZap.driver.parser import build_ir
def main():
"""
Entry point for the CLI.
Workflow:
1. Read file path from command-line arguments
2. Validate file existence (with fallback inside CodeZap/)
3. Build IR from the source file
4. Convert IR to clean JSON
5. Output result:
- print to stdout (default)
- or write to file if output path is provided
Usage:
py -m CodeZap.main <file.py> [output.json]
"""
# Step 1: Validate CLI arguments
if len(sys.argv) < 2:
print("Usage: py -m CodeZap.main <file.py> [output.json]")
sys.exit(1)
filepath = os.path.abspath(sys.argv[1])
# Step 2: Check file existence
if not os.path.exists(filepath):
alt_path = os.path.join("CodeZap", filepath)
if os.path.exists(alt_path):
filepath = alt_path
else:
print(f"Error: file '{filepath}' not found")
sys.exit(1)
# Step 3: Build IR
root = build_ir(filepath)
# Step 4: Convert IR to clean JSON
json_output = root.to_clean_dict()
# Step 5: Output result
output_file = sys.argv[2] if len(sys.argv) >= 3 else None
if output_file:
if os.path.exists(output_file):
print(f"Error: '{output_file}' already exists. Use a different name.")
sys.exit(1)
with open(output_file, "w", encoding="utf-8") as f:
json.dump(json_output, f, indent=2)
else:
print(json.dumps(json_output, indent=2))
if __name__ == "__main__":
main()