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 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 39 | for fn in os.listdir(pkgdata_dir): |
| 40 | pfn = os.path.join(pkgdata_dir, fn) |
| 41 | if not os.path.isfile(pfn): |
| 42 | continue |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 43 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 44 | packages = [] |
| 45 | match = False |
| 46 | if keyword_rc.search(fn): |
| 47 | match = True |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 48 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 49 | if not match: |
| 50 | with open(pfn, 'r') as f: |
| 51 | for line in f: |
| 52 | if line.startswith('PACKAGES:'): |
| 53 | packages = line.split(':', 1)[1].strip().split() |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 54 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 55 | for pkg in packages: |
| 56 | if keyword_rc.search(pkg): |
| 57 | match = True |
| 58 | break |
| 59 | if os.path.exists(os.path.join(pkgdata_dir, 'runtime', pkg + '.packaged')): |
| 60 | with open(os.path.join(pkgdata_dir, 'runtime', pkg), 'r') as f: |
| 61 | for line in f: |
| 62 | if ': ' in line: |
| 63 | splitline = line.split(':', 1) |
| 64 | key = splitline[0] |
| 65 | value = splitline[1].strip() |
| 66 | if key in ['PKG_%s' % pkg, 'DESCRIPTION', 'FILES_INFO'] or key.startswith('FILERPROVIDES_'): |
| 67 | if keyword_rc.search(value): |
| 68 | match = True |
| 69 | break |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 70 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 71 | if match: |
| 72 | rd = parse_recipe(config, tinfoil, fn, True) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame^] | 73 | summary = rd.getVar('SUMMARY') |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 74 | if summary == rd.expand(defsummary): |
| 75 | summary = '' |
| 76 | print("%s %s" % (fn.ljust(20), summary)) |
| 77 | finally: |
| 78 | tinfoil.shutdown() |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 79 | |
| 80 | return 0 |
| 81 | |
| 82 | def register_commands(subparsers, context): |
| 83 | """Register devtool subcommands from this plugin""" |
| 84 | parser_search = subparsers.add_parser('search', help='Search available recipes', |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 85 | description='Searches for available target recipes. Matches on recipe name, package name, description and installed files, and prints the recipe name on match.', |
| 86 | group='info') |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 87 | parser_search.add_argument('keyword', help='Keyword to search for (regular expression syntax allowed)') |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 88 | parser_search.set_defaults(func=search, no_workspace=True) |