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
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
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
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
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
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
22
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
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
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
26 """Run command as a subprocess and wait until it is finished.
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
27
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
28 The command should be given as a list of strings to avoid problems
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
29 with shell quoting. If the command exits with a return code other
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
30 than 0, a SubprocessError is raised.
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
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
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
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
2910051b91f7 fixed last commit
Bjoern Ricks <bricks@intevation.de>
parents: 346
diff changeset
44 try:
2910051b91f7 fixed last commit
Bjoern Ricks <bricks@intevation.de>
parents: 346
diff changeset
45 process = subprocess.Popen(command, env=env, **kw)
2910051b91f7 fixed last commit
Bjoern Ricks <bricks@intevation.de>
parents: 346
diff changeset
46 except OSError,e:
2910051b91f7 fixed last commit
Bjoern Ricks <bricks@intevation.de>
parents: 346
diff changeset
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
2910051b91f7 fixed last commit
Bjoern Ricks <bricks@intevation.de>
parents: 346
diff changeset
52 ret = process.wait()
0
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
53 if ret != 0:
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
54 raise SubprocessError(command, ret)
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
55
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
56
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
57 def capture_output(command, **kw):
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
58 """Return the stdout and stderr of the command as a string
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
59
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
60 The command should be given as a list of strings to avoid problems
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
61 with shell quoting. If the command exits with a return code other
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
62 than 0, a SubprocessError is raised.
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
63 """
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
64 proc = subprocess.Popen(command, stdout=subprocess.PIPE,
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
65 stderr=subprocess.STDOUT, **kw)
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
66 output = proc.communicate()[0]
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
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
f78a02e79c84 initial checkin
Bernhard Herzog <bh@intevation.de>
parents:
diff changeset
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
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)