blob: b44bed7f6f4c14ce369e77f69e2bc1dbfe65f7dc [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:
34 pkgdata_dir = tinfoil.config_data.getVar('PKGDATA_DIR', True)
35 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
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050039 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 Williamsf1e5d692016-03-30 15:21:19 -050043
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050044 packages = []
45 match = False
46 if keyword_rc.search(fn):
47 match = True
Patrick Williamsf1e5d692016-03-30 15:21:19 -050048
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050049 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 Williamsf1e5d692016-03-30 15:21:19 -050054
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050055 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 Williamsf1e5d692016-03-30 15:21:19 -050070
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050071 if match:
72 rd = parse_recipe(config, tinfoil, fn, True)
73 summary = rd.getVar('SUMMARY', True)
74 if summary == rd.expand(defsummary):
75 summary = ''
76 print("%s %s" % (fn.ljust(20), summary))
77 finally:
78 tinfoil.shutdown()
Patrick Williamsf1e5d692016-03-30 15:21:19 -050079
80 return 0
81
82def register_commands(subparsers, context):
83 """Register devtool subcommands from this plugin"""
84 parser_search = subparsers.add_parser('search', help='Search available recipes',
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050085 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 Williamsf1e5d692016-03-30 15:21:19 -050087 parser_search.add_argument('keyword', help='Keyword to search for (regular expression syntax allowed)')
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050088 parser_search.set_defaults(func=search, no_workspace=True)