-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
doc/manual: improve remove_before_wrapper wrapper
- Loading branch information
Showing
1 changed file
with
18 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |