1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
########################################################################
# Utility functions
########################################################################
import os
import subprocess
def prog_stdout(call_list):
"""Run program and return stdout (similar to shell backticks)"""
p = subprocess.Popen(call_list,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdout, stderr = p.communicate(input=None)
return stdout.strip()
def prog_retstd(call_list):
"""Run program and return stdout (similar to shell backticks)"""
p = subprocess.Popen(call_list,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdout, stderr = p.communicate(input=None)
return (p.returncode, stdout.strip(), stderr.strip())
class ProgramRunError(Exception):
"""A program run returns a retcode != 0"""
def __init__(self, call_list, retcode, cwd=None):
self.call_list = call_list
self.retcode = retcode
if cwd:
self.cwd = cwd
else:
self.cwd = os.getcwd()
def __str__(self):
return ("Error running program (%s, retcode=%d, cwd=%s)"
% (repr(self.call_list),
self.retcode,
repr(self.cwd)))
def prog_run(call_list, context):
"""Run program showing its output. Raise exception if retcode != 0."""
print "RUN:", call_list
print " in", os.getcwd()
if context.dry_run:
return None
p = subprocess.Popen(call_list)
stdout, stderr = p.communicate(input=None)
if p.returncode != 0:
raise ProgramRunError(call_list, p.returncode, os.getcwd())
return p.returncode
|