-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
executable file
·146 lines (122 loc) · 5.02 KB
/
Copy pathserver.py
File metadata and controls
executable file
·146 lines (122 loc) · 5.02 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
#!/usr/bin/env python3
"""
Accessibility Inspector Server
Serves static files and handles refresh/scan commands
"""
import http.server
import socketserver
import subprocess
import json
import os
PORT = 8080
DIRECTORY = os.path.dirname(os.path.abspath(__file__))
class InspectorHandler(http.server.SimpleHTTPRequestHandler):
def __init__(self, *args, **kwargs):
super().__init__(*args, directory=DIRECTORY, **kwargs)
def end_headers(self):
# Disable caching for all files
self.send_header('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0')
self.send_header('Pragma', 'no-cache')
self.send_header('Expires', '0')
super().end_headers()
def do_POST(self):
if self.path == '/api/refresh':
self.handle_refresh()
elif self.path == '/api/scan':
self.handle_scan()
else:
self.send_error(404, 'Not Found')
def handle_refresh(self):
"""Run the full refresh: UI dump, screenshot, parse, and scan"""
try:
self.log_message("Running refresh...")
steps = []
# 1. Capture UI dump
result = subprocess.run(
['adb', 'shell', 'uiautomator', 'dump'],
capture_output=True, text=True, timeout=30
)
steps.append({'step': 'UI dump', 'success': result.returncode == 0})
# 2. Pull the dump file
result = subprocess.run(
['adb', 'pull', '/sdcard/window_dump.xml', os.path.join(DIRECTORY, 'window_dump.xml')],
capture_output=True, text=True, timeout=30
)
steps.append({'step': 'Pull XML', 'success': result.returncode == 0})
# 3. Take screenshot
with open(os.path.join(DIRECTORY, 'screen.png'), 'wb') as f:
result = subprocess.run(
['adb', 'exec-out', 'screencap', '-p'],
stdout=f, stderr=subprocess.PIPE, timeout=30
)
steps.append({'step': 'Screenshot', 'success': result.returncode == 0})
# 4. Parse UI data
result = subprocess.run(
['python3', os.path.join(DIRECTORY, 'parse_ui.py')],
capture_output=True, text=True, timeout=30
)
steps.append({'step': 'Parse UI', 'success': result.returncode == 0})
# 5. Run accessibility check
result = subprocess.run(
['python3', os.path.join(DIRECTORY, 'wcag_inspector.py')],
capture_output=True, text=True, timeout=60
)
steps.append({'step': 'A11y scan', 'success': result.returncode == 0})
success = all(s['success'] for s in steps)
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.send_header('Access-Control-Allow-Origin', '*')
self.end_headers()
self.wfile.write(json.dumps({
'success': success,
'steps': steps
}).encode())
except subprocess.TimeoutExpired:
self.send_error_json(500, 'Command timed out')
except Exception as e:
self.send_error_json(500, str(e))
def handle_scan(self):
"""Run only the accessibility scan"""
try:
result = subprocess.run(
['python3', os.path.join(DIRECTORY, 'wcag_inspector.py')],
capture_output=True, text=True, timeout=60
)
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.send_header('Access-Control-Allow-Origin', '*')
self.end_headers()
self.wfile.write(json.dumps({
'success': result.returncode == 0,
'output': result.stdout
}).encode())
except Exception as e:
self.send_error_json(500, str(e))
def send_error_json(self, code, message):
self.send_response(code)
self.send_header('Content-type', 'application/json')
self.send_header('Access-Control-Allow-Origin', '*')
self.end_headers()
self.wfile.write(json.dumps({
'success': False,
'error': message
}).encode())
def do_OPTIONS(self):
"""Handle CORS preflight"""
self.send_response(200)
self.send_header('Access-Control-Allow-Origin', '*')
self.send_header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')
self.send_header('Access-Control-Allow-Headers', 'Content-Type')
self.end_headers()
def main():
with socketserver.TCPServer(("", PORT), InspectorHandler) as httpd:
print(f"Accessibility Inspector Server")
print(f"Serving at http://localhost:{PORT}")
print(f"Inspector: http://localhost:{PORT}/ui_inspector_v2.html")
print(f"\nPress Ctrl+C to stop")
try:
httpd.serve_forever()
except KeyboardInterrupt:
print("\nServer stopped.")
if __name__ == '__main__':
main()