forked from TresysTechnology/clip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage_pkglist.py
executable file
·370 lines (320 loc) · 12.1 KB
/
manage_pkglist.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#!/usr/bin/python
import os
import re
import sys
import subprocess
import argparse
from pykickstart.parser import KickstartParser
from pykickstart.version import makeVersion
verbose_output = False
buildrequires_regex = re.compile(r"^BuildRequires:\s*(?P<reqs>.+)$")
lorax_regex = re.compile(r"^installpkg\s*(?P<reqs>.+)$")
reposfrompath=[]
spec_req_regex = re.compile(r"^(?P<req>[a-zA-Z0-9_\(\).\+-]+)")
arch_regex = re.compile(r"^(?P<req>[a-zA-Z0-9_.\+-]+)\((?P<arch>[0-9a-zA-Z-]+)\)$")
def verbose(info):
global verbose_output
if verbose_output:
print info
def reqs_to_packages(config, reqs, arch):
# resolve the reqs to package names
package_names = dict()
repoquery_args = ["/usr/bin/repoquery", "-c", config, "--whatprovides", "--queryformat", "%{NAME},%{ARCH}"]
# TODO: see if this can be collapsed into a single call
for r in reqs:
final_arch = arch
match = arch_regex.match (r)
if match:
r = match.group("req")
final_arch = match.group("arch")
# hack - the name in the spec need x86-32 but the RPM name is i686 - ugh
if final_arch == "x86-32":
final_arch = "i686"
final_query = repoquery_args + ["--archlist",final_arch] + [r]
verbose ("Finding RPM name for dependency: %s" % " ".join (final_query))
repoquery = subprocess.Popen(final_query, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
for line in repoquery.stdout:
if line.strip():
verbose ("Req %s provided by %s" % (r, line.strip()))
match = line.strip().split(",")
pkg_arch = match[1].strip ()
if pkg_arch not in package_names.keys ():
package_names[pkg_arch] = set ()
package_names[pkg_arch].add(match[0].strip())
rc = repoquery.wait()
if rc != 0:
print "ERROR: repoquery exited with rc %s: stderr: %s" % (rc, repoquery.stderr.read())
raise Exception("ERROR: repoquery exited with rc %s: stderr: %s" % (rc, repoquery.stderr.read()))
if not package_names:
print "Unmatched regs: %s" % reqs
return set()
# resolve package names to nvra form
packages = set()
repoquery_args = ["/usr/bin/repoquery", "-c", config, "--qf", "%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}"]
for k in package_names.keys ():
repoquery = repoquery_args + ["--archlist", k]
for p in package_names[k]:
repoquery.append(p)
verbose ("Query package name: %s" % " ".join (repoquery))
repoquery = subprocess.Popen(repoquery, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
for line in repoquery.stdout:
if line.strip():
packages.add(line.strip())
rc = repoquery.wait()
if rc != 0:
print "ERROR: repoquery exited with rc %s: stderr: %s" % (rc, repoquery.stderr.read())
raise Exception("ERROR: repoquery exited with rc %s: stderr: %s" % (rc, repoquery.stderr.read()))
return packages
dep_tree_regex = re.compile(r"(?: (?:\| )*(?:\\_ ))?(?:\d+:)?(?P<pkg>.+) \[.*\]\n?")
def update_deps(config, reqs, deps, arch):
# gather deps for all reqs
repoquery_args = ["/usr/bin/repoquery", "-c", config, "--tree-requires", "--qf", "%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}"]
repoquery_args = repoquery_args + reposfrompath
for p in reqs:
final_arch = p.split(".")[-1]
if p in deps:
verbose ("not gathering deps for package %s because it has already been resolved" % (p,))
continue
if final_arch == "noarch":
final_arch = arch
final_query = repoquery_args + ["--archlist", "%s,noarch" % final_arch] + [p]
verbose ("build dependency tree for requirement: %s" % " ".join (final_query))
repoquery = subprocess.Popen(final_query, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
for line in repoquery.stdout:
match = dep_tree_regex.search(line)
if match:
pkg = match.group("pkg")
verbose ("match: '%s' from: '%s'" % (pkg, line))
deps.add(pkg)
else:
print "no match: %s" % (line,)
rc = repoquery.wait()
if rc != 0:
print "ERROR: repoquery exited with rc %s: stderr: %s" % (rc, repoquery.stderr.read())
raise Exception("ERROR: repoquery exited with rc %s: stderr: %s" % (rc, repoquery.stderr.read()))
'''
for p in explicit_packages:
print p
repoquery_args.append(p)
all_packages_single_repoquery = set()
if True:
print "executing: %s" % (repoquery_args,)
repoquery = subprocess.Popen(repoquery_args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
for line in repoquery.stdout:
match = dep_tree_regex.search(line)
if match:
print "match s: '%s' from: '%s'" % (match.group("pkg"), line)
all_packages_single_repoquery.add(match.group("pkg"))
else:
print "no match s: %s" % (line,)
rc = repoquery.wait()
if rc != 0:
print "ERROR: repoquery exited with rc %s: stderr: %s" % (rc, repoquery.stderr.read())
raise Exception("ERROR: repoquery exited with rc %s: stderr: %s" % (rc, repoquery.stderr.read()))
only_in_single_query = all_packages_single_repoquery - all_packages_multiple_repoquery
only_in_multiple_query = all_packages_multiple_repoquery - all_packages_single_repoquery
if not only_in_single_query and not only_in_multiple_query:
print "both repoquery usages produced the same results"
else:
if only_in_single_query:
print "packages found only in the single query method: %s" % (only_in_single_query,)
if only_in_multiple_query:
print "packages found only in the multiple query method: %s" % (only_in_multiple_query,)
all_packages = all_packages_single_repoquery + all_packages_multiple_repoquery
'''
'''
def get_build_deps_using_repoquery():
build_reqs = set()
package_names = get_local_package_names()
repoquery_args = ["repoquery", "-c", "test_yum.conf", "--requires", "--srpm"]
for p in package_names:
repoquery = subprocess.Popen(repoquery_args + [p], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
for line in repoquery.stdout:
if line.strip():
build_reqs.add(line.strip())
rc = repoquery.wait()
if rc != 0:
print "ERROR: repoquery exited with rc %s: stderr: %s" % (rc, repoquery.stderr.read())
raise Exception("ERROR: repoquery exited with rc %s: stderr: %s" % (rc, repoquery.stderr.read()))
return build_reqs
'''
'''
# use the rpm module to query a srpm for BuildRequires
def get_build_reqs_using_librpm(package_paths):
build_reqs = list()
ts = rpm.TransactionSet()
for package_path in package_paths:
fd = os.open(package_path, os.O_RDONLY)
h = ts.hdrFromFdno(fd)
build_reqs.update(h.dsFromHeader('requirename'))
return build_reqs
'''
spec_req_regex = re.compile(r"^(?P<req>[a-zA-Z0-9_\(\).\+-]+)")
# grep for BuildRequires in a .spec file
def get_build_reqs_from_spec(spec_path):
reqs = set()
for line in file(spec_path):
match = buildrequires_regex.match(line.strip())
if not match:
continue
reqs_str = match.group("reqs")
reqs_list = reqs_str.split(' ')
for req_str in reqs_list:
match = spec_req_regex.match(req_str)
if match:
reqs.add(match.group("req"))
return reqs
def get_build_reqs_from_lorax(lorax_path):
reqs = set()
for line in file(lorax_path):
match = lorax_regex.match(line.strip())
if not match:
continue
reqs_str = match.group("reqs")
reqs_list = reqs_str.split(' ')
for req_str in reqs_list:
match = spec_req_regex.match(req_str)
if match:
reqs.add(match.group("req"))
return reqs
# parse the %packages section of a kickstart to determine
# what packages are to be installed
def get_kickstart_reqs(kickstart_path):
# gather all of the reqs listed in the %packages sections
reqs = set()
ksparser = KickstartParser(makeVersion())
ksparser.readKickstart(kickstart_path)
reqs.update(ksparser.handler.packages.packageList)
return reqs
def repos_from_args(repo_args):
global reposfrompath
repos = list()
for r in repo_args:
repo = dict()
repo["pkgs"] = set()
repo["path"], repo["pkglist"] = r.split(",")
reposfrompath.append("--repofrompath")
reposfrompath.append("%s,%s" % (os.path.basename(repo["pkglist"]), repo["path"]))
if not os.path.exists(repo["path"]):
raise Exception("ERROR: repo path %s does not exist" % (repo["path"],))
repos.append(repo)
return repos
def resolve_packages_to_repos(pkgs, repos):
for pkg in pkgs:
found = False
for repo in repos:
pkg_paths = [
os.path.join(repo["path"], "%s.rpm" % (pkg,)),
os.path.join(repo["path"], "Packages", "%s.rpm" % (pkg,)),
os.path.join(repo["path"], pkg.lower()[0], "%s.rpm" % (pkg,)),
os.path.join(repo["path"], "Packages", pkg.lower()[0], "%s.rpm" % (pkg,)),
]
if any([os.path.exists(x) for x in pkg_paths]):
repo["pkgs"].add(pkg)
found = True
break
elif pkg in repo["pkgs"]:
found = True
break
if not found:
print "ERROR: package %s could not be found" % (pkg,)
#raise Exception("ERROR: package %s could not be found" % (pkg,))
def read_pkglist(pkglist_path):
packages = set()
if not os.path.exists(pkglist_path):
return packages
f = file(pkglist_path, "r")
for line in f:
line = line.strip()[:-4]
if line:
packages.add(line)
return packages
def read_pkglists(repos):
for repo in repos:
repo["pkgs"].update(read_pkglist(repo["pkglist"]))
def dump_pkglist(pkglist_path, pkgs):
f = file(pkglist_path, "w")
pkg_list = list(pkgs)
pkg_list.sort()
f.write("\n".join(["%s.rpm" % (p,) for p in pkg_list]) + "\n")
f.close()
print "Updated %s" % (pkglist_path,)
def dump_pkglists(repos):
for repo in repos:
dump_pkglist(repo["pkglist"], repo["pkgs"])
def main():
parser = argparse.ArgumentParser(description="Create a minimal list of dependencies from kickstarts, .spec files, and requirement strings.")
parser.add_argument("COMMAND", choices=["create", "add"])
parser.add_argument("-c", "--config", default="/etc/yum.conf")
parser.add_argument("-k", "--kickstart", action="append")
parser.add_argument("-s", "--spec", action="append")
parser.add_argument("-l", "--lorax", action="append")
parser.add_argument("-a", "--arch", default="x86_64,noarch")
parser.add_argument("-r", "--require", action="append")
parser.add_argument('-v', "--verbose", action="store_true", default=False)
parser.add_argument("repo", help="repository information. for each repository, 2 components must be supplied in the following form: repo_path,pkglis", nargs="+", metavar="PATH,PKGLIST")
parser.add_argument("-t", "--runtime", action='store_true', help="Generate list of run time dependencies instead of build time dependencies")
args = parser.parse_args()
global verbose_output
global buildrequires_regex
if args.runtime:
print "Using only Requires tags"
buildrequires_regex = re.compile(r"^Requires:\s*(?P<reqs>.+)$")
else:
print "Using both Requires and BuildRequires tags"
buildrequires_regex = re.compile(r"^(Build)?Requires:\s*(?P<reqs>.+)$")
config = args.config
command = args.COMMAND
kickstarts = args.kickstart
if kickstarts is None:
kickstarts = list()
specs = args.spec
if specs is None:
specs = list()
lorax = args.lorax
if lorax is None:
lorax = list()
manual_reqs = args.require
if manual_reqs is None:
manual_reqs = list()
verbose_output = args.verbose
arch = args.arch
repos = repos_from_args(args.repo)
reqs = set()
# gather packages to install from kickstart
for k in kickstarts:
if not os.path.exists(k):
raise Exception("ERROR: Kickstart %s does not exist" % (k,))
tmp = get_kickstart_reqs(k)
reqs.update(tmp)
# gather build requirements from .spec file
for s in specs:
if not os.path.exists(s):
raise Exception("ERROR: spec file %s does not exist" % (s,))
tmp = get_build_reqs_from_spec(s)
verbose ("List of requirements (%s) from spec: %s" % (tmp, s))
reqs.update(tmp)
for l in lorax:
if not os.path.exists(l):
raise Exception("ERROR: lorax file %s does not exist" % (l,))
tmp = get_build_reqs_from_lorax(l)
verbose ("List of requirements (%s) from spec: %s" % (tmp, l))
reqs.update(tmp)
# requirement strings (capabilities, files, packages)
reqs.update(manual_reqs)
dependencies = set()
# convert req strings to specific packages
required_packages = reqs_to_packages(config, reqs, arch)
# gather all dependent packages
update_deps(config, required_packages, dependencies, arch)
# incorporate existing pkglist contents
if command == "add":
read_pkglists(repos)
for repo in repos:
dependencies.update(repo["pkgs"])
# sort packages into the right repos
resolve_packages_to_repos(dependencies, repos)
# write new pkglist files
dump_pkglists(repos)
if __name__ == "__main__":
sys.exit(main())