Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 1 | # Development tool - search command plugin |
| 2 | # |
| 3 | # Copyright (C) 2015 Intel Corporation |
| 4 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame^] | 5 | # SPDX-License-Identifier: GPL-2.0-only |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 6 | # |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 7 | |
| 8 | """Devtool search plugin""" |
| 9 | |
| 10 | import os |
| 11 | import bb |
| 12 | import logging |
| 13 | import argparse |
| 14 | import re |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 15 | from devtool import setup_tinfoil, parse_recipe, DevtoolError |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 16 | |
| 17 | logger = logging.getLogger('devtool') |
| 18 | |
| 19 | def search(args, config, basepath, workspace): |
| 20 | """Entry point for the devtool 'search' subcommand""" |
| 21 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 22 | tinfoil = setup_tinfoil(config_only=False, basepath=basepath) |
| 23 | try: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 24 | pkgdata_dir = tinfoil.config_data.getVar('PKGDATA_DIR') |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 25 | defsummary = tinfoil.config_data.getVar('SUMMARY', False) or '' |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 26 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 27 | keyword_rc = re.compile(args.keyword) |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 28 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 29 | def print_match(pn): |
| 30 | rd = parse_recipe(config, tinfoil, pn, True) |
| 31 | if not rd: |
| 32 | return |
| 33 | summary = rd.getVar('SUMMARY') |
| 34 | if summary == rd.expand(defsummary): |
| 35 | summary = '' |
| 36 | print("%s %s" % (pn.ljust(20), summary)) |
| 37 | |
| 38 | |
| 39 | matches = [] |
| 40 | if os.path.exists(pkgdata_dir): |
| 41 | for fn in os.listdir(pkgdata_dir): |
| 42 | pfn = os.path.join(pkgdata_dir, fn) |
| 43 | if not os.path.isfile(pfn): |
| 44 | continue |
| 45 | |
| 46 | packages = [] |
| 47 | match = False |
| 48 | if keyword_rc.search(fn): |
| 49 | match = True |
| 50 | |
| 51 | if not match: |
| 52 | with open(pfn, 'r') as f: |
| 53 | for line in f: |
| 54 | if line.startswith('PACKAGES:'): |
| 55 | packages = line.split(':', 1)[1].strip().split() |
| 56 | |
| 57 | for pkg in packages: |
| 58 | if keyword_rc.search(pkg): |
| 59 | match = True |
| 60 | break |
| 61 | if os.path.exists(os.path.join(pkgdata_dir, 'runtime', pkg + '.packaged')): |
| 62 | with open(os.path.join(pkgdata_dir, 'runtime', pkg), 'r') as f: |
| 63 | for line in f: |
| 64 | if ': ' in line: |
| 65 | splitline = line.split(':', 1) |
| 66 | key = splitline[0] |
| 67 | value = splitline[1].strip() |
| 68 | if key in ['PKG_%s' % pkg, 'DESCRIPTION', 'FILES_INFO'] or key.startswith('FILERPROVIDES_'): |
| 69 | if keyword_rc.search(value): |
| 70 | match = True |
| 71 | break |
| 72 | if match: |
| 73 | print_match(fn) |
| 74 | matches.append(fn) |
| 75 | else: |
| 76 | logger.warning('Package data is not available, results may be limited') |
| 77 | |
| 78 | for recipe in tinfoil.all_recipes(): |
| 79 | if args.fixed_setup and 'nativesdk' in recipe.inherits(): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 80 | continue |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 81 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 82 | match = False |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 83 | if keyword_rc.search(recipe.pn): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 84 | match = True |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 85 | else: |
| 86 | for prov in recipe.provides: |
| 87 | if keyword_rc.search(prov): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 88 | match = True |
| 89 | break |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 90 | if not match: |
| 91 | for rprov in recipe.rprovides: |
| 92 | if keyword_rc.search(rprov): |
| 93 | match = True |
| 94 | break |
| 95 | if match and not recipe.pn in matches: |
| 96 | print_match(recipe.pn) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 97 | finally: |
| 98 | tinfoil.shutdown() |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 99 | |
| 100 | return 0 |
| 101 | |
| 102 | def register_commands(subparsers, context): |
| 103 | """Register devtool subcommands from this plugin""" |
| 104 | parser_search = subparsers.add_parser('search', help='Search available recipes', |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 105 | description='Searches for available recipes. Matches on recipe name, package name, description and installed files, and prints the recipe name and summary on match.', |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 106 | group='info') |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 107 | parser_search.add_argument('keyword', help='Keyword to search for (regular expression syntax allowed, use quotes to avoid shell expansion)') |
| 108 | parser_search.set_defaults(func=search, no_workspace=True, fixed_setup=context.fixed_setup) |