1 # vi: encoding=utf-8 ts=8 sts=4 sw=4 et
10 from hashlib import md5 as md5
14 from config import config
17 return re.sub("([\"\\\\$`])", r"\\\1", cmd)
19 def command(cmd, user = None, nostdin=""):
21 user = config.builder_user
23 nostdin = "exec < /dev/null; "
24 return "%s sudo chroot %s su - %s -c \"export LC_ALL=C; %s %s\"" \
25 % (config.sudo_chroot_wrapper, config.chroot, user, nostdin, quote(cmd))
28 return "%s sudo chroot %s /bin/sh -c \"export LC_ALL=C; exec < /dev/null; %s\"" \
29 % (config.sudo_chroot_wrapper, config.chroot, quote(cmd))
31 def popen(cmd, user = "builder", mode = "r"):
33 p = subprocess.Popen(command(cmd, user), shell=True, stdout=subprocess.PIPE, close_fds=True)
36 p = subprocess.Popen(command(cmd, user), shell=True, stdin=subprocess.PIPE, close_fds=True)
40 def run(cmd, user = "builder", logfile = None, logstdout = None):
41 c = command(cmd, user, nostdin=True)
44 c = "%s 2>&1 | /usr/bin/tee -a %s" % (c, logfile)
46 c = "%s >> %s 2>&1" % (c, logfile)
57 def cp(file, outfile, user="builder", rm=False):
59 m.update(util.to_bytes(str(random.sample(range(100000), 500))))
60 digest = m.hexdigest()
62 marker_start = "--- FILE BEGIN DIGEST %s ---" % digest
63 marker_end = "--- FILE END DIGEST %s ---" % digest
65 f = open(outfile, 'wb')
66 cmd = "echo \"%s\"; cat %s; echo \"%s\"" % (marker_start, file, marker_end)
68 cmd += "; rm %s" % file
69 c = command(cmd, user)
74 if not marker and l.strip() == marker_start:
77 me = l.find(marker_end)
80 f.write(util.to_bytes(l))
84 f.write(util.to_bytes(l))