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): |
| 116 | """Exception raised when a url cannot be proccessed due to invalid parameters.""" |
| 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 |
| 185 | fetch2 library did not comform to this. Therefore, this URI |
| 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 |
| 202 | "whitelisted" schemes (currently only file://), that requires |
| 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)) |
| 405 | url = '%s://' % type |
| 406 | if user and type != "file": |
| 407 | url += "%s" % user |
| 408 | if pswd: |
| 409 | url += ":%s" % pswd |
| 410 | url += "@" |
| 411 | if host and type != "file": |
| 412 | url += "%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("//", "/") |
| 417 | url += "%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: |
| 420 | url += ";%s=%s" % (parm, p[parm]) |
| 421 | |
| 422 | return url |
| 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 = ['', '', '', '', '', {}] |
| 433 | for loc, i in enumerate(uri_find_decoded): |
| 434 | result_decoded[loc] = uri_decoded[loc] |
| 435 | regexp = i |
| 436 | if loc == 0 and regexp and not regexp.endswith("$"): |
| 437 | # Leaving the type unanchored can mean "https" matching "file" can become "files" |
| 438 | # which is clearly undesirable. |
| 439 | regexp += "$" |
| 440 | if loc == 5: |
| 441 | # Handle URL parameters |
| 442 | if i: |
| 443 | # Any specified URL parameters must match |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 444 | for k in uri_find_decoded[loc]: |
| 445 | if uri_decoded[loc][k] != uri_find_decoded[loc][k]: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 446 | return None |
| 447 | # Overwrite any specified replacement parameters |
| 448 | for k in uri_replace_decoded[loc]: |
| 449 | for l in replacements: |
| 450 | uri_replace_decoded[loc][k] = uri_replace_decoded[loc][k].replace(l, replacements[l]) |
| 451 | result_decoded[loc][k] = uri_replace_decoded[loc][k] |
| 452 | elif (re.match(regexp, uri_decoded[loc])): |
| 453 | if not uri_replace_decoded[loc]: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 454 | result_decoded[loc] = "" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 455 | else: |
| 456 | for k in replacements: |
| 457 | uri_replace_decoded[loc] = uri_replace_decoded[loc].replace(k, replacements[k]) |
| 458 | #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] | 459 | 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] | 460 | if loc == 2: |
| 461 | # Handle path manipulations |
| 462 | basename = None |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 463 | if uri_decoded[0] != uri_replace_decoded[0] and mirrortarball: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 464 | # 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] | 465 | basename = os.path.basename(mirrortarball) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 466 | # Kill parameters, they make no sense for mirror tarballs |
| 467 | uri_decoded[5] = {} |
| 468 | elif ud.localpath and ud.method.supports_checksum(ud): |
| 469 | basename = os.path.basename(ud.localpath) |
| 470 | if basename and not result_decoded[loc].endswith(basename): |
| 471 | result_decoded[loc] = os.path.join(result_decoded[loc], basename) |
| 472 | else: |
| 473 | return None |
| 474 | result = encodeurl(result_decoded) |
| 475 | if result == ud.url: |
| 476 | return None |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 477 | logger.debug2("For url %s returning %s" % (ud.url, result)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 478 | return result |
| 479 | |
| 480 | methods = [] |
| 481 | urldata_cache = {} |
| 482 | saved_headrevs = {} |
| 483 | |
| 484 | def fetcher_init(d): |
| 485 | """ |
| 486 | Called to initialize the fetchers once the configuration data is known. |
| 487 | Calls before this must not hit the cache. |
| 488 | """ |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 489 | |
| 490 | revs = bb.persist_data.persist('BB_URI_HEADREVS', d) |
| 491 | try: |
| 492 | # fetcher_init is called multiple times, so make sure we only save the |
| 493 | # revs the first time it is called. |
| 494 | if not bb.fetch2.saved_headrevs: |
| 495 | bb.fetch2.saved_headrevs = dict(revs) |
| 496 | except: |
| 497 | pass |
| 498 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 499 | # When to drop SCM head revisions controlled by user policy |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 500 | srcrev_policy = d.getVar('BB_SRCREV_POLICY') or "clear" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 501 | if srcrev_policy == "cache": |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 502 | 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] | 503 | elif srcrev_policy == "clear": |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 504 | 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] | 505 | revs.clear() |
| 506 | else: |
| 507 | raise FetchError("Invalid SRCREV cache policy of: %s" % srcrev_policy) |
| 508 | |
| 509 | _checksum_cache.init_cache(d) |
| 510 | |
| 511 | for m in methods: |
| 512 | if hasattr(m, "init"): |
| 513 | m.init(d) |
| 514 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 515 | def fetcher_parse_save(): |
| 516 | _checksum_cache.save_extras() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 517 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 518 | def fetcher_parse_done(): |
| 519 | _checksum_cache.save_merge() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 520 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 521 | def fetcher_compare_revisions(d): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 522 | """ |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 523 | Compare the revisions in the persistent cache with the saved values from |
| 524 | when bitbake was started and return true if they have changed. |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 525 | """ |
| 526 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 527 | headrevs = dict(bb.persist_data.persist('BB_URI_HEADREVS', d)) |
| 528 | return headrevs != bb.fetch2.saved_headrevs |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 529 | |
| 530 | def mirror_from_string(data): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 531 | mirrors = (data or "").replace('\\n',' ').split() |
| 532 | # Split into pairs |
| 533 | if len(mirrors) % 2 != 0: |
| 534 | bb.warn('Invalid mirror data %s, should have paired members.' % data) |
| 535 | return list(zip(*[iter(mirrors)]*2)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 536 | |
| 537 | def verify_checksum(ud, d, precomputed={}): |
| 538 | """ |
| 539 | verify the MD5 and SHA256 checksum for downloaded src |
| 540 | |
| 541 | Raises a FetchError if one or both of the SRC_URI checksums do not match |
| 542 | the downloaded file, or if BB_STRICT_CHECKSUM is set and there are no |
| 543 | checksums specified. |
| 544 | |
| 545 | Returns a dict of checksums that can be stored in a done stamp file and |
| 546 | passed in as precomputed parameter in a later call to avoid re-computing |
| 547 | the checksums from the file. This allows verifying the checksums of the |
| 548 | file against those in the recipe each time, rather than only after |
| 549 | downloading. See https://bugzilla.yoctoproject.org/show_bug.cgi?id=5571. |
| 550 | """ |
| 551 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 552 | if ud.ignore_checksums or not ud.method.supports_checksum(ud): |
| 553 | return {} |
| 554 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 555 | def compute_checksum_info(checksum_id): |
| 556 | checksum_name = getattr(ud, "%s_name" % checksum_id) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 557 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 558 | if checksum_id in precomputed: |
| 559 | checksum_data = precomputed[checksum_id] |
| 560 | else: |
| 561 | checksum_data = getattr(bb.utils, "%s_file" % checksum_id)(ud.localpath) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 562 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 563 | checksum_expected = getattr(ud, "%s_expected" % checksum_id) |
| 564 | |
Andrew Geissler | 0903674 | 2021-06-25 14:25:14 -0500 | [diff] [blame] | 565 | if checksum_expected == '': |
| 566 | checksum_expected = None |
| 567 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 568 | return { |
| 569 | "id": checksum_id, |
| 570 | "name": checksum_name, |
| 571 | "data": checksum_data, |
| 572 | "expected": checksum_expected |
| 573 | } |
| 574 | |
| 575 | checksum_infos = [] |
| 576 | for checksum_id in CHECKSUM_LIST: |
| 577 | checksum_infos.append(compute_checksum_info(checksum_id)) |
| 578 | |
| 579 | checksum_dict = {ci["id"] : ci["data"] for ci in checksum_infos} |
| 580 | checksum_event = {"%ssum" % ci["id"] : ci["data"] for ci in checksum_infos} |
| 581 | |
| 582 | for ci in checksum_infos: |
| 583 | if ci["id"] in SHOWN_CHECKSUM_LIST: |
| 584 | checksum_lines = ["SRC_URI[%s] = \"%s\"" % (ci["name"], ci["data"])] |
| 585 | |
| 586 | # If no checksum has been provided |
| 587 | if ud.method.recommends_checksum(ud) and all(ci["expected"] is None for ci in checksum_infos): |
| 588 | messages = [] |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 589 | strict = d.getVar("BB_STRICT_CHECKSUM") or "0" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 590 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 591 | # If strict checking enabled and neither sum defined, raise error |
| 592 | if strict == "1": |
| 593 | messages.append("No checksum specified for '%s', please add at " \ |
| 594 | "least one to the recipe:" % ud.localpath) |
| 595 | messages.extend(checksum_lines) |
| 596 | logger.error("\n".join(messages)) |
| 597 | raise NoChecksumError("Missing SRC_URI checksum", ud.url) |
| 598 | |
| 599 | bb.event.fire(MissingChecksumEvent(ud.url, **checksum_event), d) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 600 | |
| 601 | if strict == "ignore": |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 602 | return checksum_dict |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 603 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 604 | # Log missing sums so user can more easily add them |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 605 | messages.append("Missing checksum for '%s', consider adding at " \ |
| 606 | "least one to the recipe:" % ud.localpath) |
| 607 | messages.extend(checksum_lines) |
| 608 | logger.warning("\n".join(messages)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 609 | |
| 610 | # We want to alert the user if a checksum is defined in the recipe but |
| 611 | # it does not match. |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 612 | messages = [] |
| 613 | messages.append("Checksum mismatch!") |
| 614 | bad_checksum = None |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 615 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 616 | for ci in checksum_infos: |
| 617 | if ci["expected"] and ci["expected"] != ci["data"]: |
Andrew Geissler | 0903674 | 2021-06-25 14:25:14 -0500 | [diff] [blame] | 618 | messages.append("File: '%s' has %s checksum '%s' when '%s' was " \ |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 619 | "expected" % (ud.localpath, ci["id"], ci["data"], ci["expected"])) |
| 620 | bad_checksum = ci["data"] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 621 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 622 | if bad_checksum: |
| 623 | messages.append("If this change is expected (e.g. you have upgraded " \ |
| 624 | "to a new version without updating the checksums) " \ |
| 625 | "then you can use these lines within the recipe:") |
| 626 | messages.extend(checksum_lines) |
| 627 | messages.append("Otherwise you should retry the download and/or " \ |
| 628 | "check with upstream to determine if the file has " \ |
| 629 | "become corrupted or otherwise unexpectedly modified.") |
| 630 | raise ChecksumError("\n".join(messages), ud.url, bad_checksum) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 631 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 632 | return checksum_dict |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 633 | |
| 634 | def verify_donestamp(ud, d, origud=None): |
| 635 | """ |
| 636 | Check whether the done stamp file has the right checksums (if the fetch |
| 637 | method supports them). If it doesn't, delete the done stamp and force |
| 638 | a re-download. |
| 639 | |
| 640 | Returns True, if the donestamp exists and is valid, False otherwise. When |
| 641 | returning False, any existing done stamps are removed. |
| 642 | """ |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 643 | if not ud.needdonestamp or (origud and not origud.needdonestamp): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 644 | return True |
| 645 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 646 | if not os.path.exists(ud.localpath): |
| 647 | # local path does not exist |
| 648 | if os.path.exists(ud.donestamp): |
| 649 | # done stamp exists, but the downloaded file does not; the done stamp |
| 650 | # must be incorrect, re-trigger the download |
| 651 | bb.utils.remove(ud.donestamp) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 652 | return False |
| 653 | |
| 654 | if (not ud.method.supports_checksum(ud) or |
| 655 | (origud and not origud.method.supports_checksum(origud))): |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 656 | # if done stamp exists and checksums not supported; assume the local |
| 657 | # file is current |
| 658 | return os.path.exists(ud.donestamp) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 659 | |
| 660 | precomputed_checksums = {} |
| 661 | # Only re-use the precomputed checksums if the donestamp is newer than the |
| 662 | # file. Do not rely on the mtime of directories, though. If ud.localpath is |
| 663 | # a directory, there will probably not be any checksums anyway. |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 664 | 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] | 665 | os.path.getmtime(ud.localpath) < os.path.getmtime(ud.donestamp)): |
| 666 | try: |
| 667 | with open(ud.donestamp, "rb") as cachefile: |
| 668 | pickled = pickle.Unpickler(cachefile) |
| 669 | precomputed_checksums.update(pickled.load()) |
| 670 | except Exception as e: |
| 671 | # Avoid the warnings on the upgrade path from emtpy done stamp |
| 672 | # files to those containing the checksums. |
| 673 | if not isinstance(e, EOFError): |
| 674 | # Ignore errors, they aren't fatal |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 675 | logger.warning("Couldn't load checksums from donestamp %s: %s " |
| 676 | "(msg: %s)" % (ud.donestamp, type(e).__name__, |
| 677 | str(e))) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 678 | |
| 679 | try: |
| 680 | checksums = verify_checksum(ud, d, precomputed_checksums) |
| 681 | # If the cache file did not have the checksums, compute and store them |
| 682 | # as an upgrade path from the previous done stamp file format. |
| 683 | if checksums != precomputed_checksums: |
| 684 | with open(ud.donestamp, "wb") as cachefile: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 685 | p = pickle.Pickler(cachefile, 2) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 686 | p.dump(checksums) |
| 687 | return True |
| 688 | except ChecksumError as e: |
| 689 | # Checksums failed to verify, trigger re-download and remove the |
| 690 | # incorrect stamp file. |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 691 | logger.warning("Checksum mismatch for local file %s\n" |
| 692 | "Cleaning and trying again." % ud.localpath) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 693 | if os.path.exists(ud.localpath): |
| 694 | rename_bad_checksum(ud, e.checksum) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 695 | bb.utils.remove(ud.donestamp) |
| 696 | return False |
| 697 | |
| 698 | |
| 699 | def update_stamp(ud, d): |
| 700 | """ |
| 701 | donestamp is file stamp indicating the whole fetching is done |
| 702 | this function update the stamp after verifying the checksum |
| 703 | """ |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 704 | if not ud.needdonestamp: |
| 705 | return |
| 706 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 707 | if os.path.exists(ud.donestamp): |
| 708 | # Touch the done stamp file to show active use of the download |
| 709 | try: |
| 710 | os.utime(ud.donestamp, None) |
| 711 | except: |
| 712 | # Errors aren't fatal here |
| 713 | pass |
| 714 | else: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 715 | try: |
| 716 | checksums = verify_checksum(ud, d) |
| 717 | # Store the checksums for later re-verification against the recipe |
| 718 | with open(ud.donestamp, "wb") as cachefile: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 719 | p = pickle.Pickler(cachefile, 2) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 720 | p.dump(checksums) |
| 721 | except ChecksumError as e: |
| 722 | # Checksums failed to verify, trigger re-download and remove the |
| 723 | # incorrect stamp file. |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 724 | logger.warning("Checksum mismatch for local file %s\n" |
| 725 | "Cleaning and trying again." % ud.localpath) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 726 | if os.path.exists(ud.localpath): |
| 727 | rename_bad_checksum(ud, e.checksum) |
| 728 | bb.utils.remove(ud.donestamp) |
| 729 | raise |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 730 | |
| 731 | def subprocess_setup(): |
| 732 | # Python installs a SIGPIPE handler by default. This is usually not what |
| 733 | # non-Python subprocesses expect. |
| 734 | # SIGPIPE errors are known issues with gzip/bash |
| 735 | signal.signal(signal.SIGPIPE, signal.SIG_DFL) |
| 736 | |
| 737 | def get_autorev(d): |
| 738 | # only not cache src rev in autorev case |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 739 | if d.getVar('BB_SRCREV_POLICY') != "cache": |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 740 | d.setVar('BB_DONT_CACHE', '1') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 741 | return "AUTOINC" |
| 742 | |
| 743 | def get_srcrev(d, method_name='sortable_revision'): |
| 744 | """ |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 745 | 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] | 746 | Most packages usually only have one SCM so we just pass on the call. |
| 747 | In the multi SCM case, we build a value based on SRCREV_FORMAT which must |
| 748 | have been set. |
| 749 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 750 | 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] | 751 | incremental, other code is then responsible for turning that into an increasing value (if needed) |
| 752 | |
| 753 | A method_name can be supplied to retrieve an alternatively formatted revision from a fetcher, if |
| 754 | that fetcher provides a method with the given name and the same signature as sortable_revision. |
| 755 | """ |
| 756 | |
| 757 | scms = [] |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 758 | fetcher = Fetch(d.getVar('SRC_URI').split(), d) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 759 | urldata = fetcher.ud |
| 760 | for u in urldata: |
| 761 | if urldata[u].method.supports_srcrev(): |
| 762 | scms.append(u) |
| 763 | |
| 764 | if len(scms) == 0: |
| 765 | raise FetchError("SRCREV was used yet no valid SCM was found in SRC_URI") |
| 766 | |
| 767 | if len(scms) == 1 and len(urldata[scms[0]].names) == 1: |
| 768 | autoinc, rev = getattr(urldata[scms[0]].method, method_name)(urldata[scms[0]], d, urldata[scms[0]].names[0]) |
| 769 | if len(rev) > 10: |
| 770 | rev = rev[:10] |
| 771 | if autoinc: |
| 772 | return "AUTOINC+" + rev |
| 773 | return rev |
| 774 | |
| 775 | # |
| 776 | # Mutiple SCMs are in SRC_URI so we resort to SRCREV_FORMAT |
| 777 | # |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 778 | format = d.getVar('SRCREV_FORMAT') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 779 | if not format: |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 780 | raise FetchError("The SRCREV_FORMAT variable must be set when multiple SCMs are used.\n"\ |
| 781 | "The SCMs are:\n%s" % '\n'.join(scms)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 782 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 783 | name_to_rev = {} |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 784 | seenautoinc = False |
| 785 | for scm in scms: |
| 786 | ud = urldata[scm] |
| 787 | for name in ud.names: |
| 788 | autoinc, rev = getattr(ud.method, method_name)(ud, d, name) |
| 789 | seenautoinc = seenautoinc or autoinc |
| 790 | if len(rev) > 10: |
| 791 | rev = rev[:10] |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 792 | name_to_rev[name] = rev |
| 793 | # Replace names by revisions in the SRCREV_FORMAT string. The approach used |
| 794 | # here can handle names being prefixes of other names and names appearing |
| 795 | # as substrings in revisions (in which case the name should not be |
| 796 | # expanded). The '|' regular expression operator tries matches from left to |
| 797 | # right, so we need to sort the names with the longest ones first. |
| 798 | names_descending_len = sorted(name_to_rev, key=len, reverse=True) |
| 799 | name_to_rev_re = "|".join(re.escape(name) for name in names_descending_len) |
| 800 | format = re.sub(name_to_rev_re, lambda match: name_to_rev[match.group(0)], format) |
| 801 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 802 | if seenautoinc: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 803 | format = "AUTOINC+" + format |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 804 | |
| 805 | return format |
| 806 | |
| 807 | def localpath(url, d): |
| 808 | fetcher = bb.fetch2.Fetch([url], d) |
| 809 | return fetcher.localpath(url) |
| 810 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 811 | def runfetchcmd(cmd, d, quiet=False, cleanup=None, log=None, workdir=None): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 812 | """ |
| 813 | Run cmd returning the command output |
| 814 | Raise an error if interrupted or cmd fails |
| 815 | Optionally echo command output to stdout |
| 816 | Optionally remove the files/directories listed in cleanup upon failure |
| 817 | """ |
| 818 | |
| 819 | # Need to export PATH as binary could be in metadata paths |
| 820 | # rather than host provided |
| 821 | # Also include some other variables. |
| 822 | # FIXME: Should really include all export varaiables? |
| 823 | exportvars = ['HOME', 'PATH', |
| 824 | 'HTTP_PROXY', 'http_proxy', |
| 825 | 'HTTPS_PROXY', 'https_proxy', |
| 826 | 'FTP_PROXY', 'ftp_proxy', |
| 827 | 'FTPS_PROXY', 'ftps_proxy', |
| 828 | 'NO_PROXY', 'no_proxy', |
| 829 | 'ALL_PROXY', 'all_proxy', |
| 830 | 'GIT_PROXY_COMMAND', |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 831 | 'GIT_SSH', |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 832 | 'GIT_SSL_CAINFO', |
| 833 | 'GIT_SMART_HTTP', |
| 834 | 'SSH_AUTH_SOCK', 'SSH_AGENT_PID', |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 835 | 'SOCKS5_USER', 'SOCKS5_PASSWD', |
| 836 | 'DBUS_SESSION_BUS_ADDRESS', |
Andrew Geissler | 5f35090 | 2021-07-23 13:09:54 -0400 | [diff] [blame] | 837 | 'P4CONFIG', |
| 838 | 'AWS_ACCESS_KEY_ID', |
| 839 | 'AWS_SECRET_ACCESS_KEY', |
| 840 | 'AWS_DEFAULT_REGION'] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 841 | |
| 842 | if not cleanup: |
| 843 | cleanup = [] |
| 844 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 845 | # If PATH contains WORKDIR which contains PV-PR which contains SRCPV we |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 846 | # can end up in circular recursion here so give the option of breaking it |
| 847 | # in a data store copy. |
| 848 | try: |
| 849 | d.getVar("PV") |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 850 | d.getVar("PR") |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 851 | except bb.data_smart.ExpansionError: |
| 852 | d = bb.data.createCopy(d) |
| 853 | d.setVar("PV", "fetcheravoidrecurse") |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 854 | d.setVar("PR", "fetcheravoidrecurse") |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 855 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 856 | origenv = d.getVar("BB_ORIGENV", False) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 857 | for var in exportvars: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 858 | val = d.getVar(var) or (origenv and origenv.getVar(var)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 859 | if val: |
| 860 | cmd = 'export ' + var + '=\"%s\"; %s' % (val, cmd) |
| 861 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 862 | # Disable pseudo as it may affect ssh, potentially causing it to hang. |
| 863 | cmd = 'export PSEUDO_DISABLED=1; ' + cmd |
| 864 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 865 | if workdir: |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 866 | logger.debug("Running '%s' in %s" % (cmd, workdir)) |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 867 | else: |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 868 | logger.debug("Running %s", cmd) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 869 | |
| 870 | success = False |
| 871 | error_message = "" |
| 872 | |
| 873 | try: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 874 | (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] | 875 | success = True |
| 876 | except bb.process.NotFoundError as e: |
| 877 | error_message = "Fetch command %s" % (e.command) |
| 878 | except bb.process.ExecutionError as e: |
| 879 | if e.stdout: |
| 880 | output = "output:\n%s\n%s" % (e.stdout, e.stderr) |
| 881 | elif e.stderr: |
| 882 | output = "output:\n%s" % e.stderr |
| 883 | else: |
| 884 | output = "no output" |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 885 | 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] | 886 | except bb.process.CmdError as e: |
| 887 | error_message = "Fetch command %s could not be run:\n%s" % (e.command, e.msg) |
| 888 | if not success: |
| 889 | for f in cleanup: |
| 890 | try: |
| 891 | bb.utils.remove(f, True) |
| 892 | except OSError: |
| 893 | pass |
| 894 | |
| 895 | raise FetchError(error_message) |
| 896 | |
| 897 | return output |
| 898 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 899 | def check_network_access(d, info, url): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 900 | """ |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 901 | log remote network access, and error if BB_NO_NETWORK is set or the given |
| 902 | URI is untrusted |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 903 | """ |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 904 | if bb.utils.to_boolean(d.getVar("BB_NO_NETWORK")): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 905 | raise NetworkAccess(url, info) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 906 | elif not trusted_network(d, url): |
| 907 | raise UntrustedUrl(url, info) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 908 | else: |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 909 | logger.debug("Fetcher accessed the network with the command %s" % info) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 910 | |
| 911 | def build_mirroruris(origud, mirrors, ld): |
| 912 | uris = [] |
| 913 | uds = [] |
| 914 | |
| 915 | replacements = {} |
| 916 | replacements["TYPE"] = origud.type |
| 917 | replacements["HOST"] = origud.host |
| 918 | replacements["PATH"] = origud.path |
| 919 | replacements["BASENAME"] = origud.path.split("/")[-1] |
| 920 | replacements["MIRRORNAME"] = origud.host.replace(':','.') + origud.path.replace('/', '.').replace('*', '.') |
| 921 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 922 | def adduri(ud, uris, uds, mirrors, tarballs): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 923 | for line in mirrors: |
| 924 | try: |
| 925 | (find, replace) = line |
| 926 | except ValueError: |
| 927 | continue |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 928 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 929 | for tarball in tarballs: |
| 930 | newuri = uri_replace(ud, find, replace, replacements, ld, tarball) |
| 931 | if not newuri or newuri in uris or newuri == origud.url: |
| 932 | continue |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 933 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 934 | if not trusted_network(ld, newuri): |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 935 | 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] | 936 | continue |
Patrick Williams | d7e9631 | 2015-09-22 08:09:05 -0500 | [diff] [blame] | 937 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 938 | # Create a local copy of the mirrors minus the current line |
| 939 | # this will prevent us from recursively processing the same line |
| 940 | # as well as indirect recursion A -> B -> C -> A |
| 941 | localmirrors = list(mirrors) |
| 942 | localmirrors.remove(line) |
| 943 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 944 | try: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 945 | newud = FetchData(newuri, ld) |
| 946 | newud.setup_localpath(ld) |
| 947 | except bb.fetch2.BBFetchException as e: |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 948 | logger.debug("Mirror fetch failure for url %s (original url: %s)" % (newuri, origud.url)) |
| 949 | logger.debug(str(e)) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 950 | try: |
| 951 | # setup_localpath of file:// urls may fail, we should still see |
| 952 | # if mirrors of the url exist |
| 953 | adduri(newud, uris, uds, localmirrors, tarballs) |
| 954 | except UnboundLocalError: |
| 955 | pass |
| 956 | continue |
| 957 | uris.append(newuri) |
| 958 | uds.append(newud) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 959 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 960 | adduri(newud, uris, uds, localmirrors, tarballs) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 961 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 962 | adduri(origud, uris, uds, mirrors, origud.mirrortarballs or [None]) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 963 | |
| 964 | return uris, uds |
| 965 | |
| 966 | def rename_bad_checksum(ud, suffix): |
| 967 | """ |
| 968 | Renames files to have suffix from parameter |
| 969 | """ |
| 970 | |
| 971 | if ud.localpath is None: |
| 972 | return |
| 973 | |
| 974 | new_localpath = "%s_bad-checksum_%s" % (ud.localpath, suffix) |
| 975 | bb.warn("Renaming %s to %s" % (ud.localpath, new_localpath)) |
Brad Bishop | 79641f2 | 2019-09-10 07:20:22 -0400 | [diff] [blame] | 976 | if not bb.utils.movefile(ud.localpath, new_localpath): |
| 977 | 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] | 978 | |
| 979 | |
| 980 | def try_mirror_url(fetch, origud, ud, ld, check = False): |
| 981 | # Return of None or a value means we're finished |
| 982 | # False means try another url |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 983 | |
| 984 | if ud.lockfile and ud.lockfile != origud.lockfile: |
| 985 | lf = bb.utils.lockfile(ud.lockfile) |
| 986 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 987 | try: |
| 988 | if check: |
| 989 | found = ud.method.checkstatus(fetch, ud, ld) |
| 990 | if found: |
| 991 | return found |
| 992 | return False |
| 993 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 994 | if not verify_donestamp(ud, ld, origud) or ud.method.need_update(ud, ld): |
| 995 | ud.method.download(ud, ld) |
| 996 | if hasattr(ud.method,"build_mirror_data"): |
| 997 | ud.method.build_mirror_data(ud, ld) |
| 998 | |
| 999 | if not ud.localpath or not os.path.exists(ud.localpath): |
| 1000 | return False |
| 1001 | |
| 1002 | if ud.localpath == origud.localpath: |
| 1003 | return ud.localpath |
| 1004 | |
| 1005 | # We may be obtaining a mirror tarball which needs further processing by the real fetcher |
| 1006 | # 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] | 1007 | dldir = ld.getVar("DL_DIR") |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1008 | |
| 1009 | 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] | 1010 | # Create donestamp in old format to avoid triggering a re-download |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 1011 | if ud.donestamp: |
| 1012 | bb.utils.mkdirhier(os.path.dirname(ud.donestamp)) |
| 1013 | open(ud.donestamp, 'w').close() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1014 | dest = os.path.join(dldir, os.path.basename(ud.localpath)) |
| 1015 | if not os.path.exists(dest): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1016 | # In case this is executing without any file locks held (as is |
| 1017 | # the case for file:// URLs), two tasks may end up here at the |
| 1018 | # same time, in which case we do not want the second task to |
| 1019 | # fail when the link has already been created by the first task. |
| 1020 | try: |
| 1021 | os.symlink(ud.localpath, dest) |
| 1022 | except FileExistsError: |
| 1023 | pass |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1024 | if not verify_donestamp(origud, ld) or origud.method.need_update(origud, ld): |
| 1025 | origud.method.download(origud, ld) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1026 | if hasattr(origud.method, "build_mirror_data"): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1027 | origud.method.build_mirror_data(origud, ld) |
Patrick Williams | f1e5d69 | 2016-03-30 15:21:19 -0500 | [diff] [blame] | 1028 | return origud.localpath |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1029 | # Otherwise the result is a local file:// and we symlink to it |
Andrew Geissler | 09209ee | 2020-12-13 08:44:15 -0600 | [diff] [blame] | 1030 | ensure_symlink(ud.localpath, origud.localpath) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1031 | update_stamp(origud, ld) |
| 1032 | return ud.localpath |
| 1033 | |
| 1034 | except bb.fetch2.NetworkAccess: |
| 1035 | raise |
| 1036 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1037 | except IOError as e: |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 1038 | if e.errno in [errno.ESTALE]: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1039 | logger.warning("Stale Error Observed %s." % ud.url) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1040 | return False |
| 1041 | raise |
| 1042 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1043 | except bb.fetch2.BBFetchException as e: |
| 1044 | if isinstance(e, ChecksumError): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1045 | logger.warning("Mirror checksum failure for url %s (original url: %s)\nCleaning and trying again." % (ud.url, origud.url)) |
| 1046 | logger.warning(str(e)) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 1047 | if os.path.exists(ud.localpath): |
| 1048 | rename_bad_checksum(ud, e.checksum) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1049 | elif isinstance(e, NoChecksumError): |
| 1050 | raise |
| 1051 | else: |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 1052 | logger.debug("Mirror fetch failure for url %s (original url: %s)" % (ud.url, origud.url)) |
| 1053 | logger.debug(str(e)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1054 | try: |
| 1055 | ud.method.clean(ud, ld) |
| 1056 | except UnboundLocalError: |
| 1057 | pass |
| 1058 | return False |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 1059 | finally: |
| 1060 | if ud.lockfile and ud.lockfile != origud.lockfile: |
| 1061 | bb.utils.unlockfile(lf) |
| 1062 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1063 | |
Andrew Geissler | 09209ee | 2020-12-13 08:44:15 -0600 | [diff] [blame] | 1064 | def ensure_symlink(target, link_name): |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 1065 | if not os.path.exists(link_name): |
| 1066 | if os.path.islink(link_name): |
| 1067 | # Broken symbolic link |
| 1068 | os.unlink(link_name) |
| 1069 | |
| 1070 | # In case this is executing without any file locks held (as is |
| 1071 | # the case for file:// URLs), two tasks may end up here at the |
| 1072 | # same time, in which case we do not want the second task to |
| 1073 | # fail when the link has already been created by the first task. |
| 1074 | try: |
| 1075 | os.symlink(target, link_name) |
| 1076 | except FileExistsError: |
| 1077 | pass |
| 1078 | |
| 1079 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1080 | def try_mirrors(fetch, d, origud, mirrors, check = False): |
| 1081 | """ |
| 1082 | Try to use a mirrored version of the sources. |
| 1083 | This method will be automatically called before the fetchers go. |
| 1084 | |
| 1085 | d Is a bb.data instance |
| 1086 | uri is the original uri we're trying to download |
| 1087 | mirrors is the list of mirrors we're going to try |
| 1088 | """ |
| 1089 | ld = d.createCopy() |
| 1090 | |
| 1091 | uris, uds = build_mirroruris(origud, mirrors, ld) |
| 1092 | |
| 1093 | for index, uri in enumerate(uris): |
| 1094 | ret = try_mirror_url(fetch, origud, uds[index], ld, check) |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1095 | if ret: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1096 | return ret |
| 1097 | return None |
| 1098 | |
| 1099 | def trusted_network(d, url): |
| 1100 | """ |
| 1101 | Use a trusted url during download if networking is enabled and |
| 1102 | BB_ALLOWED_NETWORKS is set globally or for a specific recipe. |
| 1103 | Note: modifies SRC_URI & mirrors. |
| 1104 | """ |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 1105 | if bb.utils.to_boolean(d.getVar("BB_NO_NETWORK")): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1106 | return True |
| 1107 | |
| 1108 | pkgname = d.expand(d.getVar('PN', False)) |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 1109 | trusted_hosts = None |
| 1110 | if pkgname: |
| 1111 | trusted_hosts = d.getVarFlag('BB_ALLOWED_NETWORKS', pkgname, False) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1112 | |
| 1113 | if not trusted_hosts: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1114 | trusted_hosts = d.getVar('BB_ALLOWED_NETWORKS') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1115 | |
| 1116 | # Not enabled. |
| 1117 | if not trusted_hosts: |
| 1118 | return True |
| 1119 | |
| 1120 | scheme, network, path, user, passwd, param = decodeurl(url) |
| 1121 | |
| 1122 | if not network: |
| 1123 | return True |
| 1124 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 1125 | network = network.split(':')[0] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1126 | network = network.lower() |
| 1127 | |
| 1128 | for host in trusted_hosts.split(" "): |
| 1129 | host = host.lower() |
| 1130 | if host.startswith("*.") and ("." + network).endswith(host[1:]): |
| 1131 | return True |
| 1132 | if host == network: |
| 1133 | return True |
| 1134 | |
| 1135 | return False |
| 1136 | |
| 1137 | def srcrev_internal_helper(ud, d, name): |
| 1138 | """ |
| 1139 | Return: |
| 1140 | a) a source revision if specified |
| 1141 | b) latest revision if SRCREV="AUTOINC" |
| 1142 | c) None if not specified |
| 1143 | """ |
| 1144 | |
| 1145 | srcrev = None |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1146 | pn = d.getVar("PN") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1147 | attempts = [] |
| 1148 | if name != '' and pn: |
| 1149 | attempts.append("SRCREV_%s_pn-%s" % (name, pn)) |
| 1150 | if name != '': |
| 1151 | attempts.append("SRCREV_%s" % name) |
| 1152 | if pn: |
| 1153 | attempts.append("SRCREV_pn-%s" % pn) |
| 1154 | attempts.append("SRCREV") |
| 1155 | |
| 1156 | for a in attempts: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1157 | srcrev = d.getVar(a) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1158 | if srcrev and srcrev != "INVALID": |
| 1159 | break |
| 1160 | |
| 1161 | if 'rev' in ud.parm and 'tag' in ud.parm: |
| 1162 | raise FetchError("Please specify a ;rev= parameter or a ;tag= parameter in the url %s but not both." % (ud.url)) |
| 1163 | |
| 1164 | if 'rev' in ud.parm or 'tag' in ud.parm: |
| 1165 | if 'rev' in ud.parm: |
| 1166 | parmrev = ud.parm['rev'] |
| 1167 | else: |
| 1168 | parmrev = ud.parm['tag'] |
| 1169 | if srcrev == "INVALID" or not srcrev: |
| 1170 | return parmrev |
| 1171 | if srcrev != parmrev: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1172 | 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] | 1173 | return parmrev |
| 1174 | |
| 1175 | if srcrev == "INVALID" or not srcrev: |
| 1176 | 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) |
| 1177 | if srcrev == "AUTOINC": |
| 1178 | srcrev = ud.method.latest_revision(ud, d, name) |
| 1179 | |
| 1180 | return srcrev |
| 1181 | |
| 1182 | def get_checksum_file_list(d): |
| 1183 | """ Get a list of files checksum in SRC_URI |
| 1184 | |
| 1185 | Returns the resolved local paths of all local file entries in |
| 1186 | SRC_URI as a space-separated string |
| 1187 | """ |
| 1188 | fetch = Fetch([], d, cache = False, localonly = True) |
| 1189 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1190 | dl_dir = d.getVar('DL_DIR') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1191 | filelist = [] |
| 1192 | for u in fetch.urls: |
| 1193 | ud = fetch.ud[u] |
| 1194 | |
| 1195 | if ud and isinstance(ud.method, local.Local): |
| 1196 | paths = ud.method.localpaths(ud, d) |
| 1197 | for f in paths: |
| 1198 | pth = ud.decodedurl |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1199 | if f.startswith(dl_dir): |
| 1200 | # The local fetcher's behaviour is to return a path under DL_DIR if it couldn't find the file anywhere else |
| 1201 | if os.path.exists(f): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1202 | bb.warn("Getting checksum for %s SRC_URI entry %s: file not found except in DL_DIR" % (d.getVar('PN'), os.path.basename(f))) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1203 | else: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1204 | bb.warn("Unable to get checksum for %s SRC_URI entry %s: file could not be found" % (d.getVar('PN'), os.path.basename(f))) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1205 | filelist.append(f + ":" + str(os.path.exists(f))) |
| 1206 | |
| 1207 | return " ".join(filelist) |
| 1208 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1209 | def get_file_checksums(filelist, pn, localdirsexclude): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1210 | """Get a list of the checksums for a list of local files |
| 1211 | |
| 1212 | Returns the checksums for a list of local files, caching the results as |
| 1213 | it proceeds |
| 1214 | |
| 1215 | """ |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1216 | return _checksum_cache.get_checksums(filelist, pn, localdirsexclude) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1217 | |
| 1218 | |
| 1219 | class FetchData(object): |
| 1220 | """ |
| 1221 | A class which represents the fetcher state for a given URI. |
| 1222 | """ |
| 1223 | def __init__(self, url, d, localonly = False): |
| 1224 | # localpath is the location of a downloaded result. If not set, the file is local. |
| 1225 | self.donestamp = None |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 1226 | self.needdonestamp = True |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1227 | self.localfile = "" |
| 1228 | self.localpath = None |
| 1229 | self.lockfile = None |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1230 | self.mirrortarballs = [] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1231 | self.basename = None |
| 1232 | self.basepath = None |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1233 | (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] | 1234 | self.date = self.getSRCDate(d) |
| 1235 | self.url = url |
| 1236 | if not self.user and "user" in self.parm: |
| 1237 | self.user = self.parm["user"] |
| 1238 | if not self.pswd and "pswd" in self.parm: |
| 1239 | self.pswd = self.parm["pswd"] |
| 1240 | self.setup = False |
| 1241 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1242 | def configure_checksum(checksum_id): |
| 1243 | if "name" in self.parm: |
| 1244 | checksum_name = "%s.%ssum" % (self.parm["name"], checksum_id) |
| 1245 | else: |
| 1246 | checksum_name = "%ssum" % checksum_id |
| 1247 | |
| 1248 | setattr(self, "%s_name" % checksum_id, checksum_name) |
| 1249 | |
| 1250 | if checksum_name in self.parm: |
| 1251 | checksum_expected = self.parm[checksum_name] |
Andrew Geissler | 95ac1b8 | 2021-03-31 14:34:31 -0500 | [diff] [blame] | 1252 | elif self.type not in ["http", "https", "ftp", "ftps", "sftp", "s3", "az"]: |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1253 | checksum_expected = None |
| 1254 | else: |
| 1255 | checksum_expected = d.getVarFlag("SRC_URI", checksum_name) |
| 1256 | |
| 1257 | setattr(self, "%s_expected" % checksum_id, checksum_expected) |
| 1258 | |
| 1259 | for checksum_id in CHECKSUM_LIST: |
| 1260 | configure_checksum(checksum_id) |
| 1261 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1262 | self.ignore_checksums = False |
| 1263 | |
| 1264 | self.names = self.parm.get("name",'default').split(',') |
| 1265 | |
| 1266 | self.method = None |
| 1267 | for m in methods: |
| 1268 | if m.supports(self, d): |
| 1269 | self.method = m |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1270 | break |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1271 | |
| 1272 | if not self.method: |
| 1273 | raise NoMethodError(url) |
| 1274 | |
| 1275 | if localonly and not isinstance(self.method, local.Local): |
| 1276 | raise NonLocalMethod() |
| 1277 | |
| 1278 | if self.parm.get("proto", None) and "protocol" not in self.parm: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1279 | 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] | 1280 | self.parm["protocol"] = self.parm.get("proto", None) |
| 1281 | |
| 1282 | if hasattr(self.method, "urldata_init"): |
| 1283 | self.method.urldata_init(self, d) |
| 1284 | |
| 1285 | if "localpath" in self.parm: |
| 1286 | # if user sets localpath for file, use it instead. |
| 1287 | self.localpath = self.parm["localpath"] |
| 1288 | self.basename = os.path.basename(self.localpath) |
| 1289 | elif self.localfile: |
| 1290 | self.localpath = self.method.localpath(self, d) |
| 1291 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1292 | dldir = d.getVar("DL_DIR") |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 1293 | |
| 1294 | if not self.needdonestamp: |
| 1295 | return |
| 1296 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1297 | # Note: .done and .lock files should always be in DL_DIR whereas localpath may not be. |
| 1298 | if self.localpath and self.localpath.startswith(dldir): |
| 1299 | basepath = self.localpath |
| 1300 | elif self.localpath: |
| 1301 | basepath = dldir + os.sep + os.path.basename(self.localpath) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 1302 | elif self.basepath or self.basename: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1303 | basepath = dldir + os.sep + (self.basepath or self.basename) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 1304 | else: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1305 | bb.fatal("Can't determine lock path for url %s" % url) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 1306 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1307 | self.donestamp = basepath + '.done' |
| 1308 | self.lockfile = basepath + '.lock' |
| 1309 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1310 | def setup_revisions(self, d): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1311 | self.revisions = {} |
| 1312 | for name in self.names: |
| 1313 | self.revisions[name] = srcrev_internal_helper(self, d, name) |
| 1314 | |
| 1315 | # add compatibility code for non name specified case |
| 1316 | if len(self.names) == 1: |
| 1317 | self.revision = self.revisions[self.names[0]] |
| 1318 | |
| 1319 | def setup_localpath(self, d): |
| 1320 | if not self.localpath: |
| 1321 | self.localpath = self.method.localpath(self, d) |
| 1322 | |
| 1323 | def getSRCDate(self, d): |
| 1324 | """ |
| 1325 | Return the SRC Date for the component |
| 1326 | |
| 1327 | d the bb.data module |
| 1328 | """ |
| 1329 | if "srcdate" in self.parm: |
| 1330 | return self.parm['srcdate'] |
| 1331 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1332 | pn = d.getVar("PN") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1333 | |
| 1334 | if pn: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1335 | 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] | 1336 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1337 | return d.getVar("SRCDATE") or d.getVar("DATE") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1338 | |
| 1339 | class FetchMethod(object): |
| 1340 | """Base class for 'fetch'ing data""" |
| 1341 | |
| 1342 | def __init__(self, urls=None): |
| 1343 | self.urls = [] |
| 1344 | |
| 1345 | def supports(self, urldata, d): |
| 1346 | """ |
| 1347 | Check to see if this fetch class supports a given url. |
| 1348 | """ |
| 1349 | return 0 |
| 1350 | |
| 1351 | def localpath(self, urldata, d): |
| 1352 | """ |
| 1353 | Return the local filename of a given url assuming a successful fetch. |
| 1354 | Can also setup variables in urldata for use in go (saving code duplication |
| 1355 | and duplicate code execution) |
| 1356 | """ |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1357 | return os.path.join(d.getVar("DL_DIR"), urldata.localfile) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1358 | |
| 1359 | def supports_checksum(self, urldata): |
| 1360 | """ |
| 1361 | Is localpath something that can be represented by a checksum? |
| 1362 | """ |
| 1363 | |
| 1364 | # We cannot compute checksums for directories |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1365 | if os.path.isdir(urldata.localpath): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1366 | return False |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1367 | return True |
| 1368 | |
| 1369 | def recommends_checksum(self, urldata): |
| 1370 | """ |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1371 | Is the backend on where checksumming is recommended (should warnings |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1372 | be displayed if there is no checksum)? |
| 1373 | """ |
| 1374 | return False |
| 1375 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1376 | def verify_donestamp(self, ud, d): |
| 1377 | """ |
| 1378 | Verify the donestamp file |
| 1379 | """ |
| 1380 | return verify_donestamp(ud, d) |
| 1381 | |
| 1382 | def update_donestamp(self, ud, d): |
| 1383 | """ |
| 1384 | Update the donestamp file |
| 1385 | """ |
| 1386 | update_stamp(ud, d) |
| 1387 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1388 | def _strip_leading_slashes(self, relpath): |
| 1389 | """ |
| 1390 | Remove leading slash as os.path.join can't cope |
| 1391 | """ |
| 1392 | while os.path.isabs(relpath): |
| 1393 | relpath = relpath[1:] |
| 1394 | return relpath |
| 1395 | |
| 1396 | def setUrls(self, urls): |
| 1397 | self.__urls = urls |
| 1398 | |
| 1399 | def getUrls(self): |
| 1400 | return self.__urls |
| 1401 | |
| 1402 | urls = property(getUrls, setUrls, None, "Urls property") |
| 1403 | |
| 1404 | def need_update(self, ud, d): |
| 1405 | """ |
| 1406 | Force a fetch, even if localpath exists? |
| 1407 | """ |
| 1408 | if os.path.exists(ud.localpath): |
| 1409 | return False |
| 1410 | return True |
| 1411 | |
| 1412 | def supports_srcrev(self): |
| 1413 | """ |
| 1414 | The fetcher supports auto source revisions (SRCREV) |
| 1415 | """ |
| 1416 | return False |
| 1417 | |
| 1418 | def download(self, urldata, d): |
| 1419 | """ |
| 1420 | Fetch urls |
| 1421 | Assumes localpath was called first |
| 1422 | """ |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 1423 | raise NoMethodError(urldata.url) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1424 | |
| 1425 | def unpack(self, urldata, rootdir, data): |
| 1426 | iterate = False |
| 1427 | file = urldata.localpath |
| 1428 | |
| 1429 | try: |
| 1430 | unpack = bb.utils.to_boolean(urldata.parm.get('unpack'), True) |
| 1431 | except ValueError as exc: |
| 1432 | bb.fatal("Invalid value for 'unpack' parameter for %s: %s" % |
| 1433 | (file, urldata.parm.get('unpack'))) |
| 1434 | |
| 1435 | base, ext = os.path.splitext(file) |
| 1436 | if ext in ['.gz', '.bz2', '.Z', '.xz', '.lz']: |
| 1437 | efile = os.path.join(rootdir, os.path.basename(base)) |
| 1438 | else: |
| 1439 | efile = file |
| 1440 | cmd = None |
| 1441 | |
| 1442 | if unpack: |
| 1443 | if file.endswith('.tar'): |
| 1444 | cmd = 'tar x --no-same-owner -f %s' % file |
| 1445 | elif file.endswith('.tgz') or file.endswith('.tar.gz') or file.endswith('.tar.Z'): |
| 1446 | cmd = 'tar xz --no-same-owner -f %s' % file |
| 1447 | elif file.endswith('.tbz') or file.endswith('.tbz2') or file.endswith('.tar.bz2'): |
| 1448 | cmd = 'bzip2 -dc %s | tar x --no-same-owner -f -' % file |
| 1449 | elif file.endswith('.gz') or file.endswith('.Z') or file.endswith('.z'): |
| 1450 | cmd = 'gzip -dc %s > %s' % (file, efile) |
| 1451 | elif file.endswith('.bz2'): |
| 1452 | cmd = 'bzip2 -dc %s > %s' % (file, efile) |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 1453 | elif file.endswith('.txz') or file.endswith('.tar.xz'): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1454 | cmd = 'xz -dc %s | tar x --no-same-owner -f -' % file |
| 1455 | elif file.endswith('.xz'): |
| 1456 | cmd = 'xz -dc %s > %s' % (file, efile) |
| 1457 | elif file.endswith('.tar.lz'): |
| 1458 | cmd = 'lzip -dc %s | tar x --no-same-owner -f -' % file |
| 1459 | elif file.endswith('.lz'): |
| 1460 | cmd = 'lzip -dc %s > %s' % (file, efile) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1461 | elif file.endswith('.tar.7z'): |
| 1462 | cmd = '7z x -so %s | tar x --no-same-owner -f -' % file |
| 1463 | elif file.endswith('.7z'): |
| 1464 | cmd = '7za x -y %s 1>/dev/null' % file |
Andrew Geissler | 6ce62a2 | 2020-11-30 19:58:47 -0600 | [diff] [blame] | 1465 | elif file.endswith('.tzst') or file.endswith('.tar.zst'): |
| 1466 | cmd = 'zstd --decompress --stdout %s | tar x --no-same-owner -f -' % file |
| 1467 | elif file.endswith('.zst'): |
| 1468 | cmd = 'zstd --decompress --stdout %s > %s' % (file, efile) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1469 | elif file.endswith('.zip') or file.endswith('.jar'): |
| 1470 | try: |
| 1471 | dos = bb.utils.to_boolean(urldata.parm.get('dos'), False) |
| 1472 | except ValueError as exc: |
| 1473 | bb.fatal("Invalid value for 'dos' parameter for %s: %s" % |
| 1474 | (file, urldata.parm.get('dos'))) |
| 1475 | cmd = 'unzip -q -o' |
| 1476 | if dos: |
| 1477 | cmd = '%s -a' % cmd |
| 1478 | cmd = "%s '%s'" % (cmd, file) |
| 1479 | elif file.endswith('.rpm') or file.endswith('.srpm'): |
| 1480 | if 'extract' in urldata.parm: |
| 1481 | unpack_file = urldata.parm.get('extract') |
| 1482 | cmd = 'rpm2cpio.sh %s | cpio -id %s' % (file, unpack_file) |
| 1483 | iterate = True |
| 1484 | iterate_file = unpack_file |
| 1485 | else: |
| 1486 | cmd = 'rpm2cpio.sh %s | cpio -id' % (file) |
| 1487 | elif file.endswith('.deb') or file.endswith('.ipk'): |
Brad Bishop | a5c52ff | 2018-11-23 10:55:50 +1300 | [diff] [blame] | 1488 | output = subprocess.check_output(['ar', '-t', file], preexec_fn=subprocess_setup) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1489 | datafile = None |
| 1490 | if output: |
| 1491 | for line in output.decode().splitlines(): |
| 1492 | if line.startswith('data.tar.'): |
| 1493 | datafile = line |
| 1494 | break |
| 1495 | else: |
| 1496 | raise UnpackError("Unable to unpack deb/ipk package - does not contain data.tar.* file", urldata.url) |
| 1497 | else: |
| 1498 | raise UnpackError("Unable to unpack deb/ipk package - could not list contents", urldata.url) |
| 1499 | cmd = 'ar x %s %s && tar --no-same-owner -xpf %s && rm %s' % (file, datafile, datafile, datafile) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 1500 | |
| 1501 | # If 'subdir' param exists, create a dir and use it as destination for unpack cmd |
| 1502 | if 'subdir' in urldata.parm: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1503 | subdir = urldata.parm.get('subdir') |
| 1504 | if os.path.isabs(subdir): |
| 1505 | if not os.path.realpath(subdir).startswith(os.path.realpath(rootdir)): |
| 1506 | raise UnpackError("subdir argument isn't a subdirectory of unpack root %s" % rootdir, urldata.url) |
| 1507 | unpackdir = subdir |
| 1508 | else: |
| 1509 | unpackdir = os.path.join(rootdir, subdir) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 1510 | bb.utils.mkdirhier(unpackdir) |
| 1511 | else: |
| 1512 | unpackdir = rootdir |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1513 | |
| 1514 | if not unpack or not cmd: |
| 1515 | # 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] | 1516 | dest = os.path.join(unpackdir, os.path.basename(file)) |
| 1517 | if file != dest and not (os.path.exists(dest) and os.path.samefile(file, dest)): |
| 1518 | destdir = '.' |
| 1519 | # For file:// entries all intermediate dirs in path must be created at destination |
| 1520 | if urldata.type == "file": |
| 1521 | # Trailing '/' does a copying to wrong place |
| 1522 | urlpath = urldata.path.rstrip('/') |
| 1523 | # Want files places relative to cwd so no leading '/' |
| 1524 | urlpath = urlpath.lstrip('/') |
| 1525 | if urlpath.find("/") != -1: |
| 1526 | destdir = urlpath.rsplit("/", 1)[0] + '/' |
| 1527 | bb.utils.mkdirhier("%s/%s" % (unpackdir, destdir)) |
Andrew Geissler | c3d88e4 | 2020-10-02 09:45:00 -0500 | [diff] [blame] | 1528 | cmd = 'cp -fpPRH "%s" "%s"' % (file, destdir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1529 | |
| 1530 | if not cmd: |
| 1531 | return |
| 1532 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1533 | path = data.getVar('PATH') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1534 | if path: |
| 1535 | cmd = "PATH=\"%s\" %s" % (path, cmd) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1536 | bb.note("Unpacking %s to %s/" % (file, unpackdir)) |
| 1537 | ret = subprocess.call(cmd, preexec_fn=subprocess_setup, shell=True, cwd=unpackdir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1538 | |
| 1539 | if ret != 0: |
| 1540 | raise UnpackError("Unpack command %s failed with return value %s" % (cmd, ret), urldata.url) |
| 1541 | |
| 1542 | if iterate is True: |
| 1543 | iterate_urldata = urldata |
| 1544 | iterate_urldata.localpath = "%s/%s" % (rootdir, iterate_file) |
| 1545 | self.unpack(urldata, rootdir, data) |
| 1546 | |
| 1547 | return |
| 1548 | |
| 1549 | def clean(self, urldata, d): |
| 1550 | """ |
| 1551 | Clean any existing full or partial download |
| 1552 | """ |
| 1553 | bb.utils.remove(urldata.localpath) |
| 1554 | |
| 1555 | def try_premirror(self, urldata, d): |
| 1556 | """ |
| 1557 | Should premirrors be used? |
| 1558 | """ |
| 1559 | return True |
| 1560 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1561 | def try_mirrors(self, fetch, urldata, d, mirrors, check=False): |
| 1562 | """ |
| 1563 | Try to use a mirror |
| 1564 | """ |
| 1565 | return bool(try_mirrors(fetch, d, urldata, mirrors, check)) |
| 1566 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1567 | def checkstatus(self, fetch, urldata, d): |
| 1568 | """ |
| 1569 | Check the status of a URL |
| 1570 | Assumes localpath was called first |
| 1571 | """ |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 1572 | 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] | 1573 | return True |
| 1574 | |
| 1575 | def latest_revision(self, ud, d, name): |
| 1576 | """ |
| 1577 | Look in the cache for the latest revision, if not present ask the SCM. |
| 1578 | """ |
| 1579 | if not hasattr(self, "_latest_revision"): |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 1580 | 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] | 1581 | |
| 1582 | revs = bb.persist_data.persist('BB_URI_HEADREVS', d) |
| 1583 | key = self.generate_revision_key(ud, d, name) |
| 1584 | try: |
| 1585 | return revs[key] |
| 1586 | except KeyError: |
| 1587 | revs[key] = rev = self._latest_revision(ud, d, name) |
| 1588 | return rev |
| 1589 | |
| 1590 | def sortable_revision(self, ud, d, name): |
| 1591 | latest_rev = self._build_revision(ud, d, name) |
| 1592 | return True, str(latest_rev) |
| 1593 | |
| 1594 | def generate_revision_key(self, ud, d, name): |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1595 | return self._revision_key(ud, d, name) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1596 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1597 | def latest_versionstring(self, ud, d): |
| 1598 | """ |
| 1599 | Compute the latest release name like "x.y.x" in "x.y.x+gitHASH" |
| 1600 | by searching through the tags output of ls-remote, comparing |
| 1601 | versions and returning the highest match as a (version, revision) pair. |
| 1602 | """ |
| 1603 | return ('', '') |
| 1604 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1605 | def done(self, ud, d): |
| 1606 | """ |
| 1607 | Is the download done ? |
| 1608 | """ |
| 1609 | if os.path.exists(ud.localpath): |
| 1610 | return True |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1611 | return False |
| 1612 | |
Andrew Geissler | 4ed12e1 | 2020-06-05 18:00:41 -0500 | [diff] [blame] | 1613 | def implicit_urldata(self, ud, d): |
| 1614 | """ |
| 1615 | Get a list of FetchData objects for any implicit URLs that will also |
| 1616 | be downloaded when we fetch the given URL. |
| 1617 | """ |
| 1618 | return [] |
| 1619 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1620 | class Fetch(object): |
| 1621 | def __init__(self, urls, d, cache = True, localonly = False, connection_cache = None): |
| 1622 | if localonly and cache: |
| 1623 | raise Exception("bb.fetch2.Fetch.__init__: cannot set cache and localonly at same time") |
| 1624 | |
| 1625 | if len(urls) == 0: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1626 | urls = d.getVar("SRC_URI").split() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1627 | self.urls = urls |
| 1628 | self.d = d |
| 1629 | self.ud = {} |
| 1630 | self.connection_cache = connection_cache |
| 1631 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1632 | fn = d.getVar('FILE') |
| 1633 | mc = d.getVar('__BBMULTICONFIG') or "" |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1634 | key = None |
| 1635 | if cache and fn: |
| 1636 | key = mc + fn + str(id(d)) |
| 1637 | if key in urldata_cache: |
| 1638 | self.ud = urldata_cache[key] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1639 | |
| 1640 | for url in urls: |
| 1641 | if url not in self.ud: |
| 1642 | try: |
| 1643 | self.ud[url] = FetchData(url, d, localonly) |
| 1644 | except NonLocalMethod: |
| 1645 | if localonly: |
| 1646 | self.ud[url] = None |
| 1647 | pass |
| 1648 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1649 | if key: |
| 1650 | urldata_cache[key] = self.ud |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1651 | |
| 1652 | def localpath(self, url): |
| 1653 | if url not in self.urls: |
| 1654 | self.ud[url] = FetchData(url, self.d) |
| 1655 | |
| 1656 | self.ud[url].setup_localpath(self.d) |
| 1657 | return self.d.expand(self.ud[url].localpath) |
| 1658 | |
| 1659 | def localpaths(self): |
| 1660 | """ |
| 1661 | Return a list of the local filenames, assuming successful fetch |
| 1662 | """ |
| 1663 | local = [] |
| 1664 | |
| 1665 | for u in self.urls: |
| 1666 | ud = self.ud[u] |
| 1667 | ud.setup_localpath(self.d) |
| 1668 | local.append(ud.localpath) |
| 1669 | |
| 1670 | return local |
| 1671 | |
| 1672 | def download(self, urls=None): |
| 1673 | """ |
| 1674 | Fetch all urls |
| 1675 | """ |
| 1676 | if not urls: |
| 1677 | urls = self.urls |
| 1678 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1679 | network = self.d.getVar("BB_NO_NETWORK") |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 1680 | premirroronly = bb.utils.to_boolean(self.d.getVar("BB_FETCH_PREMIRRORONLY")) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1681 | |
| 1682 | for u in urls: |
| 1683 | ud = self.ud[u] |
| 1684 | ud.setup_localpath(self.d) |
| 1685 | m = ud.method |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1686 | done = False |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1687 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 1688 | if ud.lockfile: |
| 1689 | lf = bb.utils.lockfile(ud.lockfile) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1690 | |
| 1691 | try: |
| 1692 | self.d.setVar("BB_NO_NETWORK", network) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1693 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1694 | if m.verify_donestamp(ud, self.d) and not m.need_update(ud, self.d): |
| 1695 | done = True |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1696 | elif m.try_premirror(ud, self.d): |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 1697 | logger.debug("Trying PREMIRRORS") |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1698 | mirrors = mirror_from_string(self.d.getVar('PREMIRRORS')) |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1699 | done = m.try_mirrors(self, ud, self.d, mirrors) |
| 1700 | if done: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1701 | try: |
| 1702 | # early checksum verification so that if the checksum of the premirror |
| 1703 | # contents mismatch the fetcher can still try upstream and mirrors |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1704 | m.update_donestamp(ud, self.d) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1705 | except ChecksumError as e: |
| 1706 | 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] | 1707 | logger.debug(str(e)) |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1708 | done = False |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1709 | |
| 1710 | if premirroronly: |
| 1711 | self.d.setVar("BB_NO_NETWORK", "1") |
| 1712 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1713 | firsterr = None |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1714 | verified_stamp = m.verify_donestamp(ud, self.d) |
| 1715 | 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] | 1716 | try: |
| 1717 | if not trusted_network(self.d, ud.url): |
| 1718 | raise UntrustedUrl(ud.url) |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 1719 | logger.debug("Trying Upstream") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1720 | m.download(ud, self.d) |
| 1721 | if hasattr(m, "build_mirror_data"): |
| 1722 | m.build_mirror_data(ud, self.d) |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1723 | done = True |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1724 | # early checksum verify, so that if checksum mismatched, |
| 1725 | # fetcher still have chance to fetch from mirror |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1726 | m.update_donestamp(ud, self.d) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1727 | |
| 1728 | except bb.fetch2.NetworkAccess: |
| 1729 | raise |
| 1730 | |
| 1731 | except BBFetchException as e: |
| 1732 | if isinstance(e, ChecksumError): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1733 | 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] | 1734 | logger.debug(str(e)) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 1735 | if os.path.exists(ud.localpath): |
| 1736 | rename_bad_checksum(ud, e.checksum) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1737 | elif isinstance(e, NoChecksumError): |
| 1738 | raise |
| 1739 | else: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1740 | logger.warning('Failed to fetch URL %s, attempting MIRRORS if available' % u) |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 1741 | logger.debug(str(e)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1742 | firsterr = e |
| 1743 | # Remove any incomplete fetch |
| 1744 | if not verified_stamp: |
| 1745 | m.clean(ud, self.d) |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 1746 | logger.debug("Trying MIRRORS") |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1747 | mirrors = mirror_from_string(self.d.getVar('MIRRORS')) |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1748 | done = m.try_mirrors(self, ud, self.d, mirrors) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1749 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1750 | if not done or not m.done(ud, self.d): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1751 | if firsterr: |
| 1752 | logger.error(str(firsterr)) |
| 1753 | raise FetchError("Unable to fetch URL from any source.", u) |
| 1754 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1755 | m.update_donestamp(ud, self.d) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1756 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1757 | except IOError as e: |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 1758 | if e.errno in [errno.ESTALE]: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1759 | logger.error("Stale Error Observed %s." % u) |
| 1760 | raise ChecksumError("Stale Error Detected") |
| 1761 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1762 | except BBFetchException as e: |
| 1763 | if isinstance(e, ChecksumError): |
| 1764 | logger.error("Checksum failure fetching %s" % u) |
| 1765 | raise |
| 1766 | |
| 1767 | finally: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 1768 | if ud.lockfile: |
| 1769 | bb.utils.unlockfile(lf) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1770 | |
| 1771 | def checkstatus(self, urls=None): |
| 1772 | """ |
| 1773 | Check all urls exist upstream |
| 1774 | """ |
| 1775 | |
| 1776 | if not urls: |
| 1777 | urls = self.urls |
| 1778 | |
| 1779 | for u in urls: |
| 1780 | ud = self.ud[u] |
| 1781 | ud.setup_localpath(self.d) |
| 1782 | m = ud.method |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 1783 | logger.debug("Testing URL %s", u) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1784 | # First try checking uri, u, from PREMIRRORS |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1785 | mirrors = mirror_from_string(self.d.getVar('PREMIRRORS')) |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1786 | ret = m.try_mirrors(self, ud, self.d, mirrors, True) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1787 | if not ret: |
| 1788 | # Next try checking from the original uri, u |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1789 | ret = m.checkstatus(self, ud, self.d) |
| 1790 | if not ret: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1791 | # Finally, try checking uri, u, from 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 | ret = m.try_mirrors(self, ud, self.d, mirrors, True) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1794 | |
| 1795 | if not ret: |
| 1796 | raise FetchError("URL %s doesn't work" % u, u) |
| 1797 | |
| 1798 | def unpack(self, root, urls=None): |
| 1799 | """ |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1800 | Unpack urls to root |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1801 | """ |
| 1802 | |
| 1803 | if not urls: |
| 1804 | urls = self.urls |
| 1805 | |
| 1806 | for u in urls: |
| 1807 | ud = self.ud[u] |
| 1808 | ud.setup_localpath(self.d) |
| 1809 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1810 | if ud.lockfile: |
| 1811 | lf = bb.utils.lockfile(ud.lockfile) |
| 1812 | |
| 1813 | ud.method.unpack(ud, root, self.d) |
| 1814 | |
| 1815 | if ud.lockfile: |
| 1816 | bb.utils.unlockfile(lf) |
| 1817 | |
| 1818 | def clean(self, urls=None): |
| 1819 | """ |
| 1820 | Clean files that the fetcher gets or places |
| 1821 | """ |
| 1822 | |
| 1823 | if not urls: |
| 1824 | urls = self.urls |
| 1825 | |
| 1826 | for url in urls: |
| 1827 | if url not in self.ud: |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 1828 | self.ud[url] = FetchData(url, self.d) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1829 | ud = self.ud[url] |
| 1830 | ud.setup_localpath(self.d) |
| 1831 | |
| 1832 | if not ud.localfile and ud.localpath is None: |
| 1833 | continue |
| 1834 | |
| 1835 | if ud.lockfile: |
| 1836 | lf = bb.utils.lockfile(ud.lockfile) |
| 1837 | |
| 1838 | ud.method.clean(ud, self.d) |
| 1839 | if ud.donestamp: |
| 1840 | bb.utils.remove(ud.donestamp) |
| 1841 | |
| 1842 | if ud.lockfile: |
| 1843 | bb.utils.unlockfile(lf) |
| 1844 | |
Andrew Geissler | 4ed12e1 | 2020-06-05 18:00:41 -0500 | [diff] [blame] | 1845 | def expanded_urldata(self, urls=None): |
| 1846 | """ |
| 1847 | Get an expanded list of FetchData objects covering both the given |
| 1848 | URLS and any additional implicit URLs that are added automatically by |
| 1849 | the appropriate FetchMethod. |
| 1850 | """ |
| 1851 | |
| 1852 | if not urls: |
| 1853 | urls = self.urls |
| 1854 | |
| 1855 | urldata = [] |
| 1856 | for url in urls: |
| 1857 | ud = self.ud[url] |
| 1858 | urldata.append(ud) |
| 1859 | urldata += ud.method.implicit_urldata(ud, self.d) |
| 1860 | |
| 1861 | return urldata |
| 1862 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1863 | class FetchConnectionCache(object): |
| 1864 | """ |
| 1865 | A class which represents an container for socket connections. |
| 1866 | """ |
| 1867 | def __init__(self): |
| 1868 | self.cache = {} |
| 1869 | |
| 1870 | def get_connection_name(self, host, port): |
| 1871 | return host + ':' + str(port) |
| 1872 | |
| 1873 | def add_connection(self, host, port, connection): |
| 1874 | cn = self.get_connection_name(host, port) |
| 1875 | |
| 1876 | if cn not in self.cache: |
| 1877 | self.cache[cn] = connection |
| 1878 | |
| 1879 | def get_connection(self, host, port): |
| 1880 | connection = None |
| 1881 | |
| 1882 | cn = self.get_connection_name(host, port) |
| 1883 | if cn in self.cache: |
| 1884 | connection = self.cache[cn] |
| 1885 | |
| 1886 | return connection |
| 1887 | |
| 1888 | def remove_connection(self, host, port): |
| 1889 | cn = self.get_connection_name(host, port) |
| 1890 | if cn in self.cache: |
| 1891 | self.cache[cn].close() |
| 1892 | del self.cache[cn] |
| 1893 | |
| 1894 | def close_connections(self): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1895 | for cn in list(self.cache.keys()): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1896 | self.cache[cn].close() |
| 1897 | del self.cache[cn] |
| 1898 | |
| 1899 | from . import cvs |
| 1900 | from . import git |
| 1901 | from . import gitsm |
| 1902 | from . import gitannex |
| 1903 | from . import local |
| 1904 | from . import svn |
| 1905 | from . import wget |
| 1906 | from . import ssh |
| 1907 | from . import sftp |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1908 | from . import s3 |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1909 | from . import perforce |
| 1910 | from . import bzr |
| 1911 | from . import hg |
| 1912 | from . import osc |
| 1913 | from . import repo |
| 1914 | from . import clearcase |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 1915 | from . import npm |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1916 | from . import npmsw |
Andrew Geissler | 95ac1b8 | 2021-03-31 14:34:31 -0500 | [diff] [blame] | 1917 | from . import az |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1918 | |
| 1919 | methods.append(local.Local()) |
| 1920 | methods.append(wget.Wget()) |
| 1921 | methods.append(svn.Svn()) |
| 1922 | methods.append(git.Git()) |
| 1923 | methods.append(gitsm.GitSM()) |
| 1924 | methods.append(gitannex.GitANNEX()) |
| 1925 | methods.append(cvs.Cvs()) |
| 1926 | methods.append(ssh.SSH()) |
| 1927 | methods.append(sftp.SFTP()) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1928 | methods.append(s3.S3()) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1929 | methods.append(perforce.Perforce()) |
| 1930 | methods.append(bzr.Bzr()) |
| 1931 | methods.append(hg.Hg()) |
| 1932 | methods.append(osc.Osc()) |
| 1933 | methods.append(repo.Repo()) |
| 1934 | methods.append(clearcase.ClearCase()) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 1935 | methods.append(npm.Npm()) |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1936 | methods.append(npmsw.NpmShrinkWrap()) |
Andrew Geissler | 95ac1b8 | 2021-03-31 14:34:31 -0500 | [diff] [blame] | 1937 | methods.append(az.Az()) |