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