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