Skip to content

Commit 85c63fd

Browse files
committed
[#133]: Hotfix patch_compile_commands.py
1 parent 0870a08 commit 85c63fd

File tree

1 file changed

+12
-28
lines changed

1 file changed

+12
-28
lines changed

src/patch_compile_commands.py

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,39 @@
11
#!/usr/bin/env python3
2-
from __future__ import annotations
3-
42
import json
53
import os
64
import sys
7-
from typing import Any, Dict, List, Sequence
85

6+
from typing import Any, Dict, List, Sequence
97

108
NEW_PREFIX = "/github/workspace"
119

1210

1311
def _collect_paths(entries: Sequence[Dict[str, Any]]) -> List[str]:
14-
"""Return every absolute file/dir path found in *entries*."""
15-
raw_paths = (
16-
value
17-
for entry in entries
18-
for value in (entry.get("directory"), entry.get("file"))
19-
if isinstance(value, str) # guard against None / non-string
20-
)
21-
return [os.path.realpath(path) for path in raw_paths]
12+
return [
13+
os.path.realpath(v)
14+
for e in entries
15+
for v in (e.get("directory"), e.get("file"))
16+
if isinstance(v, str)
17+
]
2218

2319

24-
def patch_compile_commands(path: str) -> None: # noqa: D401
20+
def patch_compile_commands(path: str) -> None:
2521
with open(path, "r", encoding="utf-8") as f:
2622
data: List[Dict[str, Any]] = json.load(f)
2723

28-
paths: List[str] = _collect_paths(data)
29-
if not paths: # nothing to patch
30-
print("[WARN] compile_commands.json contained no absolute paths")
31-
return
32-
33-
old_prefix = os.path.commonpath(paths)
24+
old_prefix = os.path.commonpath(_collect_paths(data))
3425
print(f"[INFO] Patching compile_commands.json: '{old_prefix}' → '{NEW_PREFIX}'")
35-
3626
for entry in data:
3727
for key in ("file", "directory", "command"):
3828
val = entry.get(key)
39-
if (
40-
isinstance(val, str)
41-
and os.path.isabs(val)
42-
and val.startswith(old_prefix)
43-
):
44-
entry[key] = val.replace(old_prefix, NEW_PREFIX, 1)
29+
if isinstance(val, str) and old_prefix in val:
30+
entry[key] = val.replace(old_prefix, NEW_PREFIX)
4531

4632
with open(path, "w", encoding="utf-8") as f:
4733
json.dump(data, f, indent=2)
4834

4935

5036
if __name__ == "__main__":
5137
if len(sys.argv) != 2:
52-
print("Usage: patch_compile_commands.py <path-to-compile_commands.json>")
53-
sys.exit(1)
54-
38+
sys.exit("usage: patch_compile_commands.py <compile_commands.json>")
5539
patch_compile_commands(sys.argv[1])

0 commit comments

Comments
 (0)