1 # vi: encoding=utf-8 ts=8 sts=4 sw=4 et
9 from hashlib import md5 as md5
13 from config import config
16 return re.sub("([\"\\\\$`])", r"\\\1", cmd)
18 def command(cmd, user = None, nostdin=""):
20 user = config.builder_user
22 nostdin = "exec < /dev/null; "
23 return "%s sudo chroot %s su - %s -c \"export LC_ALL=C; %s %s\"" \
24 % (config.sudo_chroot_wrapper, config.chroot, user, nostdin, quote(cmd))
27 return "%s sudo chroot %s /bin/sh -c \"export LC_ALL=C; exec < /dev/null; %s\"" \
28 % (config.sudo_chroot_wrapper, config.chroot, quote(cmd))
30 def popen(cmd, user = "builder", mode = "r"):
31 f = os.popen(command(cmd, user), mode)
34 def run(cmd, user = "builder", logfile = None, logstdout = None):
35 c = command(cmd, user, nostdin=True)
38 c = "%s 2>&1 | /usr/bin/tee -a %s" % (c, logfile)
40 c = "%s >> %s 2>&1" % (c, logfile)
51 def cp(file, outfile, user="builder", rm=False):
53 m.update(util.to_bytes(str(random.sample(range(100000), 500))))
54 digest = m.hexdigest()
56 marker_start = "--- FILE BEGIN DIGEST %s ---" % digest
57 marker_end = "--- FILE END DIGEST %s ---" % digest
59 f = open(outfile, 'wb')
60 cmd = "echo \"%s\"; cat %s; echo \"%s\"" % (marker_start, file, marker_end)
62 cmd += "; rm %s" % file
63 c = command(cmd, user)
68 if not marker and l.strip() == marker_start:
71 me = l.find(marker_end)
74 f.write(util.to_bytes(l))
78 f.write(util.to_bytes(l))