diff options
Diffstat (limited to 'scripts/lib/scriptutils.py')
| -rw-r--r-- | scripts/lib/scriptutils.py | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/scripts/lib/scriptutils.py b/scripts/lib/scriptutils.py new file mode 100644 index 0000000000..e7861268a5 --- /dev/null +++ b/scripts/lib/scriptutils.py | |||
| @@ -0,0 +1,60 @@ | |||
| 1 | # Script utility functions | ||
| 2 | # | ||
| 3 | # Copyright (C) 2014 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 | import sys | ||
| 19 | import os | ||
| 20 | import logging | ||
| 21 | import glob | ||
| 22 | |||
| 23 | def logger_create(name): | ||
| 24 | logger = logging.getLogger(name) | ||
| 25 | loggerhandler = logging.StreamHandler() | ||
| 26 | loggerhandler.setFormatter(logging.Formatter("%(levelname)s: %(message)s")) | ||
| 27 | logger.addHandler(loggerhandler) | ||
| 28 | logger.setLevel(logging.INFO) | ||
| 29 | return logger | ||
| 30 | |||
| 31 | def logger_setup_color(logger, color='auto'): | ||
| 32 | from bb.msg import BBLogFormatter | ||
| 33 | console = logging.StreamHandler(sys.stdout) | ||
| 34 | formatter = BBLogFormatter("%(levelname)s: %(message)s") | ||
| 35 | console.setFormatter(formatter) | ||
| 36 | logger.handlers = [console] | ||
| 37 | if color == 'always' or (color=='auto' and console.stream.isatty()): | ||
| 38 | formatter.enable_color() | ||
| 39 | |||
| 40 | |||
| 41 | def load_plugins(logger, plugins, pluginpath): | ||
| 42 | import imp | ||
| 43 | |||
| 44 | def load_plugin(name): | ||
| 45 | logger.debug('Loading plugin %s' % name) | ||
| 46 | fp, pathname, description = imp.find_module(name, [pluginpath]) | ||
| 47 | try: | ||
| 48 | return imp.load_module(name, fp, pathname, description) | ||
| 49 | finally: | ||
| 50 | if fp: | ||
| 51 | fp.close() | ||
| 52 | |||
| 53 | logger.debug('Loading plugins from %s...' % pluginpath) | ||
| 54 | for fn in glob.glob(os.path.join(pluginpath, '*.py')): | ||
| 55 | name = os.path.splitext(os.path.basename(fn))[0] | ||
| 56 | if name != '__init__': | ||
| 57 | plugin = load_plugin(name) | ||
| 58 | if hasattr(plugin, 'plugin_init'): | ||
| 59 | plugin.plugin_init(plugins) | ||
| 60 | plugins.append(plugin) | ||
