|
13 | 13 | # `src/.gitignore` so that the symlinks are not accidentally committed |
14 | 14 | # by mistake. |
15 | 15 |
|
16 | | -import os |
17 | 16 | from pathlib import Path |
18 | 17 | import subprocess |
19 | 18 |
|
20 | | -# Path to the source directory |
21 | | -SRC_DIR = Path("src") |
22 | 19 |
|
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") |
30 | 23 |
|
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') |
36 | 48 |
|
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") |
43 | 51 |
|
44 | 52 | 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 |
46 | 55 |
|
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 |
50 | 58 |
|
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) |
53 | 68 |
|
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 | | - ) |
59 | 69 |
|
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