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) |
Brad Bishop | d5ae7d9 | 2018-06-14 09:52:03 -0700 | [diff] [blame] | 280 | continue |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 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): |
Brad Bishop | d5ae7d9 | 2018-06-14 09:52:03 -0700 | [diff] [blame] | 289 | vars = ['PKGV', 'PKGE', 'PKGR', 'PN', 'PV', 'PE', 'PR', 'PKGSIZE'] |
| 290 | if args.extra: |
| 291 | vars += args.extra |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 292 | with open(pkgdatafile, 'r') as f: |
Brad Bishop | d5ae7d9 | 2018-06-14 09:52:03 -0700 | [diff] [blame] | 293 | vals = dict() |
| 294 | extra = '' |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 295 | for line in f: |
Brad Bishop | d5ae7d9 | 2018-06-14 09:52:03 -0700 | [diff] [blame] | 296 | for var in vars: |
| 297 | m = re.match(var + '(?:_\S+)?:\s*(.+?)\s*$', line) |
| 298 | if m: |
| 299 | vals[var] = m.group(1) |
| 300 | pkg_version = vals['PKGV'] or '' |
| 301 | recipe = vals['PN'] or '' |
| 302 | recipe_version = vals['PV'] or '' |
| 303 | pkg_size = vals['PKGSIZE'] or '' |
| 304 | if 'PKGE' in vals: |
| 305 | pkg_version = vals['PKGE'] + ":" + pkg_version |
| 306 | if 'PKGR' in vals: |
| 307 | pkg_version = pkg_version + "-" + vals['PKGR'] |
| 308 | if 'PE' in vals: |
| 309 | recipe_version = vals['PE'] + ":" + recipe_version |
| 310 | if 'PR' in vals: |
| 311 | recipe_version = recipe_version + "-" + vals['PR'] |
| 312 | if args.extra: |
| 313 | for var in args.extra: |
| 314 | if var in vals: |
| 315 | val = re.sub(r'\s+', ' ', vals[var]) |
| 316 | extra += ' "%s"' % val |
| 317 | 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] | 318 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 319 | # Handle both multiple arguments and multiple values within an arg (old syntax) |
| 320 | packages = [] |
| 321 | if args.file: |
| 322 | with open(args.file, 'r') as f: |
| 323 | for line in f: |
| 324 | splitline = line.split() |
| 325 | if splitline: |
| 326 | packages.append(splitline[0]) |
| 327 | else: |
| 328 | for pkgitem in args.pkg: |
| 329 | packages.extend(pkgitem.split()) |
| 330 | if not packages: |
| 331 | logger.error("No packages specified") |
| 332 | sys.exit(1) |
| 333 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 334 | for pkg in packages: |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 335 | providepkgpath = os.path.join(args.pkgdata_dir, "runtime-rprovides", pkg) |
| 336 | if os.path.exists(providepkgpath): |
| 337 | for f in os.listdir(providepkgpath): |
| 338 | if f != pkg: |
| 339 | print("%s is in the RPROVIDES of %s:" % (pkg, f)) |
| 340 | pkgdatafile = os.path.join(args.pkgdata_dir, "runtime", f) |
| 341 | parse_pkgdatafile(pkgdatafile) |
Brad Bishop | d5ae7d9 | 2018-06-14 09:52:03 -0700 | [diff] [blame] | 342 | continue |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 343 | pkgdatafile = os.path.join(args.pkgdata_dir, "runtime-reverse", pkg) |
| 344 | if not os.path.exists(pkgdatafile): |
| 345 | logger.error("Unable to find any built runtime package named %s" % pkg) |
| 346 | sys.exit(1) |
| 347 | parse_pkgdatafile(pkgdatafile) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 348 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 349 | def get_recipe_pkgs(pkgdata_dir, recipe, unpackaged): |
| 350 | recipedatafile = os.path.join(pkgdata_dir, recipe) |
| 351 | if not os.path.exists(recipedatafile): |
| 352 | logger.error("Unable to find packaged recipe with name %s" % recipe) |
| 353 | sys.exit(1) |
| 354 | packages = [] |
| 355 | with open(recipedatafile, 'r') as f: |
| 356 | for line in f: |
| 357 | fields = line.rstrip().split(': ') |
| 358 | if fields[0] == 'PACKAGES': |
| 359 | packages = fields[1].split() |
| 360 | break |
| 361 | |
| 362 | if not unpackaged: |
| 363 | pkglist = [] |
| 364 | for pkg in packages: |
| 365 | if os.path.exists(os.path.join(pkgdata_dir, 'runtime', '%s.packaged' % pkg)): |
| 366 | pkglist.append(pkg) |
| 367 | return pkglist |
| 368 | else: |
| 369 | return packages |
| 370 | |
| 371 | def list_pkgs(args): |
| 372 | found = False |
| 373 | |
| 374 | def matchpkg(pkg): |
| 375 | if args.pkgspec: |
| 376 | matched = False |
| 377 | for pkgspec in args.pkgspec: |
| 378 | if fnmatch.fnmatchcase(pkg, pkgspec): |
| 379 | matched = True |
| 380 | break |
| 381 | if not matched: |
| 382 | return False |
| 383 | if not args.unpackaged: |
| 384 | if args.runtime: |
| 385 | revlink = os.path.join(args.pkgdata_dir, "runtime-reverse", pkg) |
| 386 | if os.path.exists(revlink): |
| 387 | # We're unlikely to get here if the package was not packaged, but just in case |
| 388 | # we add the symlinks for unpackaged files in the future |
| 389 | mappedpkg = os.path.basename(os.readlink(revlink)) |
| 390 | if not os.path.exists(os.path.join(args.pkgdata_dir, 'runtime', '%s.packaged' % mappedpkg)): |
| 391 | return False |
| 392 | else: |
| 393 | return False |
| 394 | else: |
| 395 | if not os.path.exists(os.path.join(args.pkgdata_dir, 'runtime', '%s.packaged' % pkg)): |
| 396 | return False |
| 397 | return True |
| 398 | |
| 399 | if args.recipe: |
| 400 | packages = get_recipe_pkgs(args.pkgdata_dir, args.recipe, args.unpackaged) |
| 401 | |
| 402 | if args.runtime: |
| 403 | pkglist = [] |
| 404 | runtime_pkgs = lookup_pkglist(packages, args.pkgdata_dir, False) |
| 405 | for rtpkgs in runtime_pkgs.values(): |
| 406 | pkglist.extend(rtpkgs) |
| 407 | else: |
| 408 | pkglist = packages |
| 409 | |
| 410 | for pkg in pkglist: |
| 411 | if matchpkg(pkg): |
| 412 | found = True |
| 413 | print("%s" % pkg) |
| 414 | else: |
| 415 | if args.runtime: |
| 416 | searchdir = 'runtime-reverse' |
| 417 | else: |
| 418 | searchdir = 'runtime' |
| 419 | |
| 420 | for root, dirs, files in os.walk(os.path.join(args.pkgdata_dir, searchdir)): |
| 421 | for fn in files: |
| 422 | if fn.endswith('.packaged'): |
| 423 | continue |
| 424 | if matchpkg(fn): |
| 425 | found = True |
| 426 | print("%s" % fn) |
| 427 | if not found: |
| 428 | if args.pkgspec: |
| 429 | logger.error("Unable to find any package matching %s" % args.pkgspec) |
| 430 | else: |
| 431 | logger.error("No packages found") |
| 432 | sys.exit(1) |
| 433 | |
| 434 | def list_pkg_files(args): |
| 435 | import json |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 436 | def parse_pkgdatafile(pkgdatafile, long=False): |
| 437 | with open(pkgdatafile, 'r') as f: |
| 438 | found = False |
| 439 | for line in f: |
| 440 | if line.startswith('FILES_INFO:'): |
| 441 | found = True |
| 442 | val = line.split(':', 1)[1].strip() |
| 443 | dictval = json.loads(val) |
| 444 | if long: |
| 445 | width = max(map(len, dictval), default=0) |
| 446 | for fullpth in sorted(dictval): |
| 447 | print("\t{:{width}}\t{}".format(fullpth, dictval[fullpth], width=width)) |
| 448 | else: |
| 449 | for fullpth in sorted(dictval): |
| 450 | print("\t%s" % fullpth) |
| 451 | break |
| 452 | if not found: |
| 453 | logger.error("Unable to find FILES_INFO entry in %s" % pkgdatafile) |
| 454 | sys.exit(1) |
| 455 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 456 | |
| 457 | if args.recipe: |
| 458 | if args.pkg: |
| 459 | logger.error("list-pkg-files: If -p/--recipe is specified then a package name cannot be specified") |
| 460 | sys.exit(1) |
| 461 | recipepkglist = get_recipe_pkgs(args.pkgdata_dir, args.recipe, args.unpackaged) |
| 462 | if args.runtime: |
| 463 | pkglist = [] |
| 464 | runtime_pkgs = lookup_pkglist(recipepkglist, args.pkgdata_dir, False) |
| 465 | for rtpkgs in runtime_pkgs.values(): |
| 466 | pkglist.extend(rtpkgs) |
| 467 | else: |
| 468 | pkglist = recipepkglist |
| 469 | else: |
| 470 | if not args.pkg: |
| 471 | logger.error("list-pkg-files: If -p/--recipe is not specified then at least one package name must be specified") |
| 472 | sys.exit(1) |
| 473 | pkglist = args.pkg |
| 474 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 475 | for pkg in sorted(pkglist): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 476 | print("%s:" % pkg) |
| 477 | if args.runtime: |
| 478 | pkgdatafile = os.path.join(args.pkgdata_dir, "runtime-reverse", pkg) |
| 479 | if not os.path.exists(pkgdatafile): |
| 480 | if args.recipe: |
| 481 | # This package was empty and thus never packaged, ignore |
| 482 | continue |
| 483 | logger.error("Unable to find any built runtime package named %s" % pkg) |
| 484 | sys.exit(1) |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 485 | parse_pkgdatafile(pkgdatafile, args.long) |
| 486 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 487 | else: |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 488 | providepkgpath = os.path.join(args.pkgdata_dir, "runtime-rprovides", pkg) |
| 489 | if os.path.exists(providepkgpath): |
| 490 | for f in os.listdir(providepkgpath): |
| 491 | if f != pkg: |
| 492 | print("%s is in the RPROVIDES of %s:" % (pkg, f)) |
| 493 | pkgdatafile = os.path.join(args.pkgdata_dir, "runtime", f) |
| 494 | parse_pkgdatafile(pkgdatafile, args.long) |
| 495 | continue |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 496 | pkgdatafile = os.path.join(args.pkgdata_dir, "runtime", pkg) |
| 497 | if not os.path.exists(pkgdatafile): |
| 498 | logger.error("Unable to find any built recipe-space package named %s" % pkg) |
| 499 | sys.exit(1) |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 500 | parse_pkgdatafile(pkgdatafile, args.long) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 501 | |
| 502 | def find_path(args): |
| 503 | import json |
| 504 | |
| 505 | found = False |
| 506 | for root, dirs, files in os.walk(os.path.join(args.pkgdata_dir, 'runtime')): |
| 507 | for fn in files: |
| 508 | with open(os.path.join(root,fn)) as f: |
| 509 | for line in f: |
| 510 | if line.startswith('FILES_INFO:'): |
| 511 | val = line.split(':', 1)[1].strip() |
| 512 | dictval = json.loads(val) |
| 513 | for fullpth in dictval.keys(): |
| 514 | if fnmatch.fnmatchcase(fullpth, args.targetpath): |
| 515 | found = True |
| 516 | print("%s: %s" % (fn, fullpth)) |
| 517 | break |
| 518 | if not found: |
| 519 | logger.error("Unable to find any package producing path %s" % args.targetpath) |
| 520 | sys.exit(1) |
| 521 | |
| 522 | |
| 523 | def main(): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 524 | parser = argparse_oe.ArgumentParser(description="OpenEmbedded pkgdata tool - queries the pkgdata files written out during do_package", |
| 525 | 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] | 526 | parser.add_argument('-d', '--debug', help='Enable debug output', action='store_true') |
| 527 | parser.add_argument('-p', '--pkgdata-dir', help='Path to pkgdata directory (determined automatically if not specified)') |
| 528 | subparsers = parser.add_subparsers(title='subcommands', metavar='<subcommand>') |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 529 | subparsers.required = True |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 530 | |
| 531 | parser_lookup_pkg = subparsers.add_parser('lookup-pkg', |
| 532 | help='Translate between recipe-space package names and runtime package names', |
| 533 | 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.') |
| 534 | parser_lookup_pkg.add_argument('pkg', nargs='+', help='Package name to look up') |
| 535 | parser_lookup_pkg.add_argument('-r', '--reverse', help='Switch to looking up recipe-space package names from runtime package names', action='store_true') |
| 536 | parser_lookup_pkg.set_defaults(func=lookup_pkg) |
| 537 | |
| 538 | parser_list_pkgs = subparsers.add_parser('list-pkgs', |
| 539 | help='List packages', |
| 540 | description='Lists packages that have been built') |
| 541 | parser_list_pkgs.add_argument('pkgspec', nargs='*', help='Package name to search for (wildcards * ? allowed, use quotes to avoid shell expansion)') |
| 542 | parser_list_pkgs.add_argument('-r', '--runtime', help='Show runtime package names instead of recipe-space package names', action='store_true') |
| 543 | parser_list_pkgs.add_argument('-p', '--recipe', help='Limit to packages produced by the specified recipe') |
| 544 | parser_list_pkgs.add_argument('-u', '--unpackaged', help='Include unpackaged (i.e. empty) packages', action='store_true') |
| 545 | parser_list_pkgs.set_defaults(func=list_pkgs) |
| 546 | |
| 547 | parser_list_pkg_files = subparsers.add_parser('list-pkg-files', |
| 548 | help='List files within a package', |
| 549 | description='Lists files included in one or more packages') |
| 550 | parser_list_pkg_files.add_argument('pkg', nargs='*', help='Package name to report on (if -p/--recipe is not specified)') |
| 551 | 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') |
| 552 | parser_list_pkg_files.add_argument('-p', '--recipe', help='Report on all packages produced by the specified recipe') |
| 553 | 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] | 554 | 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] | 555 | parser_list_pkg_files.set_defaults(func=list_pkg_files) |
| 556 | |
| 557 | parser_lookup_recipe = subparsers.add_parser('lookup-recipe', |
| 558 | help='Find recipe producing one or more packages', |
| 559 | description='Looks up the specified runtime package(s) to see which recipe they were produced by') |
| 560 | parser_lookup_recipe.add_argument('pkg', nargs='+', help='Runtime package name to look up') |
| 561 | parser_lookup_recipe.set_defaults(func=lookup_recipe) |
| 562 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 563 | parser_package_info = subparsers.add_parser('package-info', |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 564 | help='Show version, recipe and size information for one or more packages', |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 565 | description='Looks up the specified runtime package(s) and display information') |
| 566 | parser_package_info.add_argument('pkg', nargs='*', help='Runtime package name to look up') |
| 567 | 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] | 568 | 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] | 569 | parser_package_info.set_defaults(func=package_info) |
| 570 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 571 | parser_find_path = subparsers.add_parser('find-path', |
| 572 | help='Find package providing a target path', |
| 573 | description='Finds the recipe-space package providing the specified target path') |
| 574 | parser_find_path.add_argument('targetpath', help='Path to find (wildcards * ? allowed, use quotes to avoid shell expansion)') |
| 575 | parser_find_path.set_defaults(func=find_path) |
| 576 | |
| 577 | parser_read_value = subparsers.add_parser('read-value', |
| 578 | help='Read any pkgdata value for one or more packages', |
| 579 | description='Reads the named value from the pkgdata files for the specified packages') |
| 580 | 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] | 581 | parser_read_value.add_argument('pkg', nargs='*', help='Runtime package name to look up') |
| 582 | parser_read_value.add_argument('-f', '--file', help='Read package names from the specified file (one per line, first field only)') |
| 583 | 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] | 584 | 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] | 585 | parser_read_value.set_defaults(func=read_value) |
| 586 | |
| 587 | parser_glob = subparsers.add_parser('glob', |
| 588 | help='Expand package name glob expression', |
| 589 | description='Expands one or more glob expressions over the packages listed in pkglistfile') |
| 590 | parser_glob.add_argument('pkglistfile', help='File listing packages (one package name per line)') |
| 591 | parser_glob.add_argument('glob', nargs="+", help='Glob expression for package names, e.g. *-dev') |
| 592 | parser_glob.add_argument('-x', '--exclude', help='Exclude packages matching specified regex from the glob operation') |
| 593 | parser_glob.set_defaults(func=glob) |
| 594 | |
| 595 | |
| 596 | args = parser.parse_args() |
| 597 | |
| 598 | if args.debug: |
| 599 | logger.setLevel(logging.DEBUG) |
| 600 | |
| 601 | if not args.pkgdata_dir: |
| 602 | import scriptpath |
| 603 | bitbakepath = scriptpath.add_bitbake_lib_path() |
| 604 | if not bitbakepath: |
| 605 | logger.error("Unable to find bitbake by searching parent directory of this script or PATH") |
| 606 | sys.exit(1) |
| 607 | logger.debug('Found bitbake path: %s' % bitbakepath) |
| 608 | tinfoil = tinfoil_init() |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 609 | try: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 610 | args.pkgdata_dir = tinfoil.config_data.getVar('PKGDATA_DIR') |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 611 | finally: |
| 612 | tinfoil.shutdown() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 613 | logger.debug('Value of PKGDATA_DIR is "%s"' % args.pkgdata_dir) |
| 614 | if not args.pkgdata_dir: |
| 615 | logger.error('Unable to determine pkgdata directory from PKGDATA_DIR') |
| 616 | sys.exit(1) |
| 617 | |
| 618 | if not os.path.exists(args.pkgdata_dir): |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 619 | logger.error('Unable to find pkgdata directory %s' % args.pkgdata_dir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 620 | sys.exit(1) |
| 621 | |
| 622 | ret = args.func(args) |
| 623 | |
| 624 | return ret |
| 625 | |
| 626 | |
| 627 | if __name__ == "__main__": |
| 628 | main() |