list-packageconfig-flags.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #!/usr/bin/env python3
  2. # This program is free software; you can redistribute it and/or modify
  3. # it under the terms of the GNU General Public License as published by
  4. # the Free Software Foundation; either version 2 of the License, or
  5. # (at your option) any later version.
  6. #
  7. # This program is distributed in the hope that it will be useful,
  8. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. # GNU General Public License for more details.
  11. #
  12. # You should have received a copy of the GNU General Public License
  13. # along with this program; if not, write to the Free Software Foundation.
  14. #
  15. # Copyright (C) 2013 Wind River Systems, Inc.
  16. # Copyright (C) 2014 Intel Corporation
  17. #
  18. # - list available recipes which have PACKAGECONFIG flags
  19. # - list available PACKAGECONFIG flags and all affected recipes
  20. # - list all recipes and PACKAGECONFIG information
  21. import sys
  22. import optparse
  23. import os
  24. scripts_path = os.path.abspath(os.path.dirname(os.path.abspath(sys.argv[0])))
  25. lib_path = os.path.abspath(scripts_path + '/../lib')
  26. sys.path = sys.path + [lib_path]
  27. import scriptpath
  28. # For importing the following modules
  29. bitbakepath = scriptpath.add_bitbake_lib_path()
  30. if not bitbakepath:
  31. sys.stderr.write("Unable to find bitbake by searching parent directory of this script or PATH\n")
  32. sys.exit(1)
  33. import bb.cooker
  34. import bb.providers
  35. import bb.tinfoil
  36. def get_fnlist(bbhandler, pkg_pn, preferred):
  37. ''' Get all recipe file names '''
  38. if preferred:
  39. (latest_versions, preferred_versions) = bb.providers.findProviders(bbhandler.config_data, bbhandler.cooker.recipecaches[''], pkg_pn)
  40. fn_list = []
  41. for pn in sorted(pkg_pn):
  42. if preferred:
  43. fn_list.append(preferred_versions[pn][1])
  44. else:
  45. fn_list.extend(pkg_pn[pn])
  46. return fn_list
  47. def get_recipesdata(bbhandler, preferred):
  48. ''' Get data of all available recipes which have PACKAGECONFIG flags '''
  49. pkg_pn = bbhandler.cooker.recipecaches[''].pkg_pn
  50. data_dict = {}
  51. for fn in get_fnlist(bbhandler, pkg_pn, preferred):
  52. data = bbhandler.parse_recipe_file(fn)
  53. flags = data.getVarFlags("PACKAGECONFIG")
  54. flags.pop('doc', None)
  55. if flags:
  56. data_dict[fn] = data
  57. return data_dict
  58. def collect_pkgs(data_dict):
  59. ''' Collect available pkgs in which have PACKAGECONFIG flags '''
  60. # pkg_dict = {'pkg1': ['flag1', 'flag2',...]}
  61. pkg_dict = {}
  62. for fn in data_dict:
  63. pkgconfigflags = data_dict[fn].getVarFlags("PACKAGECONFIG")
  64. pkgconfigflags.pop('doc', None)
  65. pkgname = data_dict[fn].getVar("P")
  66. pkg_dict[pkgname] = sorted(pkgconfigflags.keys())
  67. return pkg_dict
  68. def collect_flags(pkg_dict):
  69. ''' Collect available PACKAGECONFIG flags and all affected pkgs '''
  70. # flag_dict = {'flag': ['pkg1', 'pkg2',...]}
  71. flag_dict = {}
  72. for pkgname, flaglist in pkg_dict.items():
  73. for flag in flaglist:
  74. if flag in flag_dict:
  75. flag_dict[flag].append(pkgname)
  76. else:
  77. flag_dict[flag] = [pkgname]
  78. return flag_dict
  79. def display_pkgs(pkg_dict):
  80. ''' Display available pkgs which have PACKAGECONFIG flags '''
  81. pkgname_len = len("RECIPE NAME") + 1
  82. for pkgname in pkg_dict:
  83. if pkgname_len < len(pkgname):
  84. pkgname_len = len(pkgname)
  85. pkgname_len += 1
  86. header = '%-*s%s' % (pkgname_len, str("RECIPE NAME"), str("PACKAGECONFIG FLAGS"))
  87. print(header)
  88. print(str("").ljust(len(header), '='))
  89. for pkgname in sorted(pkg_dict):
  90. print('%-*s%s' % (pkgname_len, pkgname, ' '.join(pkg_dict[pkgname])))
  91. def display_flags(flag_dict):
  92. ''' Display available PACKAGECONFIG flags and all affected pkgs '''
  93. flag_len = len("PACKAGECONFIG FLAG") + 5
  94. header = '%-*s%s' % (flag_len, str("PACKAGECONFIG FLAG"), str("RECIPE NAMES"))
  95. print(header)
  96. print(str("").ljust(len(header), '='))
  97. for flag in sorted(flag_dict):
  98. print('%-*s%s' % (flag_len, flag, ' '.join(sorted(flag_dict[flag]))))
  99. def display_all(data_dict):
  100. ''' Display all pkgs and PACKAGECONFIG information '''
  101. print(str("").ljust(50, '='))
  102. for fn in data_dict:
  103. print('%s' % data_dict[fn].getVar("P"))
  104. print(fn)
  105. packageconfig = data_dict[fn].getVar("PACKAGECONFIG") or ''
  106. if packageconfig.strip() == '':
  107. packageconfig = 'None'
  108. print('PACKAGECONFIG %s' % packageconfig)
  109. for flag,flag_val in data_dict[fn].getVarFlags("PACKAGECONFIG").items():
  110. if flag == "doc":
  111. continue
  112. print('PACKAGECONFIG[%s] %s' % (flag, flag_val))
  113. print('')
  114. def main():
  115. pkg_dict = {}
  116. flag_dict = {}
  117. # Collect and validate input
  118. parser = optparse.OptionParser(
  119. description = "Lists recipes and PACKAGECONFIG flags. Without -a or -f, recipes and their available PACKAGECONFIG flags are listed.",
  120. usage = """
  121. %prog [options]""")
  122. parser.add_option("-f", "--flags",
  123. help = "list available PACKAGECONFIG flags and affected recipes",
  124. action="store_const", dest="listtype", const="flags", default="recipes")
  125. parser.add_option("-a", "--all",
  126. help = "list all recipes and PACKAGECONFIG information",
  127. action="store_const", dest="listtype", const="all")
  128. parser.add_option("-p", "--preferred-only",
  129. help = "where multiple recipe versions are available, list only the preferred version",
  130. action="store_true", dest="preferred", default=False)
  131. options, args = parser.parse_args(sys.argv)
  132. with bb.tinfoil.Tinfoil() as bbhandler:
  133. bbhandler.prepare()
  134. print("Gathering recipe data...")
  135. data_dict = get_recipesdata(bbhandler, options.preferred)
  136. if options.listtype == 'flags':
  137. pkg_dict = collect_pkgs(data_dict)
  138. flag_dict = collect_flags(pkg_dict)
  139. display_flags(flag_dict)
  140. elif options.listtype == 'recipes':
  141. pkg_dict = collect_pkgs(data_dict)
  142. display_pkgs(pkg_dict)
  143. elif options.listtype == 'all':
  144. display_all(data_dict)
  145. if __name__ == "__main__":
  146. main()