tinfoil.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # tinfoil: a simple wrapper around cooker for bitbake-based command-line utilities
  2. #
  3. # Copyright (C) 2012 Intel Corporation
  4. # Copyright (C) 2011 Mentor Graphics Corporation
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License version 2 as
  8. # published by the Free Software Foundation.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License along
  16. # with this program; if not, write to the Free Software Foundation, Inc.,
  17. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. import logging
  19. import warnings
  20. import os
  21. import sys
  22. import bb.cache
  23. import bb.cooker
  24. import bb.providers
  25. import bb.utils
  26. from bb.cooker import state, BBCooker, CookerFeatures
  27. from bb.cookerdata import CookerConfiguration, ConfigParameters
  28. import bb.fetch2
  29. class Tinfoil:
  30. def __init__(self, output=sys.stdout, tracking=False):
  31. # Needed to avoid deprecation warnings with python 2.6
  32. warnings.filterwarnings("ignore", category=DeprecationWarning)
  33. # Set up logging
  34. self.logger = logging.getLogger('BitBake')
  35. self._log_hdlr = logging.StreamHandler(output)
  36. bb.msg.addDefaultlogFilter(self._log_hdlr)
  37. format = bb.msg.BBLogFormatter("%(levelname)s: %(message)s")
  38. if output.isatty():
  39. format.enable_color()
  40. self._log_hdlr.setFormatter(format)
  41. self.logger.addHandler(self._log_hdlr)
  42. self.config = CookerConfiguration()
  43. configparams = TinfoilConfigParameters(parse_only=True)
  44. self.config.setConfigParameters(configparams)
  45. self.config.setServerRegIdleCallback(self.register_idle_function)
  46. features = []
  47. if tracking:
  48. features.append(CookerFeatures.BASEDATASTORE_TRACKING)
  49. self.cooker = BBCooker(self.config, features)
  50. self.config_data = self.cooker.data
  51. bb.providers.logger.setLevel(logging.ERROR)
  52. self.cooker_data = None
  53. def register_idle_function(self, function, data):
  54. pass
  55. def parseRecipes(self):
  56. sys.stderr.write("Parsing recipes..")
  57. self.logger.setLevel(logging.WARNING)
  58. try:
  59. while self.cooker.state in (state.initial, state.parsing):
  60. self.cooker.updateCache()
  61. except KeyboardInterrupt:
  62. self.cooker.shutdown()
  63. self.cooker.updateCache()
  64. sys.exit(2)
  65. self.logger.setLevel(logging.INFO)
  66. sys.stderr.write("done.\n")
  67. self.cooker_data = self.cooker.recipecaches['']
  68. def prepare(self, config_only = False):
  69. if not self.cooker_data:
  70. if config_only:
  71. self.cooker.parseConfiguration()
  72. self.cooker_data = self.cooker.recipecaches['']
  73. else:
  74. self.parseRecipes()
  75. def shutdown(self):
  76. self.cooker.shutdown(force=True)
  77. self.cooker.post_serve()
  78. self.cooker.unlockBitbake()
  79. self.logger.removeHandler(self._log_hdlr)
  80. class TinfoilConfigParameters(ConfigParameters):
  81. def __init__(self, **options):
  82. self.initial_options = options
  83. super(TinfoilConfigParameters, self).__init__()
  84. def parseCommandLine(self, argv=sys.argv):
  85. class DummyOptions:
  86. def __init__(self, initial_options):
  87. for key, val in initial_options.items():
  88. setattr(self, key, val)
  89. return DummyOptions(self.initial_options), None