-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmini_compile_commands_server.py
executable file
·70 lines (55 loc) · 2.07 KB
/
mini_compile_commands_server.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#! @python@/bin/python
from socketserver import UnixStreamServer, StreamRequestHandler
from time import localtime, strftime
import signal
import os
import sys
import pickle
import json
if len(sys.argv) == 1:
compile_commands_output = "compile_commands.json"
elif len(sys.argv) == 2:
compile_commands_output = sys.argv[1]
elif len(sys.argv) > 2:
print("usage: mini_compile_commands_client.py compile_commands.json")
sys.exit(1)
unix_socket_file = "/tmp/mini_compile_commands_unix_socket"
def log_message(*args, **kwargs):
print(
'[' + strftime('%H:%M:%S', localtime()) + ']',
*args, **kwargs)
class Handler(StreamRequestHandler):
def handle(self):
compile_commands_binary = self.rfile.read()
compile_commands = pickle.loads(compile_commands_binary)
log_message("recived {} compile commands".format(len(compile_commands)))
self.server.compile_commands.extend(compile_commands)
class Server(UnixStreamServer):
def __init__(self, server_address, RequestHandlerClass):
try:
super().__init__(server_address, RequestHandlerClass)
except:
raise Exception(
f'''
Failed to initialize the server. This is most likely happening
because the unix socket file already exists. Either
1. mini_compile_commands_server.py shut down abnormally at some point
2. there is already a mini_compile_commands_server.py instance running
Remove {unix_socket_file} and try again
'''
)
self.compile_commands = []
def server_close(self):
with open(compile_commands_output, 'w') as f:
f.write(json.dumps(self.compile_commands, indent=4))
f.write('\n')
super().server_close()
server = Server(unix_socket_file, Handler)
print("awaiting compile commands...")
def signal_handler(sig, frame):
print('')
server.server_close()
os.remove(unix_socket_file)
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
server.serve_forever()