-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
executable file
·423 lines (367 loc) · 16.3 KB
/
server.py
File metadata and controls
executable file
·423 lines (367 loc) · 16.3 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#!/usr/bin/env python3
"""
Simple HTTP server to serve SearchExport.json and HTML visualization
"""
import http.server
import socketserver
import json
import os
import subprocess
import sys
import socket
import time
from urllib.parse import urlparse, parse_qs
PORT = 9999
def kill_port(port):
"""Kill any process using the specified port"""
killed_any = False
try:
# Try to find processes using the port
result = subprocess.run(
['lsof', '-ti', f':{port}'],
capture_output=True,
text=True,
timeout=2
)
if result.returncode == 0 and result.stdout.strip():
pids = result.stdout.strip().split('\n')
print(f"Found {len(pids)} process(es) on port {port}, killing...")
for pid in pids:
try:
subprocess.run(['kill', '-9', pid], check=True, timeout=2)
print(f" Killed process {pid}")
killed_any = True
except subprocess.CalledProcessError:
print(f" Failed to kill process {pid}")
except Exception as e:
print(f" Error killing process {pid}: {e}")
else:
print(f"No processes found on port {port}")
except FileNotFoundError:
# lsof not available, try alternative method
try:
result = subprocess.run(
['fuser', f'{port}/tcp'],
capture_output=True,
text=True,
timeout=2
)
if result.returncode == 0:
subprocess.run(['fuser', '-k', f'{port}/tcp'], timeout=2)
print(f"Killed processes on port {port} using fuser")
killed_any = True
except FileNotFoundError:
print("Warning: Neither 'lsof' nor 'fuser' available. Cannot auto-kill port.")
except Exception as e:
print(f"Warning: Could not check/kill port {port}: {e}")
# Give processes time to release the port
if killed_any:
time.sleep(1)
return killed_any
def is_port_available(port):
"""Check if a port is actually available by trying to bind to it"""
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try:
sock.bind(('', port))
sock.close()
return True
except OSError:
return False
class MyHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
parsed_path = urlparse(self.path)
# Handle favicon requests (prevent 404 errors)
if parsed_path.path == '/favicon.ico':
self.send_response(204) # No Content
self.end_headers()
return
# Serve index page
if parsed_path.path == '/' or parsed_path.path == '/index.html':
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
with open('index.html', 'r', encoding='utf-8') as f:
self.wfile.write(f.read().encode('utf-8'))
return
# Serve JSON data
if parsed_path.path == '/data':
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.send_header('Access-Control-Allow-Origin', '*')
self.end_headers()
try:
with open('SearchExport.json', 'r', encoding='utf-8') as f:
data = json.load(f)
self.wfile.write(json.dumps(data).encode('utf-8'))
except Exception as e:
error_response = json.dumps({'error': str(e)})
self.wfile.write(error_response.encode('utf-8'))
return
# Serve depth analysis page
if parsed_path.path == '/depth' or parsed_path.path == '/depth_analysis.html':
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
with open('depth_analysis.html', 'r', encoding='utf-8') as f:
self.wfile.write(f.read().encode('utf-8'))
return
# Serve source analysis page
if parsed_path.path == '/sources' or parsed_path.path == '/source_analysis.html':
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
with open('source_analysis.html', 'r', encoding='utf-8') as f:
self.wfile.write(f.read().encode('utf-8'))
return
# Serve source analysis data
if parsed_path.path == '/source_analysis_data.json':
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.send_header('Access-Control-Allow-Origin', '*')
self.end_headers()
try:
cache_file = 'source_analysis_data.json'
if os.path.exists(cache_file):
with open(cache_file, 'r', encoding='utf-8') as f:
data = json.load(f)
self.wfile.write(json.dumps(data).encode('utf-8'))
else:
error_response = json.dumps({'error': 'Source analysis data not found. Run precompute_sources.py first.'})
self.wfile.write(error_response.encode('utf-8'))
except Exception as e:
error_response = json.dumps({'error': str(e)})
self.wfile.write(error_response.encode('utf-8'))
return
# Serve source descriptions
if parsed_path.path == '/source_descriptions.json':
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.send_header('Access-Control-Allow-Origin', '*')
self.end_headers()
try:
desc_file = 'source_descriptions.json'
if os.path.exists(desc_file):
with open(desc_file, 'r', encoding='utf-8') as f:
data = json.load(f)
self.wfile.write(json.dumps(data).encode('utf-8'))
else:
error_response = json.dumps({})
self.wfile.write(error_response.encode('utf-8'))
except Exception as e:
error_response = json.dumps({'error': str(e)})
self.wfile.write(error_response.encode('utf-8'))
return
# Serve types analysis page
if parsed_path.path == '/types' or parsed_path.path == '/types_analysis.html':
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
with open('types_analysis.html', 'r', encoding='utf-8') as f:
self.wfile.write(f.read().encode('utf-8'))
return
# Serve ISO 11179 navigation page
if parsed_path.path == '/iso11179' or parsed_path.path == '/iso11179_navigation.html':
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
with open('iso11179_navigation.html', 'r', encoding='utf-8') as f:
self.wfile.write(f.read().encode('utf-8'))
return
# Serve ISO 11179 analysis data
if parsed_path.path == '/iso11179_analysis_data.json':
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.send_header('Access-Control-Allow-Origin', '*')
self.end_headers()
try:
cache_file = 'iso11179_analysis_data.json'
if os.path.exists(cache_file):
with open(cache_file, 'r', encoding='utf-8') as f:
data = json.load(f)
self.wfile.write(json.dumps(data).encode('utf-8'))
else:
error_response = json.dumps({'error': 'ISO 11179 analysis data not found. Run precompute_iso11179.py first.'})
self.wfile.write(error_response.encode('utf-8'))
except Exception as e:
error_response = json.dumps({'error': str(e)})
self.wfile.write(error_response.encode('utf-8'))
return
# Serve types analysis data
if parsed_path.path == '/types_analysis_data.json':
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.send_header('Access-Control-Allow-Origin', '*')
self.end_headers()
try:
cache_file = 'types_analysis_data.json'
if os.path.exists(cache_file):
with open(cache_file, 'r', encoding='utf-8') as f:
data = json.load(f)
self.wfile.write(json.dumps(data).encode('utf-8'))
else:
error_response = json.dumps({'error': 'Types analysis data not found. Run precompute_types.py first.'})
self.wfile.write(error_response.encode('utf-8'))
except Exception as e:
error_response = json.dumps({'error': str(e)})
self.wfile.write(error_response.encode('utf-8'))
return
# Serve structure analysis data
if parsed_path.path == '/structure_analysis_data.json':
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.send_header('Access-Control-Allow-Origin', '*')
self.end_headers()
try:
cache_file = 'structure_analysis_data.json'
if os.path.exists(cache_file):
with open(cache_file, 'r', encoding='utf-8') as f:
data = json.load(f)
self.wfile.write(json.dumps(data).encode('utf-8'))
else:
error_response = json.dumps({'error': 'Structure analysis data not found. Run precompute_structure.py first.'})
self.wfile.write(error_response.encode('utf-8'))
except Exception as e:
error_response = json.dumps({'error': str(e)})
self.wfile.write(error_response.encode('utf-8'))
return
# Serve depth analysis data
if parsed_path.path == '/depth_data':
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.send_header('Access-Control-Allow-Origin', '*')
self.end_headers()
try:
# Load pre-computed data if available
cache_file = 'depth_analysis_data.json'
if os.path.exists(cache_file):
with open(cache_file, 'r', encoding='utf-8') as f:
data = json.load(f)
self.wfile.write(json.dumps(data).encode('utf-8'))
return
# Fallback: Run analysis on the fly
import subprocess
script = '''
import json
import sys
def find_deepest_path(item):
path = []
designation = item.get('designations', [{}])[0].get('designation', 'Unnamed Element')
path.append({'type': 'Element', 'name': designation})
steward = item.get('stewardOrg', {}).get('name', 'Unknown Steward')
path.append({'type': 'Steward', 'name': steward})
if item.get('classification'):
for cls in item['classification']:
if cls.get('elements'):
for elem in cls['elements']:
if elem.get('name'):
path.append({'type': 'Classification', 'name': elem['name']})
if item.get('dataElementConcept') and item['dataElementConcept'].get('concepts'):
for concept in item['dataElementConcept']['concepts']:
path.append({'type': 'Concept', 'name': concept.get('name', 'Unknown')})
if item.get('property') and item['property'].get('concepts'):
for prop in item['property']['concepts']:
path.append({'type': 'Property', 'name': prop.get('name', 'Unknown')})
return len(path), path
with open('SearchExport.json', 'r', encoding='utf-8') as f:
data = json.load(f)
max_depth = 0
deepest_paths = []
depth_distribution = {}
all_paths = []
for i, item in enumerate(data):
depth, path = find_deepest_path(item)
designation = item.get('designations', [{}])[0].get('designation', 'Unnamed Element')
path_data = {
'index': i,
'depth': depth,
'designation': designation,
'path': path
}
all_paths.append(path_data)
if depth > max_depth:
max_depth = depth
deepest_paths = [path_data]
elif depth == max_depth:
deepest_paths.append(path_data)
depth_distribution[depth] = depth_distribution.get(depth, 0) + 1
# Sort all paths by depth descending, take top 50
all_paths.sort(key=lambda x: x['depth'], reverse=True)
top_paths = all_paths[:50]
# Calculate average depth
total_depth = sum(d * count for d, count in depth_distribution.items())
total_items = len(data)
avg_depth = total_depth / total_items if total_items > 0 else 0
result = {
'total_items': total_items,
'max_depth': max_depth,
'avg_depth': avg_depth,
'deepest_count': len(deepest_paths),
'depth_distribution': depth_distribution,
'deepest_paths': top_paths
}
print(json.dumps(result))
'''
result = subprocess.run(
[sys.executable, '-c', script],
cwd=os.path.dirname(os.path.abspath(__file__)),
capture_output=True,
text=True,
timeout=30
)
if result.returncode == 0:
self.wfile.write(result.stdout.encode('utf-8'))
else:
error_response = json.dumps({'error': result.stderr})
self.wfile.write(error_response.encode('utf-8'))
except Exception as e:
error_response = json.dumps({'error': str(e)})
self.wfile.write(error_response.encode('utf-8'))
return
# Serve HTML page
if parsed_path.path == '/' or parsed_path.path == '/index.html':
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
with open('index.html', 'r', encoding='utf-8') as f:
self.wfile.write(f.read().encode('utf-8'))
return
# Default: serve static files
return super().do_GET()
if __name__ == '__main__':
os.chdir(os.path.dirname(os.path.abspath(__file__)))
# Kill any existing processes on the port
print(f"Checking for processes on port {PORT}...")
kill_port(PORT)
# Wait a moment and check if port is actually available
max_retries = 5
for attempt in range(max_retries):
if is_port_available(PORT):
break
print(f"Port {PORT} still not available, waiting... (attempt {attempt + 1}/{max_retries})")
time.sleep(1)
if attempt < max_retries - 1:
kill_port(PORT)
# Allow address reuse to handle TIME_WAIT states
socketserver.TCPServer.allow_reuse_address = True
try:
with socketserver.TCPServer(("", PORT), MyHTTPRequestHandler) as httpd:
print("=" * 60)
print(f"Server is running!")
print(f"Open your browser and go to: http://localhost:{PORT}/")
print("=" * 60)
print("Press Ctrl+C to stop the server")
try:
httpd.serve_forever()
except KeyboardInterrupt:
print("\nServer stopped.")
except OSError as e:
if e.errno == 98: # Address already in use
print(f"\nError: Port {PORT} is still in use after {max_retries} attempts.")
print("This might be due to a TIME_WAIT state. Options:")
print(" 1. Wait 30-60 seconds and try again")
print(" 2. Manually check: lsof -i :9999")
print(" 3. Use a different port")
else:
print(f"\nError starting server: {e}")
sys.exit(1)