search.py 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. """Devtool search plugin"""
  18. import os
  19. import bb
  20. import logging
  21. import argparse
  22. import re
  23. from devtool import setup_tinfoil, parse_recipe, DevtoolError
  24. logger = logging.getLogger('devtool')
  25. def search(args, config, basepath, workspace):
  26. """Entry point for the devtool 'search' subcommand"""
  27. tinfoil = setup_tinfoil(config_only=False, basepath=basepath)
  28. try:
  29. pkgdata_dir = tinfoil.config_data.getVar('PKGDATA_DIR')
  30. defsummary = tinfoil.config_data.getVar('SUMMARY', False) or ''
  31. keyword_rc = re.compile(args.keyword)
  32. for fn in os.listdir(pkgdata_dir):
  33. pfn = os.path.join(pkgdata_dir, fn)
  34. if not os.path.isfile(pfn):
  35. continue
  36. packages = []
  37. match = False
  38. if keyword_rc.search(fn):
  39. match = True
  40. if not match:
  41. with open(pfn, 'r') as f:
  42. for line in f:
  43. if line.startswith('PACKAGES:'):
  44. packages = line.split(':', 1)[1].strip().split()
  45. for pkg in packages:
  46. if keyword_rc.search(pkg):
  47. match = True
  48. break
  49. if os.path.exists(os.path.join(pkgdata_dir, 'runtime', pkg + '.packaged')):
  50. with open(os.path.join(pkgdata_dir, 'runtime', pkg), 'r') as f:
  51. for line in f:
  52. if ': ' in line:
  53. splitline = line.split(':', 1)
  54. key = splitline[0]
  55. value = splitline[1].strip()
  56. if key in ['PKG_%s' % pkg, 'DESCRIPTION', 'FILES_INFO'] or key.startswith('FILERPROVIDES_'):
  57. if keyword_rc.search(value):
  58. match = True
  59. break
  60. if match:
  61. rd = parse_recipe(config, tinfoil, fn, True)
  62. summary = rd.getVar('SUMMARY')
  63. if summary == rd.expand(defsummary):
  64. summary = ''
  65. print("%s %s" % (fn.ljust(20), summary))
  66. finally:
  67. tinfoil.shutdown()
  68. return 0
  69. def register_commands(subparsers, context):
  70. """Register devtool subcommands from this plugin"""
  71. parser_search = subparsers.add_parser('search', help='Search available recipes',
  72. description='Searches for available target recipes. Matches on recipe name, package name, description and installed files, and prints the recipe name on match.',
  73. group='info')
  74. parser_search.add_argument('keyword', help='Keyword to search for (regular expression syntax allowed)')
  75. parser_search.set_defaults(func=search, no_workspace=True)