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 | # |
| 5 | # This program is free software; you can redistribute it and/or modify |
| 6 | # it under the terms of the GNU General Public License version 2 as |
| 7 | # published by the Free Software Foundation. |
| 8 | # |
| 9 | # This program is distributed in the hope that it will be useful, |
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | # GNU General Public License for more details. |
| 13 | # |
| 14 | # You should have received a copy of the GNU General Public License along |
| 15 | # with this program; if not, write to the Free Software Foundation, Inc., |
| 16 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 17 | |
| 18 | """Devtool search plugin""" |
| 19 | |
| 20 | import os |
| 21 | import bb |
| 22 | import logging |
| 23 | import argparse |
| 24 | import re |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 25 | from devtool import setup_tinfoil, parse_recipe, DevtoolError |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 26 | |
| 27 | logger = logging.getLogger('devtool') |
| 28 | |
| 29 | def search(args, config, basepath, workspace): |
| 30 | """Entry point for the devtool 'search' subcommand""" |
| 31 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 32 | tinfoil = setup_tinfoil(config_only=False, basepath=basepath) |
| 33 | try: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 34 | pkgdata_dir = tinfoil.config_data.getVar('PKGDATA_DIR') |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 35 | defsummary = tinfoil.config_data.getVar('SUMMARY', False) or '' |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 36 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 37 | keyword_rc = re.compile(args.keyword) |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 38 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 39 | def print_match(pn): |
| 40 | rd = parse_recipe(config, tinfoil, pn, True) |
| 41 | if not rd: |
| 42 | return |
| 43 | summary = rd.getVar('SUMMARY') |
| 44 | if summary == rd.expand(defsummary): |
| 45 | summary = '' |
| 46 | print("%s %s" % (pn.ljust(20), summary)) |
| 47 | |
| 48 | |
| 49 | matches = [] |
| 50 | if os.path.exists(pkgdata_dir): |
| 51 | for fn in os.listdir(pkgdata_dir): |
| 52 | pfn = os.path.join(pkgdata_dir, fn) |
| 53 | if not os.path.isfile(pfn): |
| 54 | continue |
| 55 | |
| 56 | packages = [] |
| 57 | match = False |
| 58 | if keyword_rc.search(fn): |
| 59 | match = True |
| 60 | |
| 61 | if not match: |
| 62 | with open(pfn, 'r') as f: |
| 63 | for line in f: |
| 64 | if line.startswith('PACKAGES:'): |
| 65 | packages = line.split(':', 1)[1].strip().split() |
| 66 | |
| 67 | for pkg in packages: |
| 68 | if keyword_rc.search(pkg): |
| 69 | match = True |
| 70 | break |
| 71 | if os.path.exists(os.path.join(pkgdata_dir, 'runtime', pkg + '.packaged')): |
| 72 | with open(os.path.join(pkgdata_dir, 'runtime', pkg), 'r') as f: |
| 73 | for line in f: |
| 74 | if ': ' in line: |
| 75 | splitline = line.split(':', 1) |
| 76 | key = splitline[0] |
| 77 | value = splitline[1].strip() |
| 78 | if key in ['PKG_%s' % pkg, 'DESCRIPTION', 'FILES_INFO'] or key.startswith('FILERPROVIDES_'): |
| 79 | if keyword_rc.search(value): |
| 80 | match = True |
| 81 | break |
| 82 | if match: |
| 83 | print_match(fn) |
| 84 | matches.append(fn) |
| 85 | else: |
| 86 | logger.warning('Package data is not available, results may be limited') |
| 87 | |
| 88 | for recipe in tinfoil.all_recipes(): |
| 89 | if args.fixed_setup and 'nativesdk' in recipe.inherits(): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 90 | continue |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 91 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 92 | match = False |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 93 | if keyword_rc.search(recipe.pn): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 94 | match = True |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 95 | else: |
| 96 | for prov in recipe.provides: |
| 97 | if keyword_rc.search(prov): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 98 | match = True |
| 99 | break |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 100 | if not match: |
| 101 | for rprov in recipe.rprovides: |
| 102 | if keyword_rc.search(rprov): |
| 103 | match = True |
| 104 | break |
| 105 | if match and not recipe.pn in matches: |
| 106 | print_match(recipe.pn) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 107 | finally: |
| 108 | tinfoil.shutdown() |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 109 | |
| 110 | return 0 |
| 111 | |
| 112 | def register_commands(subparsers, context): |
| 113 | """Register devtool subcommands from this plugin""" |
| 114 | parser_search = subparsers.add_parser('search', help='Search available recipes', |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 115 | 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] | 116 | group='info') |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 117 | parser_search.add_argument('keyword', help='Keyword to search for (regular expression syntax allowed, use quotes to avoid shell expansion)') |
| 118 | parser_search.set_defaults(func=search, no_workspace=True, fixed_setup=context.fixed_setup) |