blob: d24040df37996b88a908fd4cac4df66076a15b16 [file] [log] [blame]
Patrick Williamsf1e5d692016-03-30 15:21:19 -05001# Development tool - search command plugin
2#
3# Copyright (C) 2015 Intel Corporation
4#
Brad Bishopc342db32019-05-15 21:57:59 -04005# SPDX-License-Identifier: GPL-2.0-only
Patrick Williamsf1e5d692016-03-30 15:21:19 -05006#
Patrick Williamsf1e5d692016-03-30 15:21:19 -05007
8"""Devtool search plugin"""
9
10import os
11import bb
12import logging
13import argparse
14import re
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050015from devtool import setup_tinfoil, parse_recipe, DevtoolError
Patrick Williamsf1e5d692016-03-30 15:21:19 -050016
17logger = logging.getLogger('devtool')
18
19def search(args, config, basepath, workspace):
20 """Entry point for the devtool 'search' subcommand"""
21
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050022 tinfoil = setup_tinfoil(config_only=False, basepath=basepath)
23 try:
Brad Bishop6e60e8b2018-02-01 10:27:11 -050024 pkgdata_dir = tinfoil.config_data.getVar('PKGDATA_DIR')
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050025 defsummary = tinfoil.config_data.getVar('SUMMARY', False) or ''
Patrick Williamsf1e5d692016-03-30 15:21:19 -050026
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050027 keyword_rc = re.compile(args.keyword)
Patrick Williamsf1e5d692016-03-30 15:21:19 -050028
Brad Bishop316dfdd2018-06-25 12:45:53 -040029 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 Williamsd8c66bc2016-06-20 12:57:21 -050080 continue
Patrick Williamsf1e5d692016-03-30 15:21:19 -050081
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050082 match = False
Brad Bishop316dfdd2018-06-25 12:45:53 -040083 if keyword_rc.search(recipe.pn):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050084 match = True
Brad Bishop316dfdd2018-06-25 12:45:53 -040085 else:
86 for prov in recipe.provides:
87 if keyword_rc.search(prov):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050088 match = True
89 break
Brad Bishop316dfdd2018-06-25 12:45:53 -040090 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 Williamsd8c66bc2016-06-20 12:57:21 -050097 finally:
98 tinfoil.shutdown()
Patrick Williamsf1e5d692016-03-30 15:21:19 -050099
100 return 0
101
102def register_commands(subparsers, context):
103 """Register devtool subcommands from this plugin"""
104 parser_search = subparsers.add_parser('search', help='Search available recipes',
Brad Bishop316dfdd2018-06-25 12:45:53 -0400105 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 Williamsd8c66bc2016-06-20 12:57:21 -0500106 group='info')
Brad Bishop316dfdd2018-06-25 12:45:53 -0400107 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)