Parent: [f4f159] (diff)

Child: [e41a45] (diff)

Download this file

command.py    68 lines (57 with data), 2.1 kB

 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
56
57
58
59
60
61
62
63
64
65
66
import os
import shutil
import logging
import subprocess
import sys
from pylons import c
log = logging.getLogger(__name__)
class Command(object):
base=None
def __init__(self, *args):
if isinstance(self.base, basestring):
base = self.base.split()
else:
base = self.base
self.args = tuple(base) + args
# consider checking return value
def run(self, output_consumer=None, cwd=None):
if cwd is None:
cwd=self.cwd()
log.info('Running command: %r in %s', self.args, cwd)
#print >> sys.stderr, 'Running command: %r in %s', self.args, cwd
self.sp = subprocess.Popen(
self.args, executable=self.args[0],
stdin=None, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, cwd=self.cwd())
if output_consumer is None:
self.output = self.sp.stdout.read()
else:
output_consumer(self.sp.stdout)
# Python docs say: Warning: This will deadlock if the child process
# generates enough output to a stdout or stderr pipe such that it
# blocks waiting for the OS pipe buffer to accept more data. Use
# communicate() to avoid that.
self.sp.wait()
log.info('command result: %s', self.sp.returncode)
if self.sp.returncode != 0:
print >> sys.stderr, 'command %r (in %s) returned: %s' % (self.args, self.cwd(), self.sp.returncode)
assert False
if hasattr(self, "finish"):
self.finish()
return self
def run_exc(self, *args, **kwargs):
result = self.run(*args, **kwargs)
assert not self.sp.returncode, self.output
def cwd(self):
try:
result = c.app.repo.repo_dir
except:
result = os.getcwd()
log.exception("Can't set cwd to app dir, defaulting to %s",
result)
return result
def clean_dir(self):
path = self.cwd()
if os.path.exists(path):
shutil.rmtree(path)
os.makedirs(path)