1 # vi: encoding=utf-8 ts=8 sts=4 sw=4 et
10 from config import config
13 return re.sub("([\"\\\\$`])", r"\\\1", cmd)
15 def command(cmd, user = None, nostdin=""):
17 user = config.builder_user
19 nostdin = "exec < /dev/null; "
20 return "%s sudo chroot %s su - %s -c \"export LC_ALL=C; %s %s\"" \
21 % (config.sudo_chroot_wrapper, config.chroot, user, nostdin, quote(cmd))
24 return "%s sudo chroot %s /bin/sh -c \"export LC_ALL=C; exec < /dev/null; %s\"" \
25 % (config.sudo_chroot_wrapper, config.chroot, quote(cmd))
27 def popen(cmd, user = "builder", mode = "r", encoding = None):
29 p = subprocess.Popen(command(cmd, user), shell=True, stdout=subprocess.PIPE, close_fds=True, encoding=encoding)
32 p = subprocess.Popen(command(cmd, user), shell=True, stdin=subprocess.PIPE, close_fds=True, encoding=encoding)
36 def run(cmd, user = "builder", logfile = None, logstdout = None):
37 c = command(cmd, user, nostdin=True)
40 c = "%s 2>&1 | /usr/bin/tee -a %s" % (c, logfile)
42 c = "%s >> %s 2>&1" % (c, logfile)
53 def cp(file, outfile, user="builder", rm=False):
54 f_out = open(outfile, 'wb')
57 cmd += "; rm %s" % file
58 p = subprocess.Popen(command(cmd, user), shell=True, stdout=subprocess.PIPE, close_fds=True)
60 shutil.copyfileobj(f_in, f_out)