Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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 |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 22 | import argparse |
| 23 | import subprocess |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 24 | |
| 25 | def logger_create(name): |
| 26 | logger = logging.getLogger(name) |
| 27 | loggerhandler = logging.StreamHandler() |
| 28 | loggerhandler.setFormatter(logging.Formatter("%(levelname)s: %(message)s")) |
| 29 | logger.addHandler(loggerhandler) |
| 30 | logger.setLevel(logging.INFO) |
| 31 | return logger |
| 32 | |
| 33 | def logger_setup_color(logger, color='auto'): |
| 34 | from bb.msg import BBLogFormatter |
| 35 | console = logging.StreamHandler(sys.stdout) |
| 36 | formatter = BBLogFormatter("%(levelname)s: %(message)s") |
| 37 | console.setFormatter(formatter) |
| 38 | logger.handlers = [console] |
| 39 | if color == 'always' or (color=='auto' and console.stream.isatty()): |
| 40 | formatter.enable_color() |
| 41 | |
| 42 | |
| 43 | def load_plugins(logger, plugins, pluginpath): |
| 44 | import imp |
| 45 | |
| 46 | def load_plugin(name): |
| 47 | logger.debug('Loading plugin %s' % name) |
| 48 | fp, pathname, description = imp.find_module(name, [pluginpath]) |
| 49 | try: |
| 50 | return imp.load_module(name, fp, pathname, description) |
| 51 | finally: |
| 52 | if fp: |
| 53 | fp.close() |
| 54 | |
| 55 | logger.debug('Loading plugins from %s...' % pluginpath) |
| 56 | for fn in glob.glob(os.path.join(pluginpath, '*.py')): |
| 57 | name = os.path.splitext(os.path.basename(fn))[0] |
| 58 | if name != '__init__': |
| 59 | plugin = load_plugin(name) |
| 60 | if hasattr(plugin, 'plugin_init'): |
| 61 | plugin.plugin_init(plugins) |
| 62 | plugins.append(plugin) |
| 63 | |
| 64 | def git_convert_standalone_clone(repodir): |
| 65 | """If specified directory is a git repository, ensure it's a standalone clone""" |
| 66 | import bb.process |
| 67 | if os.path.exists(os.path.join(repodir, '.git')): |
| 68 | alternatesfile = os.path.join(repodir, '.git', 'objects', 'info', 'alternates') |
| 69 | if os.path.exists(alternatesfile): |
| 70 | # This will have been cloned with -s, so we need to convert it so none |
| 71 | # of the contents is shared |
| 72 | bb.process.run('git repack -a', cwd=repodir) |
| 73 | os.remove(alternatesfile) |
| 74 | |
| 75 | def fetch_uri(d, uri, destdir, srcrev=None): |
| 76 | """Fetch a URI to a local directory""" |
| 77 | import bb.data |
| 78 | bb.utils.mkdirhier(destdir) |
| 79 | localdata = bb.data.createCopy(d) |
| 80 | localdata.setVar('BB_STRICT_CHECKSUM', '') |
| 81 | localdata.setVar('SRCREV', srcrev) |
| 82 | ret = (None, None) |
| 83 | olddir = os.getcwd() |
| 84 | try: |
| 85 | fetcher = bb.fetch2.Fetch([uri], localdata) |
| 86 | for u in fetcher.ud: |
| 87 | ud = fetcher.ud[u] |
| 88 | ud.ignore_checksums = True |
| 89 | fetcher.download() |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 90 | for u in fetcher.ud: |
| 91 | ud = fetcher.ud[u] |
| 92 | if ud.localpath.rstrip(os.sep) == localdata.getVar('DL_DIR', True).rstrip(os.sep): |
| 93 | raise Exception('Local path is download directory - please check that the URI "%s" is correct' % uri) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 94 | fetcher.unpack(destdir) |
| 95 | for u in fetcher.ud: |
| 96 | ud = fetcher.ud[u] |
| 97 | if ud.method.recommends_checksum(ud): |
| 98 | md5value = bb.utils.md5_file(ud.localpath) |
| 99 | sha256value = bb.utils.sha256_file(ud.localpath) |
| 100 | ret = (md5value, sha256value) |
| 101 | finally: |
| 102 | os.chdir(olddir) |
| 103 | return ret |
| 104 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 105 | def run_editor(fn): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 106 | if isinstance(fn, str): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 107 | params = '"%s"' % fn |
| 108 | else: |
| 109 | params = '' |
| 110 | for fnitem in fn: |
| 111 | params += ' "%s"' % fnitem |
| 112 | |
| 113 | editor = os.getenv('VISUAL', os.getenv('EDITOR', 'vi')) |
| 114 | try: |
| 115 | return subprocess.check_call('%s %s' % (editor, params), shell=True) |
| 116 | except OSError as exc: |
| 117 | logger.error("Execution of editor '%s' failed: %s", editor, exc) |
| 118 | return 1 |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 119 | |
| 120 | def is_src_url(param): |
| 121 | """ |
| 122 | Check if a parameter is a URL and return True if so |
| 123 | NOTE: be careful about changing this as it will influence how devtool/recipetool command line handling works |
| 124 | """ |
| 125 | if not param: |
| 126 | return False |
| 127 | elif '://' in param: |
| 128 | return True |
| 129 | elif param.startswith('git@') or ('@' in param and param.endswith('.git')): |
| 130 | return True |
| 131 | return False |