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 | # |
| 9 | # This program is free software; you can redistribute it and/or modify |
| 10 | # it under the terms of the GNU General Public License version 2 as |
| 11 | # published by the Free Software Foundation. |
| 12 | # |
| 13 | # This program is distributed in the hope that it will be useful, |
| 14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | # GNU General Public License for more details. |
| 17 | # |
| 18 | # You should have received a copy of the GNU General Public License along |
| 19 | # with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | # |
| 22 | |
| 23 | import sys |
| 24 | import os |
| 25 | import os.path |
| 26 | import fnmatch |
| 27 | import re |
| 28 | import argparse |
| 29 | import logging |
| 30 | from collections import defaultdict, OrderedDict |
| 31 | |
| 32 | scripts_path = os.path.dirname(os.path.realpath(__file__)) |
| 33 | lib_path = scripts_path + '/lib' |
| 34 | sys.path = sys.path + [lib_path] |
| 35 | import scriptutils |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 36 | import argparse_oe |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 37 | logger = scriptutils.logger_create('pkgdatautil') |
| 38 | |
| 39 | def tinfoil_init(): |
| 40 | import bb.tinfoil |
| 41 | import logging |
| 42 | tinfoil = bb.tinfoil.Tinfoil() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 43 | tinfoil.logger.setLevel(logging.WARNING) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 44 | tinfoil.prepare(True) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 45 | return tinfoil |
| 46 | |
| 47 | |
| 48 | def glob(args): |
| 49 | # Handle both multiple arguments and multiple values within an arg (old syntax) |
| 50 | globs = [] |
| 51 | for globitem in args.glob: |
| 52 | globs.extend(globitem.split()) |
| 53 | |
| 54 | if not os.path.exists(args.pkglistfile): |
| 55 | logger.error('Unable to find package list file %s' % args.pkglistfile) |
| 56 | sys.exit(1) |
| 57 | |
| 58 | skipval = "-locale-|^locale-base-|-dev$|-doc$|-dbg$|-staticdev$|^kernel-module-" |
| 59 | if args.exclude: |
| 60 | skipval += "|" + args.exclude |
| 61 | skipregex = re.compile(skipval) |
| 62 | |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 63 | skippedpkgs = set() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 64 | mappedpkgs = set() |
| 65 | with open(args.pkglistfile, 'r') as f: |
| 66 | for line in f: |
| 67 | fields = line.rstrip().split() |
| 68 | if not fields: |
| 69 | continue |
| 70 | pkg = fields[0] |
| 71 | # We don't care about other args (used to need the package architecture but the |
| 72 | # new pkgdata structure avoids the need for that) |
| 73 | |
| 74 | # Skip packages for which there is no point applying globs |
| 75 | if skipregex.search(pkg): |
| 76 | logger.debug("%s -> !!" % pkg) |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 77 | skippedpkgs.add(pkg) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 78 | continue |
| 79 | |
| 80 | # Skip packages that already match the globs, so if e.g. a dev package |
| 81 | # is already installed and thus in the list, we don't process it any further |
| 82 | # Most of these will be caught by skipregex already, but just in case... |
| 83 | already = False |
| 84 | for g in globs: |
| 85 | if fnmatch.fnmatchcase(pkg, g): |
| 86 | already = True |
| 87 | break |
| 88 | if already: |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 89 | skippedpkgs.add(pkg) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 90 | logger.debug("%s -> !" % pkg) |
| 91 | continue |
| 92 | |
| 93 | # Define some functions |
| 94 | def revpkgdata(pkgn): |
| 95 | return os.path.join(args.pkgdata_dir, "runtime-reverse", pkgn) |
| 96 | def fwdpkgdata(pkgn): |
| 97 | return os.path.join(args.pkgdata_dir, "runtime", pkgn) |
| 98 | def readpn(pkgdata_file): |
| 99 | pn = "" |
| 100 | with open(pkgdata_file, 'r') as f: |
| 101 | for line in f: |
| 102 | if line.startswith("PN:"): |
| 103 | pn = line.split(': ')[1].rstrip() |
| 104 | return pn |
| 105 | def readrenamed(pkgdata_file): |
| 106 | renamed = "" |
| 107 | pn = os.path.basename(pkgdata_file) |
| 108 | with open(pkgdata_file, 'r') as f: |
| 109 | for line in f: |
| 110 | if line.startswith("PKG_%s:" % pn): |
| 111 | renamed = line.split(': ')[1].rstrip() |
| 112 | return renamed |
| 113 | |
| 114 | # Main processing loop |
| 115 | for g in globs: |
| 116 | mappedpkg = "" |
| 117 | # First just try substitution (i.e. packagename -> packagename-dev) |
| 118 | newpkg = g.replace("*", pkg) |
| 119 | revlink = revpkgdata(newpkg) |
| 120 | if os.path.exists(revlink): |
| 121 | mappedpkg = os.path.basename(os.readlink(revlink)) |
| 122 | fwdfile = fwdpkgdata(mappedpkg) |
| 123 | if os.path.exists(fwdfile): |
| 124 | mappedpkg = readrenamed(fwdfile) |
| 125 | if not os.path.exists(fwdfile + ".packaged"): |
| 126 | mappedpkg = "" |
| 127 | else: |
| 128 | revlink = revpkgdata(pkg) |
| 129 | if os.path.exists(revlink): |
| 130 | # Check if we can map after undoing the package renaming (by resolving the symlink) |
| 131 | origpkg = os.path.basename(os.readlink(revlink)) |
| 132 | newpkg = g.replace("*", origpkg) |
| 133 | fwdfile = fwdpkgdata(newpkg) |
| 134 | if os.path.exists(fwdfile): |
| 135 | mappedpkg = readrenamed(fwdfile) |
| 136 | else: |
| 137 | # That didn't work, so now get the PN, substitute that, then map in the other direction |
| 138 | pn = readpn(revlink) |
| 139 | newpkg = g.replace("*", pn) |
| 140 | fwdfile = fwdpkgdata(newpkg) |
| 141 | if os.path.exists(fwdfile): |
| 142 | mappedpkg = readrenamed(fwdfile) |
| 143 | if not os.path.exists(fwdfile + ".packaged"): |
| 144 | mappedpkg = "" |
| 145 | else: |
| 146 | # Package doesn't even exist... |
| 147 | logger.debug("%s is not a valid package!" % (pkg)) |
| 148 | break |
| 149 | |
| 150 | if mappedpkg: |
| 151 | logger.debug("%s (%s) -> %s" % (pkg, g, mappedpkg)) |
| 152 | mappedpkgs.add(mappedpkg) |
| 153 | else: |
| 154 | logger.debug("%s (%s) -> ?" % (pkg, g)) |
| 155 | |
| 156 | logger.debug("------") |
| 157 | |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 158 | print("\n".join(mappedpkgs - skippedpkgs)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 159 | |
| 160 | def read_value(args): |
| 161 | # Handle both multiple arguments and multiple values within an arg (old syntax) |
| 162 | packages = [] |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 163 | if args.file: |
| 164 | with open(args.file, 'r') as f: |
| 165 | for line in f: |
| 166 | splitline = line.split() |
| 167 | if splitline: |
| 168 | packages.append(splitline[0]) |
| 169 | else: |
| 170 | for pkgitem in args.pkg: |
| 171 | packages.extend(pkgitem.split()) |
| 172 | if not packages: |
| 173 | logger.error("No packages specified") |
| 174 | sys.exit(1) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 175 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 176 | def readvar(pkgdata_file, valuename, mappedpkg): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 177 | val = "" |
| 178 | with open(pkgdata_file, 'r') as f: |
| 179 | for line in f: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 180 | if (line.startswith(valuename + ":") or |
| 181 | line.startswith(valuename + "_" + mappedpkg + ":")): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 182 | val = line.split(': ', 1)[1].rstrip() |
| 183 | return val |
| 184 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 185 | logger.debug("read-value('%s', '%s' '%s')" % (args.pkgdata_dir, args.valuename, packages)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 186 | for package in packages: |
| 187 | pkg_split = package.split('_') |
| 188 | pkg_name = pkg_split[0] |
| 189 | logger.debug("package: '%s'" % pkg_name) |
| 190 | revlink = os.path.join(args.pkgdata_dir, "runtime-reverse", pkg_name) |
| 191 | logger.debug(revlink) |
| 192 | if os.path.exists(revlink): |
| 193 | mappedpkg = os.path.basename(os.readlink(revlink)) |
| 194 | qvar = args.valuename |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 195 | value = readvar(revlink, qvar, mappedpkg) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 196 | if qvar == "PKGSIZE": |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 197 | # PKGSIZE is now in bytes, but we we want it in KB |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 198 | pkgsize = (int(value) + 1024 // 2) // 1024 |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 199 | value = "%d" % pkgsize |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 200 | if args.unescape: |
| 201 | import codecs |
| 202 | # escape_decode() unescapes backslash encodings in byte streams |
| 203 | value = codecs.escape_decode(bytes(value, "utf-8"))[0].decode("utf-8") |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 204 | if args.prefix_name: |
| 205 | print('%s %s' % (pkg_name, value)) |
| 206 | else: |
| 207 | print(value) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 208 | else: |
| 209 | logger.debug("revlink %s does not exist", revlink) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 210 | |
| 211 | def lookup_pkglist(pkgs, pkgdata_dir, reverse): |
| 212 | if reverse: |
| 213 | mappings = OrderedDict() |
| 214 | for pkg in pkgs: |
| 215 | revlink = os.path.join(pkgdata_dir, "runtime-reverse", pkg) |
| 216 | logger.debug(revlink) |
| 217 | if os.path.exists(revlink): |
| 218 | mappings[pkg] = os.path.basename(os.readlink(revlink)) |
| 219 | else: |
| 220 | mappings = defaultdict(list) |
| 221 | for pkg in pkgs: |
| 222 | pkgfile = os.path.join(pkgdata_dir, 'runtime', pkg) |
| 223 | if os.path.exists(pkgfile): |
| 224 | with open(pkgfile, 'r') as f: |
| 225 | for line in f: |
| 226 | fields = line.rstrip().split(': ') |
| 227 | if fields[0] == 'PKG_%s' % pkg: |
| 228 | mappings[pkg].append(fields[1]) |
| 229 | break |
| 230 | return mappings |
| 231 | |
| 232 | def lookup_pkg(args): |
| 233 | # Handle both multiple arguments and multiple values within an arg (old syntax) |
| 234 | pkgs = [] |
| 235 | for pkgitem in args.pkg: |
| 236 | pkgs.extend(pkgitem.split()) |
| 237 | |
| 238 | mappings = lookup_pkglist(pkgs, args.pkgdata_dir, args.reverse) |
| 239 | |
| 240 | if len(mappings) < len(pkgs): |
| 241 | missing = list(set(pkgs) - set(mappings.keys())) |
| 242 | logger.error("The following packages could not be found: %s" % ', '.join(missing)) |
| 243 | sys.exit(1) |
| 244 | |
| 245 | if args.reverse: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 246 | items = list(mappings.values()) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 247 | else: |
| 248 | items = [] |
| 249 | for pkg in pkgs: |
| 250 | items.extend(mappings.get(pkg, [])) |
| 251 | |
| 252 | print('\n'.join(items)) |
| 253 | |
| 254 | def lookup_recipe(args): |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame^] | 255 | def parse_pkgdatafile(pkgdatafile): |
| 256 | with open(pkgdatafile, 'r') as f: |
| 257 | found = False |
| 258 | for line in f: |
| 259 | if line.startswith('PN:'): |
| 260 | print("%s" % line.split(':', 1)[1].strip()) |
| 261 | found = True |
| 262 | break |
| 263 | if not found: |
| 264 | logger.error("Unable to find PN entry in %s" % pkgdatafile) |
| 265 | sys.exit(1) |
| 266 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 267 | # Handle both multiple arguments and multiple values within an arg (old syntax) |
| 268 | pkgs = [] |
| 269 | for pkgitem in args.pkg: |
| 270 | pkgs.extend(pkgitem.split()) |
| 271 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 272 | for pkg in pkgs: |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame^] | 273 | providepkgpath = os.path.join(args.pkgdata_dir, "runtime-rprovides", pkg) |
| 274 | if os.path.exists(providepkgpath): |
| 275 | for f in os.listdir(providepkgpath): |
| 276 | if f != pkg: |
| 277 | print("%s is in the RPROVIDES of %s:" % (pkg, f)) |
| 278 | pkgdatafile = os.path.join(args.pkgdata_dir, "runtime", f) |
| 279 | parse_pkgdatafile(pkgdatafile) |
| 280 | break |
| 281 | pkgdatafile = os.path.join(args.pkgdata_dir, 'runtime-reverse', pkg) |
| 282 | if not os.path.exists(pkgdatafile): |
| 283 | logger.error("The following packages could not be found: %s" % pkg) |
| 284 | sys.exit(1) |
| 285 | parse_pkgdatafile(pkgdatafile) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 286 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 287 | def package_info(args): |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame^] | 288 | def parse_pkgdatafile(pkgdatafile): |
| 289 | with open(pkgdatafile, 'r') as f: |
| 290 | pkge = '' |
| 291 | pkgr = '' |
| 292 | pe = '' |
| 293 | pr = '' |
| 294 | for line in f: |
| 295 | if line.startswith('PKGV:'): |
| 296 | pkg_version = line.split(':', 1)[1].strip() |
| 297 | elif line.startswith('PKGE:'): |
| 298 | pkge = line.split(':', 1)[1].strip() |
| 299 | elif line.startswith('PKGR:'): |
| 300 | pkgr = line.split(':', 1)[1].strip() |
| 301 | elif line.startswith('PN:'): |
| 302 | recipe = line.split(':', 1)[1].strip() |
| 303 | elif line.startswith('PV:'): |
| 304 | recipe_version = line.split(':', 1)[1].strip() |
| 305 | elif line.startswith('PE:'): |
| 306 | pe = line.split(':', 1)[1].strip() |
| 307 | elif line.startswith('PR:'): |
| 308 | pr = line.split(':', 1)[1].strip() |
| 309 | elif line.startswith('PKGSIZE'): |
| 310 | pkg_size = line.split(':', 1)[1].strip() |
| 311 | if pkge: |
| 312 | pkg_version = pkge + ":" + pkg_version |
| 313 | if pkgr: |
| 314 | pkg_version = pkg_version + "-" + pkgr |
| 315 | if pe: |
| 316 | recipe_version = pe + ":" + recipe_version |
| 317 | if pr: |
| 318 | recipe_version = recipe_version + "-" + pr |
| 319 | print("%s %s %s %s %s" % (pkg, pkg_version, recipe, recipe_version, pkg_size)) |
| 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) |
| 344 | break |
| 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 | |
| 401 | if args.recipe: |
| 402 | packages = get_recipe_pkgs(args.pkgdata_dir, args.recipe, args.unpackaged) |
| 403 | |
| 404 | if args.runtime: |
| 405 | pkglist = [] |
| 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 |
| 411 | |
| 412 | for pkg in pkglist: |
| 413 | if matchpkg(pkg): |
| 414 | found = True |
| 415 | print("%s" % pkg) |
| 416 | else: |
| 417 | if args.runtime: |
| 418 | searchdir = 'runtime-reverse' |
| 419 | else: |
| 420 | searchdir = 'runtime' |
| 421 | |
| 422 | for root, dirs, files in os.walk(os.path.join(args.pkgdata_dir, searchdir)): |
| 423 | for fn in files: |
| 424 | if fn.endswith('.packaged'): |
| 425 | continue |
| 426 | if matchpkg(fn): |
| 427 | found = True |
| 428 | print("%s" % fn) |
| 429 | if not found: |
| 430 | if args.pkgspec: |
| 431 | logger.error("Unable to find any package matching %s" % args.pkgspec) |
| 432 | else: |
| 433 | logger.error("No packages found") |
| 434 | sys.exit(1) |
| 435 | |
| 436 | def list_pkg_files(args): |
| 437 | import json |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame^] | 438 | def parse_pkgdatafile(pkgdatafile, long=False): |
| 439 | with open(pkgdatafile, 'r') as f: |
| 440 | found = False |
| 441 | for line in f: |
| 442 | if line.startswith('FILES_INFO:'): |
| 443 | found = True |
| 444 | val = line.split(':', 1)[1].strip() |
| 445 | dictval = json.loads(val) |
| 446 | if long: |
| 447 | width = max(map(len, dictval), default=0) |
| 448 | for fullpth in sorted(dictval): |
| 449 | print("\t{:{width}}\t{}".format(fullpth, dictval[fullpth], width=width)) |
| 450 | else: |
| 451 | for fullpth in sorted(dictval): |
| 452 | print("\t%s" % fullpth) |
| 453 | break |
| 454 | if not found: |
| 455 | logger.error("Unable to find FILES_INFO entry in %s" % pkgdatafile) |
| 456 | sys.exit(1) |
| 457 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 458 | |
| 459 | if args.recipe: |
| 460 | if args.pkg: |
| 461 | logger.error("list-pkg-files: If -p/--recipe is specified then a package name cannot be specified") |
| 462 | sys.exit(1) |
| 463 | recipepkglist = get_recipe_pkgs(args.pkgdata_dir, args.recipe, args.unpackaged) |
| 464 | if args.runtime: |
| 465 | pkglist = [] |
| 466 | runtime_pkgs = lookup_pkglist(recipepkglist, args.pkgdata_dir, False) |
| 467 | for rtpkgs in runtime_pkgs.values(): |
| 468 | pkglist.extend(rtpkgs) |
| 469 | else: |
| 470 | pkglist = recipepkglist |
| 471 | else: |
| 472 | if not args.pkg: |
| 473 | logger.error("list-pkg-files: If -p/--recipe is not specified then at least one package name must be specified") |
| 474 | sys.exit(1) |
| 475 | pkglist = args.pkg |
| 476 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 477 | for pkg in sorted(pkglist): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 478 | print("%s:" % pkg) |
| 479 | if args.runtime: |
| 480 | pkgdatafile = os.path.join(args.pkgdata_dir, "runtime-reverse", pkg) |
| 481 | if not os.path.exists(pkgdatafile): |
| 482 | if args.recipe: |
| 483 | # This package was empty and thus never packaged, ignore |
| 484 | continue |
| 485 | logger.error("Unable to find any built runtime package named %s" % pkg) |
| 486 | sys.exit(1) |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame^] | 487 | parse_pkgdatafile(pkgdatafile, args.long) |
| 488 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 489 | else: |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame^] | 490 | providepkgpath = os.path.join(args.pkgdata_dir, "runtime-rprovides", pkg) |
| 491 | if os.path.exists(providepkgpath): |
| 492 | for f in os.listdir(providepkgpath): |
| 493 | if f != pkg: |
| 494 | print("%s is in the RPROVIDES of %s:" % (pkg, f)) |
| 495 | pkgdatafile = os.path.join(args.pkgdata_dir, "runtime", f) |
| 496 | parse_pkgdatafile(pkgdatafile, args.long) |
| 497 | continue |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 498 | pkgdatafile = os.path.join(args.pkgdata_dir, "runtime", pkg) |
| 499 | if not os.path.exists(pkgdatafile): |
| 500 | logger.error("Unable to find any built recipe-space package named %s" % pkg) |
| 501 | sys.exit(1) |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame^] | 502 | parse_pkgdatafile(pkgdatafile, args.long) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 503 | |
| 504 | def find_path(args): |
| 505 | import json |
| 506 | |
| 507 | found = False |
| 508 | for root, dirs, files in os.walk(os.path.join(args.pkgdata_dir, 'runtime')): |
| 509 | for fn in files: |
| 510 | with open(os.path.join(root,fn)) as f: |
| 511 | for line in f: |
| 512 | if line.startswith('FILES_INFO:'): |
| 513 | val = line.split(':', 1)[1].strip() |
| 514 | dictval = json.loads(val) |
| 515 | for fullpth in dictval.keys(): |
| 516 | if fnmatch.fnmatchcase(fullpth, args.targetpath): |
| 517 | found = True |
| 518 | print("%s: %s" % (fn, fullpth)) |
| 519 | break |
| 520 | if not found: |
| 521 | logger.error("Unable to find any package producing path %s" % args.targetpath) |
| 522 | sys.exit(1) |
| 523 | |
| 524 | |
| 525 | def main(): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 526 | parser = argparse_oe.ArgumentParser(description="OpenEmbedded pkgdata tool - queries the pkgdata files written out during do_package", |
| 527 | 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] | 528 | parser.add_argument('-d', '--debug', help='Enable debug output', action='store_true') |
| 529 | parser.add_argument('-p', '--pkgdata-dir', help='Path to pkgdata directory (determined automatically if not specified)') |
| 530 | subparsers = parser.add_subparsers(title='subcommands', metavar='<subcommand>') |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 531 | subparsers.required = True |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 532 | |
| 533 | parser_lookup_pkg = subparsers.add_parser('lookup-pkg', |
| 534 | help='Translate between recipe-space package names and runtime package names', |
| 535 | 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.') |
| 536 | parser_lookup_pkg.add_argument('pkg', nargs='+', help='Package name to look up') |
| 537 | parser_lookup_pkg.add_argument('-r', '--reverse', help='Switch to looking up recipe-space package names from runtime package names', action='store_true') |
| 538 | parser_lookup_pkg.set_defaults(func=lookup_pkg) |
| 539 | |
| 540 | parser_list_pkgs = subparsers.add_parser('list-pkgs', |
| 541 | help='List packages', |
| 542 | description='Lists packages that have been built') |
| 543 | parser_list_pkgs.add_argument('pkgspec', nargs='*', help='Package name to search for (wildcards * ? allowed, use quotes to avoid shell expansion)') |
| 544 | parser_list_pkgs.add_argument('-r', '--runtime', help='Show runtime package names instead of recipe-space package names', action='store_true') |
| 545 | parser_list_pkgs.add_argument('-p', '--recipe', help='Limit to packages produced by the specified recipe') |
| 546 | parser_list_pkgs.add_argument('-u', '--unpackaged', help='Include unpackaged (i.e. empty) packages', action='store_true') |
| 547 | parser_list_pkgs.set_defaults(func=list_pkgs) |
| 548 | |
| 549 | parser_list_pkg_files = subparsers.add_parser('list-pkg-files', |
| 550 | help='List files within a package', |
| 551 | description='Lists files included in one or more packages') |
| 552 | parser_list_pkg_files.add_argument('pkg', nargs='*', help='Package name to report on (if -p/--recipe is not specified)') |
| 553 | 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') |
| 554 | parser_list_pkg_files.add_argument('-p', '--recipe', help='Report on all packages produced by the specified recipe') |
| 555 | 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^] | 556 | 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] | 557 | parser_list_pkg_files.set_defaults(func=list_pkg_files) |
| 558 | |
| 559 | parser_lookup_recipe = subparsers.add_parser('lookup-recipe', |
| 560 | help='Find recipe producing one or more packages', |
| 561 | description='Looks up the specified runtime package(s) to see which recipe they were produced by') |
| 562 | parser_lookup_recipe.add_argument('pkg', nargs='+', help='Runtime package name to look up') |
| 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') |
| 582 | parser_read_value.add_argument('valuename', help='Name of the value to look up') |
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) |
| 610 | tinfoil = tinfoil_init() |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 611 | try: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 612 | args.pkgdata_dir = tinfoil.config_data.getVar('PKGDATA_DIR') |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 613 | finally: |
| 614 | tinfoil.shutdown() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 615 | logger.debug('Value of PKGDATA_DIR is "%s"' % args.pkgdata_dir) |
| 616 | if not args.pkgdata_dir: |
| 617 | logger.error('Unable to determine pkgdata directory from PKGDATA_DIR') |
| 618 | sys.exit(1) |
| 619 | |
| 620 | if not os.path.exists(args.pkgdata_dir): |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 621 | logger.error('Unable to find pkgdata directory %s' % args.pkgdata_dir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 622 | sys.exit(1) |
| 623 | |
| 624 | ret = args.func(args) |
| 625 | |
| 626 | return ret |
| 627 | |
| 628 | |
| 629 | if __name__ == "__main__": |
| 630 | main() |