-
Notifications
You must be signed in to change notification settings - Fork 9
/
build.py
executable file
·340 lines (255 loc) · 10.2 KB
/
build.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#!/usr/bin/env python3
import argparse
import logging
import os
import re
import shutil
import subprocess
import sys
import tempfile
from functools import lru_cache, partial
from os.path import abspath, basename, dirname, exists, isdir, isfile, join
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
log = logging.getLogger(__name__)
log.addHandler(ch)
log.setLevel(logging.INFO)
def sh(*command, **kwargs):
log.debug('shell: %s', ' '.join(command))
return subprocess.check_call(' '.join(command), shell=True, **kwargs)
sudo = partial(sh, 'sudo')
def bt(*command, **kwargs):
log.debug('shell: %s', ' '.join(command))
out = subprocess.check_output(' '.join(command), shell=True, **kwargs)
return out.strip().decode()
BASE = os.getcwd()
ARCHROOT = join(BASE, '.archroot')
ARCHROOT_LOCK = join(BASE, '.archroot.lock')
LOCALREPO = join(BASE, '.repo/')
REMOTEREPO = 'kirby:/home/jreese/pub/arch/'
DATABASE = 'noswap.db.tar.xz'
USERGROUP = 'jreese:jreese'
CPUARCH = bt('uname -m')
PKGREGEX = "'.*/{0}-\(preview\|latest\|.?[0-9]\).*\.pkg\.tar\.xz.*'"
class ChrootBuild(object):
def __init__(self, fresh=False, clean=False):
log.debug('__init__')
self.pkgroot = join(ARCHROOT, 'packages')
self.pkgpath = '/packages'
self.fresh = fresh
self.cleanup = clean
self.rsh = partial(sh, 'sudo', 'arch-chroot', ARCHROOT)
self.rbt = partial(bt, 'sudo', 'arch-chroot', ARCHROOT)
def __enter__(self):
log.debug('__enter__')
self.unmount()
if self.fresh:
self.clean()
self.init()
return self
def __exit__(self, exc_type, exc_value, traceback):
log.debug('__exit__')
self.unmount()
if self.cleanup:
self.clean()
def unmount(self):
log.info('checking mounts')
mount_list = bt('mount').split('\n')
log.debug('current mounts:\n%s\n', '\n'.join(mount_list))
# make sure the chroot isn't already mounted
for mount in (
join(ARCHROOT, 'tmp'),
join(ARCHROOT, 'sys'),
join(ARCHROOT, 'proc'),
join(ARCHROOT, 'dev', 'pts'),
join(ARCHROOT, 'dev'),
ARCHROOT,
):
for mount_entry in mount_list:
if mount in mount_entry:
log.info('mount point %s found, unmounting', mount)
try:
sh('sudo umount -Rl', mount)
break
except:
log.error('umount failed, listing lsof for %s:', mount)
sudo('lsof', mount, '| grep', mount)
raise
def init(self):
log.debug('Initializing archroot %s', ARCHROOT)
if not exists(ARCHROOT):
log.info('Creating new archroot in %s', ARCHROOT)
try:
sudo('mkarchroot', ARCHROOT, 'base', 'base-devel')
except:
log.exception('mkarchroot returned non-zero, ignoring')
log.info('Enabling sudo access for wheel')
self.rsh('sed -i -e', "'s.# %wheel ALL=(ALL) NOPASSWD: ALL.%wheel ALL=(ALL) NOPASSWD: ALL.'", '/etc/sudoers')
log.info('Creating arch user with uid 1000')
self.rsh('useradd -m -u 1000 -U arch -G wheel')
if exists(self.pkgroot):
log.debug('Cleaning old packages from %s', self.pkgroot)
sudo('rm -r', self.pkgroot)
sudo('mkdir -p', self.pkgroot)
log.debug('Adding multilib and noswap repos')
for script in ['multilib-repo.sh', 'noswap-repo.sh']:
sudo('cp', script, self.pkgroot)
self.rbt('sh', join(self.pkgpath, script))
#log.debug('Adding trusted GPG keys')
#self.rsh('su - arch -c "gpg --recv-keys --keyserver hkp://pgp.mit.edu 1EB2638FF56C0C53"')
def clean(self):
self.unmount()
if exists(ARCHROOT):
log.info('Cleaning up archroot %s', ARCHROOT)
sudo('rm -r', ARCHROOT)
sudo('rm', ARCHROOT_LOCK)
def build(self, package):
if not isdir(package):
raise Exception('Package {0} not found'.format(package))
sh('chmod -R a+rX', package)
log.debug('Updating pacman')
self.rsh('pacman -Syu --noconfirm')
runroot = join(self.pkgroot, 'run.sh')
runpath = join(self.pkgpath, 'run.sh')
pkgroot = join(self.pkgroot, package)
pkgpath = join(self.pkgpath, package)
command = 'cd {0} && makepkg -s --noconfirm'.format(pkgpath)
try:
sh('grep arch=', join(package, 'PKGBUILD'), '| grep -q any')
arch = 'any'
except:
arch = CPUARCH
log.debug('Package arch: %s', arch)
log.debug('Copying sources to archroot')
sudo('cp -r', package, pkgroot)
with open('run.sh', 'w') as f:
f.write(command + '\n')
sudo('mv', 'run.sh', runroot)
log.debug('Building: `%s`', command)
sudo('chown -R 1000:1000', pkgroot)
self.rsh('su - arch -c "bash -l {0}"'.format(runpath))
iregex = PKGREGEX.format(package)
pkgfile = bt('find', pkgroot, '-maxdepth 1 -iregex', iregex)
log.debug('Built package file "%s"', pkgfile)
return pkgfile
def build_packages(args, packages):
completed = set()
failed = set()
with ChrootBuild(fresh=args.fresh, clean=args.clean) as chroot:
for package in packages:
try:
os.chdir(BASE)
log.info('--- %s ---', package)
pkgfile = chroot.build(package)
pkg = basename(pkgfile)
log.debug('Deleting old package from repository')
sh('find', LOCALREPO, '-iregex', PKGREGEX.format(package),
'-delete')
log.debug('Copying new package to repository')
shutil.copy(pkgfile, LOCALREPO)
os.chdir(LOCALREPO)
log.info('Signing package')
sh('gpg --detach-sign ', pkg)
log.info('Updating database')
sh('repo-add --sign --verify', DATABASE, '-f', pkg)
os.chdir(BASE)
log.debug('Syncing local repository to remote')
sh('rsync --progress -avz --delete', LOCALREPO, REMOTEREPO)
completed.add(package)
except:
log.exception('Build failed')
failed.add(package)
return completed, failed
package_dep_re = re.compile(r'^(.*?)(?:[<>=].*)?$')
@lru_cache()
def package_deps(package):
pkgbuild = join(package, 'PKGBUILD')
if not isfile(pkgbuild):
return []
try:
dependencies = bt("sh -c 'source {0} && echo $depends'".format(pkgbuild)).split()
dependencies = [package_dep_re.sub(r'\1', dep) for dep in dependencies]
log.debug('Package %s depends on %s', package, dependencies)
return dependencies
except:
return []
def resolve_dependencies(packages):
queue = list(packages)
packages = []
log.debug('Unresolved build order: %s', queue)
limit = len(queue) + 1
tries = 0
while len(queue):
tries += 1
if tries >= limit:
log.error('Could not resolve package dependencies after %d tries,\n'
'resolved packages: %s\nunresolved packages: %s',
tries, packages, queue)
return []
package = queue.pop(0)
log.debug('checking if %s has any dependencies on %s', package, queue)
for name in package_deps(package):
if name in queue:
log.debug('package %s depends on %s', package, name)
queue.append(package)
break
if package not in queue:
log.debug('package %s in no dependency lists', package)
packages.append(package)
limit = tries + len(queue) + 1
log.debug('Resolved build order: %s', packages)
return packages
def main():
os.nice(2)
os.putenv('INFAKEROOT', '1')
parser = argparse.ArgumentParser(description='Build packages')
parser.add_argument('--debug', action='store_true', default=False,
help='debug output')
parser.add_argument('--fresh', action='store_true', default=False,
help='force creating a fresh archroot')
parser.add_argument('--clean', action='store_true', default=False,
help='cleanup archroot after running builds')
parser.add_argument('--retry', type=int, default=None,
help='specify the number of retries to attempt')
parser.add_argument('packages', metavar='PACKAGE', type=str, nargs='*',
help='list of package names to build')
args = parser.parse_args()
if args.debug:
log.setLevel(logging.DEBUG)
try:
if os.getuid():
log.debug('Requesting sudo access')
sh('sudo true')
log.debug('Syncing local repository from remote')
sh('rsync --progress -avz --delete', REMOTEREPO, LOCALREPO)
finished = set()
failed = set()
waiting = resolve_dependencies(set(args.packages))
retry = 0
while ((args.retry is None and retry <= len(waiting))
or (args.retry is not None and retry <= args.retry)):
retry += 1
completed, failed = build_packages(args, waiting)
finished |= completed
for package in completed:
waiting.remove(package)
log.info('--- Pass %d ---', retry)
if completed:
log.info('Completed: %s', ', '.join(completed))
if failed:
log.info('Failed: %s', ', '.join(failed))
args.fresh = False
if args.packages:
log.info('--- Results ---')
if finished:
log.info('Builds completed: %s', ', '.join(finished))
if failed:
log.info('Builds failed: %s', ', '.join(failed))
sys.exit(-1)
except Exception as e:
if args.debug:
log.exception('Build generated exception')
else:
log.error(e)
if __name__ == '__main__':
main()