File: run-releasing-control-fd

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (49 lines) | stat: -rwxr-xr-x 1,288 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env python3

# Close the control channel and run a given command.
# Example usage:
#
#   > exec ./run-releasing-control-fd sleep 10
#
# The `exec` prefix is necessary to prevent the file descriptor being held
# by the outer shell while running the `run-relesing-control-fd`.


import subprocess
import sys
import os

def usage():
    print("Expected: [--require-control-fd] <command> [args...]")
    sys.exit(1)

def main(argv):
    require_control_fd = False

    # Parse command line options
    if len(argv) <= 1:
        usage()
    elif argv[1] == "--require-control-fd":
        if len(argv) == 2:
            usage()
        argv=argv[1:]
        require_control_fd = True
    elif argv[1][0:1] == "-":
        usage()

    exec_args = argv[1:]

    # Close the control descriptor, if given.
    control_fd = os.environ.get("LLBUILD_CONTROL_FD")
    if control_fd is None and require_control_fd:
        print("%s: missing LLBUILD_CONTROL_FD" % exec_args[0], file=sys.stderr)
        sys.exit(1)
    elif control_fd is not None:
        del os.environ["LLBUILD_CONTROL_FD"]
        os.close(int(control_fd))

    use_shell = len(exec_args) == 1 and ' ' in exec_args[0]
    subprocess.check_call(exec_args, shell=use_shell)

if __name__ == '__main__':
    main(sys.argv)