-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_pkgtree.py
executable file
·200 lines (164 loc) · 5.97 KB
/
build_pkgtree.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
#!/usr/bin/python3
import argparse
import os.path
import re
import subprocess
import shlex
import sys
import tarfile
class Package(object):
def __init__(self, pkgname=None, pkgver=None):
self.name = pkgname
self.version = pkgver
self.provides = set([])
def parse_pkgdesc(f):
pkg = Package()
current_section = None
for line in f:
if type(line) == str:
line = line.strip()
else:
line = line.decode('utf-8').strip()
if current_section is not None and len(line) <= 0:
current_section = None
elif line == '%NAME%':
current_section = 'name'
elif line == '%VERSION%':
current_section = 'version'
elif line == '%PROVIDES%':
current_section = 'provides'
elif current_section == 'name':
pkg.name = line
elif current_section == 'version':
pkg.version = line
elif current_section == 'provides':
pkg.provides.add(line.split('=')[0])
return pkg
def list_db(dbpath):
pkgs = set([])
with tarfile.open(dbpath) as tf:
for member in tf.getmembers():
if os.path.basename(member.name) == 'desc':
f = tf.extractfile(member)
pkg = parse_pkgdesc(f)
pkgs.add(pkg.name)
pkgs = pkgs.union(pkg.provides)
return pkgs
def list_installed_packages(dbpath):
pkgs = set([])
for rootdirname, dirnames, _ in os.walk(os.path.join(dbpath, 'local')):
for dirname in dirnames:
pkgname, pkgver, pkgrel = dirname.rsplit('-', 2)
pkgs.add(pkgname)
with open(os.path.join(dbpath, dirname, 'desc')) as f:
pkg = parse_pkgdesc(f)
pkgs.add(pkg.name)
pkgs = pkgs.union(pkg.provides)
return pkgs
def get_dependencies(pkgname):
pkgs = []
split_re = re.compile('[<>=]+')
try:
output = subprocess.check_output(['makepkg', '--printsrcinfo'],
cwd=pkgname)
except subprocess.CalledProcessError:
return pkgs
for line in output.decode('utf-8').splitlines():
data = line.split(' = ', 1)
if len(data) < 2:
continue
if data[0] in ('\tdepends', '\tmakedepends'):
pkgs.append(split_re.split(data[1], 1)[0])
return pkgs
def build_deptree(pkg, skip_pkgs):
pkgs = []
deps = get_dependencies(pkg)
for dep in deps:
if os.path.exists(dep):
deptree = build_deptree(dep, skip_pkgs)
for d in deptree:
pkgs.append(d)
elif dep not in skip_pkgs:
print("Warning: {0} not found".format(dep), file=sys.stderr)
pkgs.append(pkg)
return pkgs
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description="Build a package (and its dependencies) with "
"geschenkerbauer")
parser.add_argument('--buildhost', required=True, help="Build host")
parser.add_argument(
'--build-image',
default="mutantmonkey/geschenkerbauer:latest",
help="Docker image that will be launched to build each package")
parser.add_argument('--buildsrcdir',
default="/home/core/arch/packages",
help="Source package directory")
parser.add_argument('--repodir',
default="/home/core/arch/repo",
help="Repository directory")
parser.add_argument('--gpgdir',
help="GnuPG data directory")
parser.add_argument('--keyring',
help="GnuPG keyring to import")
parser.add_argument('--packager',
default="geschenkerbauer <geschenkerbauer@localhost>",
help="Packager")
parser.add_argument('--dbpath', default="/var/lib/pacman",
help="Path to local pacman database directory")
parser.add_argument('--skip-copy', action='store_true', default=False,
help="Start build without copying packages")
parser.add_argument('pkgs', nargs='+')
args = parser.parse_args()
skip_pkgs = set([])
for syncdb in ['core', 'extra', 'community']:
skip_pkgs = skip_pkgs.union(list_db(
os.path.join(args.dbpath, 'sync', '{0}.db'.format(syncdb))))
#skip_pkgs = skip_pkgs.union(list_installed_packages(args.dbpath))
pkgs_to_build = []
for pkg in args.pkgs:
pkg_deptree = build_deptree(pkg, skip_pkgs)
print(pkg, pkg_deptree)
for bpkg in pkg_deptree:
if bpkg not in pkgs_to_build:
pkgs_to_build.append(bpkg)
print(pkgs_to_build)
if not args.skip_copy:
# TODO: consider making this work from outside local buildsrcdir
# would need an argument to specify path to packages
subprocess.run(['rsync', '-avP'] + pkgs_to_build +
[':'.join([args.buildhost, args.buildsrcdir])])
if args.keyring is not None:
if not args.skip_copy:
subprocess.run(
['rsync', '-avP', args.keyring] +
[':'.join([
args.buildhost,
os.path.join(args.buildsrcdir, 'keyring.asc'),
])]
)
ssh_base_args = [
'docker',
'run',
'--rm',
'-v',
'{args.repodir}:/repo',
'-e',
'PACKAGER={0}'.format(shlex.quote(args.packager)),
]
if args.gpgdir is not None:
ssh_base_args += [
'-v',
'{args.gpgdir}:/gnupg',
'-e',
'GNUPGHOME=/gnupg',
]
if args.keyring is not None:
ssh_base_args += ['-e', f"GNUPG_PUBKEYRING={args.keyring}"]
for pkg in pkgs_to_build:
ssh_args = ssh_base_args + [
'-v',
'{0}:/buildsrc'.format(os.path.join(args.repodir, pkg)),
args.build_image,
]
subprocess.run(['ssh', args.buildhost] + ssh_args)