File: remove_before_wrapper.py

package info (click to toggle)
nix 2.26.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 12,524 kB
  • sloc: cpp: 87,540; sh: 8,864; perl: 649; yacc: 466; xml: 410; javascript: 378; lex: 329; ansic: 215; python: 128; sql: 56; makefile: 33; exp: 5; ruby: 1
file content (33 lines) | stat: -rw-r--r-- 930 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/env python3

import os
import subprocess
import sys
import shutil
import typing as t

def main():
    if len(sys.argv) < 4 or '--' not in sys.argv:
        print("Usage: remove-before-wrapper <output> -- <nix command...>")
        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_temp: str = output + '.tmp'

    # 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
    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
    os.rename(output_temp, output)

if __name__ == "__main__":
    main()