-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathattachandrun
executable file
·88 lines (73 loc) · 2.49 KB
/
attachandrun
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/python
from __future__ import print_function
import errno
import os
import subprocess
import sys
progname = 'attachandrun'
def extract(args):
if len(args) < 1:
raise Exception('Missing arguments')
return (args[0], args[1:])
def usage():
print('Usage: %s [--check|-c] locker program program_name [args...]' % (progname,), file=sys.stderr)
sys.exit(1)
def invoke(args):
try:
process = subprocess.Popen(args,
stdout=subprocess.PIPE, universal_newlines=True)
result, _ = process.communicate()
if process.returncode != 0:
raise Exception("Non-zero return code from '%s'" % (args[0],))
return result.strip()
except subprocess.CalledProcessError as e:
print("Unable to invoke '%s'." % (args[0],), file=sys.stderr)
sys.exit(1)
def attach(locker):
try:
return invoke(['attach', '-p', locker])
except Exception as e:
raise Exception("Could not find locker '%s'" % (locker,))
def athdir(path):
try:
return invoke(['athdir', '-t', 'bin', '-p', path])
except Exception as e:
raise Exception("Could not find binary directory in '%s'" % (path,))
def execute(path, args):
try:
os.execv(path, args)
except OSError as e:
return e.errno
def main():
# First, figure out our name, in the strange case it isn't
# 'attachandrun'. After we process each argument, consume it.
global progname
potential, argv = extract(sys.argv)
progname = os.path.basename(potential)
if len(argv) < 1:
raise Exception('Missing arguments')
check = False
if argv[0] == '--check' or argv[0] == '-c':
check = True
_, argv = extract(argv)
locker, argv = extract(argv)
program, argv = extract(argv)
# Use attach and athdir to figure out where the program we want to
# execute lives
locker_path = attach(locker)
program_location = os.path.join(athdir(locker_path), program)
# If we're in check (aka dry-run), see if we can execute this
# program
if check:
if os.access(program_location, os.X_OK):
sys.exit(0)
sys.exit(1)
# Actually execute the program,
error = execute(program_location, argv)
print("%s: %s: %s" % (progname, program_location, os.strerror(error)), file=sys.stderr)
if __name__ == '__main__':
try:
main()
except Exception as e:
print("%s: %s" % (progname, e), file=sys.stderr)
usage()