Parent: [e59c8a] (diff)

Child: [f4f159] (diff)

Download this file

command.py    53 lines (43 with data), 1.4 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
import os
import shutil
import logging
import subprocess
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
def run(self, output_consumer=None, cwd=None):
if cwd is None:
cwd=self.cwd()
log.info('Running command: %r', self.args)
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)
self.sp.wait()
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)