Mercurial > treepkg > treepkg
annotate treepkg/run.py @ 561:1f7746e2288e
Fix typo
author | Bjoern Ricks <bricks@intevation.de> |
---|---|
date | Fri, 02 Sep 2011 09:43:21 +0000 |
parents | eacfd3744d16 |
children |
rev | line source |
---|---|
124
e08b4b09d35f
New parameter treepkg.run.call: extra_env for additional environment
Bernhard Herzog <bh@intevation.de>
parents:
4
diff
changeset
|
1 # Copyright (C) 2007, 2008 by Intevation GmbH |
0 | 2 # Authors: |
3 # Bernhard Herzog <bh@intevation.de> | |
4 # | |
5 # This program is free software under the GPL (>=v2) | |
6 # Read the file COPYING coming with the software for details. | |
7 | |
8 """Helper functions to run subprocesses in various ways""" | |
9 | |
10 import os | |
11 import subprocess | |
12 | |
13 | |
14 class SubprocessError(EnvironmentError): | |
15 | |
165
c7ac67366492
Make treepkg.run.capture_output include the output in the
Bernhard Herzog <bh@intevation.de>
parents:
164
diff
changeset
|
16 def __init__(self, command, returncode, output=None): |
0 | 17 EnvironmentError.__init__(self, |
18 "Command %r finished with return code %d" | |
19 % (command, returncode)) | |
20 self.returncode = returncode | |
165
c7ac67366492
Make treepkg.run.capture_output include the output in the
Bernhard Herzog <bh@intevation.de>
parents:
164
diff
changeset
|
21 self.output = output |
0 | 22 |
23 | |
193
98de92b816d4
Add logfile argument to treepkg.run.call to log the output of the
Bernhard Herzog <bh@intevation.de>
parents:
165
diff
changeset
|
24 def call(command, suppress_output=False, extra_env=None, inputdata=None, |
98de92b816d4
Add logfile argument to treepkg.run.call to log the output of the
Bernhard Herzog <bh@intevation.de>
parents:
165
diff
changeset
|
25 logfile=None, **kw): |
0 | 26 """Run command as a subprocess and wait until it is finished. |
27 | |
28 The command should be given as a list of strings to avoid problems | |
29 with shell quoting. If the command exits with a return code other | |
30 than 0, a SubprocessError is raised. | |
31 """ | |
164
a68a4e22549c
Extend treepkg.run.call so that data can be fed to stdin of the subprocess.
Bernhard Herzog <bh@intevation.de>
parents:
124
diff
changeset
|
32 if inputdata is not None: |
a68a4e22549c
Extend treepkg.run.call so that data can be fed to stdin of the subprocess.
Bernhard Herzog <bh@intevation.de>
parents:
124
diff
changeset
|
33 kw["stdin"] = subprocess.PIPE |
193
98de92b816d4
Add logfile argument to treepkg.run.call to log the output of the
Bernhard Herzog <bh@intevation.de>
parents:
165
diff
changeset
|
34 if logfile: |
98de92b816d4
Add logfile argument to treepkg.run.call to log the output of the
Bernhard Herzog <bh@intevation.de>
parents:
165
diff
changeset
|
35 kw["stdout"] = kw["stderr"] = open(logfile, "w") |
98de92b816d4
Add logfile argument to treepkg.run.call to log the output of the
Bernhard Herzog <bh@intevation.de>
parents:
165
diff
changeset
|
36 elif suppress_output: |
0 | 37 kw["stdout"] = open(os.devnull, "w") |
4
fee641fec94e
Separate the kolab specific parts.
Bernhard Herzog <bh@intevation.de>
parents:
0
diff
changeset
|
38 kw["stderr"] = open(os.devnull, "w") |
124
e08b4b09d35f
New parameter treepkg.run.call: extra_env for additional environment
Bernhard Herzog <bh@intevation.de>
parents:
4
diff
changeset
|
39 env = kw.pop("env", None) |
e08b4b09d35f
New parameter treepkg.run.call: extra_env for additional environment
Bernhard Herzog <bh@intevation.de>
parents:
4
diff
changeset
|
40 if extra_env: |
e08b4b09d35f
New parameter treepkg.run.call: extra_env for additional environment
Bernhard Herzog <bh@intevation.de>
parents:
4
diff
changeset
|
41 if env is None: |
e08b4b09d35f
New parameter treepkg.run.call: extra_env for additional environment
Bernhard Herzog <bh@intevation.de>
parents:
4
diff
changeset
|
42 env = os.environ.copy() |
e08b4b09d35f
New parameter treepkg.run.call: extra_env for additional environment
Bernhard Herzog <bh@intevation.de>
parents:
4
diff
changeset
|
43 env.update(extra_env) |
347 | 44 try: |
45 process = subprocess.Popen(command, env=env, **kw) | |
46 except OSError,e: | |
47 raise SubprocessError(command, e.errno, e.strerror) | |
164
a68a4e22549c
Extend treepkg.run.call so that data can be fed to stdin of the subprocess.
Bernhard Herzog <bh@intevation.de>
parents:
124
diff
changeset
|
48 |
a68a4e22549c
Extend treepkg.run.call so that data can be fed to stdin of the subprocess.
Bernhard Herzog <bh@intevation.de>
parents:
124
diff
changeset
|
49 if inputdata is not None: |
a68a4e22549c
Extend treepkg.run.call so that data can be fed to stdin of the subprocess.
Bernhard Herzog <bh@intevation.de>
parents:
124
diff
changeset
|
50 process.stdin.write(inputdata) |
a68a4e22549c
Extend treepkg.run.call so that data can be fed to stdin of the subprocess.
Bernhard Herzog <bh@intevation.de>
parents:
124
diff
changeset
|
51 process.stdin.close() |
347 | 52 ret = process.wait() |
0 | 53 if ret != 0: |
54 raise SubprocessError(command, ret) | |
55 | |
56 | |
57 def capture_output(command, **kw): | |
58 """Return the stdout and stderr of the command as a string | |
59 | |
60 The command should be given as a list of strings to avoid problems | |
61 with shell quoting. If the command exits with a return code other | |
62 than 0, a SubprocessError is raised. | |
63 """ | |
64 proc = subprocess.Popen(command, stdout=subprocess.PIPE, | |
65 stderr=subprocess.STDOUT, **kw) | |
66 output = proc.communicate()[0] | |
67 if proc.returncode != 0: | |
165
c7ac67366492
Make treepkg.run.capture_output include the output in the
Bernhard Herzog <bh@intevation.de>
parents:
164
diff
changeset
|
68 raise SubprocessError(command, proc.returncode, output) |
0 | 69 return output |
451
eacfd3744d16
capture only stdout for treepkginfo
Bjoern Ricks <bricks@intevation.de>
parents:
347
diff
changeset
|
70 |
eacfd3744d16
capture only stdout for treepkginfo
Bjoern Ricks <bricks@intevation.de>
parents:
347
diff
changeset
|
71 def capture_stdout(command, **kw): |
eacfd3744d16
capture only stdout for treepkginfo
Bjoern Ricks <bricks@intevation.de>
parents:
347
diff
changeset
|
72 """Return the stdout and stderr of the command as a string |
eacfd3744d16
capture only stdout for treepkginfo
Bjoern Ricks <bricks@intevation.de>
parents:
347
diff
changeset
|
73 |
eacfd3744d16
capture only stdout for treepkginfo
Bjoern Ricks <bricks@intevation.de>
parents:
347
diff
changeset
|
74 The command should be given as a list of strings to avoid problems |
eacfd3744d16
capture only stdout for treepkginfo
Bjoern Ricks <bricks@intevation.de>
parents:
347
diff
changeset
|
75 with shell quoting. If the command exits with a return code other |
eacfd3744d16
capture only stdout for treepkginfo
Bjoern Ricks <bricks@intevation.de>
parents:
347
diff
changeset
|
76 than 0, a SubprocessError is raised. |
eacfd3744d16
capture only stdout for treepkginfo
Bjoern Ricks <bricks@intevation.de>
parents:
347
diff
changeset
|
77 """ |
eacfd3744d16
capture only stdout for treepkginfo
Bjoern Ricks <bricks@intevation.de>
parents:
347
diff
changeset
|
78 proc = subprocess.Popen(command, stdout=subprocess.PIPE, |
eacfd3744d16
capture only stdout for treepkginfo
Bjoern Ricks <bricks@intevation.de>
parents:
347
diff
changeset
|
79 stderr=subprocess.PIPE, **kw) |
eacfd3744d16
capture only stdout for treepkginfo
Bjoern Ricks <bricks@intevation.de>
parents:
347
diff
changeset
|
80 output, error = proc.communicate() |
eacfd3744d16
capture only stdout for treepkginfo
Bjoern Ricks <bricks@intevation.de>
parents:
347
diff
changeset
|
81 if proc.returncode != 0: |
eacfd3744d16
capture only stdout for treepkginfo
Bjoern Ricks <bricks@intevation.de>
parents:
347
diff
changeset
|
82 raise SubprocessError(command, proc.returncode, error) |
eacfd3744d16
capture only stdout for treepkginfo
Bjoern Ricks <bricks@intevation.de>
parents:
347
diff
changeset
|
83 return output |