-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadt-runscript.py
executable file
·181 lines (157 loc) · 6.4 KB
/
adt-runscript.py
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/env python
"""
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Copyright 2013 Julian Taylor
"""
import subprocess as sp
from debian import deb822
from debian import debfile
import os
import sys
import tempfile
if len(sys.argv) < 2:
print "usage: adt-runscript.py <dsc> [changes]"
print "Prepare script to run autopkgtests in pbuilder via --execute\n"
print "if provided with only a dsc it will download the dependencies " \
"from the archive."
print "if provided with a dsc and a changes file it will use the local "\
"packages from the changes file."
sys.exit(0)
class Test:
def __init__(self, name):
self.name = name
self.depends = []
self.restrictions = []
def parse_depends(self, d):
self.depends = set([x.split()[0] for x in d.split(",") if "libc" not in x])
target = "trusty"
arch = "amd64"
dsc = os.path.abspath(sys.argv[1])
loc_pkg_paths = set()
loc_pkg_names = set()
if len(sys.argv) > 2:
changes = os.path.abspath(sys.argv[2])
c = deb822.Changes.iter_paragraphs(open(changes)).next()
target = c["Distribution"]
arch = c["Architecture"].split()[0]
loc_pkg_paths = set([os.path.join(os.path.dirname(changes), x["name"])
for x in c["Checksums-Sha1"] if x["name"].endswith("deb")])
loc_pkg_names = set([os.path.basename(pkg).split("_")[0] for pkg in loc_pkg_paths])
# gather dependencies of the local packages
loc_pkg_deps = dict()
for dp in loc_pkg_paths:
dc = dict(debfile.DebFile(dp).debcontrol().items())
dn = dc["Package"]
if "Depends" in dc:
loc_pkg_deps[dn] = set([pkg.split()[0]
for pkg in dc["Depends"].split(",")]) & loc_pkg_names
# extract source package
logfile = "/tmp/%s-%s-%s.log" % (os.path.splitext(os.path.basename(dsc))[0], target, arch)
tmp = "/tmp/sadttmp-%s-%s-%s" % (os.path.basename(dsc).split("_")[0], target, arch) #tempfile.mkdtemp()
try:
os.makedirs(tmp)
except OSError:
pass
os.chdir(tmp)
sp.call(["dpkg-source", "-x", dsc])
for d in os.listdir("."):
if os.path.isdir(d) and d.startswith(os.path.basename(dsc).split("_")[0]):
pkgdir = d
break
with_exp = "-t experimental" if target == "experimental" else ""
# gather tests
tests = []
for f in deb822.Deb822.iter_paragraphs(open(os.path.join(pkgdir, "debian/tests/control"))):
for k, v in f.iteritems():
if k == "Tests":
tests.append(Test(v))
elif k == "Depends":
if v == "@":
tests[-1].depends = list(loc_pkg_names)
else:
tests[-1].parse_depends(v)
elif k == "Restrictions":
if v.lower() in ("breaks-testbed", "rw-build-tree"):
raise ValueError("Restriction %s not supported" % v)
tests[-1].restrictions.append(v)
# no Depends means implicit @
for t in tests:
if not t.depends:
t.depends = list(loc_pkg_names)
# copy the local pacakges
for pkg in loc_pkg_paths:
import shutil
shutil.copy(pkg, ".")
loc_pkg_paths = [os.path.basename(pkg) for pkg in loc_pkg_paths]
# prepare the script
with open("runscript.sh", "w") as f:
f.write("#!/bin/bash\n")
f.write("set -xe\n")
# same id as pbuilder for firewalling
f.write("id adttesting || useradd -u 1234 adttesting\n")
# remove build-essential usually present in packaging chroots
f.write("apt-get remove -f -y --force-yes libc6-dev gcc g++ make dpkg-dev\n")
f.write("cd %s\n" % os.path.join(tmp, pkgdir))
f.write("chmod a-w -R .\n")
for t in tests:
aptdep = " ".join(t.depends - loc_pkg_names)
# resolve local dependencies
dpkgdep = loc_pkg_names & t.depends
plen = 0
while len(dpkgdep) != plen:
plen = len(dpkgdep)
for d in list(dpkgdep):
if d in loc_pkg_deps:
print t.name, d, list(loc_pkg_deps[d])
dpkgdep = dpkgdep.union(loc_pkg_deps[d])
if dpkgdep:
dpkgdeppath = [os.path.join(tmp, d) for d in loc_pkg_paths if d.split("_")[0] in dpkgdep]
f.write("dpkg -i %s || true\n" % " ".join(dpkgdeppath))
f.write("apt-get install -f -y --force-yes %s\n" % with_exp)
all_installed = " ".join(t.depends | dpkgdep)
asuser = "" if "needs-root" in t.restrictions else "su adttesting -c"
f.write("""\
apt-get install -y --force-yes {aptdep} {exp}
rm -rf /tmp/sadt
mkdir -p /tmp/sadt
chown adttesting /tmp/sadt
set +e
TMPDIR=/tmp/sadt ADTTMP=/tmp/sadt AUTOPKGTEST_TMP=/tmp/sadt {asuser} debian/tests/{testname} 2> errlog
ret=$?
set -e
if [ ! $(cat errlog | wc -l) -eq 0 ]; then
echo 'STDERR LOG:';
cat errlog;
echo STDERR FAILURE;
apt-get install -y vim less
/bin/bash < /dev/tty > /dev/tty 2> /dev/tty
exit 1;
fi
if [ $ret -ne 0 ]; then
echo FAILURE $ret;
apt-get install -y vim less
/bin/bash < /dev/tty > /dev/tty 2> /dev/tty
exit $ret;
fi
""".format(aptdep=aptdep, testname=t.name, asuser=asuser, exp=with_exp))
if t != tests[-1] and "--fast" not in sys.argv:
# remove build-essential usually present in packaging chroots
f.write("apt-get remove -f -y --force-yes libc6-dev gcc g++ make dpkg-dev\n")
f.write("apt-get autoremove -y --force-yes --purge %s\n" % all_installed)
f.write("echo SUCCESS\n")
print 'to run the tests execute:'
print 'sudo DIST=%s ARCH=%s cowbuilder --execute "%s/runscript.sh" --bindmount "%s" --logfile "%s"' % (target, arch, tmp, tmp, logfile)