Skip to content

Commit b6cb4be

Browse files
committed
link-headers: use pathlib consistenly and fix type errors
1 parent f51d3c8 commit b6cb4be

File tree

1 file changed

+44
-38
lines changed

1 file changed

+44
-38
lines changed

maintainers/link-headers

+44-38
Original file line numberDiff line numberDiff line change
@@ -13,53 +13,59 @@
1313
# `src/.gitignore` so that the symlinks are not accidentally committed
1414
# by mistake.
1515

16-
import os
1716
from pathlib import Path
1817
import subprocess
1918

20-
# Path to the source directory
21-
SRC_DIR = Path("src")
2219

23-
# Get header files from git
24-
result = subprocess.run(
25-
["git", "-C", str(SRC_DIR), "ls-files", "*/include/nix/**.hh"],
26-
text=True, capture_output=True, check=True
27-
)
28-
header_files = result.stdout.strip().split("\n")
29-
header_files.sort()
20+
def main() -> None:
21+
# Path to the source directory
22+
SRC_DIR = Path("src")
3023

31-
# Build header pairs
32-
header_pairs = []
33-
for file in header_files:
34-
project, header = file.split("/include/nix/", 1)
35-
header_pairs.append((Path(project), Path(header)))
24+
# Get header files from git
25+
result = subprocess.run(
26+
["git", "-C", str(SRC_DIR), "ls-files", "*/include/nix/**.hh"],
27+
text=True,
28+
capture_output=True,
29+
check=True,
30+
)
31+
header_files = result.stdout.strip().split("\n")
32+
header_files.sort()
33+
34+
# Build header pairs
35+
header_pairs = []
36+
for file_str in header_files:
37+
project_str, header_str = file_str.split("/include/nix/", 1)
38+
header_pairs.append((Path(project_str), Path(header_str)))
39+
40+
# Generate .gitignore file
41+
gitignore_path = SRC_DIR / ".gitignore"
42+
with gitignore_path.open("w") as gitignore:
43+
gitignore.write("# DO NOT EDIT! Autogenerated\n")
44+
gitignore.write(
45+
"# Symlinks for headers to be next to sources for development\n"
46+
)
47+
gitignore.write('# Run "maintainers/link-headers" to regenerate\n\n')
3648

37-
# Generate .gitignore file
38-
gitignore_path = SRC_DIR / ".gitignore"
39-
with open(gitignore_path, "w") as gitignore:
40-
gitignore.write("# DO NOT EDIT! Autogenerated\n")
41-
gitignore.write("# Symlinks for headers to be next to sources for development\n")
42-
gitignore.write('# Run "maintainers/link-headers" to regenerate\n\n')
49+
for project, header in header_pairs:
50+
gitignore.write(f"/{project / header}\n")
4351

4452
for project, header in header_pairs:
45-
gitignore.write(f"/{project / header}\n")
53+
# Reconstruct the full path (relative to SRC_DIR) to the header file.
54+
file = project / "include" / "nix" / header
4655

47-
for project, header in header_pairs:
48-
# Reconstruct the full path (relative to SRC_DIR) to the header file.
49-
file = project / "include" / "nix" / header
56+
# The symlink should be created at "project/header", i.e. next to the project's sources.
57+
link = project / header
5058

51-
# The symlink should be created at "project/header", i.e. next to the project's sources.
52-
link = project / header
59+
# Compute a relative path from the symlink's parent directory to the actual header file.
60+
relative_source = (SRC_DIR / file).relative_to(SRC_DIR / link.parent)
61+
62+
# Create the symbolic link.
63+
full_link_path = SRC_DIR / link
64+
full_link_path.parent.mkdir(parents=True, exist_ok=True)
65+
if full_link_path.is_symlink():
66+
full_link_path.unlink()
67+
full_link_path.symlink_to(relative_source)
5368

54-
# Compute a relative path from the symlink's parent directory to the actual header file.
55-
relative_source = os.path.relpath(
56-
SRC_DIR / file,
57-
SRC_DIR / link.parent
58-
)
5969

60-
# Create the symbolic link.
61-
full_link_path = SRC_DIR / link
62-
os.makedirs(full_link_path.parent, exist_ok=True)
63-
if os.path.islink(full_link_path):
64-
os.remove(full_link_path)
65-
os.symlink(relative_source, full_link_path)
70+
if __name__ == "__main__":
71+
main()

0 commit comments

Comments
 (0)