forked from gratipay/gratipay.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
swaddle
executable file
·42 lines (37 loc) · 1.11 KB
/
swaddle
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
#!/usr/bin/env python
"""Run a command with an environment specified in a file.
"""
import os, sys
if len(sys.argv) < 1:
m ="[SWADDLE] Usage: %s {definition.env} {command} {args}" % sys.argv[0]
raise SystemExit(m)
envdef = sys.argv[1]
if envdef == '-':
m = "[SWADDLE] reading environment from stdin."
print >> sys.stderr, m
fp = sys.stdin
splitter = ": "
elif not os.path.isfile(envdef):
m = "[SWADDLE] %s is not a file; environment unchanged." % envdef
print >> sys.stderr, m
envdef = ""
else:
fp = open(envdef)
splitter = "="
args = sys.argv[2:]
if not args:
m ="[SWADDLE] No command specified; exiting."
raise SystemExit(m)
if not os.path.isfile(args[0]):
m ="[SWADDLE] Command %s does not exist; exiting." % args[0]
raise SystemExit(m)
if envdef:
for line in fp:
line = line.split('#')[0].strip()
if splitter not in line:
m = "[SWADDLE] Skipping line: %s." % line
print >> sys.stderr, m
continue
key, val = line.split(splitter, 1)
os.environ[key.strip()] = val.strip()
os.execv(args[0], args)