-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathextract-command.py
More file actions
executable file
·60 lines (52 loc) · 1.67 KB
/
extract-command.py
File metadata and controls
executable file
·60 lines (52 loc) · 1.67 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
#!/usr/bin/python3
import sys, os, shutil
import fcntl
import json
#Disclaimer: This isn't general purpose, and probably won't work for another project
#This specifically works with the behaviour of this project's Makefile
#Determine the project root, output file path, the compiler and source file
scriptPath = os.path.dirname(os.path.realpath(__file__))
outputPath = f"{sys.argv[1]}/compile_commands.json"
compiler = shutil.which(sys.argv[2])
inputFile = sys.argv[3]
#Determine the compiler output file and arguments used to generate it
compilerOutputFile = ""
args = [compiler]
for i in range(3, len(sys.argv)):
if sys.argv[i] == "-o":
compilerOutputFile = sys.argv[i + 1]
args.append(sys.argv[i])
outputObject = {
'directory': scriptPath,
'file': inputFile,
'output': compilerOutputFile,
'arguments': args
}
#Open the compile command file, create if missing and lock it
compileCommands = None
try:
compileCommands = open(outputPath, 'x+')
except FileExistsError:
compileCommands = open(outputPath, 'r+')
fcntl.flock(compileCommands, fcntl.LOCK_EX)
#Read the current contents as JSON
content = compileCommands.read()
compileCommands.seek(0)
contentObject = []
if content != "":
contentObject = json.loads(content)
#Update the loaded JSON with the new entry
written = False
for i in range(len(contentObject)):
if contentObject[i]["file"] == inputFile:
contentObject[i] = outputObject
written = True
break
if not written:
contentObject.append(outputObject)
#Save the new JSON and release the lock
compileCommands.truncate(0)
compileCommands.write(json.dumps(contentObject))
compileCommands.flush()
fcntl.flock(compileCommands, fcntl.LOCK_UN)
compileCommands.close()