annotate treepkg/run.py @ 0:f78a02e79c84

initial checkin
author Bernhard Herzog <bh@intevation.de>
date Tue, 06 Mar 2007 17:37:32 +0100
parents
children fee641fec94e
rev   line source
0
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
1 # Copyright (C) 2007 by Intevation GmbH
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
2 # Authors:
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
3 # Bernhard Herzog <bh@intevation.de>
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
4 #
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
5 # This program is free software under the GPL (>=v2)
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
6 # Read the file COPYING coming with the software for details.
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
7
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
8 """Helper functions to run subprocesses in various ways"""
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
9
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
10 import os
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
11 import subprocess
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
12
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
13
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
14 class SubprocessError(EnvironmentError):
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
15
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
16 def __init__(self, command, returncode):
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
17 EnvironmentError.__init__(self,
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
18 "Command %r finished with return code %d"
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
19 % (command, returncode))
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
20 self.returncode = returncode
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
21
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
22
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
23 def call(command, suppress_output=False, **kw):
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
24 """Run command as a subprocess and wait until it is finished.
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
25
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
26 The command should be given as a list of strings to avoid problems
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
27 with shell quoting. If the command exits with a return code other
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
28 than 0, a SubprocessError is raised.
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
29 """
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
30 if suppress_output:
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
31 kw["stdout"] = open(os.devnull, "w")
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
32 ret = subprocess.call(command, **kw)
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
33 if ret != 0:
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
34 raise SubprocessError(command, ret)
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
35
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
36
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
37 def capture_output(command, **kw):
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
38 """Return the stdout and stderr of the command as a string
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
39
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
40 The command should be given as a list of strings to avoid problems
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
41 with shell quoting. If the command exits with a return code other
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
42 than 0, a SubprocessError is raised.
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
43 """
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
44 proc = subprocess.Popen(command, stdout=subprocess.PIPE,
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
45 stderr=subprocess.STDOUT, **kw)
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
46 output = proc.communicate()[0]
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
47 if proc.returncode != 0:
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
48 raise SubprocessError(command, proc.returncode)
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
49 return output
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)