equal
deleted
inserted
replaced
1 """ |
1 """ |
2 Invoke external commands, with python kwargs -> options mangling. |
2 Invoke external commands, with python kwargs -> options mangling. |
3 """ |
3 """ |
4 |
4 |
|
5 import logging |
|
6 import os |
5 import subprocess |
7 import subprocess |
6 import logging |
|
7 |
8 |
8 log = logging.getLogger('pvl.invoke') |
9 log = logging.getLogger('pvl.invoke') |
9 |
10 |
10 class InvokeError (Exception) : |
11 class InvokeError (Exception) : |
11 def __init__ (self, cmd, exit, error) : |
12 def __init__ (self, cmd, exit, error) : |
14 self.error = error |
15 self.error = error |
15 |
16 |
16 def __str__ (self) : |
17 def __str__ (self) : |
17 return "{self.cmd} failed ({self.exit}): {self.error}".format(self=self) |
18 return "{self.cmd} failed ({self.exit}): {self.error}".format(self=self) |
18 |
19 |
19 def invoke (cmd, args, stdin=None) : |
20 def invoke (cmd, args, stdin=None, setenv=None) : |
20 """ |
21 """ |
21 Invoke a command directly. |
22 Invoke a command directly. |
22 |
23 |
23 stdin: |
24 stdin: |
24 False -> passthrough stdin/stdout |
25 False -> passthrough stdin/stdout |
43 else : |
44 else : |
44 # return stdout |
45 # return stdout |
45 io = subprocess.PIPE |
46 io = subprocess.PIPE |
46 input = None |
47 input = None |
47 |
48 |
48 p = subprocess.Popen([cmd] + args, stdin=io, stdout=io, stderr=io) |
49 if setenv : |
|
50 env = dict(os.environ) |
|
51 env.update(setenv) |
|
52 |
|
53 else : |
|
54 env = None |
|
55 |
|
56 p = subprocess.Popen([cmd] + list(args), stdin=io, stdout=io, stderr=io, env=env) |
49 |
57 |
50 # get output |
58 # get output |
51 # returns None if not io |
59 # returns None if not io |
52 stdout, stderr = p.communicate(input=input) |
60 stdout, stderr = p.communicate(input=input) |
53 |
61 |