10850.1.1
by Bjorn Tillenius
Pass -S to shhh.py's python, to get rid of "'import site' failed" errors when running make. |
1 |
#! /usr/bin/python -S
|
8687.15.4
by Karl Fogel
Add the copyright header block to more files; tweak format in a few files. |
2 |
#
|
3 |
# Copyright 2009 Canonical Ltd. This software is licensed under the
|
|
4 |
# GNU Affero General Public License version 3 (see the file LICENSE).
|
|
5 |
||
1831
by Canonical.com Patch Queue Manager
New config machinery, database helpers and oddsnsods required for staging |
6 |
"""
|
7 |
Run a command and suppress output unless it returns a non-zero exit status
|
|
8 |
"""
|
|
9 |
||
10 |
__metaclass__ = type |
|
11 |
||
14612.2.6
by William Grant
utilities |
12 |
from subprocess import ( |
13 |
PIPE, |
|
14 |
Popen, |
|
15 |
)
|
|
1831
by Canonical.com Patch Queue Manager
New config machinery, database helpers and oddsnsods required for staging |
16 |
import sys |
14612.2.6
by William Grant
utilities |
17 |
|
1831
by Canonical.com Patch Queue Manager
New config machinery, database helpers and oddsnsods required for staging |
18 |
|
19 |
def shhh(cmd): |
|
20 |
r"""Run a command and suppress output unless it returns a non-zero exitcode |
|
21 |
||
22 |
If output is generated, stderr will be output before stdout, so output
|
|
23 |
order may be messed up if the command attempts to control order by
|
|
24 |
flushing stdout at points or setting it to unbuffered.
|
|
25 |
||
26 |
||
27 |
To test, we invoke both this method and this script with some commands
|
|
28 |
and examine the output and exitvalue
|
|
29 |
||
30 |
>>> python = sys.executable
|
|
31 |
||
32 |
>>> def shhh_script(cmd):
|
|
33 |
... from subprocess import Popen, PIPE
|
|
34 |
... script = '%s %s' % (python, __file__)
|
|
35 |
... cmd = "%s '%s'" % (script, cmd)
|
|
36 |
... p = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE)
|
|
37 |
... (out, err) = p.communicate()
|
|
38 |
... return (out, err, p.returncode)
|
|
39 |
||
40 |
>>> cmd = '''%s -c "import sys; sys.exit(%d)"''' % (python, 0)
|
|
41 |
>>> shhh(cmd)
|
|
42 |
0
|
|
43 |
>>> shhh_script(cmd)
|
|
44 |
('', '', 0)
|
|
45 |
||
46 |
>>> cmd = '''%s -c "import sys; sys.exit(%d)"''' % (python, 1)
|
|
47 |
>>> shhh(cmd)
|
|
48 |
1
|
|
49 |
>>> shhh_script(cmd)
|
|
50 |
('', '', 1)
|
|
51 |
||
52 |
>>> cmd = '''%s -c "import sys; print 666; sys.exit(%d)"''' % (
|
|
53 |
... python, 42)
|
|
54 |
>>> shhh(cmd)
|
|
55 |
666
|
|
56 |
42
|
|
57 |
>>> shhh_script(cmd)
|
|
58 |
('666\n', '', 42)
|
|
59 |
||
60 |
>>> cmd = (
|
|
61 |
... '''%s -c "import sys; print 666; '''
|
|
62 |
... '''print >> sys.stderr, 667; sys.exit(42)"''' % python
|
|
63 |
... )
|
|
64 |
>>> shhh_script(cmd)
|
|
65 |
('666\n', '667\n', 42)
|
|
66 |
"""
|
|
67 |
||
68 |
process = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True) |
|
69 |
(out, err) = process.communicate() |
|
70 |
if process.returncode == 0: |
|
71 |
return 0 |
|
72 |
else: |
|
73 |
sys.stderr.write(err) |
|
74 |
sys.stdout.write(out) |
|
75 |
return process.returncode |
|
76 |
||
77 |
||
78 |
if __name__ == '__main__': |
|
79 |
cmd = ' '.join(sys.argv[1:]) |
|
80 |
sys.exit(shhh(cmd)) |
|
81 |