-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconvert.py
68 lines (51 loc) · 2.19 KB
/
convert.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
import subprocess
import re
import json
import argparse
"""
Script to convert already existing compile_commands.json compile database with
Windows paths to use WSL compatible paths instead. This allows Vim usage on WSL
side but project code to be compiled on Windows side with Windows tools.
Usage:
python convert.py win_compile_commands.json
This will convert paths in the given file and write output to
compile_commands.json file.
"""
path_cache = {}
def convert_paths(string):
string = convert_slashes(string)
return convert_drive_paths(string)
def convert_drive_paths(path):
drive_path = r'(?:(?<=[I| |"])|^)(\w+:[\\\/]+)'
return re.sub(drive_path, replace_path, path)
def convert_slashes(path):
slash_pattern = r'(?<=\w)(\\+)(?=\w)'
return re.sub(slash_pattern, '/', path)
def replace_path(path):
return wsl_path(path.group(1))
def wsl_path(windows_path):
wsl_path = path_cache.get(windows_path)
if not wsl_path:
completed_process = subprocess.run(['wslpath', '-a', windows_path], check=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
wsl_path = completed_process.stdout.decode('ascii').strip()
path_cache[windows_path] = wsl_path
return wsl_path
def main():
parser = argparse.ArgumentParser(description='Convert given compile_commands.json with Windows paths to Windows Subsystem for Linux (WSL) compatible paths')
parser.add_argument('compile_commands', help='compile_commands.json file with Windows paths')
args = parser.parse_args()
database = None
with open(args.compile_commands, 'r') as input_file:
database = json.load(input_file)
length = len(database)
for index, command in enumerate(database):
command['command'] = convert_paths(command['command'])
command['directory'] = convert_paths(command['directory'])
command['file'] = convert_paths(command['file'])
if 'output' in command:
command['output'] = convert_paths(command['output'])
print('{0}/{1}: {2}'.format(index + 1, length, command['file']))
with open('compile_commands.json', 'w') as output_file:
json.dump(database, output_file, indent=2)
if __name__ == '__main__':
main()