Skip to content

Commit

Permalink
doc/manual: improve remove_before_wrapper wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
Mic92 committed Dec 3, 2024
1 parent 08e0982 commit a61b8f7
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions doc/manual/remove_before_wrapper.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
#!/usr/bin/env python3

import os
import subprocess
import sys
import shutil
import typing as t
from pathlib import Path
from tempfile import TemporaryDirectory

def main():
if len(sys.argv) < 4 or '--' not in sys.argv:
print("Usage: remove-before-wrapper <output> -- <nix command...>")

def main() -> None:
if len(sys.argv) < 4 or "--" not in sys.argv:
print("Usage: remove-before-wrapper <output> -- <nix command...>", file=sys.stderr)
sys.exit(1)

# Extract the parts
output: str = sys.argv[1]
nix_command_idx: int = sys.argv.index('--') + 1
nix_command: t.List[str] = sys.argv[nix_command_idx:]
output = Path(sys.argv[1])
nix_command_idx = sys.argv.index("--") + 1
nix_command = sys.argv[nix_command_idx:]

with TemporaryDirectory(prefix=str(output.parent.resolve() / "tmp")) as temp:
output_temp = Path(temp) / "output"

output_temp: str = output + '.tmp'
# Remove the output and temp output in case they exist
shutil.rmtree(output, ignore_errors=True)

# Remove the output and temp output in case they exist
shutil.rmtree(output, ignore_errors=True)
shutil.rmtree(output_temp, ignore_errors=True)
# Execute nix command with `--write-to` tempary output
subprocess.run([*nix_command, "--write-to", str(output_temp)], check=True)

# Execute nix command with `--write-to` tempary output
nix_command_write_to = nix_command + ['--write-to', output_temp]
subprocess.run(nix_command_write_to, check=True)
# Move the temporary output to the intended location
Path(output_temp).rename(output)

# Move the temporary output to the intended location
os.rename(output_temp, output)

if __name__ == "__main__":
main()

0 comments on commit a61b8f7

Please sign in to comment.