Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 2 | |
| 3 | # OpenEmbedded pkgdata utility |
| 4 | # |
| 5 | # Written by: Paul Eggleton <paul.eggleton@linux.intel.com> |
| 6 | # |
| 7 | # Copyright 2012-2015 Intel Corporation |
| 8 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 9 | # SPDX-License-Identifier: GPL-2.0-only |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 10 | # |
| 11 | |
| 12 | import sys |
| 13 | import os |
| 14 | import os.path |
| 15 | import fnmatch |
| 16 | import re |
| 17 | import argparse |
| 18 | import logging |
| 19 | from collections import defaultdict, OrderedDict |
| 20 | |
| 21 | scripts_path = os.path.dirname(os.path.realpath(__file__)) |
| 22 | lib_path = scripts_path + '/lib' |
| 23 | sys.path = sys.path + [lib_path] |
| 24 | import scriptutils |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 25 | import argparse_oe |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 26 | logger = scriptutils.logger_create('pkgdatautil') |
| 27 | |
| 28 | def tinfoil_init(): |
| 29 | import bb.tinfoil |
| 30 | import logging |
| 31 | tinfoil = bb.tinfoil.Tinfoil() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 32 | tinfoil.logger.setLevel(logging.WARNING) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 33 | tinfoil.prepare(True) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 34 | return tinfoil |
| 35 | |
| 36 | |
| 37 | def 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 Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 52 | skippedpkgs = set() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 53 | 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 Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 66 | skippedpkgs.add(pkg) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 67 | 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 Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 78 | skippedpkgs.add(pkg) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 79 | 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 Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 99 | if line.startswith("PKG:%s:" % pn): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 100 | 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 Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 147 | print("\n".join(mappedpkgs - skippedpkgs)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 148 | |
| 149 | def read_value(args): |
| 150 | # Handle both multiple arguments and multiple values within an arg (old syntax) |
| 151 | packages = [] |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 152 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 164 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 165 | def readvar(pkgdata_file, valuename, mappedpkg): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 166 | val = "" |
| 167 | with open(pkgdata_file, 'r') as f: |
| 168 | for line in f: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 169 | if (line.startswith(valuename + ":") or |
| 170 | line.startswith(valuename + "_" + mappedpkg + ":")): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 171 | val = line.split(': ', 1)[1].rstrip() |
| 172 | return val |
| 173 | |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 174 | logger.debug("read-value('%s', '%s' '%s')" % (args.pkgdata_dir, args.valuenames, packages)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 175 | 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 Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 183 | 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 Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 202 | if args.prefix_name: |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 203 | print('%s %s' % (pkg_name, values_str)) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 204 | else: |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 205 | print(values_str) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 206 | else: |
| 207 | logger.debug("revlink %s does not exist", revlink) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 208 | |
| 209 | def 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 Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 225 | if fields[0] == 'PKG:%s' % pkg: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 226 | mappings[pkg].append(fields[1]) |
| 227 | break |
| 228 | return mappings |
| 229 | |
| 230 | def 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 Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 244 | items = list(mappings.values()) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 245 | else: |
| 246 | items = [] |
| 247 | for pkg in pkgs: |
| 248 | items.extend(mappings.get(pkg, [])) |
| 249 | |
| 250 | print('\n'.join(items)) |
| 251 | |
| 252 | def lookup_recipe(args): |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 253 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 265 | # 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 270 | for pkg in pkgs: |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 271 | 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 Bishop | d5ae7d9 | 2018-06-14 09:52:03 -0700 | [diff] [blame] | 278 | continue |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 279 | pkgdatafile = os.path.join(args.pkgdata_dir, 'runtime-reverse', pkg) |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 280 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 288 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 289 | def package_info(args): |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 290 | def parse_pkgdatafile(pkgdatafile): |
Brad Bishop | d5ae7d9 | 2018-06-14 09:52:03 -0700 | [diff] [blame] | 291 | vars = ['PKGV', 'PKGE', 'PKGR', 'PN', 'PV', 'PE', 'PR', 'PKGSIZE'] |
| 292 | if args.extra: |
| 293 | vars += args.extra |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 294 | with open(pkgdatafile, 'r') as f: |
Brad Bishop | d5ae7d9 | 2018-06-14 09:52:03 -0700 | [diff] [blame] | 295 | vals = dict() |
| 296 | extra = '' |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 297 | for line in f: |
Brad Bishop | d5ae7d9 | 2018-06-14 09:52:03 -0700 | [diff] [blame] | 298 | for var in vars: |
Patrick Williams | 73bd93f | 2024-02-20 08:07:48 -0600 | [diff] [blame^] | 299 | m = re.match(var + r'(?::\S+)?:\s*(.+?)\s*$', line) |
Brad Bishop | d5ae7d9 | 2018-06-14 09:52:03 -0700 | [diff] [blame] | 300 | 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 Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 320 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 321 | # 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 Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 336 | for pkg in packages: |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 337 | 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 Bishop | d5ae7d9 | 2018-06-14 09:52:03 -0700 | [diff] [blame] | 344 | continue |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 345 | 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 Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 350 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 351 | def 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 | |
| 373 | def 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 Bishop | 64c979e | 2019-11-04 13:55:29 -0500 | [diff] [blame] | 401 | pkglist = [] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 402 | if args.recipe: |
| 403 | packages = get_recipe_pkgs(args.pkgdata_dir, args.recipe, args.unpackaged) |
| 404 | |
| 405 | if args.runtime: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 406 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 411 | 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 Bishop | 64c979e | 2019-11-04 13:55:29 -0500 | [diff] [blame] | 421 | pkglist.append(fn) |
| 422 | |
| 423 | for pkg in sorted(pkglist): |
| 424 | if matchpkg(pkg): |
| 425 | found = True |
| 426 | print("%s" % pkg) |
| 427 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 428 | 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 | |
| 435 | def list_pkg_files(args): |
| 436 | import json |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 437 | 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 Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 443 | val = line.split(': ', 1)[1].strip() |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 444 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 457 | |
| 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 Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 476 | for pkg in sorted(pkglist): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 477 | 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 Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 486 | parse_pkgdatafile(pkgdatafile, args.long) |
| 487 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 488 | else: |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 489 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 497 | 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 Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 501 | parse_pkgdatafile(pkgdatafile, args.long) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 502 | |
| 503 | def 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 Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 512 | val = line.split(': ', 1)[1].strip() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 513 | 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 | |
| 524 | def main(): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 525 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 527 | 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 Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 530 | subparsers.required = True |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 531 | |
| 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 Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 555 | parser_list_pkg_files.add_argument('-l', '--long', help='Show more information per file', action='store_true') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 556 | 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 Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 562 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 563 | parser_lookup_recipe.set_defaults(func=lookup_recipe) |
| 564 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 565 | parser_package_info = subparsers.add_parser('package-info', |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 566 | help='Show version, recipe and size information for one or more packages', |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 567 | 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 Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 570 | parser_package_info.add_argument('-e', '--extra', help='Extra variables to display, e.g., LICENSE (can be specified multiple times)', action='append') |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 571 | parser_package_info.set_defaults(func=package_info) |
| 572 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 573 | 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 Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 582 | parser_read_value.add_argument('valuenames', help='Name of the value/s to look up (separated by commas, no spaces)') |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 583 | 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 Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 586 | parser_read_value.add_argument('-u', '--unescape', help='Expand escapes such as \\n', action='store_true') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 587 | 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 Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 610 | 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 Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 613 | tinfoil = tinfoil_init() |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 614 | try: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 615 | args.pkgdata_dir = tinfoil.config_data.getVar('PKGDATA_DIR') |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 616 | finally: |
| 617 | tinfoil.shutdown() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 618 | 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 Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 624 | logger.error('Unable to find pkgdata directory %s' % args.pkgdata_dir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 625 | sys.exit(1) |
| 626 | |
| 627 | ret = args.func(args) |
| 628 | |
| 629 | return ret |
| 630 | |
| 631 | |
| 632 | if __name__ == "__main__": |
| 633 | main() |