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 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 18 | import argparse |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 19 | import glob |
| 20 | import logging |
| 21 | import os |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 22 | import random |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 23 | import shlex |
| 24 | import shutil |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 25 | import string |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 26 | import subprocess |
| 27 | import sys |
| 28 | import tempfile |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 29 | import importlib |
| 30 | from importlib import machinery |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 31 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 32 | def logger_create(name, stream=None): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 33 | logger = logging.getLogger(name) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 34 | loggerhandler = logging.StreamHandler(stream=stream) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 35 | loggerhandler.setFormatter(logging.Formatter("%(levelname)s: %(message)s")) |
| 36 | logger.addHandler(loggerhandler) |
| 37 | logger.setLevel(logging.INFO) |
| 38 | return logger |
| 39 | |
| 40 | def logger_setup_color(logger, color='auto'): |
| 41 | from bb.msg import BBLogFormatter |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 42 | |
| 43 | for handler in logger.handlers: |
| 44 | if (isinstance(handler, logging.StreamHandler) and |
| 45 | isinstance(handler.formatter, BBLogFormatter)): |
| 46 | if color == 'always' or (color == 'auto' and handler.stream.isatty()): |
| 47 | handler.formatter.enable_color() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 48 | |
| 49 | |
| 50 | def load_plugins(logger, plugins, pluginpath): |
| 51 | import imp |
| 52 | |
| 53 | def load_plugin(name): |
| 54 | logger.debug('Loading plugin %s' % name) |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 55 | spec = importlib.machinery.PathFinder.find_spec(name, path=[pluginpath] ) |
| 56 | if spec: |
| 57 | return spec.loader.load_module() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 58 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 59 | def plugin_name(filename): |
| 60 | return os.path.splitext(os.path.basename(filename))[0] |
| 61 | |
| 62 | known_plugins = [plugin_name(p.__name__) for p in plugins] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 63 | logger.debug('Loading plugins from %s...' % pluginpath) |
| 64 | for fn in glob.glob(os.path.join(pluginpath, '*.py')): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 65 | name = plugin_name(fn) |
| 66 | if name != '__init__' and name not in known_plugins: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 67 | plugin = load_plugin(name) |
| 68 | if hasattr(plugin, 'plugin_init'): |
| 69 | plugin.plugin_init(plugins) |
| 70 | plugins.append(plugin) |
| 71 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 72 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 73 | def git_convert_standalone_clone(repodir): |
| 74 | """If specified directory is a git repository, ensure it's a standalone clone""" |
| 75 | import bb.process |
| 76 | if os.path.exists(os.path.join(repodir, '.git')): |
| 77 | alternatesfile = os.path.join(repodir, '.git', 'objects', 'info', 'alternates') |
| 78 | if os.path.exists(alternatesfile): |
| 79 | # This will have been cloned with -s, so we need to convert it so none |
| 80 | # of the contents is shared |
| 81 | bb.process.run('git repack -a', cwd=repodir) |
| 82 | os.remove(alternatesfile) |
| 83 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 84 | def _get_temp_recipe_dir(d): |
| 85 | # This is a little bit hacky but we need to find a place where we can put |
| 86 | # the recipe so that bitbake can find it. We're going to delete it at the |
| 87 | # end so it doesn't really matter where we put it. |
| 88 | bbfiles = d.getVar('BBFILES').split() |
| 89 | fetchrecipedir = None |
| 90 | for pth in bbfiles: |
| 91 | if pth.endswith('.bb'): |
| 92 | pthdir = os.path.dirname(pth) |
| 93 | if os.access(os.path.dirname(os.path.dirname(pthdir)), os.W_OK): |
| 94 | fetchrecipedir = pthdir.replace('*', 'recipetool') |
| 95 | if pthdir.endswith('workspace/recipes/*'): |
| 96 | # Prefer the workspace |
| 97 | break |
| 98 | return fetchrecipedir |
| 99 | |
| 100 | class FetchUrlFailure(Exception): |
| 101 | def __init__(self, url): |
| 102 | self.url = url |
| 103 | def __str__(self): |
| 104 | return "Failed to fetch URL %s" % self.url |
| 105 | |
| 106 | def fetch_url(tinfoil, srcuri, srcrev, destdir, logger, preserve_tmp=False, mirrors=False): |
| 107 | """ |
| 108 | Fetch the specified URL using normal do_fetch and do_unpack tasks, i.e. |
| 109 | any dependencies that need to be satisfied in order to support the fetch |
| 110 | operation will be taken care of |
| 111 | """ |
| 112 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 113 | import bb |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 114 | |
| 115 | checksums = {} |
| 116 | fetchrecipepn = None |
| 117 | |
| 118 | # We need to put our temp directory under ${BASE_WORKDIR} otherwise |
| 119 | # we may have problems with the recipe-specific sysroot population |
| 120 | tmpparent = tinfoil.config_data.getVar('BASE_WORKDIR') |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 121 | bb.utils.mkdirhier(tmpparent) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 122 | tmpdir = tempfile.mkdtemp(prefix='recipetool-', dir=tmpparent) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 123 | try: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 124 | tmpworkdir = os.path.join(tmpdir, 'work') |
| 125 | logger.debug('fetch_url: temp dir is %s' % tmpdir) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 126 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 127 | fetchrecipedir = _get_temp_recipe_dir(tinfoil.config_data) |
| 128 | if not fetchrecipedir: |
| 129 | logger.error('Searched BBFILES but unable to find a writeable place to put temporary recipe') |
| 130 | sys.exit(1) |
| 131 | fetchrecipe = None |
| 132 | bb.utils.mkdirhier(fetchrecipedir) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 133 | try: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 134 | # Generate a dummy recipe so we can follow more or less normal paths |
| 135 | # for do_fetch and do_unpack |
| 136 | # I'd use tempfile functions here but underscores can be produced by that and those |
| 137 | # aren't allowed in recipe file names except to separate the version |
| 138 | rndstring = ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(8)) |
| 139 | fetchrecipe = os.path.join(fetchrecipedir, 'tmp-recipetool-%s.bb' % rndstring) |
| 140 | fetchrecipepn = os.path.splitext(os.path.basename(fetchrecipe))[0] |
| 141 | logger.debug('Generating initial recipe %s for fetching' % fetchrecipe) |
| 142 | with open(fetchrecipe, 'w') as f: |
| 143 | # We don't want to have to specify LIC_FILES_CHKSUM |
| 144 | f.write('LICENSE = "CLOSED"\n') |
| 145 | # We don't need the cross-compiler |
| 146 | f.write('INHIBIT_DEFAULT_DEPS = "1"\n') |
| 147 | # We don't have the checksums yet so we can't require them |
| 148 | f.write('BB_STRICT_CHECKSUM = "ignore"\n') |
| 149 | f.write('SRC_URI = "%s"\n' % srcuri) |
| 150 | f.write('SRCREV = "%s"\n' % srcrev) |
| 151 | f.write('WORKDIR = "%s"\n' % tmpworkdir) |
| 152 | # Set S out of the way so it doesn't get created under the workdir |
| 153 | f.write('S = "%s"\n' % os.path.join(tmpdir, 'emptysrc')) |
| 154 | if not mirrors: |
| 155 | # We do not need PREMIRRORS since we are almost certainly |
| 156 | # fetching new source rather than something that has already |
| 157 | # been fetched. Hence, we disable them by default. |
| 158 | # However, we provide an option for users to enable it. |
| 159 | f.write('PREMIRRORS = ""\n') |
| 160 | f.write('MIRRORS = ""\n') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 161 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 162 | logger.info('Fetching %s...' % srcuri) |
| 163 | |
| 164 | # FIXME this is too noisy at the moment |
| 165 | |
| 166 | # Parse recipes so our new recipe gets picked up |
| 167 | tinfoil.parse_recipes() |
| 168 | |
| 169 | def eventhandler(event): |
| 170 | if isinstance(event, bb.fetch2.MissingChecksumEvent): |
| 171 | checksums.update(event.checksums) |
| 172 | return True |
| 173 | return False |
| 174 | |
| 175 | # Run the fetch + unpack tasks |
| 176 | res = tinfoil.build_targets(fetchrecipepn, |
| 177 | 'do_unpack', |
| 178 | handle_events=True, |
| 179 | extra_events=['bb.fetch2.MissingChecksumEvent'], |
| 180 | event_callback=eventhandler) |
| 181 | if not res: |
| 182 | raise FetchUrlFailure(srcuri) |
| 183 | |
| 184 | # Remove unneeded directories |
| 185 | rd = tinfoil.parse_recipe(fetchrecipepn) |
| 186 | if rd: |
| 187 | pathvars = ['T', 'RECIPE_SYSROOT', 'RECIPE_SYSROOT_NATIVE'] |
| 188 | for pathvar in pathvars: |
| 189 | path = rd.getVar(pathvar) |
| 190 | shutil.rmtree(path) |
| 191 | finally: |
| 192 | if fetchrecipe: |
| 193 | try: |
| 194 | os.remove(fetchrecipe) |
| 195 | except FileNotFoundError: |
| 196 | pass |
| 197 | try: |
| 198 | os.rmdir(fetchrecipedir) |
| 199 | except OSError as e: |
| 200 | import errno |
| 201 | if e.errno != errno.ENOTEMPTY: |
| 202 | raise |
| 203 | |
| 204 | bb.utils.mkdirhier(destdir) |
| 205 | for fn in os.listdir(tmpworkdir): |
| 206 | shutil.move(os.path.join(tmpworkdir, fn), destdir) |
| 207 | |
| 208 | finally: |
| 209 | if not preserve_tmp: |
| 210 | shutil.rmtree(tmpdir) |
| 211 | tmpdir = None |
| 212 | |
| 213 | return checksums, tmpdir |
| 214 | |
| 215 | |
| 216 | def run_editor(fn, logger=None): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 217 | if isinstance(fn, str): |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 218 | files = [fn] |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 219 | else: |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 220 | files = fn |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 221 | |
| 222 | editor = os.getenv('VISUAL', os.getenv('EDITOR', 'vi')) |
| 223 | try: |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 224 | #print(shlex.split(editor) + files) |
| 225 | return subprocess.check_call(shlex.split(editor) + files) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 226 | except subprocess.CalledProcessError as exc: |
| 227 | logger.error("Execution of '%s' failed: %s" % (editor, exc)) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 228 | return 1 |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 229 | |
| 230 | def is_src_url(param): |
| 231 | """ |
| 232 | Check if a parameter is a URL and return True if so |
| 233 | NOTE: be careful about changing this as it will influence how devtool/recipetool command line handling works |
| 234 | """ |
| 235 | if not param: |
| 236 | return False |
| 237 | elif '://' in param: |
| 238 | return True |
| 239 | elif param.startswith('git@') or ('@' in param and param.endswith('.git')): |
| 240 | return True |
| 241 | return False |