-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
214 lines (164 loc) · 7.5 KB
/
server.py
File metadata and controls
214 lines (164 loc) · 7.5 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
"""
Flask server for Code Mosaic Visualizer.
Serves the front-end static files and exposes a small API so the
browser can read JSON trace data at runtime.
Usage:
pip install flask
python server.py
Then open http://localhost:5000 in your browser.
"""
import os
import json
import sys
import tempfile
from pathlib import Path
from flask import Flask, send_from_directory, jsonify, abort, request
app = Flask(__name__)
# ── paths ────────────────────────────────────────────────────────────
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_DIR = os.path.join(BASE_DIR, "mosiacs")
DATA_DIR = os.path.join(STATIC_DIR, "data")
JSON_DIR = os.path.join(DATA_DIR, "json")
PARSER_DIR = os.path.join(BASE_DIR, "parser")
TESTS_DIR = os.path.join(PARSER_DIR, "tests")
# ── API: serve code files ────────────────────────────────────────────
@app.route("/api/codefiles")
def list_code_files():
"""List all available .c and .py source files in data/."""
if not os.path.isdir(DATA_DIR):
return jsonify([])
files = [f for f in os.listdir(DATA_DIR)
if f.endswith((".c", ".py")) and os.path.isfile(os.path.join(DATA_DIR, f))]
files.sort()
return jsonify(files)
@app.route("/api/upload", methods=["POST"])
def upload_file():
"""Save uploaded file to data/ directory without processing."""
if "file" not in request.files:
return jsonify({"success": False, "error": "No file provided"}), 400
file = request.files["file"]
if not file.filename:
return jsonify({"success": False, "error": "No file selected"}), 400
ext = os.path.splitext(file.filename)[1].lower()
if ext not in (".c", ".py"):
return jsonify({"success": False, "error": f"Unsupported file type '{ext}'. Only .c and .py files are accepted."}), 400
# Save directly to DATA_DIR
os.makedirs(DATA_DIR, exist_ok=True)
file_path = os.path.join(DATA_DIR, file.filename)
file.save(file_path)
return jsonify({"success": True, "filename": file.filename})
@app.route("/api/process-file", methods=["POST"])
def process_file():
"""Process an uploaded file and return trace data without saving to disk."""
if "file" not in request.files:
return jsonify({"success": False, "error": {"stage": "upload", "message": "No file provided"}}), 400
file = request.files["file"]
if not file.filename:
return jsonify({"success": False, "error": {"stage": "upload", "message": "No file selected"}}), 400
ext = os.path.splitext(file.filename)[1].lower()
if ext not in (".c", ".py"):
return jsonify({"success": False, "error": {"stage": "upload", "message": f"Unsupported file type '{ext}'. Only .c and .py files are accepted."}}), 400
# Add parser directory to path
if PARSER_DIR not in sys.path:
sys.path.insert(0, PARSER_DIR)
from run import deal
# Process in temp directory
with tempfile.TemporaryDirectory() as tmpdir:
# Save uploaded file to temp directory
src_path = os.path.join(tmpdir, file.filename)
file.save(src_path)
# Output JSON path in temp directory
stem = os.path.splitext(file.filename)[0]
out_path = os.path.join(tmpdir, f"{stem}.json")
# Run the full pipeline: instrument → compile → run → normalize
return_code = deal(src_path, output=out_path, seed=-1)
# Read the result
with open(out_path) as f:
result = json.load(f)
return jsonify(result)
@app.route("/api/process", methods=["POST"])
def process_code_files():
"""Process selected code files from data/ directory."""
data = request.get_json()
if not data or "files" not in data:
return jsonify({
"success": False,
"error": {"stage": "request", "message": "No files specified"}
}), 400
files = data["files"]
if not isinstance(files, list) or len(files) == 0:
return jsonify({
"success": False,
"error": {"stage": "request", "message": "Files must be a non-empty array"}
}), 400
# Add parser directory to path
if PARSER_DIR not in sys.path:
sys.path.insert(0, PARSER_DIR)
from run import deal
results = []
errors = []
for filename in files:
# Security check
if "/" in filename or "\\" in filename or ".." in filename:
errors.append({"file": filename, "stage": "validation", "message": "Invalid filename"})
continue
input_path = os.path.join(DATA_DIR, filename)
if not os.path.isfile(input_path):
errors.append({"file": filename, "stage": "validation", "message": "File not found"})
continue
# Process the file
try:
with tempfile.TemporaryDirectory() as tmpdir:
output_path = os.path.join(tmpdir, f"{os.path.splitext(filename)[0]}.json")
# Run the parser
return_code = deal(input_path, output=output_path, seed=-1)
# Read the result
with open(output_path) as f:
result = json.load(f)
# ALWAYS include the result for visualization, even with no traces
# The frontend error panel will display compile/runtime errors
# Save to data/json directory
os.makedirs(JSON_DIR, exist_ok=True)
save_name = f"{os.path.splitext(filename)[0]}.json"
save_path = os.path.join(JSON_DIR, save_name)
with open(save_path, "w") as f:
json.dump(result, f, indent=2)
is_success = result.get("success", False)
results.append({
"file": filename,
"output": save_name,
"success": is_success,
"data": result, # Include result regardless of success or traces
"warning": result.get("error", {}).get("message") if not is_success else None
})
except Exception as e:
errors.append({
"file": filename,
"stage": "processing",
"message": str(e)
})
# Return results - always return 200 if we have any results (even with warnings)
# This allows visualization to proceed even with runtime errors
if len(results) > 0:
return jsonify({
"success": True,
"processed": len(results),
"results": results,
"errors": errors if errors else None
})
# Only return error status if we have zero results
return jsonify({
"success": False,
"errors": errors
}), 400
# ── Static files: serve the front-end ────────────────────────────────
@app.route("/")
def index():
return send_from_directory(STATIC_DIR, "index.html")
@app.route("/<path:path>")
def static_files(path):
return send_from_directory(STATIC_DIR, path)
# ── Run ──────────────────────────────────────────────────────────────
if __name__ == "__main__":
print(" Code Mosaic server running at http://localhost:5000")
app.run(debug=True, port=5000)