Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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 | |
| 19 | import logging |
| 20 | import warnings |
| 21 | import os |
| 22 | import sys |
| 23 | |
| 24 | import bb.cache |
| 25 | import bb.cooker |
| 26 | import bb.providers |
| 27 | import bb.utils |
| 28 | from bb.cooker import state, BBCooker, CookerFeatures |
| 29 | from bb.cookerdata import CookerConfiguration, ConfigParameters |
| 30 | import bb.fetch2 |
| 31 | |
| 32 | class Tinfoil: |
| 33 | def __init__(self, output=sys.stdout, tracking=False): |
| 34 | # Needed to avoid deprecation warnings with python 2.6 |
| 35 | warnings.filterwarnings("ignore", category=DeprecationWarning) |
| 36 | |
| 37 | # Set up logging |
| 38 | self.logger = logging.getLogger('BitBake') |
| 39 | console = logging.StreamHandler(output) |
| 40 | bb.msg.addDefaultlogFilter(console) |
| 41 | format = bb.msg.BBLogFormatter("%(levelname)s: %(message)s") |
| 42 | if output.isatty(): |
| 43 | format.enable_color() |
| 44 | console.setFormatter(format) |
| 45 | self.logger.addHandler(console) |
| 46 | |
| 47 | self.config = CookerConfiguration() |
| 48 | configparams = TinfoilConfigParameters(parse_only=True) |
| 49 | self.config.setConfigParameters(configparams) |
| 50 | self.config.setServerRegIdleCallback(self.register_idle_function) |
| 51 | features = [] |
| 52 | if tracking: |
| 53 | features.append(CookerFeatures.BASEDATASTORE_TRACKING) |
| 54 | self.cooker = BBCooker(self.config, features) |
| 55 | self.config_data = self.cooker.data |
| 56 | bb.providers.logger.setLevel(logging.ERROR) |
| 57 | self.cooker_data = None |
| 58 | |
| 59 | def register_idle_function(self, function, data): |
| 60 | pass |
| 61 | |
| 62 | def parseRecipes(self): |
| 63 | sys.stderr.write("Parsing recipes..") |
| 64 | self.logger.setLevel(logging.WARNING) |
| 65 | |
| 66 | try: |
| 67 | while self.cooker.state in (state.initial, state.parsing): |
| 68 | self.cooker.updateCache() |
| 69 | except KeyboardInterrupt: |
| 70 | self.cooker.shutdown() |
| 71 | self.cooker.updateCache() |
| 72 | sys.exit(2) |
| 73 | |
| 74 | self.logger.setLevel(logging.INFO) |
| 75 | sys.stderr.write("done.\n") |
| 76 | |
| 77 | self.cooker_data = self.cooker.recipecache |
| 78 | |
| 79 | def prepare(self, config_only = False): |
| 80 | if not self.cooker_data: |
| 81 | if config_only: |
| 82 | self.cooker.parseConfiguration() |
| 83 | self.cooker_data = self.cooker.recipecache |
| 84 | else: |
| 85 | self.parseRecipes() |
| 86 | |
| 87 | def shutdown(self): |
| 88 | self.cooker.shutdown(force=True) |
| 89 | self.cooker.post_serve() |
| 90 | self.cooker.unlockBitbake() |
| 91 | |
| 92 | class TinfoilConfigParameters(ConfigParameters): |
| 93 | |
| 94 | def __init__(self, **options): |
| 95 | self.initial_options = options |
| 96 | super(TinfoilConfigParameters, self).__init__() |
| 97 | |
| 98 | def parseCommandLine(self, argv=sys.argv): |
| 99 | class DummyOptions: |
| 100 | def __init__(self, initial_options): |
| 101 | for key, val in initial_options.items(): |
| 102 | setattr(self, key, val) |
| 103 | |
| 104 | return DummyOptions(self.initial_options), None |