blob: 0126e0d7c790e7d10e27fcc5bcf1d04c8b65c666 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001"""
2BitBake 'Fetch' implementations
3
4Classes for obtaining upstream sources for the
5BitBake build tools.
6"""
7
8# Copyright (C) 2003, 2004 Chris Larson
9# Copyright (C) 2012 Intel Corporation
10#
Brad Bishopc342db32019-05-15 21:57:59 -040011# SPDX-License-Identifier: GPL-2.0-only
Patrick Williamsc124f4f2015-09-15 14:41:29 -050012#
13# Based on functions from the base bb module, Copyright 2003 Holger Schurig
14
Patrick Williamsc124f4f2015-09-15 14:41:29 -050015import os, re
16import signal
Patrick Williamsc124f4f2015-09-15 14:41:29 -050017import logging
Patrick Williamsc0f7c042017-02-23 20:41:17 -060018import urllib.request, urllib.parse, urllib.error
19if 'git' not in urllib.parse.uses_netloc:
20 urllib.parse.uses_netloc.append('git')
21import operator
22import collections
23import subprocess
24import pickle
Brad Bishop6e60e8b2018-02-01 10:27:11 -050025import errno
Patrick Williamsc124f4f2015-09-15 14:41:29 -050026import bb.persist_data, bb.utils
27import bb.checksum
Patrick Williamsc124f4f2015-09-15 14:41:29 -050028import bb.process
Brad Bishopd7bf8c12018-02-25 22:55:05 -050029import bb.event
Patrick Williamsc124f4f2015-09-15 14:41:29 -050030
31__version__ = "2"
32_checksum_cache = bb.checksum.FileChecksumCache()
33
34logger = logging.getLogger("BitBake.Fetcher")
35
Patrick Williamsc124f4f2015-09-15 14:41:29 -050036class BBFetchException(Exception):
37 """Class all fetch exceptions inherit from"""
38 def __init__(self, message):
Brad Bishopd7bf8c12018-02-25 22:55:05 -050039 self.msg = message
40 Exception.__init__(self, message)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050041
42 def __str__(self):
Brad Bishopd7bf8c12018-02-25 22:55:05 -050043 return self.msg
Patrick Williamsc124f4f2015-09-15 14:41:29 -050044
45class UntrustedUrl(BBFetchException):
46 """Exception raised when encountering a host not listed in BB_ALLOWED_NETWORKS"""
47 def __init__(self, url, message=''):
48 if message:
49 msg = message
50 else:
51 msg = "The URL: '%s' is not trusted and cannot be used" % url
52 self.url = url
53 BBFetchException.__init__(self, msg)
54 self.args = (url,)
55
56class MalformedUrl(BBFetchException):
57 """Exception raised when encountering an invalid url"""
58 def __init__(self, url, message=''):
Brad Bishopd7bf8c12018-02-25 22:55:05 -050059 if message:
60 msg = message
61 else:
62 msg = "The URL: '%s' is invalid and cannot be interpreted" % url
63 self.url = url
64 BBFetchException.__init__(self, msg)
65 self.args = (url,)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050066
67class FetchError(BBFetchException):
68 """General fetcher exception when something happens incorrectly"""
69 def __init__(self, message, url = None):
Brad Bishopd7bf8c12018-02-25 22:55:05 -050070 if url:
Patrick Williamsc124f4f2015-09-15 14:41:29 -050071 msg = "Fetcher failure for URL: '%s'. %s" % (url, message)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050072 else:
Patrick Williamsc124f4f2015-09-15 14:41:29 -050073 msg = "Fetcher failure: %s" % message
Brad Bishopd7bf8c12018-02-25 22:55:05 -050074 self.url = url
75 BBFetchException.__init__(self, msg)
76 self.args = (message, url)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050077
78class ChecksumError(FetchError):
79 """Exception when mismatched checksum encountered"""
80 def __init__(self, message, url = None, checksum = None):
81 self.checksum = checksum
82 FetchError.__init__(self, message, url)
83
84class NoChecksumError(FetchError):
85 """Exception when no checksum is specified, but BB_STRICT_CHECKSUM is set"""
86
87class UnpackError(BBFetchException):
88 """General fetcher exception when something happens incorrectly when unpacking"""
89 def __init__(self, message, url):
Brad Bishopd7bf8c12018-02-25 22:55:05 -050090 msg = "Unpack failure for URL: '%s'. %s" % (url, message)
91 self.url = url
92 BBFetchException.__init__(self, msg)
93 self.args = (message, url)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050094
95class NoMethodError(BBFetchException):
96 """Exception raised when there is no method to obtain a supplied url or set of urls"""
97 def __init__(self, url):
Brad Bishopd7bf8c12018-02-25 22:55:05 -050098 msg = "Could not find a fetcher which supports the URL: '%s'" % url
99 self.url = url
100 BBFetchException.__init__(self, msg)
101 self.args = (url,)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500102
103class MissingParameterError(BBFetchException):
104 """Exception raised when a fetch method is missing a critical parameter in the url"""
105 def __init__(self, missing, url):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500106 msg = "URL: '%s' is missing the required parameter '%s'" % (url, missing)
107 self.url = url
108 self.missing = missing
109 BBFetchException.__init__(self, msg)
110 self.args = (missing, url)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500111
112class ParameterError(BBFetchException):
113 """Exception raised when a url cannot be proccessed due to invalid parameters."""
114 def __init__(self, message, url):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500115 msg = "URL: '%s' has invalid parameters. %s" % (url, message)
116 self.url = url
117 BBFetchException.__init__(self, msg)
118 self.args = (message, url)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500119
120class NetworkAccess(BBFetchException):
121 """Exception raised when network access is disabled but it is required."""
122 def __init__(self, url, cmd):
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500123 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)
124 self.url = url
125 self.cmd = cmd
126 BBFetchException.__init__(self, msg)
127 self.args = (url, cmd)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500128
129class NonLocalMethod(Exception):
130 def __init__(self):
131 Exception.__init__(self)
132
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500133class MissingChecksumEvent(bb.event.Event):
134 def __init__(self, url, md5sum, sha256sum):
135 self.url = url
136 self.checksums = {'md5sum': md5sum,
137 'sha256sum': sha256sum}
138 bb.event.Event.__init__(self)
139
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500140
141class URI(object):
142 """
143 A class representing a generic URI, with methods for
144 accessing the URI components, and stringifies to the
145 URI.
146
147 It is constructed by calling it with a URI, or setting
148 the attributes manually:
149
150 uri = URI("http://example.com/")
151
152 uri = URI()
153 uri.scheme = 'http'
154 uri.hostname = 'example.com'
155 uri.path = '/'
156
157 It has the following attributes:
158
159 * scheme (read/write)
160 * userinfo (authentication information) (read/write)
161 * username (read/write)
162 * password (read/write)
163
164 Note, password is deprecated as of RFC 3986.
165
166 * hostname (read/write)
167 * port (read/write)
168 * hostport (read only)
169 "hostname:port", if both are set, otherwise just "hostname"
170 * path (read/write)
171 * path_quoted (read/write)
172 A URI quoted version of path
173 * params (dict) (read/write)
174 * query (dict) (read/write)
175 * relative (bool) (read only)
176 True if this is a "relative URI", (e.g. file:foo.diff)
177
178 It stringifies to the URI itself.
179
180 Some notes about relative URIs: while it's specified that
181 a URI beginning with <scheme>:// should either be directly
182 followed by a hostname or a /, the old URI handling of the
183 fetch2 library did not comform to this. Therefore, this URI
184 class has some kludges to make sure that URIs are parsed in
185 a way comforming to bitbake's current usage. This URI class
186 supports the following:
187
188 file:relative/path.diff (IETF compliant)
189 git:relative/path.git (IETF compliant)
190 git:///absolute/path.git (IETF compliant)
191 file:///absolute/path.diff (IETF compliant)
192
193 file://relative/path.diff (not IETF compliant)
194
195 But it does not support the following:
196
197 file://hostname/absolute/path.diff (would be IETF compliant)
198
199 Note that the last case only applies to a list of
200 "whitelisted" schemes (currently only file://), that requires
201 its URIs to not have a network location.
202 """
203
204 _relative_schemes = ['file', 'git']
205 _netloc_forbidden = ['file']
206
207 def __init__(self, uri=None):
208 self.scheme = ''
209 self.userinfo = ''
210 self.hostname = ''
211 self.port = None
212 self._path = ''
213 self.params = {}
214 self.query = {}
215 self.relative = False
216
217 if not uri:
218 return
219
220 # We hijack the URL parameters, since the way bitbake uses
221 # them are not quite RFC compliant.
222 uri, param_str = (uri.split(";", 1) + [None])[:2]
223
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600224 urlp = urllib.parse.urlparse(uri)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500225 self.scheme = urlp.scheme
226
227 reparse = 0
228
229 # Coerce urlparse to make URI scheme use netloc
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600230 if not self.scheme in urllib.parse.uses_netloc:
231 urllib.parse.uses_params.append(self.scheme)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500232 reparse = 1
233
234 # Make urlparse happy(/ier) by converting local resources
235 # to RFC compliant URL format. E.g.:
236 # file://foo.diff -> file:foo.diff
237 if urlp.scheme in self._netloc_forbidden:
238 uri = re.sub("(?<=:)//(?!/)", "", uri, 1)
239 reparse = 1
240
241 if reparse:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600242 urlp = urllib.parse.urlparse(uri)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500243
244 # Identify if the URI is relative or not
245 if urlp.scheme in self._relative_schemes and \
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800246 re.compile(r"^\w+:(?!//)").match(uri):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500247 self.relative = True
248
249 if not self.relative:
250 self.hostname = urlp.hostname or ''
251 self.port = urlp.port
252
253 self.userinfo += urlp.username or ''
254
255 if urlp.password:
256 self.userinfo += ':%s' % urlp.password
257
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600258 self.path = urllib.parse.unquote(urlp.path)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500259
260 if param_str:
261 self.params = self._param_str_split(param_str, ";")
262 if urlp.query:
263 self.query = self._param_str_split(urlp.query, "&")
264
265 def __str__(self):
266 userinfo = self.userinfo
267 if userinfo:
268 userinfo += '@'
269
270 return "%s:%s%s%s%s%s%s" % (
271 self.scheme,
272 '' if self.relative else '//',
273 userinfo,
274 self.hostport,
275 self.path_quoted,
276 self._query_str(),
277 self._param_str())
278
279 def _param_str(self):
280 return (
281 ''.join([';', self._param_str_join(self.params, ";")])
282 if self.params else '')
283
284 def _query_str(self):
285 return (
286 ''.join(['?', self._param_str_join(self.query, "&")])
287 if self.query else '')
288
289 def _param_str_split(self, string, elmdelim, kvdelim="="):
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600290 ret = collections.OrderedDict()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500291 for k, v in [x.split(kvdelim, 1) for x in string.split(elmdelim)]:
292 ret[k] = v
293 return ret
294
295 def _param_str_join(self, dict_, elmdelim, kvdelim="="):
296 return elmdelim.join([kvdelim.join([k, v]) for k, v in dict_.items()])
297
298 @property
299 def hostport(self):
300 if not self.port:
301 return self.hostname
302 return "%s:%d" % (self.hostname, self.port)
303
304 @property
305 def path_quoted(self):
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600306 return urllib.parse.quote(self.path)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500307
308 @path_quoted.setter
309 def path_quoted(self, path):
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600310 self.path = urllib.parse.unquote(path)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500311
312 @property
313 def path(self):
314 return self._path
315
316 @path.setter
317 def path(self, path):
318 self._path = path
319
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500320 if not path or re.compile("^/").match(path):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500321 self.relative = False
322 else:
323 self.relative = True
324
325 @property
326 def username(self):
327 if self.userinfo:
328 return (self.userinfo.split(":", 1))[0]
329 return ''
330
331 @username.setter
332 def username(self, username):
333 password = self.password
334 self.userinfo = username
335 if password:
336 self.userinfo += ":%s" % password
337
338 @property
339 def password(self):
340 if self.userinfo and ":" in self.userinfo:
341 return (self.userinfo.split(":", 1))[1]
342 return ''
343
344 @password.setter
345 def password(self, password):
346 self.userinfo = "%s:%s" % (self.username, password)
347
348def decodeurl(url):
349 """Decodes an URL into the tokens (scheme, network location, path,
350 user, password, parameters).
351 """
352
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500353 m = re.compile('(?P<type>[^:]*)://((?P<user>[^/;]+)@)?(?P<location>[^;]+)(;(?P<parm>.*))?').match(url)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500354 if not m:
355 raise MalformedUrl(url)
356
357 type = m.group('type')
358 location = m.group('location')
359 if not location:
360 raise MalformedUrl(url)
361 user = m.group('user')
362 parm = m.group('parm')
363
364 locidx = location.find('/')
365 if locidx != -1 and type.lower() != 'file':
366 host = location[:locidx]
367 path = location[locidx:]
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500368 elif type.lower() == 'file':
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500369 host = ""
370 path = location
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500371 else:
372 host = location
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800373 path = "/"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500374 if user:
375 m = re.compile('(?P<user>[^:]+)(:?(?P<pswd>.*))').match(user)
376 if m:
377 user = m.group('user')
378 pswd = m.group('pswd')
379 else:
380 user = ''
381 pswd = ''
382
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600383 p = collections.OrderedDict()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500384 if parm:
385 for s in parm.split(';'):
386 if s:
387 if not '=' in s:
388 raise MalformedUrl(url, "The URL: '%s' is invalid: parameter %s does not specify a value (missing '=')" % (url, s))
389 s1, s2 = s.split('=')
390 p[s1] = s2
391
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600392 return type, host, urllib.parse.unquote(path), user, pswd, p
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500393
394def encodeurl(decoded):
395 """Encodes a URL from tokens (scheme, network location, path,
396 user, password, parameters).
397 """
398
399 type, host, path, user, pswd, p = decoded
400
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500401 if not type:
402 raise MissingParameterError('type', "encoded from the data %s" % str(decoded))
403 url = '%s://' % type
404 if user and type != "file":
405 url += "%s" % user
406 if pswd:
407 url += ":%s" % pswd
408 url += "@"
409 if host and type != "file":
410 url += "%s" % host
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500411 if path:
412 # Standardise path to ensure comparisons work
413 while '//' in path:
414 path = path.replace("//", "/")
415 url += "%s" % urllib.parse.quote(path)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500416 if p:
417 for parm in p:
418 url += ";%s=%s" % (parm, p[parm])
419
420 return url
421
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500422def uri_replace(ud, uri_find, uri_replace, replacements, d, mirrortarball=None):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500423 if not ud.url or not uri_find or not uri_replace:
424 logger.error("uri_replace: passed an undefined value, not replacing")
425 return None
426 uri_decoded = list(decodeurl(ud.url))
427 uri_find_decoded = list(decodeurl(uri_find))
428 uri_replace_decoded = list(decodeurl(uri_replace))
429 logger.debug(2, "For url %s comparing %s to %s" % (uri_decoded, uri_find_decoded, uri_replace_decoded))
430 result_decoded = ['', '', '', '', '', {}]
431 for loc, i in enumerate(uri_find_decoded):
432 result_decoded[loc] = uri_decoded[loc]
433 regexp = i
434 if loc == 0 and regexp and not regexp.endswith("$"):
435 # Leaving the type unanchored can mean "https" matching "file" can become "files"
436 # which is clearly undesirable.
437 regexp += "$"
438 if loc == 5:
439 # Handle URL parameters
440 if i:
441 # Any specified URL parameters must match
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800442 for k in uri_find_decoded[loc]:
443 if uri_decoded[loc][k] != uri_find_decoded[loc][k]:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500444 return None
445 # Overwrite any specified replacement parameters
446 for k in uri_replace_decoded[loc]:
447 for l in replacements:
448 uri_replace_decoded[loc][k] = uri_replace_decoded[loc][k].replace(l, replacements[l])
449 result_decoded[loc][k] = uri_replace_decoded[loc][k]
450 elif (re.match(regexp, uri_decoded[loc])):
451 if not uri_replace_decoded[loc]:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500452 result_decoded[loc] = ""
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500453 else:
454 for k in replacements:
455 uri_replace_decoded[loc] = uri_replace_decoded[loc].replace(k, replacements[k])
456 #bb.note("%s %s %s" % (regexp, uri_replace_decoded[loc], uri_decoded[loc]))
Patrick Williamsd7e96312015-09-22 08:09:05 -0500457 result_decoded[loc] = re.sub(regexp, uri_replace_decoded[loc], uri_decoded[loc], 1)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500458 if loc == 2:
459 # Handle path manipulations
460 basename = None
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500461 if uri_decoded[0] != uri_replace_decoded[0] and mirrortarball:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500462 # If the source and destination url types differ, must be a mirrortarball mapping
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500463 basename = os.path.basename(mirrortarball)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500464 # Kill parameters, they make no sense for mirror tarballs
465 uri_decoded[5] = {}
466 elif ud.localpath and ud.method.supports_checksum(ud):
467 basename = os.path.basename(ud.localpath)
468 if basename and not result_decoded[loc].endswith(basename):
469 result_decoded[loc] = os.path.join(result_decoded[loc], basename)
470 else:
471 return None
472 result = encodeurl(result_decoded)
473 if result == ud.url:
474 return None
475 logger.debug(2, "For url %s returning %s" % (ud.url, result))
476 return result
477
478methods = []
479urldata_cache = {}
480saved_headrevs = {}
481
482def fetcher_init(d):
483 """
484 Called to initialize the fetchers once the configuration data is known.
485 Calls before this must not hit the cache.
486 """
487 # When to drop SCM head revisions controlled by user policy
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500488 srcrev_policy = d.getVar('BB_SRCREV_POLICY') or "clear"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500489 if srcrev_policy == "cache":
490 logger.debug(1, "Keeping SRCREV cache due to cache policy of: %s", srcrev_policy)
491 elif srcrev_policy == "clear":
492 logger.debug(1, "Clearing SRCREV cache due to cache policy of: %s", srcrev_policy)
493 revs = bb.persist_data.persist('BB_URI_HEADREVS', d)
494 try:
495 bb.fetch2.saved_headrevs = revs.items()
496 except:
497 pass
498 revs.clear()
499 else:
500 raise FetchError("Invalid SRCREV cache policy of: %s" % srcrev_policy)
501
502 _checksum_cache.init_cache(d)
503
504 for m in methods:
505 if hasattr(m, "init"):
506 m.init(d)
507
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500508def fetcher_parse_save():
509 _checksum_cache.save_extras()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500510
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500511def fetcher_parse_done():
512 _checksum_cache.save_merge()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500513
Brad Bishop19323692019-04-05 15:28:33 -0400514def fetcher_compare_revisions(d):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500515 """
516 Compare the revisions in the persistant cache with current values and
517 return true/false on whether they've changed.
518 """
519
520 data = bb.persist_data.persist('BB_URI_HEADREVS', d).items()
521 data2 = bb.fetch2.saved_headrevs
522
523 changed = False
524 for key in data:
525 if key not in data2 or data2[key] != data[key]:
526 logger.debug(1, "%s changed", key)
527 changed = True
528 return True
529 else:
530 logger.debug(2, "%s did not change", key)
531 return False
532
533def mirror_from_string(data):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500534 mirrors = (data or "").replace('\\n',' ').split()
535 # Split into pairs
536 if len(mirrors) % 2 != 0:
537 bb.warn('Invalid mirror data %s, should have paired members.' % data)
538 return list(zip(*[iter(mirrors)]*2))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500539
540def verify_checksum(ud, d, precomputed={}):
541 """
542 verify the MD5 and SHA256 checksum for downloaded src
543
544 Raises a FetchError if one or both of the SRC_URI checksums do not match
545 the downloaded file, or if BB_STRICT_CHECKSUM is set and there are no
546 checksums specified.
547
548 Returns a dict of checksums that can be stored in a done stamp file and
549 passed in as precomputed parameter in a later call to avoid re-computing
550 the checksums from the file. This allows verifying the checksums of the
551 file against those in the recipe each time, rather than only after
552 downloading. See https://bugzilla.yoctoproject.org/show_bug.cgi?id=5571.
553 """
554
555 _MD5_KEY = "md5"
556 _SHA256_KEY = "sha256"
557
558 if ud.ignore_checksums or not ud.method.supports_checksum(ud):
559 return {}
560
561 if _MD5_KEY in precomputed:
562 md5data = precomputed[_MD5_KEY]
563 else:
564 md5data = bb.utils.md5_file(ud.localpath)
565
566 if _SHA256_KEY in precomputed:
567 sha256data = precomputed[_SHA256_KEY]
568 else:
569 sha256data = bb.utils.sha256_file(ud.localpath)
570
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500571 if ud.method.recommends_checksum(ud) and not ud.md5_expected and not ud.sha256_expected:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500572 # If strict checking enabled and neither sum defined, raise error
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500573 strict = d.getVar("BB_STRICT_CHECKSUM") or "0"
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500574 if strict == "1":
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500575 logger.error('No checksum specified for %s, please add at least one to the recipe:\n'
576 'SRC_URI[%s] = "%s"\nSRC_URI[%s] = "%s"' %
577 (ud.localpath, ud.md5_name, md5data,
578 ud.sha256_name, sha256data))
579 raise NoChecksumError('Missing SRC_URI checksum', ud.url)
580
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500581 bb.event.fire(MissingChecksumEvent(ud.url, md5data, sha256data), d)
582
583 if strict == "ignore":
584 return {
585 _MD5_KEY: md5data,
586 _SHA256_KEY: sha256data
587 }
588
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500589 # Log missing sums so user can more easily add them
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600590 logger.warning('Missing md5 SRC_URI checksum for %s, consider adding to the recipe:\n'
591 'SRC_URI[%s] = "%s"',
592 ud.localpath, ud.md5_name, md5data)
593 logger.warning('Missing sha256 SRC_URI checksum for %s, consider adding to the recipe:\n'
594 'SRC_URI[%s] = "%s"',
595 ud.localpath, ud.sha256_name, sha256data)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500596
597 # We want to alert the user if a checksum is defined in the recipe but
598 # it does not match.
599 msg = ""
600 mismatch = False
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500601 if ud.md5_expected and ud.md5_expected != md5data:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500602 msg = msg + "\nFile: '%s' has %s checksum %s when %s was expected" % (ud.localpath, 'md5', md5data, ud.md5_expected)
603 mismatch = True;
604
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500605 if ud.sha256_expected and ud.sha256_expected != sha256data:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500606 msg = msg + "\nFile: '%s' has %s checksum %s when %s was expected" % (ud.localpath, 'sha256', sha256data, ud.sha256_expected)
607 mismatch = True;
608
609 if mismatch:
610 msg = msg + '\nIf this change is expected (e.g. you have upgraded to a new version without updating the checksums) then you can use these lines within the recipe:\nSRC_URI[%s] = "%s"\nSRC_URI[%s] = "%s"\nOtherwise you should retry the download and/or check with upstream to determine if the file has become corrupted or otherwise unexpectedly modified.\n' % (ud.md5_name, md5data, ud.sha256_name, sha256data)
611
612 if len(msg):
613 raise ChecksumError('Checksum mismatch!%s' % msg, ud.url, md5data)
614
615 return {
616 _MD5_KEY: md5data,
617 _SHA256_KEY: sha256data
618 }
619
620
621def verify_donestamp(ud, d, origud=None):
622 """
623 Check whether the done stamp file has the right checksums (if the fetch
624 method supports them). If it doesn't, delete the done stamp and force
625 a re-download.
626
627 Returns True, if the donestamp exists and is valid, False otherwise. When
628 returning False, any existing done stamps are removed.
629 """
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500630 if not ud.needdonestamp or (origud and not origud.needdonestamp):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500631 return True
632
Brad Bishop316dfdd2018-06-25 12:45:53 -0400633 if not os.path.exists(ud.localpath):
634 # local path does not exist
635 if os.path.exists(ud.donestamp):
636 # done stamp exists, but the downloaded file does not; the done stamp
637 # must be incorrect, re-trigger the download
638 bb.utils.remove(ud.donestamp)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500639 return False
640
641 if (not ud.method.supports_checksum(ud) or
642 (origud and not origud.method.supports_checksum(origud))):
Brad Bishop316dfdd2018-06-25 12:45:53 -0400643 # if done stamp exists and checksums not supported; assume the local
644 # file is current
645 return os.path.exists(ud.donestamp)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500646
647 precomputed_checksums = {}
648 # Only re-use the precomputed checksums if the donestamp is newer than the
649 # file. Do not rely on the mtime of directories, though. If ud.localpath is
650 # a directory, there will probably not be any checksums anyway.
Brad Bishop316dfdd2018-06-25 12:45:53 -0400651 if os.path.exists(ud.donestamp) and (os.path.isdir(ud.localpath) or
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500652 os.path.getmtime(ud.localpath) < os.path.getmtime(ud.donestamp)):
653 try:
654 with open(ud.donestamp, "rb") as cachefile:
655 pickled = pickle.Unpickler(cachefile)
656 precomputed_checksums.update(pickled.load())
657 except Exception as e:
658 # Avoid the warnings on the upgrade path from emtpy done stamp
659 # files to those containing the checksums.
660 if not isinstance(e, EOFError):
661 # Ignore errors, they aren't fatal
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600662 logger.warning("Couldn't load checksums from donestamp %s: %s "
663 "(msg: %s)" % (ud.donestamp, type(e).__name__,
664 str(e)))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500665
666 try:
667 checksums = verify_checksum(ud, d, precomputed_checksums)
668 # If the cache file did not have the checksums, compute and store them
669 # as an upgrade path from the previous done stamp file format.
670 if checksums != precomputed_checksums:
671 with open(ud.donestamp, "wb") as cachefile:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600672 p = pickle.Pickler(cachefile, 2)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500673 p.dump(checksums)
674 return True
675 except ChecksumError as e:
676 # Checksums failed to verify, trigger re-download and remove the
677 # incorrect stamp file.
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600678 logger.warning("Checksum mismatch for local file %s\n"
679 "Cleaning and trying again." % ud.localpath)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500680 if os.path.exists(ud.localpath):
681 rename_bad_checksum(ud, e.checksum)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500682 bb.utils.remove(ud.donestamp)
683 return False
684
685
686def update_stamp(ud, d):
687 """
688 donestamp is file stamp indicating the whole fetching is done
689 this function update the stamp after verifying the checksum
690 """
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500691 if not ud.needdonestamp:
692 return
693
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500694 if os.path.exists(ud.donestamp):
695 # Touch the done stamp file to show active use of the download
696 try:
697 os.utime(ud.donestamp, None)
698 except:
699 # Errors aren't fatal here
700 pass
701 else:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500702 try:
703 checksums = verify_checksum(ud, d)
704 # Store the checksums for later re-verification against the recipe
705 with open(ud.donestamp, "wb") as cachefile:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600706 p = pickle.Pickler(cachefile, 2)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500707 p.dump(checksums)
708 except ChecksumError as e:
709 # Checksums failed to verify, trigger re-download and remove the
710 # incorrect stamp file.
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600711 logger.warning("Checksum mismatch for local file %s\n"
712 "Cleaning and trying again." % ud.localpath)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500713 if os.path.exists(ud.localpath):
714 rename_bad_checksum(ud, e.checksum)
715 bb.utils.remove(ud.donestamp)
716 raise
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500717
718def subprocess_setup():
719 # Python installs a SIGPIPE handler by default. This is usually not what
720 # non-Python subprocesses expect.
721 # SIGPIPE errors are known issues with gzip/bash
722 signal.signal(signal.SIGPIPE, signal.SIG_DFL)
723
724def get_autorev(d):
725 # only not cache src rev in autorev case
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500726 if d.getVar('BB_SRCREV_POLICY') != "cache":
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500727 d.setVar('BB_DONT_CACHE', '1')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500728 return "AUTOINC"
729
730def get_srcrev(d, method_name='sortable_revision'):
731 """
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500732 Return the revision string, usually for use in the version string (PV) of the current package
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500733 Most packages usually only have one SCM so we just pass on the call.
734 In the multi SCM case, we build a value based on SRCREV_FORMAT which must
735 have been set.
736
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500737 The idea here is that we put the string "AUTOINC+" into return value if the revisions are not
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500738 incremental, other code is then responsible for turning that into an increasing value (if needed)
739
740 A method_name can be supplied to retrieve an alternatively formatted revision from a fetcher, if
741 that fetcher provides a method with the given name and the same signature as sortable_revision.
742 """
743
744 scms = []
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500745 fetcher = Fetch(d.getVar('SRC_URI').split(), d)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500746 urldata = fetcher.ud
747 for u in urldata:
748 if urldata[u].method.supports_srcrev():
749 scms.append(u)
750
751 if len(scms) == 0:
752 raise FetchError("SRCREV was used yet no valid SCM was found in SRC_URI")
753
754 if len(scms) == 1 and len(urldata[scms[0]].names) == 1:
755 autoinc, rev = getattr(urldata[scms[0]].method, method_name)(urldata[scms[0]], d, urldata[scms[0]].names[0])
756 if len(rev) > 10:
757 rev = rev[:10]
758 if autoinc:
759 return "AUTOINC+" + rev
760 return rev
761
762 #
763 # Mutiple SCMs are in SRC_URI so we resort to SRCREV_FORMAT
764 #
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500765 format = d.getVar('SRCREV_FORMAT')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500766 if not format:
Brad Bishop19323692019-04-05 15:28:33 -0400767 raise FetchError("The SRCREV_FORMAT variable must be set when multiple SCMs are used.\n"\
768 "The SCMs are:\n%s" % '\n'.join(scms))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500769
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600770 name_to_rev = {}
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500771 seenautoinc = False
772 for scm in scms:
773 ud = urldata[scm]
774 for name in ud.names:
775 autoinc, rev = getattr(ud.method, method_name)(ud, d, name)
776 seenautoinc = seenautoinc or autoinc
777 if len(rev) > 10:
778 rev = rev[:10]
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600779 name_to_rev[name] = rev
780 # Replace names by revisions in the SRCREV_FORMAT string. The approach used
781 # here can handle names being prefixes of other names and names appearing
782 # as substrings in revisions (in which case the name should not be
783 # expanded). The '|' regular expression operator tries matches from left to
784 # right, so we need to sort the names with the longest ones first.
785 names_descending_len = sorted(name_to_rev, key=len, reverse=True)
786 name_to_rev_re = "|".join(re.escape(name) for name in names_descending_len)
787 format = re.sub(name_to_rev_re, lambda match: name_to_rev[match.group(0)], format)
788
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500789 if seenautoinc:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500790 format = "AUTOINC+" + format
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500791
792 return format
793
794def localpath(url, d):
795 fetcher = bb.fetch2.Fetch([url], d)
796 return fetcher.localpath(url)
797
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600798def runfetchcmd(cmd, d, quiet=False, cleanup=None, log=None, workdir=None):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500799 """
800 Run cmd returning the command output
801 Raise an error if interrupted or cmd fails
802 Optionally echo command output to stdout
803 Optionally remove the files/directories listed in cleanup upon failure
804 """
805
806 # Need to export PATH as binary could be in metadata paths
807 # rather than host provided
808 # Also include some other variables.
809 # FIXME: Should really include all export varaiables?
810 exportvars = ['HOME', 'PATH',
811 'HTTP_PROXY', 'http_proxy',
812 'HTTPS_PROXY', 'https_proxy',
813 'FTP_PROXY', 'ftp_proxy',
814 'FTPS_PROXY', 'ftps_proxy',
815 'NO_PROXY', 'no_proxy',
816 'ALL_PROXY', 'all_proxy',
817 'GIT_PROXY_COMMAND',
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800818 'GIT_SSH',
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500819 'GIT_SSL_CAINFO',
820 'GIT_SMART_HTTP',
821 'SSH_AUTH_SOCK', 'SSH_AGENT_PID',
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600822 'SOCKS5_USER', 'SOCKS5_PASSWD',
823 'DBUS_SESSION_BUS_ADDRESS',
824 'P4CONFIG']
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500825
826 if not cleanup:
827 cleanup = []
828
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800829 # If PATH contains WORKDIR which contains PV-PR which contains SRCPV we
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500830 # can end up in circular recursion here so give the option of breaking it
831 # in a data store copy.
832 try:
833 d.getVar("PV")
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800834 d.getVar("PR")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500835 except bb.data_smart.ExpansionError:
836 d = bb.data.createCopy(d)
837 d.setVar("PV", "fetcheravoidrecurse")
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800838 d.setVar("PR", "fetcheravoidrecurse")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500839
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600840 origenv = d.getVar("BB_ORIGENV", False)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500841 for var in exportvars:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500842 val = d.getVar(var) or (origenv and origenv.getVar(var))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500843 if val:
844 cmd = 'export ' + var + '=\"%s\"; %s' % (val, cmd)
845
Brad Bishop316dfdd2018-06-25 12:45:53 -0400846 # Disable pseudo as it may affect ssh, potentially causing it to hang.
847 cmd = 'export PSEUDO_DISABLED=1; ' + cmd
848
Brad Bishop19323692019-04-05 15:28:33 -0400849 if workdir:
850 logger.debug(1, "Running '%s' in %s" % (cmd, workdir))
851 else:
852 logger.debug(1, "Running %s", cmd)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500853
854 success = False
855 error_message = ""
856
857 try:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600858 (output, errors) = bb.process.run(cmd, log=log, shell=True, stderr=subprocess.PIPE, cwd=workdir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500859 success = True
860 except bb.process.NotFoundError as e:
861 error_message = "Fetch command %s" % (e.command)
862 except bb.process.ExecutionError as e:
863 if e.stdout:
864 output = "output:\n%s\n%s" % (e.stdout, e.stderr)
865 elif e.stderr:
866 output = "output:\n%s" % e.stderr
867 else:
868 output = "no output"
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600869 error_message = "Fetch command %s failed with exit code %s, %s" % (e.command, e.exitcode, output)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500870 except bb.process.CmdError as e:
871 error_message = "Fetch command %s could not be run:\n%s" % (e.command, e.msg)
872 if not success:
873 for f in cleanup:
874 try:
875 bb.utils.remove(f, True)
876 except OSError:
877 pass
878
879 raise FetchError(error_message)
880
881 return output
882
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500883def check_network_access(d, info, url):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500884 """
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500885 log remote network access, and error if BB_NO_NETWORK is set or the given
886 URI is untrusted
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500887 """
Brad Bishop19323692019-04-05 15:28:33 -0400888 if bb.utils.to_boolean(d.getVar("BB_NO_NETWORK")):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500889 raise NetworkAccess(url, info)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500890 elif not trusted_network(d, url):
891 raise UntrustedUrl(url, info)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500892 else:
893 logger.debug(1, "Fetcher accessed the network with the command %s" % info)
894
895def build_mirroruris(origud, mirrors, ld):
896 uris = []
897 uds = []
898
899 replacements = {}
900 replacements["TYPE"] = origud.type
901 replacements["HOST"] = origud.host
902 replacements["PATH"] = origud.path
903 replacements["BASENAME"] = origud.path.split("/")[-1]
904 replacements["MIRRORNAME"] = origud.host.replace(':','.') + origud.path.replace('/', '.').replace('*', '.')
905
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500906 def adduri(ud, uris, uds, mirrors, tarballs):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500907 for line in mirrors:
908 try:
909 (find, replace) = line
910 except ValueError:
911 continue
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500912
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500913 for tarball in tarballs:
914 newuri = uri_replace(ud, find, replace, replacements, ld, tarball)
915 if not newuri or newuri in uris or newuri == origud.url:
916 continue
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500917
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500918 if not trusted_network(ld, newuri):
919 logger.debug(1, "Mirror %s not in the list of trusted networks, skipping" % (newuri))
920 continue
Patrick Williamsd7e96312015-09-22 08:09:05 -0500921
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500922 # Create a local copy of the mirrors minus the current line
923 # this will prevent us from recursively processing the same line
924 # as well as indirect recursion A -> B -> C -> A
925 localmirrors = list(mirrors)
926 localmirrors.remove(line)
927
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500928 try:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500929 newud = FetchData(newuri, ld)
930 newud.setup_localpath(ld)
931 except bb.fetch2.BBFetchException as e:
932 logger.debug(1, "Mirror fetch failure for url %s (original url: %s)" % (newuri, origud.url))
933 logger.debug(1, str(e))
934 try:
935 # setup_localpath of file:// urls may fail, we should still see
936 # if mirrors of the url exist
937 adduri(newud, uris, uds, localmirrors, tarballs)
938 except UnboundLocalError:
939 pass
940 continue
941 uris.append(newuri)
942 uds.append(newud)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500943
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500944 adduri(newud, uris, uds, localmirrors, tarballs)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500945
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500946 adduri(origud, uris, uds, mirrors, origud.mirrortarballs or [None])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500947
948 return uris, uds
949
950def rename_bad_checksum(ud, suffix):
951 """
952 Renames files to have suffix from parameter
953 """
954
955 if ud.localpath is None:
956 return
957
958 new_localpath = "%s_bad-checksum_%s" % (ud.localpath, suffix)
959 bb.warn("Renaming %s to %s" % (ud.localpath, new_localpath))
960 bb.utils.movefile(ud.localpath, new_localpath)
961
962
963def try_mirror_url(fetch, origud, ud, ld, check = False):
964 # Return of None or a value means we're finished
965 # False means try another url
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500966
967 if ud.lockfile and ud.lockfile != origud.lockfile:
968 lf = bb.utils.lockfile(ud.lockfile)
969
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500970 try:
971 if check:
972 found = ud.method.checkstatus(fetch, ud, ld)
973 if found:
974 return found
975 return False
976
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500977 if not verify_donestamp(ud, ld, origud) or ud.method.need_update(ud, ld):
978 ud.method.download(ud, ld)
979 if hasattr(ud.method,"build_mirror_data"):
980 ud.method.build_mirror_data(ud, ld)
981
982 if not ud.localpath or not os.path.exists(ud.localpath):
983 return False
984
985 if ud.localpath == origud.localpath:
986 return ud.localpath
987
988 # We may be obtaining a mirror tarball which needs further processing by the real fetcher
989 # If that tarball is a local file:// we need to provide a symlink to it
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500990 dldir = ld.getVar("DL_DIR")
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500991
992 if origud.mirrortarballs and os.path.basename(ud.localpath) in origud.mirrortarballs and os.path.basename(ud.localpath) != os.path.basename(origud.localpath):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500993 # Create donestamp in old format to avoid triggering a re-download
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500994 if ud.donestamp:
995 bb.utils.mkdirhier(os.path.dirname(ud.donestamp))
996 open(ud.donestamp, 'w').close()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500997 dest = os.path.join(dldir, os.path.basename(ud.localpath))
998 if not os.path.exists(dest):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500999 # In case this is executing without any file locks held (as is
1000 # the case for file:// URLs), two tasks may end up here at the
1001 # same time, in which case we do not want the second task to
1002 # fail when the link has already been created by the first task.
1003 try:
1004 os.symlink(ud.localpath, dest)
1005 except FileExistsError:
1006 pass
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001007 if not verify_donestamp(origud, ld) or origud.method.need_update(origud, ld):
1008 origud.method.download(origud, ld)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001009 if hasattr(origud.method, "build_mirror_data"):
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001010 origud.method.build_mirror_data(origud, ld)
Patrick Williamsf1e5d692016-03-30 15:21:19 -05001011 return origud.localpath
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001012 # Otherwise the result is a local file:// and we symlink to it
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001013 ensure_symlink(ud.localpath, origud.localpath)
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001014 update_stamp(origud, ld)
1015 return ud.localpath
1016
1017 except bb.fetch2.NetworkAccess:
1018 raise
1019
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001020 except IOError as e:
Brad Bishop19323692019-04-05 15:28:33 -04001021 if e.errno in [errno.ESTALE]:
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001022 logger.warning("Stale Error Observed %s." % ud.url)
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001023 return False
1024 raise
1025
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001026 except bb.fetch2.BBFetchException as e:
1027 if isinstance(e, ChecksumError):
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001028 logger.warning("Mirror checksum failure for url %s (original url: %s)\nCleaning and trying again." % (ud.url, origud.url))
1029 logger.warning(str(e))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05001030 if os.path.exists(ud.localpath):
1031 rename_bad_checksum(ud, e.checksum)
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001032 elif isinstance(e, NoChecksumError):
1033 raise
1034 else:
1035 logger.debug(1, "Mirror fetch failure for url %s (original url: %s)" % (ud.url, origud.url))
1036 logger.debug(1, str(e))
1037 try:
1038 ud.method.clean(ud, ld)
1039 except UnboundLocalError:
1040 pass
1041 return False
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05001042 finally:
1043 if ud.lockfile and ud.lockfile != origud.lockfile:
1044 bb.utils.unlockfile(lf)
1045
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001046
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001047def ensure_symlink(target, link_name):
1048 if not os.path.exists(link_name):
1049 if os.path.islink(link_name):
1050 # Broken symbolic link
1051 os.unlink(link_name)
1052
1053 # In case this is executing without any file locks held (as is
1054 # the case for file:// URLs), two tasks may end up here at the
1055 # same time, in which case we do not want the second task to
1056 # fail when the link has already been created by the first task.
1057 try:
1058 os.symlink(target, link_name)
1059 except FileExistsError:
1060 pass
1061
1062
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001063def try_mirrors(fetch, d, origud, mirrors, check = False):
1064 """
1065 Try to use a mirrored version of the sources.
1066 This method will be automatically called before the fetchers go.
1067
1068 d Is a bb.data instance
1069 uri is the original uri we're trying to download
1070 mirrors is the list of mirrors we're going to try
1071 """
1072 ld = d.createCopy()
1073
1074 uris, uds = build_mirroruris(origud, mirrors, ld)
1075
1076 for index, uri in enumerate(uris):
1077 ret = try_mirror_url(fetch, origud, uds[index], ld, check)
1078 if ret != False:
1079 return ret
1080 return None
1081
1082def trusted_network(d, url):
1083 """
1084 Use a trusted url during download if networking is enabled and
1085 BB_ALLOWED_NETWORKS is set globally or for a specific recipe.
1086 Note: modifies SRC_URI & mirrors.
1087 """
Brad Bishop19323692019-04-05 15:28:33 -04001088 if bb.utils.to_boolean(d.getVar("BB_NO_NETWORK")):
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001089 return True
1090
1091 pkgname = d.expand(d.getVar('PN', False))
Brad Bishop1a4b7ee2018-12-16 17:11:34 -08001092 trusted_hosts = None
1093 if pkgname:
1094 trusted_hosts = d.getVarFlag('BB_ALLOWED_NETWORKS', pkgname, False)
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001095
1096 if not trusted_hosts:
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001097 trusted_hosts = d.getVar('BB_ALLOWED_NETWORKS')
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001098
1099 # Not enabled.
1100 if not trusted_hosts:
1101 return True
1102
1103 scheme, network, path, user, passwd, param = decodeurl(url)
1104
1105 if not network:
1106 return True
1107
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05001108 network = network.split(':')[0]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001109 network = network.lower()
1110
1111 for host in trusted_hosts.split(" "):
1112 host = host.lower()
1113 if host.startswith("*.") and ("." + network).endswith(host[1:]):
1114 return True
1115 if host == network:
1116 return True
1117
1118 return False
1119
1120def srcrev_internal_helper(ud, d, name):
1121 """
1122 Return:
1123 a) a source revision if specified
1124 b) latest revision if SRCREV="AUTOINC"
1125 c) None if not specified
1126 """
1127
1128 srcrev = None
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001129 pn = d.getVar("PN")
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001130 attempts = []
1131 if name != '' and pn:
1132 attempts.append("SRCREV_%s_pn-%s" % (name, pn))
1133 if name != '':
1134 attempts.append("SRCREV_%s" % name)
1135 if pn:
1136 attempts.append("SRCREV_pn-%s" % pn)
1137 attempts.append("SRCREV")
1138
1139 for a in attempts:
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001140 srcrev = d.getVar(a)
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001141 if srcrev and srcrev != "INVALID":
1142 break
1143
1144 if 'rev' in ud.parm and 'tag' in ud.parm:
1145 raise FetchError("Please specify a ;rev= parameter or a ;tag= parameter in the url %s but not both." % (ud.url))
1146
1147 if 'rev' in ud.parm or 'tag' in ud.parm:
1148 if 'rev' in ud.parm:
1149 parmrev = ud.parm['rev']
1150 else:
1151 parmrev = ud.parm['tag']
1152 if srcrev == "INVALID" or not srcrev:
1153 return parmrev
1154 if srcrev != parmrev:
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001155 raise FetchError("Conflicting revisions (%s from SRCREV and %s from the url) found, please specify one valid value" % (srcrev, parmrev))
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001156 return parmrev
1157
1158 if srcrev == "INVALID" or not srcrev:
1159 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)
1160 if srcrev == "AUTOINC":
1161 srcrev = ud.method.latest_revision(ud, d, name)
1162
1163 return srcrev
1164
1165def get_checksum_file_list(d):
1166 """ Get a list of files checksum in SRC_URI
1167
1168 Returns the resolved local paths of all local file entries in
1169 SRC_URI as a space-separated string
1170 """
1171 fetch = Fetch([], d, cache = False, localonly = True)
1172
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001173 dl_dir = d.getVar('DL_DIR')
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001174 filelist = []
1175 for u in fetch.urls:
1176 ud = fetch.ud[u]
1177
1178 if ud and isinstance(ud.method, local.Local):
1179 paths = ud.method.localpaths(ud, d)
1180 for f in paths:
1181 pth = ud.decodedurl
1182 if '*' in pth:
1183 f = os.path.join(os.path.abspath(f), pth)
1184 if f.startswith(dl_dir):
1185 # The local fetcher's behaviour is to return a path under DL_DIR if it couldn't find the file anywhere else
1186 if os.path.exists(f):
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001187 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 Williamsc124f4f2015-09-15 14:41:29 -05001188 else:
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001189 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 Williamsc124f4f2015-09-15 14:41:29 -05001190 filelist.append(f + ":" + str(os.path.exists(f)))
1191
1192 return " ".join(filelist)
1193
1194def get_file_checksums(filelist, pn):
1195 """Get a list of the checksums for a list of local files
1196
1197 Returns the checksums for a list of local files, caching the results as
1198 it proceeds
1199
1200 """
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05001201 return _checksum_cache.get_checksums(filelist, pn)
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001202
1203
1204class FetchData(object):
1205 """
1206 A class which represents the fetcher state for a given URI.
1207 """
1208 def __init__(self, url, d, localonly = False):
1209 # localpath is the location of a downloaded result. If not set, the file is local.
1210 self.donestamp = None
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05001211 self.needdonestamp = True
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001212 self.localfile = ""
1213 self.localpath = None
1214 self.lockfile = None
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001215 self.mirrortarballs = []
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001216 self.basename = None
1217 self.basepath = None
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001218 (self.type, self.host, self.path, self.user, self.pswd, self.parm) = decodeurl(d.expand(url))
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001219 self.date = self.getSRCDate(d)
1220 self.url = url
1221 if not self.user and "user" in self.parm:
1222 self.user = self.parm["user"]
1223 if not self.pswd and "pswd" in self.parm:
1224 self.pswd = self.parm["pswd"]
1225 self.setup = False
1226
1227 if "name" in self.parm:
1228 self.md5_name = "%s.md5sum" % self.parm["name"]
1229 self.sha256_name = "%s.sha256sum" % self.parm["name"]
1230 else:
1231 self.md5_name = "md5sum"
1232 self.sha256_name = "sha256sum"
1233 if self.md5_name in self.parm:
1234 self.md5_expected = self.parm[self.md5_name]
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001235 elif self.type not in ["http", "https", "ftp", "ftps", "sftp", "s3"]:
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001236 self.md5_expected = None
1237 else:
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001238 self.md5_expected = d.getVarFlag("SRC_URI", self.md5_name)
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001239 if self.sha256_name in self.parm:
1240 self.sha256_expected = self.parm[self.sha256_name]
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001241 elif self.type not in ["http", "https", "ftp", "ftps", "sftp", "s3"]:
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001242 self.sha256_expected = None
1243 else:
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001244 self.sha256_expected = d.getVarFlag("SRC_URI", self.sha256_name)
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001245 self.ignore_checksums = False
1246
1247 self.names = self.parm.get("name",'default').split(',')
1248
1249 self.method = None
1250 for m in methods:
1251 if m.supports(self, d):
1252 self.method = m
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001253 break
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001254
1255 if not self.method:
1256 raise NoMethodError(url)
1257
1258 if localonly and not isinstance(self.method, local.Local):
1259 raise NonLocalMethod()
1260
1261 if self.parm.get("proto", None) and "protocol" not in self.parm:
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001262 logger.warning('Consider updating %s recipe to use "protocol" not "proto" in SRC_URI.', d.getVar('PN'))
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001263 self.parm["protocol"] = self.parm.get("proto", None)
1264
1265 if hasattr(self.method, "urldata_init"):
1266 self.method.urldata_init(self, d)
1267
1268 if "localpath" in self.parm:
1269 # if user sets localpath for file, use it instead.
1270 self.localpath = self.parm["localpath"]
1271 self.basename = os.path.basename(self.localpath)
1272 elif self.localfile:
1273 self.localpath = self.method.localpath(self, d)
1274
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001275 dldir = d.getVar("DL_DIR")
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05001276
1277 if not self.needdonestamp:
1278 return
1279
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001280 # Note: .done and .lock files should always be in DL_DIR whereas localpath may not be.
1281 if self.localpath and self.localpath.startswith(dldir):
1282 basepath = self.localpath
1283 elif self.localpath:
1284 basepath = dldir + os.sep + os.path.basename(self.localpath)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05001285 elif self.basepath or self.basename:
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001286 basepath = dldir + os.sep + (self.basepath or self.basename)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05001287 else:
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001288 bb.fatal("Can't determine lock path for url %s" % url)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05001289
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001290 self.donestamp = basepath + '.done'
1291 self.lockfile = basepath + '.lock'
1292
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001293 def setup_revisions(self, d):
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001294 self.revisions = {}
1295 for name in self.names:
1296 self.revisions[name] = srcrev_internal_helper(self, d, name)
1297
1298 # add compatibility code for non name specified case
1299 if len(self.names) == 1:
1300 self.revision = self.revisions[self.names[0]]
1301
1302 def setup_localpath(self, d):
1303 if not self.localpath:
1304 self.localpath = self.method.localpath(self, d)
1305
1306 def getSRCDate(self, d):
1307 """
1308 Return the SRC Date for the component
1309
1310 d the bb.data module
1311 """
1312 if "srcdate" in self.parm:
1313 return self.parm['srcdate']
1314
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001315 pn = d.getVar("PN")
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001316
1317 if pn:
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001318 return d.getVar("SRCDATE_%s" % pn) or d.getVar("SRCDATE") or d.getVar("DATE")
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001319
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001320 return d.getVar("SRCDATE") or d.getVar("DATE")
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001321
1322class FetchMethod(object):
1323 """Base class for 'fetch'ing data"""
1324
1325 def __init__(self, urls=None):
1326 self.urls = []
1327
1328 def supports(self, urldata, d):
1329 """
1330 Check to see if this fetch class supports a given url.
1331 """
1332 return 0
1333
1334 def localpath(self, urldata, d):
1335 """
1336 Return the local filename of a given url assuming a successful fetch.
1337 Can also setup variables in urldata for use in go (saving code duplication
1338 and duplicate code execution)
1339 """
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001340 return os.path.join(d.getVar("DL_DIR"), urldata.localfile)
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001341
1342 def supports_checksum(self, urldata):
1343 """
1344 Is localpath something that can be represented by a checksum?
1345 """
1346
1347 # We cannot compute checksums for directories
1348 if os.path.isdir(urldata.localpath) == True:
1349 return False
1350 if urldata.localpath.find("*") != -1:
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001351 return False
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001352
1353 return True
1354
1355 def recommends_checksum(self, urldata):
1356 """
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001357 Is the backend on where checksumming is recommended (should warnings
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001358 be displayed if there is no checksum)?
1359 """
1360 return False
1361
1362 def _strip_leading_slashes(self, relpath):
1363 """
1364 Remove leading slash as os.path.join can't cope
1365 """
1366 while os.path.isabs(relpath):
1367 relpath = relpath[1:]
1368 return relpath
1369
1370 def setUrls(self, urls):
1371 self.__urls = urls
1372
1373 def getUrls(self):
1374 return self.__urls
1375
1376 urls = property(getUrls, setUrls, None, "Urls property")
1377
1378 def need_update(self, ud, d):
1379 """
1380 Force a fetch, even if localpath exists?
1381 """
1382 if os.path.exists(ud.localpath):
1383 return False
1384 return True
1385
1386 def supports_srcrev(self):
1387 """
1388 The fetcher supports auto source revisions (SRCREV)
1389 """
1390 return False
1391
1392 def download(self, urldata, d):
1393 """
1394 Fetch urls
1395 Assumes localpath was called first
1396 """
Brad Bishop19323692019-04-05 15:28:33 -04001397 raise NoMethodError(urldata.url)
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001398
1399 def unpack(self, urldata, rootdir, data):
1400 iterate = False
1401 file = urldata.localpath
1402
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05001403 # Localpath can't deal with 'dir/*' entries, so it converts them to '.',
1404 # but it must be corrected back for local files copying
1405 if urldata.basename == '*' and file.endswith('/.'):
1406 file = '%s/%s' % (file.rstrip('/.'), urldata.path)
1407
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001408 try:
1409 unpack = bb.utils.to_boolean(urldata.parm.get('unpack'), True)
1410 except ValueError as exc:
1411 bb.fatal("Invalid value for 'unpack' parameter for %s: %s" %
1412 (file, urldata.parm.get('unpack')))
1413
1414 base, ext = os.path.splitext(file)
1415 if ext in ['.gz', '.bz2', '.Z', '.xz', '.lz']:
1416 efile = os.path.join(rootdir, os.path.basename(base))
1417 else:
1418 efile = file
1419 cmd = None
1420
1421 if unpack:
1422 if file.endswith('.tar'):
1423 cmd = 'tar x --no-same-owner -f %s' % file
1424 elif file.endswith('.tgz') or file.endswith('.tar.gz') or file.endswith('.tar.Z'):
1425 cmd = 'tar xz --no-same-owner -f %s' % file
1426 elif file.endswith('.tbz') or file.endswith('.tbz2') or file.endswith('.tar.bz2'):
1427 cmd = 'bzip2 -dc %s | tar x --no-same-owner -f -' % file
1428 elif file.endswith('.gz') or file.endswith('.Z') or file.endswith('.z'):
1429 cmd = 'gzip -dc %s > %s' % (file, efile)
1430 elif file.endswith('.bz2'):
1431 cmd = 'bzip2 -dc %s > %s' % (file, efile)
Brad Bishop316dfdd2018-06-25 12:45:53 -04001432 elif file.endswith('.txz') or file.endswith('.tar.xz'):
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001433 cmd = 'xz -dc %s | tar x --no-same-owner -f -' % file
1434 elif file.endswith('.xz'):
1435 cmd = 'xz -dc %s > %s' % (file, efile)
1436 elif file.endswith('.tar.lz'):
1437 cmd = 'lzip -dc %s | tar x --no-same-owner -f -' % file
1438 elif file.endswith('.lz'):
1439 cmd = 'lzip -dc %s > %s' % (file, efile)
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001440 elif file.endswith('.tar.7z'):
1441 cmd = '7z x -so %s | tar x --no-same-owner -f -' % file
1442 elif file.endswith('.7z'):
1443 cmd = '7za x -y %s 1>/dev/null' % file
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001444 elif file.endswith('.zip') or file.endswith('.jar'):
1445 try:
1446 dos = bb.utils.to_boolean(urldata.parm.get('dos'), False)
1447 except ValueError as exc:
1448 bb.fatal("Invalid value for 'dos' parameter for %s: %s" %
1449 (file, urldata.parm.get('dos')))
1450 cmd = 'unzip -q -o'
1451 if dos:
1452 cmd = '%s -a' % cmd
1453 cmd = "%s '%s'" % (cmd, file)
1454 elif file.endswith('.rpm') or file.endswith('.srpm'):
1455 if 'extract' in urldata.parm:
1456 unpack_file = urldata.parm.get('extract')
1457 cmd = 'rpm2cpio.sh %s | cpio -id %s' % (file, unpack_file)
1458 iterate = True
1459 iterate_file = unpack_file
1460 else:
1461 cmd = 'rpm2cpio.sh %s | cpio -id' % (file)
1462 elif file.endswith('.deb') or file.endswith('.ipk'):
Brad Bishopa5c52ff2018-11-23 10:55:50 +13001463 output = subprocess.check_output(['ar', '-t', file], preexec_fn=subprocess_setup)
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001464 datafile = None
1465 if output:
1466 for line in output.decode().splitlines():
1467 if line.startswith('data.tar.'):
1468 datafile = line
1469 break
1470 else:
1471 raise UnpackError("Unable to unpack deb/ipk package - does not contain data.tar.* file", urldata.url)
1472 else:
1473 raise UnpackError("Unable to unpack deb/ipk package - could not list contents", urldata.url)
1474 cmd = 'ar x %s %s && tar --no-same-owner -xpf %s && rm %s' % (file, datafile, datafile, datafile)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05001475
1476 # If 'subdir' param exists, create a dir and use it as destination for unpack cmd
1477 if 'subdir' in urldata.parm:
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001478 subdir = urldata.parm.get('subdir')
1479 if os.path.isabs(subdir):
1480 if not os.path.realpath(subdir).startswith(os.path.realpath(rootdir)):
1481 raise UnpackError("subdir argument isn't a subdirectory of unpack root %s" % rootdir, urldata.url)
1482 unpackdir = subdir
1483 else:
1484 unpackdir = os.path.join(rootdir, subdir)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05001485 bb.utils.mkdirhier(unpackdir)
1486 else:
1487 unpackdir = rootdir
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001488
1489 if not unpack or not cmd:
1490 # If file == dest, then avoid any copies, as we already put the file into dest!
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05001491 dest = os.path.join(unpackdir, os.path.basename(file))
1492 if file != dest and not (os.path.exists(dest) and os.path.samefile(file, dest)):
1493 destdir = '.'
1494 # For file:// entries all intermediate dirs in path must be created at destination
1495 if urldata.type == "file":
1496 # Trailing '/' does a copying to wrong place
1497 urlpath = urldata.path.rstrip('/')
1498 # Want files places relative to cwd so no leading '/'
1499 urlpath = urlpath.lstrip('/')
1500 if urlpath.find("/") != -1:
1501 destdir = urlpath.rsplit("/", 1)[0] + '/'
1502 bb.utils.mkdirhier("%s/%s" % (unpackdir, destdir))
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001503 cmd = 'cp -fpPRH %s %s' % (file, destdir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001504
1505 if not cmd:
1506 return
1507
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001508 path = data.getVar('PATH')
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001509 if path:
1510 cmd = "PATH=\"%s\" %s" % (path, cmd)
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001511 bb.note("Unpacking %s to %s/" % (file, unpackdir))
1512 ret = subprocess.call(cmd, preexec_fn=subprocess_setup, shell=True, cwd=unpackdir)
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001513
1514 if ret != 0:
1515 raise UnpackError("Unpack command %s failed with return value %s" % (cmd, ret), urldata.url)
1516
1517 if iterate is True:
1518 iterate_urldata = urldata
1519 iterate_urldata.localpath = "%s/%s" % (rootdir, iterate_file)
1520 self.unpack(urldata, rootdir, data)
1521
1522 return
1523
1524 def clean(self, urldata, d):
1525 """
1526 Clean any existing full or partial download
1527 """
1528 bb.utils.remove(urldata.localpath)
1529
1530 def try_premirror(self, urldata, d):
1531 """
1532 Should premirrors be used?
1533 """
1534 return True
1535
1536 def checkstatus(self, fetch, urldata, d):
1537 """
1538 Check the status of a URL
1539 Assumes localpath was called first
1540 """
Brad Bishop19323692019-04-05 15:28:33 -04001541 logger.info("URL %s could not be checked for status since no method exists.", urldata.url)
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001542 return True
1543
1544 def latest_revision(self, ud, d, name):
1545 """
1546 Look in the cache for the latest revision, if not present ask the SCM.
1547 """
1548 if not hasattr(self, "_latest_revision"):
Brad Bishop19323692019-04-05 15:28:33 -04001549 raise ParameterError("The fetcher for this URL does not support _latest_revision", ud.url)
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001550
1551 revs = bb.persist_data.persist('BB_URI_HEADREVS', d)
1552 key = self.generate_revision_key(ud, d, name)
1553 try:
1554 return revs[key]
1555 except KeyError:
1556 revs[key] = rev = self._latest_revision(ud, d, name)
1557 return rev
1558
1559 def sortable_revision(self, ud, d, name):
1560 latest_rev = self._build_revision(ud, d, name)
1561 return True, str(latest_rev)
1562
1563 def generate_revision_key(self, ud, d, name):
1564 key = self._revision_key(ud, d, name)
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001565 return "%s-%s" % (key, d.getVar("PN") or "")
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001566
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001567 def latest_versionstring(self, ud, d):
1568 """
1569 Compute the latest release name like "x.y.x" in "x.y.x+gitHASH"
1570 by searching through the tags output of ls-remote, comparing
1571 versions and returning the highest match as a (version, revision) pair.
1572 """
1573 return ('', '')
1574
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001575class Fetch(object):
1576 def __init__(self, urls, d, cache = True, localonly = False, connection_cache = None):
1577 if localonly and cache:
1578 raise Exception("bb.fetch2.Fetch.__init__: cannot set cache and localonly at same time")
1579
1580 if len(urls) == 0:
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001581 urls = d.getVar("SRC_URI").split()
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001582 self.urls = urls
1583 self.d = d
1584 self.ud = {}
1585 self.connection_cache = connection_cache
1586
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001587 fn = d.getVar('FILE')
1588 mc = d.getVar('__BBMULTICONFIG') or ""
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001589 if cache and fn and mc + fn in urldata_cache:
1590 self.ud = urldata_cache[mc + fn]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001591
1592 for url in urls:
1593 if url not in self.ud:
1594 try:
1595 self.ud[url] = FetchData(url, d, localonly)
1596 except NonLocalMethod:
1597 if localonly:
1598 self.ud[url] = None
1599 pass
1600
1601 if fn and cache:
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001602 urldata_cache[mc + fn] = self.ud
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001603
1604 def localpath(self, url):
1605 if url not in self.urls:
1606 self.ud[url] = FetchData(url, self.d)
1607
1608 self.ud[url].setup_localpath(self.d)
1609 return self.d.expand(self.ud[url].localpath)
1610
1611 def localpaths(self):
1612 """
1613 Return a list of the local filenames, assuming successful fetch
1614 """
1615 local = []
1616
1617 for u in self.urls:
1618 ud = self.ud[u]
1619 ud.setup_localpath(self.d)
1620 local.append(ud.localpath)
1621
1622 return local
1623
1624 def download(self, urls=None):
1625 """
1626 Fetch all urls
1627 """
1628 if not urls:
1629 urls = self.urls
1630
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001631 network = self.d.getVar("BB_NO_NETWORK")
Brad Bishop19323692019-04-05 15:28:33 -04001632 premirroronly = bb.utils.to_boolean(self.d.getVar("BB_FETCH_PREMIRRORONLY"))
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001633
1634 for u in urls:
1635 ud = self.ud[u]
1636 ud.setup_localpath(self.d)
1637 m = ud.method
1638 localpath = ""
1639
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05001640 if ud.lockfile:
1641 lf = bb.utils.lockfile(ud.lockfile)
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001642
1643 try:
1644 self.d.setVar("BB_NO_NETWORK", network)
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001645
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001646 if verify_donestamp(ud, self.d) and not m.need_update(ud, self.d):
1647 localpath = ud.localpath
1648 elif m.try_premirror(ud, self.d):
1649 logger.debug(1, "Trying PREMIRRORS")
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001650 mirrors = mirror_from_string(self.d.getVar('PREMIRRORS'))
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001651 localpath = try_mirrors(self, self.d, ud, mirrors, False)
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001652 if localpath:
1653 try:
1654 # early checksum verification so that if the checksum of the premirror
1655 # contents mismatch the fetcher can still try upstream and mirrors
1656 update_stamp(ud, self.d)
1657 except ChecksumError as e:
1658 logger.warning("Checksum failure encountered with premirror download of %s - will attempt other sources." % u)
1659 logger.debug(1, str(e))
1660 localpath = ""
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001661
1662 if premirroronly:
1663 self.d.setVar("BB_NO_NETWORK", "1")
1664
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001665 firsterr = None
1666 verified_stamp = verify_donestamp(ud, self.d)
1667 if not localpath and (not verified_stamp or m.need_update(ud, self.d)):
1668 try:
1669 if not trusted_network(self.d, ud.url):
1670 raise UntrustedUrl(ud.url)
1671 logger.debug(1, "Trying Upstream")
1672 m.download(ud, self.d)
1673 if hasattr(m, "build_mirror_data"):
1674 m.build_mirror_data(ud, self.d)
1675 localpath = ud.localpath
1676 # early checksum verify, so that if checksum mismatched,
1677 # fetcher still have chance to fetch from mirror
1678 update_stamp(ud, self.d)
1679
1680 except bb.fetch2.NetworkAccess:
1681 raise
1682
1683 except BBFetchException as e:
1684 if isinstance(e, ChecksumError):
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001685 logger.warning("Checksum failure encountered with download of %s - will attempt other sources if available" % u)
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001686 logger.debug(1, str(e))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05001687 if os.path.exists(ud.localpath):
1688 rename_bad_checksum(ud, e.checksum)
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001689 elif isinstance(e, NoChecksumError):
1690 raise
1691 else:
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001692 logger.warning('Failed to fetch URL %s, attempting MIRRORS if available' % u)
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001693 logger.debug(1, str(e))
1694 firsterr = e
1695 # Remove any incomplete fetch
1696 if not verified_stamp:
1697 m.clean(ud, self.d)
1698 logger.debug(1, "Trying MIRRORS")
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001699 mirrors = mirror_from_string(self.d.getVar('MIRRORS'))
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001700 localpath = try_mirrors(self, self.d, ud, mirrors)
1701
1702 if not localpath or ((not os.path.exists(localpath)) and localpath.find("*") == -1):
1703 if firsterr:
1704 logger.error(str(firsterr))
1705 raise FetchError("Unable to fetch URL from any source.", u)
1706
1707 update_stamp(ud, self.d)
1708
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001709 except IOError as e:
Brad Bishop19323692019-04-05 15:28:33 -04001710 if e.errno in [errno.ESTALE]:
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001711 logger.error("Stale Error Observed %s." % u)
1712 raise ChecksumError("Stale Error Detected")
1713
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001714 except BBFetchException as e:
1715 if isinstance(e, ChecksumError):
1716 logger.error("Checksum failure fetching %s" % u)
1717 raise
1718
1719 finally:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05001720 if ud.lockfile:
1721 bb.utils.unlockfile(lf)
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001722
1723 def checkstatus(self, urls=None):
1724 """
1725 Check all urls exist upstream
1726 """
1727
1728 if not urls:
1729 urls = self.urls
1730
1731 for u in urls:
1732 ud = self.ud[u]
1733 ud.setup_localpath(self.d)
1734 m = ud.method
1735 logger.debug(1, "Testing URL %s", u)
1736 # First try checking uri, u, from PREMIRRORS
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001737 mirrors = mirror_from_string(self.d.getVar('PREMIRRORS'))
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001738 ret = try_mirrors(self, self.d, ud, mirrors, True)
1739 if not ret:
1740 # Next try checking from the original uri, u
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001741 ret = m.checkstatus(self, ud, self.d)
1742 if not ret:
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001743 # Finally, try checking uri, u, from MIRRORS
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001744 mirrors = mirror_from_string(self.d.getVar('MIRRORS'))
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001745 ret = try_mirrors(self, self.d, ud, mirrors, True)
1746
1747 if not ret:
1748 raise FetchError("URL %s doesn't work" % u, u)
1749
1750 def unpack(self, root, urls=None):
1751 """
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001752 Unpack urls to root
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001753 """
1754
1755 if not urls:
1756 urls = self.urls
1757
1758 for u in urls:
1759 ud = self.ud[u]
1760 ud.setup_localpath(self.d)
1761
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001762 if ud.lockfile:
1763 lf = bb.utils.lockfile(ud.lockfile)
1764
1765 ud.method.unpack(ud, root, self.d)
1766
1767 if ud.lockfile:
1768 bb.utils.unlockfile(lf)
1769
1770 def clean(self, urls=None):
1771 """
1772 Clean files that the fetcher gets or places
1773 """
1774
1775 if not urls:
1776 urls = self.urls
1777
1778 for url in urls:
1779 if url not in self.ud:
Brad Bishop19323692019-04-05 15:28:33 -04001780 self.ud[url] = FetchData(url, self.d)
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001781 ud = self.ud[url]
1782 ud.setup_localpath(self.d)
1783
1784 if not ud.localfile and ud.localpath is None:
1785 continue
1786
1787 if ud.lockfile:
1788 lf = bb.utils.lockfile(ud.lockfile)
1789
1790 ud.method.clean(ud, self.d)
1791 if ud.donestamp:
1792 bb.utils.remove(ud.donestamp)
1793
1794 if ud.lockfile:
1795 bb.utils.unlockfile(lf)
1796
1797class FetchConnectionCache(object):
1798 """
1799 A class which represents an container for socket connections.
1800 """
1801 def __init__(self):
1802 self.cache = {}
1803
1804 def get_connection_name(self, host, port):
1805 return host + ':' + str(port)
1806
1807 def add_connection(self, host, port, connection):
1808 cn = self.get_connection_name(host, port)
1809
1810 if cn not in self.cache:
1811 self.cache[cn] = connection
1812
1813 def get_connection(self, host, port):
1814 connection = None
1815
1816 cn = self.get_connection_name(host, port)
1817 if cn in self.cache:
1818 connection = self.cache[cn]
1819
1820 return connection
1821
1822 def remove_connection(self, host, port):
1823 cn = self.get_connection_name(host, port)
1824 if cn in self.cache:
1825 self.cache[cn].close()
1826 del self.cache[cn]
1827
1828 def close_connections(self):
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001829 for cn in list(self.cache.keys()):
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001830 self.cache[cn].close()
1831 del self.cache[cn]
1832
1833from . import cvs
1834from . import git
1835from . import gitsm
1836from . import gitannex
1837from . import local
1838from . import svn
1839from . import wget
1840from . import ssh
1841from . import sftp
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001842from . import s3
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001843from . import perforce
1844from . import bzr
1845from . import hg
1846from . import osc
1847from . import repo
1848from . import clearcase
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05001849from . import npm
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001850
1851methods.append(local.Local())
1852methods.append(wget.Wget())
1853methods.append(svn.Svn())
1854methods.append(git.Git())
1855methods.append(gitsm.GitSM())
1856methods.append(gitannex.GitANNEX())
1857methods.append(cvs.Cvs())
1858methods.append(ssh.SSH())
1859methods.append(sftp.SFTP())
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001860methods.append(s3.S3())
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001861methods.append(perforce.Perforce())
1862methods.append(bzr.Bzr())
1863methods.append(hg.Hg())
1864methods.append(osc.Osc())
1865methods.append(repo.Repo())
1866methods.append(clearcase.ClearCase())
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05001867methods.append(npm.Npm())