Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | """ |
| 2 | BitBake 'Fetch' implementations |
| 3 | |
| 4 | Classes for obtaining upstream sources for the |
| 5 | BitBake build tools. |
| 6 | """ |
| 7 | |
| 8 | # Copyright (C) 2003, 2004 Chris Larson |
| 9 | # Copyright (C) 2012 Intel Corporation |
| 10 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 11 | # SPDX-License-Identifier: GPL-2.0-only |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 12 | # |
| 13 | # Based on functions from the base bb module, Copyright 2003 Holger Schurig |
| 14 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 15 | import os, re |
| 16 | import signal |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 17 | import logging |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 18 | import urllib.request, urllib.parse, urllib.error |
| 19 | if 'git' not in urllib.parse.uses_netloc: |
| 20 | urllib.parse.uses_netloc.append('git') |
| 21 | import operator |
| 22 | import collections |
| 23 | import subprocess |
| 24 | import pickle |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 25 | import errno |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 26 | import bb.persist_data, bb.utils |
| 27 | import bb.checksum |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 28 | import bb.process |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 29 | import bb.event |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 30 | |
| 31 | __version__ = "2" |
| 32 | _checksum_cache = bb.checksum.FileChecksumCache() |
| 33 | |
| 34 | logger = logging.getLogger("BitBake.Fetcher") |
| 35 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 36 | CHECKSUM_LIST = [ "md5", "sha256", "sha1", "sha384", "sha512" ] |
| 37 | SHOWN_CHECKSUM_LIST = ["sha256"] |
| 38 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 39 | class BBFetchException(Exception): |
| 40 | """Class all fetch exceptions inherit from""" |
| 41 | def __init__(self, message): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 42 | self.msg = message |
| 43 | Exception.__init__(self, message) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 44 | |
| 45 | def __str__(self): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 46 | return self.msg |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 47 | |
| 48 | class UntrustedUrl(BBFetchException): |
| 49 | """Exception raised when encountering a host not listed in BB_ALLOWED_NETWORKS""" |
| 50 | def __init__(self, url, message=''): |
| 51 | if message: |
| 52 | msg = message |
| 53 | else: |
| 54 | msg = "The URL: '%s' is not trusted and cannot be used" % url |
| 55 | self.url = url |
| 56 | BBFetchException.__init__(self, msg) |
| 57 | self.args = (url,) |
| 58 | |
| 59 | class MalformedUrl(BBFetchException): |
| 60 | """Exception raised when encountering an invalid url""" |
| 61 | def __init__(self, url, message=''): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 62 | if message: |
| 63 | msg = message |
| 64 | else: |
| 65 | msg = "The URL: '%s' is invalid and cannot be interpreted" % url |
| 66 | self.url = url |
| 67 | BBFetchException.__init__(self, msg) |
| 68 | self.args = (url,) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 69 | |
| 70 | class FetchError(BBFetchException): |
| 71 | """General fetcher exception when something happens incorrectly""" |
| 72 | def __init__(self, message, url = None): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 73 | if url: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 74 | msg = "Fetcher failure for URL: '%s'. %s" % (url, message) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 75 | else: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 76 | msg = "Fetcher failure: %s" % message |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 77 | self.url = url |
| 78 | BBFetchException.__init__(self, msg) |
| 79 | self.args = (message, url) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 80 | |
| 81 | class ChecksumError(FetchError): |
| 82 | """Exception when mismatched checksum encountered""" |
| 83 | def __init__(self, message, url = None, checksum = None): |
| 84 | self.checksum = checksum |
| 85 | FetchError.__init__(self, message, url) |
| 86 | |
| 87 | class NoChecksumError(FetchError): |
| 88 | """Exception when no checksum is specified, but BB_STRICT_CHECKSUM is set""" |
| 89 | |
| 90 | class UnpackError(BBFetchException): |
| 91 | """General fetcher exception when something happens incorrectly when unpacking""" |
| 92 | def __init__(self, message, url): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 93 | msg = "Unpack failure for URL: '%s'. %s" % (url, message) |
| 94 | self.url = url |
| 95 | BBFetchException.__init__(self, msg) |
| 96 | self.args = (message, url) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 97 | |
| 98 | class NoMethodError(BBFetchException): |
| 99 | """Exception raised when there is no method to obtain a supplied url or set of urls""" |
| 100 | def __init__(self, url): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 101 | msg = "Could not find a fetcher which supports the URL: '%s'" % url |
| 102 | self.url = url |
| 103 | BBFetchException.__init__(self, msg) |
| 104 | self.args = (url,) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 105 | |
| 106 | class MissingParameterError(BBFetchException): |
| 107 | """Exception raised when a fetch method is missing a critical parameter in the url""" |
| 108 | def __init__(self, missing, url): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 109 | msg = "URL: '%s' is missing the required parameter '%s'" % (url, missing) |
| 110 | self.url = url |
| 111 | self.missing = missing |
| 112 | BBFetchException.__init__(self, msg) |
| 113 | self.args = (missing, url) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 114 | |
| 115 | class ParameterError(BBFetchException): |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 116 | """Exception raised when a url cannot be processed due to invalid parameters.""" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 117 | def __init__(self, message, url): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 118 | msg = "URL: '%s' has invalid parameters. %s" % (url, message) |
| 119 | self.url = url |
| 120 | BBFetchException.__init__(self, msg) |
| 121 | self.args = (message, url) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 122 | |
| 123 | class NetworkAccess(BBFetchException): |
| 124 | """Exception raised when network access is disabled but it is required.""" |
| 125 | def __init__(self, url, cmd): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 126 | msg = "Network access disabled through BB_NO_NETWORK (or set indirectly due to use of BB_FETCH_PREMIRRORONLY) but access requested with command %s (for url %s)" % (cmd, url) |
| 127 | self.url = url |
| 128 | self.cmd = cmd |
| 129 | BBFetchException.__init__(self, msg) |
| 130 | self.args = (url, cmd) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 131 | |
| 132 | class NonLocalMethod(Exception): |
| 133 | def __init__(self): |
| 134 | Exception.__init__(self) |
| 135 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 136 | class MissingChecksumEvent(bb.event.Event): |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 137 | def __init__(self, url, **checksums): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 138 | self.url = url |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 139 | self.checksums = checksums |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 140 | bb.event.Event.__init__(self) |
| 141 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 142 | |
| 143 | class URI(object): |
| 144 | """ |
| 145 | A class representing a generic URI, with methods for |
| 146 | accessing the URI components, and stringifies to the |
| 147 | URI. |
| 148 | |
| 149 | It is constructed by calling it with a URI, or setting |
| 150 | the attributes manually: |
| 151 | |
| 152 | uri = URI("http://example.com/") |
| 153 | |
| 154 | uri = URI() |
| 155 | uri.scheme = 'http' |
| 156 | uri.hostname = 'example.com' |
| 157 | uri.path = '/' |
| 158 | |
| 159 | It has the following attributes: |
| 160 | |
| 161 | * scheme (read/write) |
| 162 | * userinfo (authentication information) (read/write) |
| 163 | * username (read/write) |
| 164 | * password (read/write) |
| 165 | |
| 166 | Note, password is deprecated as of RFC 3986. |
| 167 | |
| 168 | * hostname (read/write) |
| 169 | * port (read/write) |
| 170 | * hostport (read only) |
| 171 | "hostname:port", if both are set, otherwise just "hostname" |
| 172 | * path (read/write) |
| 173 | * path_quoted (read/write) |
| 174 | A URI quoted version of path |
| 175 | * params (dict) (read/write) |
| 176 | * query (dict) (read/write) |
| 177 | * relative (bool) (read only) |
| 178 | True if this is a "relative URI", (e.g. file:foo.diff) |
| 179 | |
| 180 | It stringifies to the URI itself. |
| 181 | |
| 182 | Some notes about relative URIs: while it's specified that |
| 183 | a URI beginning with <scheme>:// should either be directly |
| 184 | followed by a hostname or a /, the old URI handling of the |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 185 | fetch2 library did not conform to this. Therefore, this URI |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 186 | class has some kludges to make sure that URIs are parsed in |
| 187 | a way comforming to bitbake's current usage. This URI class |
| 188 | supports the following: |
| 189 | |
| 190 | file:relative/path.diff (IETF compliant) |
| 191 | git:relative/path.git (IETF compliant) |
| 192 | git:///absolute/path.git (IETF compliant) |
| 193 | file:///absolute/path.diff (IETF compliant) |
| 194 | |
| 195 | file://relative/path.diff (not IETF compliant) |
| 196 | |
| 197 | But it does not support the following: |
| 198 | |
| 199 | file://hostname/absolute/path.diff (would be IETF compliant) |
| 200 | |
| 201 | Note that the last case only applies to a list of |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 202 | explicitly allowed schemes (currently only file://), that requires |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 203 | its URIs to not have a network location. |
| 204 | """ |
| 205 | |
| 206 | _relative_schemes = ['file', 'git'] |
| 207 | _netloc_forbidden = ['file'] |
| 208 | |
| 209 | def __init__(self, uri=None): |
| 210 | self.scheme = '' |
| 211 | self.userinfo = '' |
| 212 | self.hostname = '' |
| 213 | self.port = None |
| 214 | self._path = '' |
| 215 | self.params = {} |
| 216 | self.query = {} |
| 217 | self.relative = False |
| 218 | |
| 219 | if not uri: |
| 220 | return |
| 221 | |
| 222 | # We hijack the URL parameters, since the way bitbake uses |
| 223 | # them are not quite RFC compliant. |
| 224 | uri, param_str = (uri.split(";", 1) + [None])[:2] |
| 225 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 226 | urlp = urllib.parse.urlparse(uri) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 227 | self.scheme = urlp.scheme |
| 228 | |
| 229 | reparse = 0 |
| 230 | |
| 231 | # Coerce urlparse to make URI scheme use netloc |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 232 | if not self.scheme in urllib.parse.uses_netloc: |
| 233 | urllib.parse.uses_params.append(self.scheme) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 234 | reparse = 1 |
| 235 | |
| 236 | # Make urlparse happy(/ier) by converting local resources |
| 237 | # to RFC compliant URL format. E.g.: |
| 238 | # file://foo.diff -> file:foo.diff |
| 239 | if urlp.scheme in self._netloc_forbidden: |
| 240 | uri = re.sub("(?<=:)//(?!/)", "", uri, 1) |
| 241 | reparse = 1 |
| 242 | |
| 243 | if reparse: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 244 | urlp = urllib.parse.urlparse(uri) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 245 | |
| 246 | # Identify if the URI is relative or not |
| 247 | if urlp.scheme in self._relative_schemes and \ |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 248 | re.compile(r"^\w+:(?!//)").match(uri): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 249 | self.relative = True |
| 250 | |
| 251 | if not self.relative: |
| 252 | self.hostname = urlp.hostname or '' |
| 253 | self.port = urlp.port |
| 254 | |
| 255 | self.userinfo += urlp.username or '' |
| 256 | |
| 257 | if urlp.password: |
| 258 | self.userinfo += ':%s' % urlp.password |
| 259 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 260 | self.path = urllib.parse.unquote(urlp.path) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 261 | |
| 262 | if param_str: |
| 263 | self.params = self._param_str_split(param_str, ";") |
| 264 | if urlp.query: |
| 265 | self.query = self._param_str_split(urlp.query, "&") |
| 266 | |
| 267 | def __str__(self): |
| 268 | userinfo = self.userinfo |
| 269 | if userinfo: |
| 270 | userinfo += '@' |
| 271 | |
| 272 | return "%s:%s%s%s%s%s%s" % ( |
| 273 | self.scheme, |
| 274 | '' if self.relative else '//', |
| 275 | userinfo, |
| 276 | self.hostport, |
| 277 | self.path_quoted, |
| 278 | self._query_str(), |
| 279 | self._param_str()) |
| 280 | |
| 281 | def _param_str(self): |
| 282 | return ( |
| 283 | ''.join([';', self._param_str_join(self.params, ";")]) |
| 284 | if self.params else '') |
| 285 | |
| 286 | def _query_str(self): |
| 287 | return ( |
| 288 | ''.join(['?', self._param_str_join(self.query, "&")]) |
| 289 | if self.query else '') |
| 290 | |
| 291 | def _param_str_split(self, string, elmdelim, kvdelim="="): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 292 | ret = collections.OrderedDict() |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 293 | for k, v in [x.split(kvdelim, 1) for x in string.split(elmdelim) if x]: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 294 | ret[k] = v |
| 295 | return ret |
| 296 | |
| 297 | def _param_str_join(self, dict_, elmdelim, kvdelim="="): |
| 298 | return elmdelim.join([kvdelim.join([k, v]) for k, v in dict_.items()]) |
| 299 | |
| 300 | @property |
| 301 | def hostport(self): |
| 302 | if not self.port: |
| 303 | return self.hostname |
| 304 | return "%s:%d" % (self.hostname, self.port) |
| 305 | |
| 306 | @property |
| 307 | def path_quoted(self): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 308 | return urllib.parse.quote(self.path) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 309 | |
| 310 | @path_quoted.setter |
| 311 | def path_quoted(self, path): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 312 | self.path = urllib.parse.unquote(path) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 313 | |
| 314 | @property |
| 315 | def path(self): |
| 316 | return self._path |
| 317 | |
| 318 | @path.setter |
| 319 | def path(self, path): |
| 320 | self._path = path |
| 321 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 322 | if not path or re.compile("^/").match(path): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 323 | self.relative = False |
| 324 | else: |
| 325 | self.relative = True |
| 326 | |
| 327 | @property |
| 328 | def username(self): |
| 329 | if self.userinfo: |
| 330 | return (self.userinfo.split(":", 1))[0] |
| 331 | return '' |
| 332 | |
| 333 | @username.setter |
| 334 | def username(self, username): |
| 335 | password = self.password |
| 336 | self.userinfo = username |
| 337 | if password: |
| 338 | self.userinfo += ":%s" % password |
| 339 | |
| 340 | @property |
| 341 | def password(self): |
| 342 | if self.userinfo and ":" in self.userinfo: |
| 343 | return (self.userinfo.split(":", 1))[1] |
| 344 | return '' |
| 345 | |
| 346 | @password.setter |
| 347 | def password(self, password): |
| 348 | self.userinfo = "%s:%s" % (self.username, password) |
| 349 | |
| 350 | def decodeurl(url): |
| 351 | """Decodes an URL into the tokens (scheme, network location, path, |
| 352 | user, password, parameters). |
| 353 | """ |
| 354 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 355 | m = re.compile('(?P<type>[^:]*)://((?P<user>[^/;]+)@)?(?P<location>[^;]+)(;(?P<parm>.*))?').match(url) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 356 | if not m: |
| 357 | raise MalformedUrl(url) |
| 358 | |
| 359 | type = m.group('type') |
| 360 | location = m.group('location') |
| 361 | if not location: |
| 362 | raise MalformedUrl(url) |
| 363 | user = m.group('user') |
| 364 | parm = m.group('parm') |
| 365 | |
| 366 | locidx = location.find('/') |
| 367 | if locidx != -1 and type.lower() != 'file': |
| 368 | host = location[:locidx] |
| 369 | path = location[locidx:] |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 370 | elif type.lower() == 'file': |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 371 | host = "" |
| 372 | path = location |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 373 | else: |
| 374 | host = location |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 375 | path = "/" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 376 | if user: |
| 377 | m = re.compile('(?P<user>[^:]+)(:?(?P<pswd>.*))').match(user) |
| 378 | if m: |
| 379 | user = m.group('user') |
| 380 | pswd = m.group('pswd') |
| 381 | else: |
| 382 | user = '' |
| 383 | pswd = '' |
| 384 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 385 | p = collections.OrderedDict() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 386 | if parm: |
| 387 | for s in parm.split(';'): |
| 388 | if s: |
| 389 | if not '=' in s: |
| 390 | raise MalformedUrl(url, "The URL: '%s' is invalid: parameter %s does not specify a value (missing '=')" % (url, s)) |
| 391 | s1, s2 = s.split('=') |
| 392 | p[s1] = s2 |
| 393 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 394 | return type, host, urllib.parse.unquote(path), user, pswd, p |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 395 | |
| 396 | def encodeurl(decoded): |
| 397 | """Encodes a URL from tokens (scheme, network location, path, |
| 398 | user, password, parameters). |
| 399 | """ |
| 400 | |
| 401 | type, host, path, user, pswd, p = decoded |
| 402 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 403 | if not type: |
| 404 | raise MissingParameterError('type', "encoded from the data %s" % str(decoded)) |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 405 | url = ['%s://' % type] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 406 | if user and type != "file": |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 407 | url.append("%s" % user) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 408 | if pswd: |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 409 | url.append(":%s" % pswd) |
| 410 | url.append("@") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 411 | if host and type != "file": |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 412 | url.append("%s" % host) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 413 | if path: |
| 414 | # Standardise path to ensure comparisons work |
| 415 | while '//' in path: |
| 416 | path = path.replace("//", "/") |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 417 | url.append("%s" % urllib.parse.quote(path)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 418 | if p: |
| 419 | for parm in p: |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 420 | url.append(";%s=%s" % (parm, p[parm])) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 421 | |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 422 | return "".join(url) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 423 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 424 | def uri_replace(ud, uri_find, uri_replace, replacements, d, mirrortarball=None): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 425 | if not ud.url or not uri_find or not uri_replace: |
| 426 | logger.error("uri_replace: passed an undefined value, not replacing") |
| 427 | return None |
| 428 | uri_decoded = list(decodeurl(ud.url)) |
| 429 | uri_find_decoded = list(decodeurl(uri_find)) |
| 430 | uri_replace_decoded = list(decodeurl(uri_replace)) |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 431 | logger.debug2("For url %s comparing %s to %s" % (uri_decoded, uri_find_decoded, uri_replace_decoded)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 432 | result_decoded = ['', '', '', '', '', {}] |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 433 | # 0 - type, 1 - host, 2 - path, 3 - user, 4- pswd, 5 - params |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 434 | for loc, i in enumerate(uri_find_decoded): |
| 435 | result_decoded[loc] = uri_decoded[loc] |
| 436 | regexp = i |
| 437 | if loc == 0 and regexp and not regexp.endswith("$"): |
| 438 | # Leaving the type unanchored can mean "https" matching "file" can become "files" |
| 439 | # which is clearly undesirable. |
| 440 | regexp += "$" |
| 441 | if loc == 5: |
| 442 | # Handle URL parameters |
| 443 | if i: |
| 444 | # Any specified URL parameters must match |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 445 | for k in uri_find_decoded[loc]: |
| 446 | if uri_decoded[loc][k] != uri_find_decoded[loc][k]: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 447 | return None |
| 448 | # Overwrite any specified replacement parameters |
| 449 | for k in uri_replace_decoded[loc]: |
| 450 | for l in replacements: |
| 451 | uri_replace_decoded[loc][k] = uri_replace_decoded[loc][k].replace(l, replacements[l]) |
| 452 | result_decoded[loc][k] = uri_replace_decoded[loc][k] |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 453 | elif (loc == 3 or loc == 4) and uri_replace_decoded[loc]: |
| 454 | # User/password in the replacement is just a straight replacement |
| 455 | result_decoded[loc] = uri_replace_decoded[loc] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 456 | elif (re.match(regexp, uri_decoded[loc])): |
| 457 | if not uri_replace_decoded[loc]: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 458 | result_decoded[loc] = "" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 459 | else: |
| 460 | for k in replacements: |
| 461 | uri_replace_decoded[loc] = uri_replace_decoded[loc].replace(k, replacements[k]) |
| 462 | #bb.note("%s %s %s" % (regexp, uri_replace_decoded[loc], uri_decoded[loc])) |
Patrick Williams | d7e9631 | 2015-09-22 08:09:05 -0500 | [diff] [blame] | 463 | result_decoded[loc] = re.sub(regexp, uri_replace_decoded[loc], uri_decoded[loc], 1) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 464 | if loc == 2: |
| 465 | # Handle path manipulations |
| 466 | basename = None |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 467 | if uri_decoded[0] != uri_replace_decoded[0] and mirrortarball: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 468 | # If the source and destination url types differ, must be a mirrortarball mapping |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 469 | basename = os.path.basename(mirrortarball) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 470 | # Kill parameters, they make no sense for mirror tarballs |
| 471 | uri_decoded[5] = {} |
Andrew Geissler | c5535c9 | 2023-01-27 16:10:19 -0600 | [diff] [blame] | 472 | uri_find_decoded[5] = {} |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 473 | elif ud.localpath and ud.method.supports_checksum(ud): |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 474 | basename = os.path.basename(ud.localpath) |
| 475 | if basename: |
| 476 | uri_basename = os.path.basename(uri_decoded[loc]) |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 477 | # Prefix with a slash as a sentinel in case |
| 478 | # result_decoded[loc] does not contain one. |
| 479 | path = "/" + result_decoded[loc] |
| 480 | if uri_basename and basename != uri_basename and path.endswith("/" + uri_basename): |
| 481 | result_decoded[loc] = path[1:-len(uri_basename)] + basename |
| 482 | elif not path.endswith("/" + basename): |
| 483 | result_decoded[loc] = os.path.join(path[1:], basename) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 484 | else: |
| 485 | return None |
| 486 | result = encodeurl(result_decoded) |
| 487 | if result == ud.url: |
| 488 | return None |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 489 | logger.debug2("For url %s returning %s" % (ud.url, result)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 490 | return result |
| 491 | |
| 492 | methods = [] |
| 493 | urldata_cache = {} |
| 494 | saved_headrevs = {} |
| 495 | |
| 496 | def fetcher_init(d): |
| 497 | """ |
| 498 | Called to initialize the fetchers once the configuration data is known. |
| 499 | Calls before this must not hit the cache. |
| 500 | """ |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 501 | |
| 502 | revs = bb.persist_data.persist('BB_URI_HEADREVS', d) |
| 503 | try: |
| 504 | # fetcher_init is called multiple times, so make sure we only save the |
| 505 | # revs the first time it is called. |
| 506 | if not bb.fetch2.saved_headrevs: |
| 507 | bb.fetch2.saved_headrevs = dict(revs) |
| 508 | except: |
| 509 | pass |
| 510 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 511 | # When to drop SCM head revisions controlled by user policy |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 512 | srcrev_policy = d.getVar('BB_SRCREV_POLICY') or "clear" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 513 | if srcrev_policy == "cache": |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 514 | logger.debug("Keeping SRCREV cache due to cache policy of: %s", srcrev_policy) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 515 | elif srcrev_policy == "clear": |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 516 | logger.debug("Clearing SRCREV cache due to cache policy of: %s", srcrev_policy) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 517 | revs.clear() |
| 518 | else: |
| 519 | raise FetchError("Invalid SRCREV cache policy of: %s" % srcrev_policy) |
| 520 | |
Andrew Geissler | c5535c9 | 2023-01-27 16:10:19 -0600 | [diff] [blame] | 521 | _checksum_cache.init_cache(d.getVar("BB_CACHEDIR")) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 522 | |
| 523 | for m in methods: |
| 524 | if hasattr(m, "init"): |
| 525 | m.init(d) |
| 526 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 527 | def fetcher_parse_save(): |
| 528 | _checksum_cache.save_extras() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 529 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 530 | def fetcher_parse_done(): |
| 531 | _checksum_cache.save_merge() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 532 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 533 | def fetcher_compare_revisions(d): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 534 | """ |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 535 | Compare the revisions in the persistent cache with the saved values from |
| 536 | when bitbake was started and return true if they have changed. |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 537 | """ |
| 538 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 539 | headrevs = dict(bb.persist_data.persist('BB_URI_HEADREVS', d)) |
| 540 | return headrevs != bb.fetch2.saved_headrevs |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 541 | |
| 542 | def mirror_from_string(data): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 543 | mirrors = (data or "").replace('\\n',' ').split() |
| 544 | # Split into pairs |
| 545 | if len(mirrors) % 2 != 0: |
| 546 | bb.warn('Invalid mirror data %s, should have paired members.' % data) |
| 547 | return list(zip(*[iter(mirrors)]*2)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 548 | |
Andrew Geissler | 87f5cff | 2022-09-30 13:13:31 -0500 | [diff] [blame] | 549 | def verify_checksum(ud, d, precomputed={}, localpath=None, fatal_nochecksum=True): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 550 | """ |
| 551 | verify the MD5 and SHA256 checksum for downloaded src |
| 552 | |
| 553 | Raises a FetchError if one or both of the SRC_URI checksums do not match |
| 554 | the downloaded file, or if BB_STRICT_CHECKSUM is set and there are no |
| 555 | checksums specified. |
| 556 | |
| 557 | Returns a dict of checksums that can be stored in a done stamp file and |
| 558 | passed in as precomputed parameter in a later call to avoid re-computing |
| 559 | the checksums from the file. This allows verifying the checksums of the |
| 560 | file against those in the recipe each time, rather than only after |
| 561 | downloading. See https://bugzilla.yoctoproject.org/show_bug.cgi?id=5571. |
| 562 | """ |
| 563 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 564 | if ud.ignore_checksums or not ud.method.supports_checksum(ud): |
| 565 | return {} |
| 566 | |
Andrew Geissler | 87f5cff | 2022-09-30 13:13:31 -0500 | [diff] [blame] | 567 | if localpath is None: |
| 568 | localpath = ud.localpath |
| 569 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 570 | def compute_checksum_info(checksum_id): |
| 571 | checksum_name = getattr(ud, "%s_name" % checksum_id) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 572 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 573 | if checksum_id in precomputed: |
| 574 | checksum_data = precomputed[checksum_id] |
| 575 | else: |
Andrew Geissler | 87f5cff | 2022-09-30 13:13:31 -0500 | [diff] [blame] | 576 | checksum_data = getattr(bb.utils, "%s_file" % checksum_id)(localpath) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 577 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 578 | checksum_expected = getattr(ud, "%s_expected" % checksum_id) |
| 579 | |
Andrew Geissler | 0903674 | 2021-06-25 14:25:14 -0500 | [diff] [blame] | 580 | if checksum_expected == '': |
| 581 | checksum_expected = None |
| 582 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 583 | return { |
| 584 | "id": checksum_id, |
| 585 | "name": checksum_name, |
| 586 | "data": checksum_data, |
| 587 | "expected": checksum_expected |
| 588 | } |
| 589 | |
| 590 | checksum_infos = [] |
| 591 | for checksum_id in CHECKSUM_LIST: |
| 592 | checksum_infos.append(compute_checksum_info(checksum_id)) |
| 593 | |
| 594 | checksum_dict = {ci["id"] : ci["data"] for ci in checksum_infos} |
| 595 | checksum_event = {"%ssum" % ci["id"] : ci["data"] for ci in checksum_infos} |
| 596 | |
| 597 | for ci in checksum_infos: |
| 598 | if ci["id"] in SHOWN_CHECKSUM_LIST: |
| 599 | checksum_lines = ["SRC_URI[%s] = \"%s\"" % (ci["name"], ci["data"])] |
| 600 | |
| 601 | # If no checksum has been provided |
Andrew Geissler | 87f5cff | 2022-09-30 13:13:31 -0500 | [diff] [blame] | 602 | if fatal_nochecksum and ud.method.recommends_checksum(ud) and all(ci["expected"] is None for ci in checksum_infos): |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 603 | messages = [] |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 604 | strict = d.getVar("BB_STRICT_CHECKSUM") or "0" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 605 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 606 | # If strict checking enabled and neither sum defined, raise error |
| 607 | if strict == "1": |
| 608 | messages.append("No checksum specified for '%s', please add at " \ |
| 609 | "least one to the recipe:" % ud.localpath) |
| 610 | messages.extend(checksum_lines) |
| 611 | logger.error("\n".join(messages)) |
| 612 | raise NoChecksumError("Missing SRC_URI checksum", ud.url) |
| 613 | |
| 614 | bb.event.fire(MissingChecksumEvent(ud.url, **checksum_event), d) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 615 | |
| 616 | if strict == "ignore": |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 617 | return checksum_dict |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 618 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 619 | # Log missing sums so user can more easily add them |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 620 | messages.append("Missing checksum for '%s', consider adding at " \ |
| 621 | "least one to the recipe:" % ud.localpath) |
| 622 | messages.extend(checksum_lines) |
| 623 | logger.warning("\n".join(messages)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 624 | |
| 625 | # We want to alert the user if a checksum is defined in the recipe but |
| 626 | # it does not match. |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 627 | messages = [] |
| 628 | messages.append("Checksum mismatch!") |
| 629 | bad_checksum = None |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 630 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 631 | for ci in checksum_infos: |
| 632 | if ci["expected"] and ci["expected"] != ci["data"]: |
Andrew Geissler | 0903674 | 2021-06-25 14:25:14 -0500 | [diff] [blame] | 633 | messages.append("File: '%s' has %s checksum '%s' when '%s' was " \ |
Andrew Geissler | 87f5cff | 2022-09-30 13:13:31 -0500 | [diff] [blame] | 634 | "expected" % (localpath, ci["id"], ci["data"], ci["expected"])) |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 635 | bad_checksum = ci["data"] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 636 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 637 | if bad_checksum: |
| 638 | messages.append("If this change is expected (e.g. you have upgraded " \ |
| 639 | "to a new version without updating the checksums) " \ |
| 640 | "then you can use these lines within the recipe:") |
| 641 | messages.extend(checksum_lines) |
| 642 | messages.append("Otherwise you should retry the download and/or " \ |
| 643 | "check with upstream to determine if the file has " \ |
| 644 | "become corrupted or otherwise unexpectedly modified.") |
| 645 | raise ChecksumError("\n".join(messages), ud.url, bad_checksum) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 646 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 647 | return checksum_dict |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 648 | |
| 649 | def verify_donestamp(ud, d, origud=None): |
| 650 | """ |
| 651 | Check whether the done stamp file has the right checksums (if the fetch |
| 652 | method supports them). If it doesn't, delete the done stamp and force |
| 653 | a re-download. |
| 654 | |
| 655 | Returns True, if the donestamp exists and is valid, False otherwise. When |
| 656 | returning False, any existing done stamps are removed. |
| 657 | """ |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 658 | if not ud.needdonestamp or (origud and not origud.needdonestamp): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 659 | return True |
| 660 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 661 | if not os.path.exists(ud.localpath): |
| 662 | # local path does not exist |
| 663 | if os.path.exists(ud.donestamp): |
| 664 | # done stamp exists, but the downloaded file does not; the done stamp |
| 665 | # must be incorrect, re-trigger the download |
| 666 | bb.utils.remove(ud.donestamp) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 667 | return False |
| 668 | |
| 669 | if (not ud.method.supports_checksum(ud) or |
| 670 | (origud and not origud.method.supports_checksum(origud))): |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 671 | # if done stamp exists and checksums not supported; assume the local |
| 672 | # file is current |
| 673 | return os.path.exists(ud.donestamp) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 674 | |
| 675 | precomputed_checksums = {} |
| 676 | # Only re-use the precomputed checksums if the donestamp is newer than the |
| 677 | # file. Do not rely on the mtime of directories, though. If ud.localpath is |
| 678 | # a directory, there will probably not be any checksums anyway. |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 679 | if os.path.exists(ud.donestamp) and (os.path.isdir(ud.localpath) or |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 680 | os.path.getmtime(ud.localpath) < os.path.getmtime(ud.donestamp)): |
| 681 | try: |
| 682 | with open(ud.donestamp, "rb") as cachefile: |
| 683 | pickled = pickle.Unpickler(cachefile) |
| 684 | precomputed_checksums.update(pickled.load()) |
| 685 | except Exception as e: |
| 686 | # Avoid the warnings on the upgrade path from emtpy done stamp |
| 687 | # files to those containing the checksums. |
| 688 | if not isinstance(e, EOFError): |
| 689 | # Ignore errors, they aren't fatal |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 690 | logger.warning("Couldn't load checksums from donestamp %s: %s " |
| 691 | "(msg: %s)" % (ud.donestamp, type(e).__name__, |
| 692 | str(e))) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 693 | |
| 694 | try: |
| 695 | checksums = verify_checksum(ud, d, precomputed_checksums) |
| 696 | # If the cache file did not have the checksums, compute and store them |
| 697 | # as an upgrade path from the previous done stamp file format. |
| 698 | if checksums != precomputed_checksums: |
| 699 | with open(ud.donestamp, "wb") as cachefile: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 700 | p = pickle.Pickler(cachefile, 2) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 701 | p.dump(checksums) |
| 702 | return True |
| 703 | except ChecksumError as e: |
| 704 | # Checksums failed to verify, trigger re-download and remove the |
| 705 | # incorrect stamp file. |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 706 | logger.warning("Checksum mismatch for local file %s\n" |
| 707 | "Cleaning and trying again." % ud.localpath) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 708 | if os.path.exists(ud.localpath): |
| 709 | rename_bad_checksum(ud, e.checksum) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 710 | bb.utils.remove(ud.donestamp) |
| 711 | return False |
| 712 | |
| 713 | |
| 714 | def update_stamp(ud, d): |
| 715 | """ |
| 716 | donestamp is file stamp indicating the whole fetching is done |
| 717 | this function update the stamp after verifying the checksum |
| 718 | """ |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 719 | if not ud.needdonestamp: |
| 720 | return |
| 721 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 722 | if os.path.exists(ud.donestamp): |
| 723 | # Touch the done stamp file to show active use of the download |
| 724 | try: |
| 725 | os.utime(ud.donestamp, None) |
| 726 | except: |
| 727 | # Errors aren't fatal here |
| 728 | pass |
| 729 | else: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 730 | try: |
| 731 | checksums = verify_checksum(ud, d) |
| 732 | # Store the checksums for later re-verification against the recipe |
| 733 | with open(ud.donestamp, "wb") as cachefile: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 734 | p = pickle.Pickler(cachefile, 2) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 735 | p.dump(checksums) |
| 736 | except ChecksumError as e: |
| 737 | # Checksums failed to verify, trigger re-download and remove the |
| 738 | # incorrect stamp file. |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 739 | logger.warning("Checksum mismatch for local file %s\n" |
| 740 | "Cleaning and trying again." % ud.localpath) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 741 | if os.path.exists(ud.localpath): |
| 742 | rename_bad_checksum(ud, e.checksum) |
| 743 | bb.utils.remove(ud.donestamp) |
| 744 | raise |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 745 | |
| 746 | def subprocess_setup(): |
| 747 | # Python installs a SIGPIPE handler by default. This is usually not what |
| 748 | # non-Python subprocesses expect. |
| 749 | # SIGPIPE errors are known issues with gzip/bash |
| 750 | signal.signal(signal.SIGPIPE, signal.SIG_DFL) |
| 751 | |
| 752 | def get_autorev(d): |
| 753 | # only not cache src rev in autorev case |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 754 | if d.getVar('BB_SRCREV_POLICY') != "cache": |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 755 | d.setVar('BB_DONT_CACHE', '1') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 756 | return "AUTOINC" |
| 757 | |
| 758 | def get_srcrev(d, method_name='sortable_revision'): |
| 759 | """ |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 760 | Return the revision string, usually for use in the version string (PV) of the current package |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 761 | Most packages usually only have one SCM so we just pass on the call. |
| 762 | In the multi SCM case, we build a value based on SRCREV_FORMAT which must |
| 763 | have been set. |
| 764 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 765 | The idea here is that we put the string "AUTOINC+" into return value if the revisions are not |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 766 | incremental, other code is then responsible for turning that into an increasing value (if needed) |
| 767 | |
| 768 | A method_name can be supplied to retrieve an alternatively formatted revision from a fetcher, if |
| 769 | that fetcher provides a method with the given name and the same signature as sortable_revision. |
| 770 | """ |
| 771 | |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 772 | d.setVar("__BBSEENSRCREV", "1") |
Andrew Geissler | 5199d83 | 2021-09-24 16:47:35 -0500 | [diff] [blame] | 773 | recursion = d.getVar("__BBINSRCREV") |
| 774 | if recursion: |
| 775 | raise FetchError("There are recursive references in fetcher variables, likely through SRC_URI") |
| 776 | d.setVar("__BBINSRCREV", True) |
| 777 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 778 | scms = [] |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 779 | fetcher = Fetch(d.getVar('SRC_URI').split(), d) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 780 | urldata = fetcher.ud |
| 781 | for u in urldata: |
| 782 | if urldata[u].method.supports_srcrev(): |
| 783 | scms.append(u) |
| 784 | |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 785 | if not scms: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 786 | raise FetchError("SRCREV was used yet no valid SCM was found in SRC_URI") |
| 787 | |
| 788 | if len(scms) == 1 and len(urldata[scms[0]].names) == 1: |
| 789 | autoinc, rev = getattr(urldata[scms[0]].method, method_name)(urldata[scms[0]], d, urldata[scms[0]].names[0]) |
| 790 | if len(rev) > 10: |
| 791 | rev = rev[:10] |
Andrew Geissler | 5199d83 | 2021-09-24 16:47:35 -0500 | [diff] [blame] | 792 | d.delVar("__BBINSRCREV") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 793 | if autoinc: |
| 794 | return "AUTOINC+" + rev |
| 795 | return rev |
| 796 | |
| 797 | # |
| 798 | # Mutiple SCMs are in SRC_URI so we resort to SRCREV_FORMAT |
| 799 | # |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 800 | format = d.getVar('SRCREV_FORMAT') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 801 | if not format: |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 802 | raise FetchError("The SRCREV_FORMAT variable must be set when multiple SCMs are used.\n"\ |
| 803 | "The SCMs are:\n%s" % '\n'.join(scms)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 804 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 805 | name_to_rev = {} |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 806 | seenautoinc = False |
| 807 | for scm in scms: |
| 808 | ud = urldata[scm] |
| 809 | for name in ud.names: |
| 810 | autoinc, rev = getattr(ud.method, method_name)(ud, d, name) |
| 811 | seenautoinc = seenautoinc or autoinc |
| 812 | if len(rev) > 10: |
| 813 | rev = rev[:10] |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 814 | name_to_rev[name] = rev |
| 815 | # Replace names by revisions in the SRCREV_FORMAT string. The approach used |
| 816 | # here can handle names being prefixes of other names and names appearing |
| 817 | # as substrings in revisions (in which case the name should not be |
| 818 | # expanded). The '|' regular expression operator tries matches from left to |
| 819 | # right, so we need to sort the names with the longest ones first. |
| 820 | names_descending_len = sorted(name_to_rev, key=len, reverse=True) |
| 821 | name_to_rev_re = "|".join(re.escape(name) for name in names_descending_len) |
| 822 | format = re.sub(name_to_rev_re, lambda match: name_to_rev[match.group(0)], format) |
| 823 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 824 | if seenautoinc: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 825 | format = "AUTOINC+" + format |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 826 | |
Andrew Geissler | 5199d83 | 2021-09-24 16:47:35 -0500 | [diff] [blame] | 827 | d.delVar("__BBINSRCREV") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 828 | return format |
| 829 | |
| 830 | def localpath(url, d): |
| 831 | fetcher = bb.fetch2.Fetch([url], d) |
| 832 | return fetcher.localpath(url) |
| 833 | |
Patrick Williams | 0ca19cc | 2021-08-16 14:03:13 -0500 | [diff] [blame] | 834 | # Need to export PATH as binary could be in metadata paths |
| 835 | # rather than host provided |
| 836 | # Also include some other variables. |
| 837 | FETCH_EXPORT_VARS = ['HOME', 'PATH', |
| 838 | 'HTTP_PROXY', 'http_proxy', |
| 839 | 'HTTPS_PROXY', 'https_proxy', |
| 840 | 'FTP_PROXY', 'ftp_proxy', |
| 841 | 'FTPS_PROXY', 'ftps_proxy', |
| 842 | 'NO_PROXY', 'no_proxy', |
| 843 | 'ALL_PROXY', 'all_proxy', |
| 844 | 'GIT_PROXY_COMMAND', |
| 845 | 'GIT_SSH', |
Patrick Williams | 03907ee | 2022-05-01 06:28:52 -0500 | [diff] [blame] | 846 | 'GIT_SSH_COMMAND', |
Patrick Williams | 0ca19cc | 2021-08-16 14:03:13 -0500 | [diff] [blame] | 847 | 'GIT_SSL_CAINFO', |
| 848 | 'GIT_SMART_HTTP', |
| 849 | 'SSH_AUTH_SOCK', 'SSH_AGENT_PID', |
| 850 | 'SOCKS5_USER', 'SOCKS5_PASSWD', |
| 851 | 'DBUS_SESSION_BUS_ADDRESS', |
| 852 | 'P4CONFIG', |
| 853 | 'SSL_CERT_FILE', |
Patrick Williams | 864cc43 | 2023-02-09 14:54:44 -0600 | [diff] [blame^] | 854 | 'NODE_EXTRA_CA_CERTS', |
Andrew Geissler | 5199d83 | 2021-09-24 16:47:35 -0500 | [diff] [blame] | 855 | 'AWS_PROFILE', |
Patrick Williams | 0ca19cc | 2021-08-16 14:03:13 -0500 | [diff] [blame] | 856 | 'AWS_ACCESS_KEY_ID', |
| 857 | 'AWS_SECRET_ACCESS_KEY', |
| 858 | 'AWS_DEFAULT_REGION'] |
| 859 | |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 860 | def get_fetcher_environment(d): |
| 861 | newenv = {} |
| 862 | origenv = d.getVar("BB_ORIGENV") |
| 863 | for name in bb.fetch2.FETCH_EXPORT_VARS: |
| 864 | value = d.getVar(name) |
| 865 | if not value and origenv: |
| 866 | value = origenv.getVar(name) |
| 867 | if value: |
| 868 | newenv[name] = value |
| 869 | return newenv |
| 870 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 871 | def runfetchcmd(cmd, d, quiet=False, cleanup=None, log=None, workdir=None): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 872 | """ |
| 873 | Run cmd returning the command output |
| 874 | Raise an error if interrupted or cmd fails |
| 875 | Optionally echo command output to stdout |
| 876 | Optionally remove the files/directories listed in cleanup upon failure |
| 877 | """ |
| 878 | |
Patrick Williams | 0ca19cc | 2021-08-16 14:03:13 -0500 | [diff] [blame] | 879 | exportvars = FETCH_EXPORT_VARS |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 880 | |
| 881 | if not cleanup: |
| 882 | cleanup = [] |
| 883 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 884 | # If PATH contains WORKDIR which contains PV-PR which contains SRCPV we |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 885 | # can end up in circular recursion here so give the option of breaking it |
| 886 | # in a data store copy. |
| 887 | try: |
| 888 | d.getVar("PV") |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 889 | d.getVar("PR") |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 890 | except bb.data_smart.ExpansionError: |
| 891 | d = bb.data.createCopy(d) |
| 892 | d.setVar("PV", "fetcheravoidrecurse") |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 893 | d.setVar("PR", "fetcheravoidrecurse") |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 894 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 895 | origenv = d.getVar("BB_ORIGENV", False) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 896 | for var in exportvars: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 897 | val = d.getVar(var) or (origenv and origenv.getVar(var)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 898 | if val: |
| 899 | cmd = 'export ' + var + '=\"%s\"; %s' % (val, cmd) |
| 900 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 901 | # Disable pseudo as it may affect ssh, potentially causing it to hang. |
| 902 | cmd = 'export PSEUDO_DISABLED=1; ' + cmd |
| 903 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 904 | if workdir: |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 905 | logger.debug("Running '%s' in %s" % (cmd, workdir)) |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 906 | else: |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 907 | logger.debug("Running %s", cmd) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 908 | |
| 909 | success = False |
| 910 | error_message = "" |
| 911 | |
| 912 | try: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 913 | (output, errors) = bb.process.run(cmd, log=log, shell=True, stderr=subprocess.PIPE, cwd=workdir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 914 | success = True |
| 915 | except bb.process.NotFoundError as e: |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 916 | error_message = "Fetch command %s not found" % (e.command) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 917 | except bb.process.ExecutionError as e: |
| 918 | if e.stdout: |
| 919 | output = "output:\n%s\n%s" % (e.stdout, e.stderr) |
| 920 | elif e.stderr: |
| 921 | output = "output:\n%s" % e.stderr |
| 922 | else: |
| 923 | output = "no output" |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 924 | error_message = "Fetch command %s failed with exit code %s, %s" % (e.command, e.exitcode, output) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 925 | except bb.process.CmdError as e: |
| 926 | error_message = "Fetch command %s could not be run:\n%s" % (e.command, e.msg) |
| 927 | if not success: |
| 928 | for f in cleanup: |
| 929 | try: |
| 930 | bb.utils.remove(f, True) |
| 931 | except OSError: |
| 932 | pass |
| 933 | |
| 934 | raise FetchError(error_message) |
| 935 | |
| 936 | return output |
| 937 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 938 | def check_network_access(d, info, url): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 939 | """ |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 940 | log remote network access, and error if BB_NO_NETWORK is set or the given |
| 941 | URI is untrusted |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 942 | """ |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 943 | if bb.utils.to_boolean(d.getVar("BB_NO_NETWORK")): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 944 | raise NetworkAccess(url, info) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 945 | elif not trusted_network(d, url): |
| 946 | raise UntrustedUrl(url, info) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 947 | else: |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 948 | logger.debug("Fetcher accessed the network with the command %s" % info) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 949 | |
| 950 | def build_mirroruris(origud, mirrors, ld): |
| 951 | uris = [] |
| 952 | uds = [] |
| 953 | |
| 954 | replacements = {} |
| 955 | replacements["TYPE"] = origud.type |
| 956 | replacements["HOST"] = origud.host |
| 957 | replacements["PATH"] = origud.path |
| 958 | replacements["BASENAME"] = origud.path.split("/")[-1] |
| 959 | replacements["MIRRORNAME"] = origud.host.replace(':','.') + origud.path.replace('/', '.').replace('*', '.') |
| 960 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 961 | def adduri(ud, uris, uds, mirrors, tarballs): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 962 | for line in mirrors: |
| 963 | try: |
| 964 | (find, replace) = line |
| 965 | except ValueError: |
| 966 | continue |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 967 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 968 | for tarball in tarballs: |
| 969 | newuri = uri_replace(ud, find, replace, replacements, ld, tarball) |
| 970 | if not newuri or newuri in uris or newuri == origud.url: |
| 971 | continue |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 972 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 973 | if not trusted_network(ld, newuri): |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 974 | logger.debug("Mirror %s not in the list of trusted networks, skipping" % (newuri)) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 975 | continue |
Patrick Williams | d7e9631 | 2015-09-22 08:09:05 -0500 | [diff] [blame] | 976 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 977 | # Create a local copy of the mirrors minus the current line |
| 978 | # this will prevent us from recursively processing the same line |
| 979 | # as well as indirect recursion A -> B -> C -> A |
| 980 | localmirrors = list(mirrors) |
| 981 | localmirrors.remove(line) |
| 982 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 983 | try: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 984 | newud = FetchData(newuri, ld) |
Andrew Geissler | 87f5cff | 2022-09-30 13:13:31 -0500 | [diff] [blame] | 985 | newud.ignore_checksums = True |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 986 | newud.setup_localpath(ld) |
| 987 | except bb.fetch2.BBFetchException as e: |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 988 | logger.debug("Mirror fetch failure for url %s (original url: %s)" % (newuri, origud.url)) |
| 989 | logger.debug(str(e)) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 990 | try: |
| 991 | # setup_localpath of file:// urls may fail, we should still see |
| 992 | # if mirrors of the url exist |
| 993 | adduri(newud, uris, uds, localmirrors, tarballs) |
| 994 | except UnboundLocalError: |
| 995 | pass |
| 996 | continue |
| 997 | uris.append(newuri) |
| 998 | uds.append(newud) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 999 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1000 | adduri(newud, uris, uds, localmirrors, tarballs) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1001 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1002 | adduri(origud, uris, uds, mirrors, origud.mirrortarballs or [None]) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1003 | |
| 1004 | return uris, uds |
| 1005 | |
| 1006 | def rename_bad_checksum(ud, suffix): |
| 1007 | """ |
| 1008 | Renames files to have suffix from parameter |
| 1009 | """ |
| 1010 | |
| 1011 | if ud.localpath is None: |
| 1012 | return |
| 1013 | |
| 1014 | new_localpath = "%s_bad-checksum_%s" % (ud.localpath, suffix) |
| 1015 | bb.warn("Renaming %s to %s" % (ud.localpath, new_localpath)) |
Brad Bishop | 79641f2 | 2019-09-10 07:20:22 -0400 | [diff] [blame] | 1016 | if not bb.utils.movefile(ud.localpath, new_localpath): |
| 1017 | bb.warn("Renaming %s to %s failed, grep movefile in log.do_fetch to see why" % (ud.localpath, new_localpath)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1018 | |
| 1019 | |
| 1020 | def try_mirror_url(fetch, origud, ud, ld, check = False): |
| 1021 | # Return of None or a value means we're finished |
| 1022 | # False means try another url |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 1023 | |
| 1024 | if ud.lockfile and ud.lockfile != origud.lockfile: |
| 1025 | lf = bb.utils.lockfile(ud.lockfile) |
| 1026 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1027 | try: |
| 1028 | if check: |
| 1029 | found = ud.method.checkstatus(fetch, ud, ld) |
| 1030 | if found: |
| 1031 | return found |
| 1032 | return False |
| 1033 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1034 | if not verify_donestamp(ud, ld, origud) or ud.method.need_update(ud, ld): |
| 1035 | ud.method.download(ud, ld) |
| 1036 | if hasattr(ud.method,"build_mirror_data"): |
| 1037 | ud.method.build_mirror_data(ud, ld) |
| 1038 | |
| 1039 | if not ud.localpath or not os.path.exists(ud.localpath): |
| 1040 | return False |
| 1041 | |
| 1042 | if ud.localpath == origud.localpath: |
| 1043 | return ud.localpath |
| 1044 | |
| 1045 | # We may be obtaining a mirror tarball which needs further processing by the real fetcher |
| 1046 | # If that tarball is a local file:// we need to provide a symlink to it |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1047 | dldir = ld.getVar("DL_DIR") |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1048 | |
| 1049 | if origud.mirrortarballs and os.path.basename(ud.localpath) in origud.mirrortarballs and os.path.basename(ud.localpath) != os.path.basename(origud.localpath): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1050 | # Create donestamp in old format to avoid triggering a re-download |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 1051 | if ud.donestamp: |
| 1052 | bb.utils.mkdirhier(os.path.dirname(ud.donestamp)) |
| 1053 | open(ud.donestamp, 'w').close() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1054 | dest = os.path.join(dldir, os.path.basename(ud.localpath)) |
| 1055 | if not os.path.exists(dest): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1056 | # In case this is executing without any file locks held (as is |
| 1057 | # the case for file:// URLs), two tasks may end up here at the |
| 1058 | # same time, in which case we do not want the second task to |
| 1059 | # fail when the link has already been created by the first task. |
| 1060 | try: |
| 1061 | os.symlink(ud.localpath, dest) |
| 1062 | except FileExistsError: |
| 1063 | pass |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1064 | if not verify_donestamp(origud, ld) or origud.method.need_update(origud, ld): |
| 1065 | origud.method.download(origud, ld) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1066 | if hasattr(origud.method, "build_mirror_data"): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1067 | origud.method.build_mirror_data(origud, ld) |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 1068 | return origud.localpath |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1069 | # Otherwise the result is a local file:// and we symlink to it |
Andrew Geissler | 09209ee | 2020-12-13 08:44:15 -0600 | [diff] [blame] | 1070 | ensure_symlink(ud.localpath, origud.localpath) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1071 | update_stamp(origud, ld) |
| 1072 | return ud.localpath |
| 1073 | |
| 1074 | except bb.fetch2.NetworkAccess: |
| 1075 | raise |
| 1076 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1077 | except IOError as e: |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 1078 | if e.errno in [errno.ESTALE]: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1079 | logger.warning("Stale Error Observed %s." % ud.url) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1080 | return False |
| 1081 | raise |
| 1082 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1083 | except bb.fetch2.BBFetchException as e: |
| 1084 | if isinstance(e, ChecksumError): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1085 | logger.warning("Mirror checksum failure for url %s (original url: %s)\nCleaning and trying again." % (ud.url, origud.url)) |
| 1086 | logger.warning(str(e)) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 1087 | if os.path.exists(ud.localpath): |
| 1088 | rename_bad_checksum(ud, e.checksum) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1089 | elif isinstance(e, NoChecksumError): |
| 1090 | raise |
| 1091 | else: |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 1092 | logger.debug("Mirror fetch failure for url %s (original url: %s)" % (ud.url, origud.url)) |
| 1093 | logger.debug(str(e)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1094 | try: |
| 1095 | ud.method.clean(ud, ld) |
| 1096 | except UnboundLocalError: |
| 1097 | pass |
| 1098 | return False |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 1099 | finally: |
| 1100 | if ud.lockfile and ud.lockfile != origud.lockfile: |
| 1101 | bb.utils.unlockfile(lf) |
| 1102 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1103 | |
Andrew Geissler | 09209ee | 2020-12-13 08:44:15 -0600 | [diff] [blame] | 1104 | def ensure_symlink(target, link_name): |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 1105 | if not os.path.exists(link_name): |
Andrew Geissler | 615f2f1 | 2022-07-15 14:00:58 -0500 | [diff] [blame] | 1106 | dirname = os.path.dirname(link_name) |
| 1107 | bb.utils.mkdirhier(dirname) |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 1108 | if os.path.islink(link_name): |
| 1109 | # Broken symbolic link |
| 1110 | os.unlink(link_name) |
| 1111 | |
| 1112 | # In case this is executing without any file locks held (as is |
| 1113 | # the case for file:// URLs), two tasks may end up here at the |
| 1114 | # same time, in which case we do not want the second task to |
| 1115 | # fail when the link has already been created by the first task. |
| 1116 | try: |
| 1117 | os.symlink(target, link_name) |
| 1118 | except FileExistsError: |
| 1119 | pass |
| 1120 | |
| 1121 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1122 | def try_mirrors(fetch, d, origud, mirrors, check = False): |
| 1123 | """ |
| 1124 | Try to use a mirrored version of the sources. |
| 1125 | This method will be automatically called before the fetchers go. |
| 1126 | |
| 1127 | d Is a bb.data instance |
| 1128 | uri is the original uri we're trying to download |
| 1129 | mirrors is the list of mirrors we're going to try |
| 1130 | """ |
| 1131 | ld = d.createCopy() |
| 1132 | |
| 1133 | uris, uds = build_mirroruris(origud, mirrors, ld) |
| 1134 | |
| 1135 | for index, uri in enumerate(uris): |
| 1136 | ret = try_mirror_url(fetch, origud, uds[index], ld, check) |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1137 | if ret: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1138 | return ret |
| 1139 | return None |
| 1140 | |
| 1141 | def trusted_network(d, url): |
| 1142 | """ |
| 1143 | Use a trusted url during download if networking is enabled and |
| 1144 | BB_ALLOWED_NETWORKS is set globally or for a specific recipe. |
| 1145 | Note: modifies SRC_URI & mirrors. |
| 1146 | """ |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 1147 | if bb.utils.to_boolean(d.getVar("BB_NO_NETWORK")): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1148 | return True |
| 1149 | |
| 1150 | pkgname = d.expand(d.getVar('PN', False)) |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 1151 | trusted_hosts = None |
| 1152 | if pkgname: |
| 1153 | trusted_hosts = d.getVarFlag('BB_ALLOWED_NETWORKS', pkgname, False) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1154 | |
| 1155 | if not trusted_hosts: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1156 | trusted_hosts = d.getVar('BB_ALLOWED_NETWORKS') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1157 | |
| 1158 | # Not enabled. |
| 1159 | if not trusted_hosts: |
| 1160 | return True |
| 1161 | |
| 1162 | scheme, network, path, user, passwd, param = decodeurl(url) |
| 1163 | |
| 1164 | if not network: |
| 1165 | return True |
| 1166 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 1167 | network = network.split(':')[0] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1168 | network = network.lower() |
| 1169 | |
| 1170 | for host in trusted_hosts.split(" "): |
| 1171 | host = host.lower() |
| 1172 | if host.startswith("*.") and ("." + network).endswith(host[1:]): |
| 1173 | return True |
| 1174 | if host == network: |
| 1175 | return True |
| 1176 | |
| 1177 | return False |
| 1178 | |
| 1179 | def srcrev_internal_helper(ud, d, name): |
| 1180 | """ |
| 1181 | Return: |
| 1182 | a) a source revision if specified |
| 1183 | b) latest revision if SRCREV="AUTOINC" |
| 1184 | c) None if not specified |
| 1185 | """ |
| 1186 | |
| 1187 | srcrev = None |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1188 | pn = d.getVar("PN") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1189 | attempts = [] |
| 1190 | if name != '' and pn: |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 1191 | attempts.append("SRCREV_%s:pn-%s" % (name, pn)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1192 | if name != '': |
| 1193 | attempts.append("SRCREV_%s" % name) |
| 1194 | if pn: |
Patrick Williams | 213cb26 | 2021-08-07 19:21:33 -0500 | [diff] [blame] | 1195 | attempts.append("SRCREV:pn-%s" % pn) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1196 | attempts.append("SRCREV") |
| 1197 | |
| 1198 | for a in attempts: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1199 | srcrev = d.getVar(a) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1200 | if srcrev and srcrev != "INVALID": |
| 1201 | break |
| 1202 | |
| 1203 | if 'rev' in ud.parm and 'tag' in ud.parm: |
| 1204 | raise FetchError("Please specify a ;rev= parameter or a ;tag= parameter in the url %s but not both." % (ud.url)) |
| 1205 | |
| 1206 | if 'rev' in ud.parm or 'tag' in ud.parm: |
| 1207 | if 'rev' in ud.parm: |
| 1208 | parmrev = ud.parm['rev'] |
| 1209 | else: |
| 1210 | parmrev = ud.parm['tag'] |
| 1211 | if srcrev == "INVALID" or not srcrev: |
| 1212 | return parmrev |
| 1213 | if srcrev != parmrev: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1214 | raise FetchError("Conflicting revisions (%s from SRCREV and %s from the url) found, please specify one valid value" % (srcrev, parmrev)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1215 | return parmrev |
| 1216 | |
| 1217 | if srcrev == "INVALID" or not srcrev: |
| 1218 | raise FetchError("Please set a valid SRCREV for url %s (possible key names are %s, or use a ;rev=X URL parameter)" % (str(attempts), ud.url), ud.url) |
| 1219 | if srcrev == "AUTOINC": |
| 1220 | srcrev = ud.method.latest_revision(ud, d, name) |
| 1221 | |
| 1222 | return srcrev |
| 1223 | |
| 1224 | def get_checksum_file_list(d): |
| 1225 | """ Get a list of files checksum in SRC_URI |
| 1226 | |
| 1227 | Returns the resolved local paths of all local file entries in |
| 1228 | SRC_URI as a space-separated string |
| 1229 | """ |
| 1230 | fetch = Fetch([], d, cache = False, localonly = True) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1231 | filelist = [] |
| 1232 | for u in fetch.urls: |
| 1233 | ud = fetch.ud[u] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1234 | if ud and isinstance(ud.method, local.Local): |
Andrew Geissler | 615f2f1 | 2022-07-15 14:00:58 -0500 | [diff] [blame] | 1235 | found = False |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1236 | paths = ud.method.localpaths(ud, d) |
| 1237 | for f in paths: |
| 1238 | pth = ud.decodedurl |
Andrew Geissler | 615f2f1 | 2022-07-15 14:00:58 -0500 | [diff] [blame] | 1239 | if os.path.exists(f): |
| 1240 | found = True |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1241 | filelist.append(f + ":" + str(os.path.exists(f))) |
Andrew Geissler | 615f2f1 | 2022-07-15 14:00:58 -0500 | [diff] [blame] | 1242 | if not found: |
| 1243 | bb.fatal(("Unable to get checksum for %s SRC_URI entry %s: file could not be found" |
| 1244 | "\nThe following paths were searched:" |
| 1245 | "\n%s") % (d.getVar('PN'), os.path.basename(f), '\n'.join(paths))) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1246 | |
| 1247 | return " ".join(filelist) |
| 1248 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1249 | def get_file_checksums(filelist, pn, localdirsexclude): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1250 | """Get a list of the checksums for a list of local files |
| 1251 | |
| 1252 | Returns the checksums for a list of local files, caching the results as |
| 1253 | it proceeds |
| 1254 | |
| 1255 | """ |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1256 | return _checksum_cache.get_checksums(filelist, pn, localdirsexclude) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1257 | |
| 1258 | |
| 1259 | class FetchData(object): |
| 1260 | """ |
| 1261 | A class which represents the fetcher state for a given URI. |
| 1262 | """ |
| 1263 | def __init__(self, url, d, localonly = False): |
| 1264 | # localpath is the location of a downloaded result. If not set, the file is local. |
| 1265 | self.donestamp = None |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 1266 | self.needdonestamp = True |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1267 | self.localfile = "" |
| 1268 | self.localpath = None |
| 1269 | self.lockfile = None |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1270 | self.mirrortarballs = [] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1271 | self.basename = None |
| 1272 | self.basepath = None |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1273 | (self.type, self.host, self.path, self.user, self.pswd, self.parm) = decodeurl(d.expand(url)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1274 | self.date = self.getSRCDate(d) |
| 1275 | self.url = url |
| 1276 | if not self.user and "user" in self.parm: |
| 1277 | self.user = self.parm["user"] |
| 1278 | if not self.pswd and "pswd" in self.parm: |
| 1279 | self.pswd = self.parm["pswd"] |
| 1280 | self.setup = False |
| 1281 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1282 | def configure_checksum(checksum_id): |
| 1283 | if "name" in self.parm: |
| 1284 | checksum_name = "%s.%ssum" % (self.parm["name"], checksum_id) |
| 1285 | else: |
| 1286 | checksum_name = "%ssum" % checksum_id |
| 1287 | |
| 1288 | setattr(self, "%s_name" % checksum_id, checksum_name) |
| 1289 | |
| 1290 | if checksum_name in self.parm: |
| 1291 | checksum_expected = self.parm[checksum_name] |
Andrew Geissler | 95ac1b8 | 2021-03-31 14:34:31 -0500 | [diff] [blame] | 1292 | elif self.type not in ["http", "https", "ftp", "ftps", "sftp", "s3", "az"]: |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1293 | checksum_expected = None |
| 1294 | else: |
| 1295 | checksum_expected = d.getVarFlag("SRC_URI", checksum_name) |
| 1296 | |
| 1297 | setattr(self, "%s_expected" % checksum_id, checksum_expected) |
| 1298 | |
| 1299 | for checksum_id in CHECKSUM_LIST: |
| 1300 | configure_checksum(checksum_id) |
| 1301 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1302 | self.ignore_checksums = False |
| 1303 | |
| 1304 | self.names = self.parm.get("name",'default').split(',') |
| 1305 | |
| 1306 | self.method = None |
| 1307 | for m in methods: |
| 1308 | if m.supports(self, d): |
| 1309 | self.method = m |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1310 | break |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1311 | |
| 1312 | if not self.method: |
| 1313 | raise NoMethodError(url) |
| 1314 | |
| 1315 | if localonly and not isinstance(self.method, local.Local): |
| 1316 | raise NonLocalMethod() |
| 1317 | |
| 1318 | if self.parm.get("proto", None) and "protocol" not in self.parm: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1319 | logger.warning('Consider updating %s recipe to use "protocol" not "proto" in SRC_URI.', d.getVar('PN')) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1320 | self.parm["protocol"] = self.parm.get("proto", None) |
| 1321 | |
| 1322 | if hasattr(self.method, "urldata_init"): |
| 1323 | self.method.urldata_init(self, d) |
| 1324 | |
| 1325 | if "localpath" in self.parm: |
| 1326 | # if user sets localpath for file, use it instead. |
| 1327 | self.localpath = self.parm["localpath"] |
| 1328 | self.basename = os.path.basename(self.localpath) |
| 1329 | elif self.localfile: |
| 1330 | self.localpath = self.method.localpath(self, d) |
| 1331 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1332 | dldir = d.getVar("DL_DIR") |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 1333 | |
| 1334 | if not self.needdonestamp: |
| 1335 | return |
| 1336 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1337 | # Note: .done and .lock files should always be in DL_DIR whereas localpath may not be. |
| 1338 | if self.localpath and self.localpath.startswith(dldir): |
| 1339 | basepath = self.localpath |
| 1340 | elif self.localpath: |
| 1341 | basepath = dldir + os.sep + os.path.basename(self.localpath) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 1342 | elif self.basepath or self.basename: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1343 | basepath = dldir + os.sep + (self.basepath or self.basename) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 1344 | else: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1345 | bb.fatal("Can't determine lock path for url %s" % url) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 1346 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1347 | self.donestamp = basepath + '.done' |
| 1348 | self.lockfile = basepath + '.lock' |
| 1349 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1350 | def setup_revisions(self, d): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1351 | self.revisions = {} |
| 1352 | for name in self.names: |
| 1353 | self.revisions[name] = srcrev_internal_helper(self, d, name) |
| 1354 | |
| 1355 | # add compatibility code for non name specified case |
| 1356 | if len(self.names) == 1: |
| 1357 | self.revision = self.revisions[self.names[0]] |
| 1358 | |
| 1359 | def setup_localpath(self, d): |
| 1360 | if not self.localpath: |
| 1361 | self.localpath = self.method.localpath(self, d) |
| 1362 | |
| 1363 | def getSRCDate(self, d): |
| 1364 | """ |
| 1365 | Return the SRC Date for the component |
| 1366 | |
| 1367 | d the bb.data module |
| 1368 | """ |
| 1369 | if "srcdate" in self.parm: |
| 1370 | return self.parm['srcdate'] |
| 1371 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1372 | pn = d.getVar("PN") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1373 | |
| 1374 | if pn: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1375 | return d.getVar("SRCDATE_%s" % pn) or d.getVar("SRCDATE") or d.getVar("DATE") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1376 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1377 | return d.getVar("SRCDATE") or d.getVar("DATE") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1378 | |
| 1379 | class FetchMethod(object): |
| 1380 | """Base class for 'fetch'ing data""" |
| 1381 | |
| 1382 | def __init__(self, urls=None): |
| 1383 | self.urls = [] |
| 1384 | |
| 1385 | def supports(self, urldata, d): |
| 1386 | """ |
| 1387 | Check to see if this fetch class supports a given url. |
| 1388 | """ |
| 1389 | return 0 |
| 1390 | |
| 1391 | def localpath(self, urldata, d): |
| 1392 | """ |
| 1393 | Return the local filename of a given url assuming a successful fetch. |
| 1394 | Can also setup variables in urldata for use in go (saving code duplication |
| 1395 | and duplicate code execution) |
| 1396 | """ |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1397 | return os.path.join(d.getVar("DL_DIR"), urldata.localfile) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1398 | |
| 1399 | def supports_checksum(self, urldata): |
| 1400 | """ |
| 1401 | Is localpath something that can be represented by a checksum? |
| 1402 | """ |
| 1403 | |
| 1404 | # We cannot compute checksums for directories |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1405 | if os.path.isdir(urldata.localpath): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1406 | return False |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1407 | return True |
| 1408 | |
| 1409 | def recommends_checksum(self, urldata): |
| 1410 | """ |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1411 | Is the backend on where checksumming is recommended (should warnings |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1412 | be displayed if there is no checksum)? |
| 1413 | """ |
| 1414 | return False |
| 1415 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1416 | def verify_donestamp(self, ud, d): |
| 1417 | """ |
| 1418 | Verify the donestamp file |
| 1419 | """ |
| 1420 | return verify_donestamp(ud, d) |
| 1421 | |
| 1422 | def update_donestamp(self, ud, d): |
| 1423 | """ |
| 1424 | Update the donestamp file |
| 1425 | """ |
| 1426 | update_stamp(ud, d) |
| 1427 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1428 | def _strip_leading_slashes(self, relpath): |
| 1429 | """ |
| 1430 | Remove leading slash as os.path.join can't cope |
| 1431 | """ |
| 1432 | while os.path.isabs(relpath): |
| 1433 | relpath = relpath[1:] |
| 1434 | return relpath |
| 1435 | |
| 1436 | def setUrls(self, urls): |
| 1437 | self.__urls = urls |
| 1438 | |
| 1439 | def getUrls(self): |
| 1440 | return self.__urls |
| 1441 | |
| 1442 | urls = property(getUrls, setUrls, None, "Urls property") |
| 1443 | |
| 1444 | def need_update(self, ud, d): |
| 1445 | """ |
| 1446 | Force a fetch, even if localpath exists? |
| 1447 | """ |
| 1448 | if os.path.exists(ud.localpath): |
| 1449 | return False |
| 1450 | return True |
| 1451 | |
| 1452 | def supports_srcrev(self): |
| 1453 | """ |
| 1454 | The fetcher supports auto source revisions (SRCREV) |
| 1455 | """ |
| 1456 | return False |
| 1457 | |
| 1458 | def download(self, urldata, d): |
| 1459 | """ |
| 1460 | Fetch urls |
| 1461 | Assumes localpath was called first |
| 1462 | """ |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 1463 | raise NoMethodError(urldata.url) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1464 | |
| 1465 | def unpack(self, urldata, rootdir, data): |
| 1466 | iterate = False |
| 1467 | file = urldata.localpath |
| 1468 | |
| 1469 | try: |
| 1470 | unpack = bb.utils.to_boolean(urldata.parm.get('unpack'), True) |
| 1471 | except ValueError as exc: |
| 1472 | bb.fatal("Invalid value for 'unpack' parameter for %s: %s" % |
| 1473 | (file, urldata.parm.get('unpack'))) |
| 1474 | |
| 1475 | base, ext = os.path.splitext(file) |
| 1476 | if ext in ['.gz', '.bz2', '.Z', '.xz', '.lz']: |
| 1477 | efile = os.path.join(rootdir, os.path.basename(base)) |
| 1478 | else: |
| 1479 | efile = file |
| 1480 | cmd = None |
| 1481 | |
| 1482 | if unpack: |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 1483 | tar_cmd = 'tar --extract --no-same-owner' |
| 1484 | if 'striplevel' in urldata.parm: |
| 1485 | tar_cmd += ' --strip-components=%s' % urldata.parm['striplevel'] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1486 | if file.endswith('.tar'): |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 1487 | cmd = '%s -f %s' % (tar_cmd, file) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1488 | elif file.endswith('.tgz') or file.endswith('.tar.gz') or file.endswith('.tar.Z'): |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 1489 | cmd = '%s -z -f %s' % (tar_cmd, file) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1490 | elif file.endswith('.tbz') or file.endswith('.tbz2') or file.endswith('.tar.bz2'): |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 1491 | cmd = 'bzip2 -dc %s | %s -f -' % (file, tar_cmd) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1492 | elif file.endswith('.gz') or file.endswith('.Z') or file.endswith('.z'): |
| 1493 | cmd = 'gzip -dc %s > %s' % (file, efile) |
| 1494 | elif file.endswith('.bz2'): |
| 1495 | cmd = 'bzip2 -dc %s > %s' % (file, efile) |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 1496 | elif file.endswith('.txz') or file.endswith('.tar.xz'): |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 1497 | cmd = 'xz -dc %s | %s -f -' % (file, tar_cmd) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1498 | elif file.endswith('.xz'): |
| 1499 | cmd = 'xz -dc %s > %s' % (file, efile) |
| 1500 | elif file.endswith('.tar.lz'): |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 1501 | cmd = 'lzip -dc %s | %s -f -' % (file, tar_cmd) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1502 | elif file.endswith('.lz'): |
| 1503 | cmd = 'lzip -dc %s > %s' % (file, efile) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1504 | elif file.endswith('.tar.7z'): |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 1505 | cmd = '7z x -so %s | %s -f -' % (file, tar_cmd) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1506 | elif file.endswith('.7z'): |
| 1507 | cmd = '7za x -y %s 1>/dev/null' % file |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 1508 | elif file.endswith('.tzst') or file.endswith('.tar.zst'): |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 1509 | cmd = 'zstd --decompress --stdout %s | %s -f -' % (file, tar_cmd) |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 1510 | elif file.endswith('.zst'): |
| 1511 | cmd = 'zstd --decompress --stdout %s > %s' % (file, efile) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1512 | elif file.endswith('.zip') or file.endswith('.jar'): |
| 1513 | try: |
| 1514 | dos = bb.utils.to_boolean(urldata.parm.get('dos'), False) |
| 1515 | except ValueError as exc: |
| 1516 | bb.fatal("Invalid value for 'dos' parameter for %s: %s" % |
| 1517 | (file, urldata.parm.get('dos'))) |
| 1518 | cmd = 'unzip -q -o' |
| 1519 | if dos: |
| 1520 | cmd = '%s -a' % cmd |
| 1521 | cmd = "%s '%s'" % (cmd, file) |
| 1522 | elif file.endswith('.rpm') or file.endswith('.srpm'): |
| 1523 | if 'extract' in urldata.parm: |
| 1524 | unpack_file = urldata.parm.get('extract') |
| 1525 | cmd = 'rpm2cpio.sh %s | cpio -id %s' % (file, unpack_file) |
| 1526 | iterate = True |
| 1527 | iterate_file = unpack_file |
| 1528 | else: |
| 1529 | cmd = 'rpm2cpio.sh %s | cpio -id' % (file) |
| 1530 | elif file.endswith('.deb') or file.endswith('.ipk'): |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 1531 | output = subprocess.check_output(['ar', '-t', file], preexec_fn=subprocess_setup) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1532 | datafile = None |
| 1533 | if output: |
| 1534 | for line in output.decode().splitlines(): |
| 1535 | if line.startswith('data.tar.'): |
| 1536 | datafile = line |
| 1537 | break |
| 1538 | else: |
| 1539 | raise UnpackError("Unable to unpack deb/ipk package - does not contain data.tar.* file", urldata.url) |
| 1540 | else: |
| 1541 | raise UnpackError("Unable to unpack deb/ipk package - could not list contents", urldata.url) |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 1542 | cmd = 'ar x %s %s && %s -p -f %s && rm %s' % (file, datafile, tar_cmd, datafile, datafile) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 1543 | |
| 1544 | # If 'subdir' param exists, create a dir and use it as destination for unpack cmd |
| 1545 | if 'subdir' in urldata.parm: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1546 | subdir = urldata.parm.get('subdir') |
| 1547 | if os.path.isabs(subdir): |
| 1548 | if not os.path.realpath(subdir).startswith(os.path.realpath(rootdir)): |
| 1549 | raise UnpackError("subdir argument isn't a subdirectory of unpack root %s" % rootdir, urldata.url) |
| 1550 | unpackdir = subdir |
| 1551 | else: |
| 1552 | unpackdir = os.path.join(rootdir, subdir) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 1553 | bb.utils.mkdirhier(unpackdir) |
| 1554 | else: |
| 1555 | unpackdir = rootdir |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1556 | |
| 1557 | if not unpack or not cmd: |
| 1558 | # If file == dest, then avoid any copies, as we already put the file into dest! |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 1559 | dest = os.path.join(unpackdir, os.path.basename(file)) |
| 1560 | if file != dest and not (os.path.exists(dest) and os.path.samefile(file, dest)): |
| 1561 | destdir = '.' |
| 1562 | # For file:// entries all intermediate dirs in path must be created at destination |
| 1563 | if urldata.type == "file": |
| 1564 | # Trailing '/' does a copying to wrong place |
| 1565 | urlpath = urldata.path.rstrip('/') |
| 1566 | # Want files places relative to cwd so no leading '/' |
| 1567 | urlpath = urlpath.lstrip('/') |
| 1568 | if urlpath.find("/") != -1: |
| 1569 | destdir = urlpath.rsplit("/", 1)[0] + '/' |
| 1570 | bb.utils.mkdirhier("%s/%s" % (unpackdir, destdir)) |
Andrew Geissler | c3d88e4 | 2020-10-02 09:45:00 -0500 | [diff] [blame] | 1571 | cmd = 'cp -fpPRH "%s" "%s"' % (file, destdir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1572 | |
| 1573 | if not cmd: |
| 1574 | return |
| 1575 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1576 | path = data.getVar('PATH') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1577 | if path: |
| 1578 | cmd = "PATH=\"%s\" %s" % (path, cmd) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1579 | bb.note("Unpacking %s to %s/" % (file, unpackdir)) |
| 1580 | ret = subprocess.call(cmd, preexec_fn=subprocess_setup, shell=True, cwd=unpackdir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1581 | |
| 1582 | if ret != 0: |
| 1583 | raise UnpackError("Unpack command %s failed with return value %s" % (cmd, ret), urldata.url) |
| 1584 | |
| 1585 | if iterate is True: |
| 1586 | iterate_urldata = urldata |
| 1587 | iterate_urldata.localpath = "%s/%s" % (rootdir, iterate_file) |
| 1588 | self.unpack(urldata, rootdir, data) |
| 1589 | |
| 1590 | return |
| 1591 | |
| 1592 | def clean(self, urldata, d): |
| 1593 | """ |
| 1594 | Clean any existing full or partial download |
| 1595 | """ |
| 1596 | bb.utils.remove(urldata.localpath) |
| 1597 | |
| 1598 | def try_premirror(self, urldata, d): |
| 1599 | """ |
| 1600 | Should premirrors be used? |
| 1601 | """ |
| 1602 | return True |
| 1603 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1604 | def try_mirrors(self, fetch, urldata, d, mirrors, check=False): |
| 1605 | """ |
| 1606 | Try to use a mirror |
| 1607 | """ |
| 1608 | return bool(try_mirrors(fetch, d, urldata, mirrors, check)) |
| 1609 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1610 | def checkstatus(self, fetch, urldata, d): |
| 1611 | """ |
| 1612 | Check the status of a URL |
| 1613 | Assumes localpath was called first |
| 1614 | """ |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 1615 | logger.info("URL %s could not be checked for status since no method exists.", urldata.url) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1616 | return True |
| 1617 | |
| 1618 | def latest_revision(self, ud, d, name): |
| 1619 | """ |
| 1620 | Look in the cache for the latest revision, if not present ask the SCM. |
| 1621 | """ |
| 1622 | if not hasattr(self, "_latest_revision"): |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 1623 | raise ParameterError("The fetcher for this URL does not support _latest_revision", ud.url) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1624 | |
| 1625 | revs = bb.persist_data.persist('BB_URI_HEADREVS', d) |
| 1626 | key = self.generate_revision_key(ud, d, name) |
| 1627 | try: |
| 1628 | return revs[key] |
| 1629 | except KeyError: |
| 1630 | revs[key] = rev = self._latest_revision(ud, d, name) |
| 1631 | return rev |
| 1632 | |
| 1633 | def sortable_revision(self, ud, d, name): |
| 1634 | latest_rev = self._build_revision(ud, d, name) |
| 1635 | return True, str(latest_rev) |
| 1636 | |
| 1637 | def generate_revision_key(self, ud, d, name): |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1638 | return self._revision_key(ud, d, name) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1639 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1640 | def latest_versionstring(self, ud, d): |
| 1641 | """ |
| 1642 | Compute the latest release name like "x.y.x" in "x.y.x+gitHASH" |
| 1643 | by searching through the tags output of ls-remote, comparing |
| 1644 | versions and returning the highest match as a (version, revision) pair. |
| 1645 | """ |
| 1646 | return ('', '') |
| 1647 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1648 | def done(self, ud, d): |
| 1649 | """ |
| 1650 | Is the download done ? |
| 1651 | """ |
| 1652 | if os.path.exists(ud.localpath): |
| 1653 | return True |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1654 | return False |
| 1655 | |
Andrew Geissler | 4ed12e1 | 2020-06-05 18:00:41 -0500 | [diff] [blame] | 1656 | def implicit_urldata(self, ud, d): |
| 1657 | """ |
| 1658 | Get a list of FetchData objects for any implicit URLs that will also |
| 1659 | be downloaded when we fetch the given URL. |
| 1660 | """ |
| 1661 | return [] |
| 1662 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1663 | class Fetch(object): |
| 1664 | def __init__(self, urls, d, cache = True, localonly = False, connection_cache = None): |
| 1665 | if localonly and cache: |
| 1666 | raise Exception("bb.fetch2.Fetch.__init__: cannot set cache and localonly at same time") |
| 1667 | |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 1668 | if not urls: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1669 | urls = d.getVar("SRC_URI").split() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1670 | self.urls = urls |
| 1671 | self.d = d |
| 1672 | self.ud = {} |
| 1673 | self.connection_cache = connection_cache |
| 1674 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1675 | fn = d.getVar('FILE') |
| 1676 | mc = d.getVar('__BBMULTICONFIG') or "" |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1677 | key = None |
| 1678 | if cache and fn: |
| 1679 | key = mc + fn + str(id(d)) |
| 1680 | if key in urldata_cache: |
| 1681 | self.ud = urldata_cache[key] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1682 | |
| 1683 | for url in urls: |
| 1684 | if url not in self.ud: |
| 1685 | try: |
| 1686 | self.ud[url] = FetchData(url, d, localonly) |
| 1687 | except NonLocalMethod: |
| 1688 | if localonly: |
| 1689 | self.ud[url] = None |
| 1690 | pass |
| 1691 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1692 | if key: |
| 1693 | urldata_cache[key] = self.ud |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1694 | |
| 1695 | def localpath(self, url): |
| 1696 | if url not in self.urls: |
| 1697 | self.ud[url] = FetchData(url, self.d) |
| 1698 | |
| 1699 | self.ud[url].setup_localpath(self.d) |
| 1700 | return self.d.expand(self.ud[url].localpath) |
| 1701 | |
| 1702 | def localpaths(self): |
| 1703 | """ |
| 1704 | Return a list of the local filenames, assuming successful fetch |
| 1705 | """ |
| 1706 | local = [] |
| 1707 | |
| 1708 | for u in self.urls: |
| 1709 | ud = self.ud[u] |
| 1710 | ud.setup_localpath(self.d) |
| 1711 | local.append(ud.localpath) |
| 1712 | |
| 1713 | return local |
| 1714 | |
| 1715 | def download(self, urls=None): |
| 1716 | """ |
| 1717 | Fetch all urls |
| 1718 | """ |
| 1719 | if not urls: |
| 1720 | urls = self.urls |
| 1721 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1722 | network = self.d.getVar("BB_NO_NETWORK") |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 1723 | premirroronly = bb.utils.to_boolean(self.d.getVar("BB_FETCH_PREMIRRORONLY")) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1724 | |
| 1725 | for u in urls: |
| 1726 | ud = self.ud[u] |
| 1727 | ud.setup_localpath(self.d) |
| 1728 | m = ud.method |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1729 | done = False |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1730 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 1731 | if ud.lockfile: |
| 1732 | lf = bb.utils.lockfile(ud.lockfile) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1733 | |
| 1734 | try: |
| 1735 | self.d.setVar("BB_NO_NETWORK", network) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1736 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1737 | if m.verify_donestamp(ud, self.d) and not m.need_update(ud, self.d): |
| 1738 | done = True |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1739 | elif m.try_premirror(ud, self.d): |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 1740 | logger.debug("Trying PREMIRRORS") |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1741 | mirrors = mirror_from_string(self.d.getVar('PREMIRRORS')) |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1742 | done = m.try_mirrors(self, ud, self.d, mirrors) |
| 1743 | if done: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1744 | try: |
| 1745 | # early checksum verification so that if the checksum of the premirror |
| 1746 | # contents mismatch the fetcher can still try upstream and mirrors |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1747 | m.update_donestamp(ud, self.d) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1748 | except ChecksumError as e: |
| 1749 | logger.warning("Checksum failure encountered with premirror download of %s - will attempt other sources." % u) |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 1750 | logger.debug(str(e)) |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1751 | done = False |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1752 | |
| 1753 | if premirroronly: |
| 1754 | self.d.setVar("BB_NO_NETWORK", "1") |
| 1755 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1756 | firsterr = None |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 1757 | verified_stamp = False |
| 1758 | if done: |
| 1759 | verified_stamp = m.verify_donestamp(ud, self.d) |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1760 | if not done and (not verified_stamp or m.need_update(ud, self.d)): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1761 | try: |
| 1762 | if not trusted_network(self.d, ud.url): |
| 1763 | raise UntrustedUrl(ud.url) |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 1764 | logger.debug("Trying Upstream") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1765 | m.download(ud, self.d) |
| 1766 | if hasattr(m, "build_mirror_data"): |
| 1767 | m.build_mirror_data(ud, self.d) |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1768 | done = True |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1769 | # early checksum verify, so that if checksum mismatched, |
| 1770 | # fetcher still have chance to fetch from mirror |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1771 | m.update_donestamp(ud, self.d) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1772 | |
| 1773 | except bb.fetch2.NetworkAccess: |
| 1774 | raise |
| 1775 | |
| 1776 | except BBFetchException as e: |
| 1777 | if isinstance(e, ChecksumError): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1778 | logger.warning("Checksum failure encountered with download of %s - will attempt other sources if available" % u) |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 1779 | logger.debug(str(e)) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 1780 | if os.path.exists(ud.localpath): |
| 1781 | rename_bad_checksum(ud, e.checksum) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1782 | elif isinstance(e, NoChecksumError): |
| 1783 | raise |
| 1784 | else: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1785 | logger.warning('Failed to fetch URL %s, attempting MIRRORS if available' % u) |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 1786 | logger.debug(str(e)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1787 | firsterr = e |
| 1788 | # Remove any incomplete fetch |
| 1789 | if not verified_stamp: |
| 1790 | m.clean(ud, self.d) |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 1791 | logger.debug("Trying MIRRORS") |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1792 | mirrors = mirror_from_string(self.d.getVar('MIRRORS')) |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1793 | done = m.try_mirrors(self, ud, self.d, mirrors) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1794 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1795 | if not done or not m.done(ud, self.d): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1796 | if firsterr: |
| 1797 | logger.error(str(firsterr)) |
| 1798 | raise FetchError("Unable to fetch URL from any source.", u) |
| 1799 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1800 | m.update_donestamp(ud, self.d) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1801 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1802 | except IOError as e: |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 1803 | if e.errno in [errno.ESTALE]: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1804 | logger.error("Stale Error Observed %s." % u) |
| 1805 | raise ChecksumError("Stale Error Detected") |
| 1806 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1807 | except BBFetchException as e: |
| 1808 | if isinstance(e, ChecksumError): |
| 1809 | logger.error("Checksum failure fetching %s" % u) |
| 1810 | raise |
| 1811 | |
| 1812 | finally: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 1813 | if ud.lockfile: |
| 1814 | bb.utils.unlockfile(lf) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1815 | |
| 1816 | def checkstatus(self, urls=None): |
| 1817 | """ |
Andrew Geissler | eff2747 | 2021-10-29 15:35:00 -0500 | [diff] [blame] | 1818 | Check all URLs exist upstream. |
| 1819 | |
| 1820 | Returns None if the URLs exist, raises FetchError if the check wasn't |
| 1821 | successful but there wasn't an error (such as file not found), and |
| 1822 | raises other exceptions in error cases. |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1823 | """ |
| 1824 | |
| 1825 | if not urls: |
| 1826 | urls = self.urls |
| 1827 | |
| 1828 | for u in urls: |
| 1829 | ud = self.ud[u] |
| 1830 | ud.setup_localpath(self.d) |
| 1831 | m = ud.method |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 1832 | logger.debug("Testing URL %s", u) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1833 | # First try checking uri, u, from PREMIRRORS |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1834 | mirrors = mirror_from_string(self.d.getVar('PREMIRRORS')) |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1835 | ret = m.try_mirrors(self, ud, self.d, mirrors, True) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1836 | if not ret: |
| 1837 | # Next try checking from the original uri, u |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1838 | ret = m.checkstatus(self, ud, self.d) |
| 1839 | if not ret: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1840 | # Finally, try checking uri, u, from MIRRORS |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1841 | mirrors = mirror_from_string(self.d.getVar('MIRRORS')) |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1842 | ret = m.try_mirrors(self, ud, self.d, mirrors, True) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1843 | |
| 1844 | if not ret: |
| 1845 | raise FetchError("URL %s doesn't work" % u, u) |
| 1846 | |
| 1847 | def unpack(self, root, urls=None): |
| 1848 | """ |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1849 | Unpack urls to root |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1850 | """ |
| 1851 | |
| 1852 | if not urls: |
| 1853 | urls = self.urls |
| 1854 | |
| 1855 | for u in urls: |
| 1856 | ud = self.ud[u] |
| 1857 | ud.setup_localpath(self.d) |
| 1858 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1859 | if ud.lockfile: |
| 1860 | lf = bb.utils.lockfile(ud.lockfile) |
| 1861 | |
| 1862 | ud.method.unpack(ud, root, self.d) |
| 1863 | |
| 1864 | if ud.lockfile: |
| 1865 | bb.utils.unlockfile(lf) |
| 1866 | |
| 1867 | def clean(self, urls=None): |
| 1868 | """ |
| 1869 | Clean files that the fetcher gets or places |
| 1870 | """ |
| 1871 | |
| 1872 | if not urls: |
| 1873 | urls = self.urls |
| 1874 | |
| 1875 | for url in urls: |
| 1876 | if url not in self.ud: |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 1877 | self.ud[url] = FetchData(url, self.d) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1878 | ud = self.ud[url] |
| 1879 | ud.setup_localpath(self.d) |
| 1880 | |
| 1881 | if not ud.localfile and ud.localpath is None: |
| 1882 | continue |
| 1883 | |
| 1884 | if ud.lockfile: |
| 1885 | lf = bb.utils.lockfile(ud.lockfile) |
| 1886 | |
| 1887 | ud.method.clean(ud, self.d) |
| 1888 | if ud.donestamp: |
| 1889 | bb.utils.remove(ud.donestamp) |
| 1890 | |
| 1891 | if ud.lockfile: |
| 1892 | bb.utils.unlockfile(lf) |
| 1893 | |
Andrew Geissler | 4ed12e1 | 2020-06-05 18:00:41 -0500 | [diff] [blame] | 1894 | def expanded_urldata(self, urls=None): |
| 1895 | """ |
| 1896 | Get an expanded list of FetchData objects covering both the given |
| 1897 | URLS and any additional implicit URLs that are added automatically by |
| 1898 | the appropriate FetchMethod. |
| 1899 | """ |
| 1900 | |
| 1901 | if not urls: |
| 1902 | urls = self.urls |
| 1903 | |
| 1904 | urldata = [] |
| 1905 | for url in urls: |
| 1906 | ud = self.ud[url] |
| 1907 | urldata.append(ud) |
| 1908 | urldata += ud.method.implicit_urldata(ud, self.d) |
| 1909 | |
| 1910 | return urldata |
| 1911 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1912 | class FetchConnectionCache(object): |
| 1913 | """ |
| 1914 | A class which represents an container for socket connections. |
| 1915 | """ |
| 1916 | def __init__(self): |
| 1917 | self.cache = {} |
| 1918 | |
| 1919 | def get_connection_name(self, host, port): |
| 1920 | return host + ':' + str(port) |
| 1921 | |
| 1922 | def add_connection(self, host, port, connection): |
| 1923 | cn = self.get_connection_name(host, port) |
| 1924 | |
| 1925 | if cn not in self.cache: |
| 1926 | self.cache[cn] = connection |
| 1927 | |
| 1928 | def get_connection(self, host, port): |
| 1929 | connection = None |
| 1930 | |
| 1931 | cn = self.get_connection_name(host, port) |
| 1932 | if cn in self.cache: |
| 1933 | connection = self.cache[cn] |
| 1934 | |
| 1935 | return connection |
| 1936 | |
| 1937 | def remove_connection(self, host, port): |
| 1938 | cn = self.get_connection_name(host, port) |
| 1939 | if cn in self.cache: |
| 1940 | self.cache[cn].close() |
| 1941 | del self.cache[cn] |
| 1942 | |
| 1943 | def close_connections(self): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1944 | for cn in list(self.cache.keys()): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1945 | self.cache[cn].close() |
| 1946 | del self.cache[cn] |
| 1947 | |
| 1948 | from . import cvs |
| 1949 | from . import git |
| 1950 | from . import gitsm |
| 1951 | from . import gitannex |
| 1952 | from . import local |
| 1953 | from . import svn |
| 1954 | from . import wget |
| 1955 | from . import ssh |
| 1956 | from . import sftp |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1957 | from . import s3 |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1958 | from . import perforce |
| 1959 | from . import bzr |
| 1960 | from . import hg |
| 1961 | from . import osc |
| 1962 | from . import repo |
| 1963 | from . import clearcase |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 1964 | from . import npm |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1965 | from . import npmsw |
Andrew Geissler | 95ac1b8 | 2021-03-31 14:34:31 -0500 | [diff] [blame] | 1966 | from . import az |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 1967 | from . import crate |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1968 | |
| 1969 | methods.append(local.Local()) |
| 1970 | methods.append(wget.Wget()) |
| 1971 | methods.append(svn.Svn()) |
| 1972 | methods.append(git.Git()) |
| 1973 | methods.append(gitsm.GitSM()) |
| 1974 | methods.append(gitannex.GitANNEX()) |
| 1975 | methods.append(cvs.Cvs()) |
| 1976 | methods.append(ssh.SSH()) |
| 1977 | methods.append(sftp.SFTP()) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1978 | methods.append(s3.S3()) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1979 | methods.append(perforce.Perforce()) |
| 1980 | methods.append(bzr.Bzr()) |
| 1981 | methods.append(hg.Hg()) |
| 1982 | methods.append(osc.Osc()) |
| 1983 | methods.append(repo.Repo()) |
| 1984 | methods.append(clearcase.ClearCase()) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 1985 | methods.append(npm.Npm()) |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1986 | methods.append(npmsw.NpmShrinkWrap()) |
Andrew Geissler | 95ac1b8 | 2021-03-31 14:34:31 -0500 | [diff] [blame] | 1987 | methods.append(az.Az()) |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 1988 | methods.append(crate.Crate()) |