blob: b4f209b7e3a2cc51df849941c29a794a1b4e5b98 [file] [log] [blame]
Patrick Williamsf1e5d692016-03-30 15:21:19 -05001# 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
20import os
21import bb
22import logging
23import argparse
24import re
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050025from devtool import setup_tinfoil, parse_recipe, DevtoolError
Patrick Williamsf1e5d692016-03-30 15:21:19 -050026
27logger = logging.getLogger('devtool')
28
29def search(args, config, basepath, workspace):
30 """Entry point for the devtool 'search' subcommand"""
31
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050032 tinfoil = setup_tinfoil(config_only=False, basepath=basepath)
33 try:
Brad Bishop6e60e8b2018-02-01 10:27:11 -050034 pkgdata_dir = tinfoil.config_data.getVar('PKGDATA_DIR')
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050035 defsummary = tinfoil.config_data.getVar('SUMMARY', False) or ''
Patrick Williamsf1e5d692016-03-30 15:21:19 -050036
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050037 keyword_rc = re.compile(args.keyword)
Patrick Williamsf1e5d692016-03-30 15:21:19 -050038
Brad Bishop316dfdd2018-06-25 12:45:53 -040039 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 Williamsd8c66bc2016-06-20 12:57:21 -050090 continue
Patrick Williamsf1e5d692016-03-30 15:21:19 -050091
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050092 match = False
Brad Bishop316dfdd2018-06-25 12:45:53 -040093 if keyword_rc.search(recipe.pn):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050094 match = True
Brad Bishop316dfdd2018-06-25 12:45:53 -040095 else:
96 for prov in recipe.provides:
97 if keyword_rc.search(prov):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050098 match = True
99 break
Brad Bishop316dfdd2018-06-25 12:45:53 -0400100 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 Williamsd8c66bc2016-06-20 12:57:21 -0500107 finally:
108 tinfoil.shutdown()
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500109
110 return 0
111
112def register_commands(subparsers, context):
113 """Register devtool subcommands from this plugin"""
114 parser_search = subparsers.add_parser('search', help='Search available recipes',
Brad Bishop316dfdd2018-06-25 12:45:53 -0400115 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 -0500116 group='info')
Brad Bishop316dfdd2018-06-25 12:45:53 -0400117 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)