blob: 44ae40549ae0dfdf19d63328a59ade03ffd05d16 [file] [log] [blame]
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001#!/usr/bin/env python3
Patrick Williamsc124f4f2015-09-15 14:41:29 -05002
3# OpenEmbedded pkgdata utility
4#
5# Written by: Paul Eggleton <paul.eggleton@linux.intel.com>
6#
7# Copyright 2012-2015 Intel Corporation
8#
Brad Bishopc342db32019-05-15 21:57:59 -04009# SPDX-License-Identifier: GPL-2.0-only
Patrick Williamsc124f4f2015-09-15 14:41:29 -050010#
11
12import sys
13import os
14import os.path
15import fnmatch
16import re
17import argparse
18import logging
19from collections import defaultdict, OrderedDict
20
21scripts_path = os.path.dirname(os.path.realpath(__file__))
22lib_path = scripts_path + '/lib'
23sys.path = sys.path + [lib_path]
24import scriptutils
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050025import argparse_oe
Patrick Williamsc124f4f2015-09-15 14:41:29 -050026logger = scriptutils.logger_create('pkgdatautil')
27
28def tinfoil_init():
29 import bb.tinfoil
30 import logging
31 tinfoil = bb.tinfoil.Tinfoil()
Patrick Williamsc124f4f2015-09-15 14:41:29 -050032 tinfoil.logger.setLevel(logging.WARNING)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050033 tinfoil.prepare(True)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050034 return tinfoil
35
36
37def glob(args):
38 # Handle both multiple arguments and multiple values within an arg (old syntax)
39 globs = []
40 for globitem in args.glob:
41 globs.extend(globitem.split())
42
43 if not os.path.exists(args.pkglistfile):
44 logger.error('Unable to find package list file %s' % args.pkglistfile)
45 sys.exit(1)
46
47 skipval = "-locale-|^locale-base-|-dev$|-doc$|-dbg$|-staticdev$|^kernel-module-"
48 if args.exclude:
49 skipval += "|" + args.exclude
50 skipregex = re.compile(skipval)
51
Patrick Williamsf1e5d692016-03-30 15:21:19 -050052 skippedpkgs = set()
Patrick Williamsc124f4f2015-09-15 14:41:29 -050053 mappedpkgs = set()
54 with open(args.pkglistfile, 'r') as f:
55 for line in f:
56 fields = line.rstrip().split()
57 if not fields:
58 continue
59 pkg = fields[0]
60 # We don't care about other args (used to need the package architecture but the
61 # new pkgdata structure avoids the need for that)
62
63 # Skip packages for which there is no point applying globs
64 if skipregex.search(pkg):
65 logger.debug("%s -> !!" % pkg)
Patrick Williamsf1e5d692016-03-30 15:21:19 -050066 skippedpkgs.add(pkg)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050067 continue
68
69 # Skip packages that already match the globs, so if e.g. a dev package
70 # is already installed and thus in the list, we don't process it any further
71 # Most of these will be caught by skipregex already, but just in case...
72 already = False
73 for g in globs:
74 if fnmatch.fnmatchcase(pkg, g):
75 already = True
76 break
77 if already:
Patrick Williamsf1e5d692016-03-30 15:21:19 -050078 skippedpkgs.add(pkg)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050079 logger.debug("%s -> !" % pkg)
80 continue
81
82 # Define some functions
83 def revpkgdata(pkgn):
84 return os.path.join(args.pkgdata_dir, "runtime-reverse", pkgn)
85 def fwdpkgdata(pkgn):
86 return os.path.join(args.pkgdata_dir, "runtime", pkgn)
87 def readpn(pkgdata_file):
88 pn = ""
89 with open(pkgdata_file, 'r') as f:
90 for line in f:
91 if line.startswith("PN:"):
92 pn = line.split(': ')[1].rstrip()
93 return pn
94 def readrenamed(pkgdata_file):
95 renamed = ""
96 pn = os.path.basename(pkgdata_file)
97 with open(pkgdata_file, 'r') as f:
98 for line in f:
Patrick Williams213cb262021-08-07 19:21:33 -050099 if line.startswith("PKG:%s:" % pn):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500100 renamed = line.split(': ')[1].rstrip()
101 return renamed
102
103 # Main processing loop
104 for g in globs:
105 mappedpkg = ""
106 # First just try substitution (i.e. packagename -> packagename-dev)
107 newpkg = g.replace("*", pkg)
108 revlink = revpkgdata(newpkg)
109 if os.path.exists(revlink):
110 mappedpkg = os.path.basename(os.readlink(revlink))
111 fwdfile = fwdpkgdata(mappedpkg)
112 if os.path.exists(fwdfile):
113 mappedpkg = readrenamed(fwdfile)
114 if not os.path.exists(fwdfile + ".packaged"):
115 mappedpkg = ""
116 else:
117 revlink = revpkgdata(pkg)
118 if os.path.exists(revlink):
119 # Check if we can map after undoing the package renaming (by resolving the symlink)
120 origpkg = os.path.basename(os.readlink(revlink))
121 newpkg = g.replace("*", origpkg)
122 fwdfile = fwdpkgdata(newpkg)
123 if os.path.exists(fwdfile):
124 mappedpkg = readrenamed(fwdfile)
125 else:
126 # That didn't work, so now get the PN, substitute that, then map in the other direction
127 pn = readpn(revlink)
128 newpkg = g.replace("*", pn)
129 fwdfile = fwdpkgdata(newpkg)
130 if os.path.exists(fwdfile):
131 mappedpkg = readrenamed(fwdfile)
132 if not os.path.exists(fwdfile + ".packaged"):
133 mappedpkg = ""
134 else:
135 # Package doesn't even exist...
136 logger.debug("%s is not a valid package!" % (pkg))
137 break
138
139 if mappedpkg:
140 logger.debug("%s (%s) -> %s" % (pkg, g, mappedpkg))
141 mappedpkgs.add(mappedpkg)
142 else:
143 logger.debug("%s (%s) -> ?" % (pkg, g))
144
145 logger.debug("------")
146
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500147 print("\n".join(mappedpkgs - skippedpkgs))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500148
149def read_value(args):
150 # Handle both multiple arguments and multiple values within an arg (old syntax)
151 packages = []
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500152 if args.file:
153 with open(args.file, 'r') as f:
154 for line in f:
155 splitline = line.split()
156 if splitline:
157 packages.append(splitline[0])
158 else:
159 for pkgitem in args.pkg:
160 packages.extend(pkgitem.split())
161 if not packages:
162 logger.error("No packages specified")
163 sys.exit(1)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500164
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500165 def readvar(pkgdata_file, valuename, mappedpkg):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500166 val = ""
167 with open(pkgdata_file, 'r') as f:
168 for line in f:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500169 if (line.startswith(valuename + ":") or
170 line.startswith(valuename + "_" + mappedpkg + ":")):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500171 val = line.split(': ', 1)[1].rstrip()
172 return val
173
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500174 logger.debug("read-value('%s', '%s' '%s')" % (args.pkgdata_dir, args.valuenames, packages))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500175 for package in packages:
176 pkg_split = package.split('_')
177 pkg_name = pkg_split[0]
178 logger.debug("package: '%s'" % pkg_name)
179 revlink = os.path.join(args.pkgdata_dir, "runtime-reverse", pkg_name)
180 logger.debug(revlink)
181 if os.path.exists(revlink):
182 mappedpkg = os.path.basename(os.readlink(revlink))
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500183 qvars = args.valuenames
184 val_names = qvars.split(',')
185 values = []
186 for qvar in val_names:
187 if qvar == "PACKAGE":
188 value = mappedpkg
189 else:
190 value = readvar(revlink, qvar, mappedpkg)
191 if qvar == "PKGSIZE":
192 # PKGSIZE is now in bytes, but we we want it in KB
193 pkgsize = (int(value) + 1024 // 2) // 1024
194 value = "%d" % pkgsize
195 if args.unescape:
196 import codecs
197 # escape_decode() unescapes backslash encodings in byte streams
198 value = codecs.escape_decode(bytes(value, "utf-8"))[0].decode("utf-8")
199 values.append(value)
200
201 values_str = ' '.join(values)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500202 if args.prefix_name:
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500203 print('%s %s' % (pkg_name, values_str))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500204 else:
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500205 print(values_str)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500206 else:
207 logger.debug("revlink %s does not exist", revlink)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500208
209def lookup_pkglist(pkgs, pkgdata_dir, reverse):
210 if reverse:
211 mappings = OrderedDict()
212 for pkg in pkgs:
213 revlink = os.path.join(pkgdata_dir, "runtime-reverse", pkg)
214 logger.debug(revlink)
215 if os.path.exists(revlink):
216 mappings[pkg] = os.path.basename(os.readlink(revlink))
217 else:
218 mappings = defaultdict(list)
219 for pkg in pkgs:
220 pkgfile = os.path.join(pkgdata_dir, 'runtime', pkg)
221 if os.path.exists(pkgfile):
222 with open(pkgfile, 'r') as f:
223 for line in f:
224 fields = line.rstrip().split(': ')
Patrick Williams213cb262021-08-07 19:21:33 -0500225 if fields[0] == 'PKG:%s' % pkg:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500226 mappings[pkg].append(fields[1])
227 break
228 return mappings
229
230def lookup_pkg(args):
231 # Handle both multiple arguments and multiple values within an arg (old syntax)
232 pkgs = []
233 for pkgitem in args.pkg:
234 pkgs.extend(pkgitem.split())
235
236 mappings = lookup_pkglist(pkgs, args.pkgdata_dir, args.reverse)
237
238 if len(mappings) < len(pkgs):
239 missing = list(set(pkgs) - set(mappings.keys()))
240 logger.error("The following packages could not be found: %s" % ', '.join(missing))
241 sys.exit(1)
242
243 if args.reverse:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600244 items = list(mappings.values())
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500245 else:
246 items = []
247 for pkg in pkgs:
248 items.extend(mappings.get(pkg, []))
249
250 print('\n'.join(items))
251
252def lookup_recipe(args):
Brad Bishop316dfdd2018-06-25 12:45:53 -0400253 def parse_pkgdatafile(pkgdatafile):
254 with open(pkgdatafile, 'r') as f:
255 found = False
256 for line in f:
257 if line.startswith('PN:'):
258 print("%s" % line.split(':', 1)[1].strip())
259 found = True
260 break
261 if not found:
262 logger.error("Unable to find PN entry in %s" % pkgdatafile)
263 sys.exit(1)
264
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500265 # Handle both multiple arguments and multiple values within an arg (old syntax)
266 pkgs = []
267 for pkgitem in args.pkg:
268 pkgs.extend(pkgitem.split())
269
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500270 for pkg in pkgs:
Brad Bishop316dfdd2018-06-25 12:45:53 -0400271 providepkgpath = os.path.join(args.pkgdata_dir, "runtime-rprovides", pkg)
272 if os.path.exists(providepkgpath):
273 for f in os.listdir(providepkgpath):
274 if f != pkg:
275 print("%s is in the RPROVIDES of %s:" % (pkg, f))
276 pkgdatafile = os.path.join(args.pkgdata_dir, "runtime", f)
277 parse_pkgdatafile(pkgdatafile)
Brad Bishopd5ae7d92018-06-14 09:52:03 -0700278 continue
Brad Bishop316dfdd2018-06-25 12:45:53 -0400279 pkgdatafile = os.path.join(args.pkgdata_dir, 'runtime-reverse', pkg)
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800280 if os.path.exists(pkgdatafile):
281 parse_pkgdatafile(pkgdatafile)
282 else:
283 if args.carryon:
284 print("The following packages could not be found: %s" % pkg)
285 else:
286 logger.error("The following packages could not be found: %s" % pkg)
287 sys.exit(1)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500288
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600289def package_info(args):
Brad Bishop316dfdd2018-06-25 12:45:53 -0400290 def parse_pkgdatafile(pkgdatafile):
Brad Bishopd5ae7d92018-06-14 09:52:03 -0700291 vars = ['PKGV', 'PKGE', 'PKGR', 'PN', 'PV', 'PE', 'PR', 'PKGSIZE']
292 if args.extra:
293 vars += args.extra
Brad Bishop316dfdd2018-06-25 12:45:53 -0400294 with open(pkgdatafile, 'r') as f:
Brad Bishopd5ae7d92018-06-14 09:52:03 -0700295 vals = dict()
296 extra = ''
Brad Bishop316dfdd2018-06-25 12:45:53 -0400297 for line in f:
Brad Bishopd5ae7d92018-06-14 09:52:03 -0700298 for var in vars:
Patrick Williams73bd93f2024-02-20 08:07:48 -0600299 m = re.match(var + r'(?::\S+)?:\s*(.+?)\s*$', line)
Brad Bishopd5ae7d92018-06-14 09:52:03 -0700300 if m:
301 vals[var] = m.group(1)
302 pkg_version = vals['PKGV'] or ''
303 recipe = vals['PN'] or ''
304 recipe_version = vals['PV'] or ''
305 pkg_size = vals['PKGSIZE'] or ''
306 if 'PKGE' in vals:
307 pkg_version = vals['PKGE'] + ":" + pkg_version
308 if 'PKGR' in vals:
309 pkg_version = pkg_version + "-" + vals['PKGR']
310 if 'PE' in vals:
311 recipe_version = vals['PE'] + ":" + recipe_version
312 if 'PR' in vals:
313 recipe_version = recipe_version + "-" + vals['PR']
314 if args.extra:
315 for var in args.extra:
316 if var in vals:
317 val = re.sub(r'\s+', ' ', vals[var])
318 extra += ' "%s"' % val
319 print("%s %s %s %s %s%s" % (pkg, pkg_version, recipe, recipe_version, pkg_size, extra))
Brad Bishop316dfdd2018-06-25 12:45:53 -0400320
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600321 # Handle both multiple arguments and multiple values within an arg (old syntax)
322 packages = []
323 if args.file:
324 with open(args.file, 'r') as f:
325 for line in f:
326 splitline = line.split()
327 if splitline:
328 packages.append(splitline[0])
329 else:
330 for pkgitem in args.pkg:
331 packages.extend(pkgitem.split())
332 if not packages:
333 logger.error("No packages specified")
334 sys.exit(1)
335
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600336 for pkg in packages:
Brad Bishop316dfdd2018-06-25 12:45:53 -0400337 providepkgpath = os.path.join(args.pkgdata_dir, "runtime-rprovides", pkg)
338 if os.path.exists(providepkgpath):
339 for f in os.listdir(providepkgpath):
340 if f != pkg:
341 print("%s is in the RPROVIDES of %s:" % (pkg, f))
342 pkgdatafile = os.path.join(args.pkgdata_dir, "runtime", f)
343 parse_pkgdatafile(pkgdatafile)
Brad Bishopd5ae7d92018-06-14 09:52:03 -0700344 continue
Brad Bishop316dfdd2018-06-25 12:45:53 -0400345 pkgdatafile = os.path.join(args.pkgdata_dir, "runtime-reverse", pkg)
346 if not os.path.exists(pkgdatafile):
347 logger.error("Unable to find any built runtime package named %s" % pkg)
348 sys.exit(1)
349 parse_pkgdatafile(pkgdatafile)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600350
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500351def get_recipe_pkgs(pkgdata_dir, recipe, unpackaged):
352 recipedatafile = os.path.join(pkgdata_dir, recipe)
353 if not os.path.exists(recipedatafile):
354 logger.error("Unable to find packaged recipe with name %s" % recipe)
355 sys.exit(1)
356 packages = []
357 with open(recipedatafile, 'r') as f:
358 for line in f:
359 fields = line.rstrip().split(': ')
360 if fields[0] == 'PACKAGES':
361 packages = fields[1].split()
362 break
363
364 if not unpackaged:
365 pkglist = []
366 for pkg in packages:
367 if os.path.exists(os.path.join(pkgdata_dir, 'runtime', '%s.packaged' % pkg)):
368 pkglist.append(pkg)
369 return pkglist
370 else:
371 return packages
372
373def list_pkgs(args):
374 found = False
375
376 def matchpkg(pkg):
377 if args.pkgspec:
378 matched = False
379 for pkgspec in args.pkgspec:
380 if fnmatch.fnmatchcase(pkg, pkgspec):
381 matched = True
382 break
383 if not matched:
384 return False
385 if not args.unpackaged:
386 if args.runtime:
387 revlink = os.path.join(args.pkgdata_dir, "runtime-reverse", pkg)
388 if os.path.exists(revlink):
389 # We're unlikely to get here if the package was not packaged, but just in case
390 # we add the symlinks for unpackaged files in the future
391 mappedpkg = os.path.basename(os.readlink(revlink))
392 if not os.path.exists(os.path.join(args.pkgdata_dir, 'runtime', '%s.packaged' % mappedpkg)):
393 return False
394 else:
395 return False
396 else:
397 if not os.path.exists(os.path.join(args.pkgdata_dir, 'runtime', '%s.packaged' % pkg)):
398 return False
399 return True
400
Brad Bishop64c979e2019-11-04 13:55:29 -0500401 pkglist = []
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500402 if args.recipe:
403 packages = get_recipe_pkgs(args.pkgdata_dir, args.recipe, args.unpackaged)
404
405 if args.runtime:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500406 runtime_pkgs = lookup_pkglist(packages, args.pkgdata_dir, False)
407 for rtpkgs in runtime_pkgs.values():
408 pkglist.extend(rtpkgs)
409 else:
410 pkglist = packages
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500411 else:
412 if args.runtime:
413 searchdir = 'runtime-reverse'
414 else:
415 searchdir = 'runtime'
416
417 for root, dirs, files in os.walk(os.path.join(args.pkgdata_dir, searchdir)):
418 for fn in files:
419 if fn.endswith('.packaged'):
420 continue
Brad Bishop64c979e2019-11-04 13:55:29 -0500421 pkglist.append(fn)
422
423 for pkg in sorted(pkglist):
424 if matchpkg(pkg):
425 found = True
426 print("%s" % pkg)
427
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500428 if not found:
429 if args.pkgspec:
430 logger.error("Unable to find any package matching %s" % args.pkgspec)
431 else:
432 logger.error("No packages found")
433 sys.exit(1)
434
435def list_pkg_files(args):
436 import json
Brad Bishop316dfdd2018-06-25 12:45:53 -0400437 def parse_pkgdatafile(pkgdatafile, long=False):
438 with open(pkgdatafile, 'r') as f:
439 found = False
440 for line in f:
441 if line.startswith('FILES_INFO:'):
442 found = True
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500443 val = line.split(': ', 1)[1].strip()
Brad Bishop316dfdd2018-06-25 12:45:53 -0400444 dictval = json.loads(val)
445 if long:
446 width = max(map(len, dictval), default=0)
447 for fullpth in sorted(dictval):
448 print("\t{:{width}}\t{}".format(fullpth, dictval[fullpth], width=width))
449 else:
450 for fullpth in sorted(dictval):
451 print("\t%s" % fullpth)
452 break
453 if not found:
454 logger.error("Unable to find FILES_INFO entry in %s" % pkgdatafile)
455 sys.exit(1)
456
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500457
458 if args.recipe:
459 if args.pkg:
460 logger.error("list-pkg-files: If -p/--recipe is specified then a package name cannot be specified")
461 sys.exit(1)
462 recipepkglist = get_recipe_pkgs(args.pkgdata_dir, args.recipe, args.unpackaged)
463 if args.runtime:
464 pkglist = []
465 runtime_pkgs = lookup_pkglist(recipepkglist, args.pkgdata_dir, False)
466 for rtpkgs in runtime_pkgs.values():
467 pkglist.extend(rtpkgs)
468 else:
469 pkglist = recipepkglist
470 else:
471 if not args.pkg:
472 logger.error("list-pkg-files: If -p/--recipe is not specified then at least one package name must be specified")
473 sys.exit(1)
474 pkglist = args.pkg
475
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500476 for pkg in sorted(pkglist):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500477 print("%s:" % pkg)
478 if args.runtime:
479 pkgdatafile = os.path.join(args.pkgdata_dir, "runtime-reverse", pkg)
480 if not os.path.exists(pkgdatafile):
481 if args.recipe:
482 # This package was empty and thus never packaged, ignore
483 continue
484 logger.error("Unable to find any built runtime package named %s" % pkg)
485 sys.exit(1)
Brad Bishop316dfdd2018-06-25 12:45:53 -0400486 parse_pkgdatafile(pkgdatafile, args.long)
487
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500488 else:
Brad Bishop316dfdd2018-06-25 12:45:53 -0400489 providepkgpath = os.path.join(args.pkgdata_dir, "runtime-rprovides", pkg)
490 if os.path.exists(providepkgpath):
491 for f in os.listdir(providepkgpath):
492 if f != pkg:
493 print("%s is in the RPROVIDES of %s:" % (pkg, f))
494 pkgdatafile = os.path.join(args.pkgdata_dir, "runtime", f)
495 parse_pkgdatafile(pkgdatafile, args.long)
496 continue
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500497 pkgdatafile = os.path.join(args.pkgdata_dir, "runtime", pkg)
498 if not os.path.exists(pkgdatafile):
499 logger.error("Unable to find any built recipe-space package named %s" % pkg)
500 sys.exit(1)
Brad Bishop316dfdd2018-06-25 12:45:53 -0400501 parse_pkgdatafile(pkgdatafile, args.long)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500502
503def find_path(args):
504 import json
505
506 found = False
507 for root, dirs, files in os.walk(os.path.join(args.pkgdata_dir, 'runtime')):
508 for fn in files:
509 with open(os.path.join(root,fn)) as f:
510 for line in f:
511 if line.startswith('FILES_INFO:'):
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500512 val = line.split(': ', 1)[1].strip()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500513 dictval = json.loads(val)
514 for fullpth in dictval.keys():
515 if fnmatch.fnmatchcase(fullpth, args.targetpath):
516 found = True
517 print("%s: %s" % (fn, fullpth))
518 break
519 if not found:
520 logger.error("Unable to find any package producing path %s" % args.targetpath)
521 sys.exit(1)
522
523
524def main():
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500525 parser = argparse_oe.ArgumentParser(description="OpenEmbedded pkgdata tool - queries the pkgdata files written out during do_package",
526 epilog="Use %(prog)s <subcommand> --help to get help on a specific command")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500527 parser.add_argument('-d', '--debug', help='Enable debug output', action='store_true')
528 parser.add_argument('-p', '--pkgdata-dir', help='Path to pkgdata directory (determined automatically if not specified)')
529 subparsers = parser.add_subparsers(title='subcommands', metavar='<subcommand>')
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600530 subparsers.required = True
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500531
532 parser_lookup_pkg = subparsers.add_parser('lookup-pkg',
533 help='Translate between recipe-space package names and runtime package names',
534 description='Looks up the specified recipe-space package name(s) to see what the final runtime package name is (e.g. glibc becomes libc6), or with -r/--reverse looks up the other way.')
535 parser_lookup_pkg.add_argument('pkg', nargs='+', help='Package name to look up')
536 parser_lookup_pkg.add_argument('-r', '--reverse', help='Switch to looking up recipe-space package names from runtime package names', action='store_true')
537 parser_lookup_pkg.set_defaults(func=lookup_pkg)
538
539 parser_list_pkgs = subparsers.add_parser('list-pkgs',
540 help='List packages',
541 description='Lists packages that have been built')
542 parser_list_pkgs.add_argument('pkgspec', nargs='*', help='Package name to search for (wildcards * ? allowed, use quotes to avoid shell expansion)')
543 parser_list_pkgs.add_argument('-r', '--runtime', help='Show runtime package names instead of recipe-space package names', action='store_true')
544 parser_list_pkgs.add_argument('-p', '--recipe', help='Limit to packages produced by the specified recipe')
545 parser_list_pkgs.add_argument('-u', '--unpackaged', help='Include unpackaged (i.e. empty) packages', action='store_true')
546 parser_list_pkgs.set_defaults(func=list_pkgs)
547
548 parser_list_pkg_files = subparsers.add_parser('list-pkg-files',
549 help='List files within a package',
550 description='Lists files included in one or more packages')
551 parser_list_pkg_files.add_argument('pkg', nargs='*', help='Package name to report on (if -p/--recipe is not specified)')
552 parser_list_pkg_files.add_argument('-r', '--runtime', help='Specified package(s) are runtime package names instead of recipe-space package names', action='store_true')
553 parser_list_pkg_files.add_argument('-p', '--recipe', help='Report on all packages produced by the specified recipe')
554 parser_list_pkg_files.add_argument('-u', '--unpackaged', help='Include unpackaged (i.e. empty) packages (only useful with -p/--recipe)', action='store_true')
Brad Bishop316dfdd2018-06-25 12:45:53 -0400555 parser_list_pkg_files.add_argument('-l', '--long', help='Show more information per file', action='store_true')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500556 parser_list_pkg_files.set_defaults(func=list_pkg_files)
557
558 parser_lookup_recipe = subparsers.add_parser('lookup-recipe',
559 help='Find recipe producing one or more packages',
560 description='Looks up the specified runtime package(s) to see which recipe they were produced by')
561 parser_lookup_recipe.add_argument('pkg', nargs='+', help='Runtime package name to look up')
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800562 parser_lookup_recipe.add_argument('-c', '--continue', dest="carryon", help='Continue looking up recipes even if we can not find one', action='store_true')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500563 parser_lookup_recipe.set_defaults(func=lookup_recipe)
564
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600565 parser_package_info = subparsers.add_parser('package-info',
Brad Bishop37a0e4d2017-12-04 01:01:44 -0500566 help='Show version, recipe and size information for one or more packages',
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600567 description='Looks up the specified runtime package(s) and display information')
568 parser_package_info.add_argument('pkg', nargs='*', help='Runtime package name to look up')
569 parser_package_info.add_argument('-f', '--file', help='Read package names from the specified file (one per line, first field only)')
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500570 parser_package_info.add_argument('-e', '--extra', help='Extra variables to display, e.g., LICENSE (can be specified multiple times)', action='append')
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600571 parser_package_info.set_defaults(func=package_info)
572
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500573 parser_find_path = subparsers.add_parser('find-path',
574 help='Find package providing a target path',
575 description='Finds the recipe-space package providing the specified target path')
576 parser_find_path.add_argument('targetpath', help='Path to find (wildcards * ? allowed, use quotes to avoid shell expansion)')
577 parser_find_path.set_defaults(func=find_path)
578
579 parser_read_value = subparsers.add_parser('read-value',
580 help='Read any pkgdata value for one or more packages',
581 description='Reads the named value from the pkgdata files for the specified packages')
Andrew Geisslerd159c7f2021-09-02 21:05:58 -0500582 parser_read_value.add_argument('valuenames', help='Name of the value/s to look up (separated by commas, no spaces)')
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500583 parser_read_value.add_argument('pkg', nargs='*', help='Runtime package name to look up')
584 parser_read_value.add_argument('-f', '--file', help='Read package names from the specified file (one per line, first field only)')
585 parser_read_value.add_argument('-n', '--prefix-name', help='Prefix output with package name', action='store_true')
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500586 parser_read_value.add_argument('-u', '--unescape', help='Expand escapes such as \\n', action='store_true')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500587 parser_read_value.set_defaults(func=read_value)
588
589 parser_glob = subparsers.add_parser('glob',
590 help='Expand package name glob expression',
591 description='Expands one or more glob expressions over the packages listed in pkglistfile')
592 parser_glob.add_argument('pkglistfile', help='File listing packages (one package name per line)')
593 parser_glob.add_argument('glob', nargs="+", help='Glob expression for package names, e.g. *-dev')
594 parser_glob.add_argument('-x', '--exclude', help='Exclude packages matching specified regex from the glob operation')
595 parser_glob.set_defaults(func=glob)
596
597
598 args = parser.parse_args()
599
600 if args.debug:
601 logger.setLevel(logging.DEBUG)
602
603 if not args.pkgdata_dir:
604 import scriptpath
605 bitbakepath = scriptpath.add_bitbake_lib_path()
606 if not bitbakepath:
607 logger.error("Unable to find bitbake by searching parent directory of this script or PATH")
608 sys.exit(1)
609 logger.debug('Found bitbake path: %s' % bitbakepath)
Andrew Geisslerd1e89492021-02-12 15:35:20 -0600610 if not os.environ.get('BUILDDIR', ''):
611 logger.error("This script can only be run after initialising the build environment (e.g. by using oe-init-build-env)")
612 sys.exit(1)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500613 tinfoil = tinfoil_init()
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600614 try:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500615 args.pkgdata_dir = tinfoil.config_data.getVar('PKGDATA_DIR')
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600616 finally:
617 tinfoil.shutdown()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500618 logger.debug('Value of PKGDATA_DIR is "%s"' % args.pkgdata_dir)
619 if not args.pkgdata_dir:
620 logger.error('Unable to determine pkgdata directory from PKGDATA_DIR')
621 sys.exit(1)
622
623 if not os.path.exists(args.pkgdata_dir):
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500624 logger.error('Unable to find pkgdata directory %s' % args.pkgdata_dir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500625 sys.exit(1)
626
627 ret = args.func(args)
628
629 return ret
630
631
632if __name__ == "__main__":
633 main()