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 | |
| 9 | # Copyright (C) 2003, 2004 Chris Larson |
| 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 | |
Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 15 | import shlex |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 16 | import re |
| 17 | import tempfile |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 18 | import os |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 19 | import errno |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 20 | import bb |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 21 | import bb.progress |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 22 | import socket |
| 23 | import http.client |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 24 | import urllib.request, urllib.parse, urllib.error |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 25 | from bb.fetch2 import FetchMethod |
| 26 | from bb.fetch2 import FetchError |
| 27 | from bb.fetch2 import logger |
| 28 | from bb.fetch2 import runfetchcmd |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 29 | from bb.utils import export_proxies |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 30 | from bs4 import BeautifulSoup |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 31 | from bs4 import SoupStrainer |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 32 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 33 | class WgetProgressHandler(bb.progress.LineFilterProgressHandler): |
| 34 | """ |
| 35 | Extract progress information from wget output. |
| 36 | Note: relies on --progress=dot (with -v or without -q/-nv) being |
| 37 | specified on the wget command line. |
| 38 | """ |
| 39 | def __init__(self, d): |
| 40 | super(WgetProgressHandler, self).__init__(d) |
| 41 | # Send an initial progress event so the bar gets shown |
| 42 | self._fire_progress(0) |
| 43 | |
| 44 | def writeline(self, line): |
| 45 | percs = re.findall(r'(\d+)%\s+([\d.]+[A-Z])', line) |
| 46 | if percs: |
| 47 | progress = int(percs[-1][0]) |
| 48 | rate = percs[-1][1] + '/s' |
| 49 | self.update(progress, rate) |
| 50 | return False |
| 51 | return True |
| 52 | |
| 53 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 54 | class Wget(FetchMethod): |
Patrick Williams | 0ca19cc | 2021-08-16 14:03:13 -0500 | [diff] [blame] | 55 | """Class to fetch urls via 'wget'""" |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 56 | |
| 57 | # CDNs like CloudFlare may do a 'browser integrity test' which can fail |
| 58 | # with the standard wget/urllib User-Agent, so pretend to be a modern |
| 59 | # browser. |
| 60 | user_agent = "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:84.0) Gecko/20100101 Firefox/84.0" |
| 61 | |
Patrick Williams | 0ca19cc | 2021-08-16 14:03:13 -0500 | [diff] [blame] | 62 | def check_certs(self, d): |
| 63 | """ |
| 64 | Should certificates be checked? |
| 65 | """ |
| 66 | return (d.getVar("BB_CHECK_SSL_CERTS") or "1") != "0" |
| 67 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 68 | def supports(self, ud, d): |
| 69 | """ |
| 70 | Check to see if a given url can be fetched with wget. |
| 71 | """ |
Andrew Geissler | 5199d83 | 2021-09-24 16:47:35 -0500 | [diff] [blame] | 72 | return ud.type in ['http', 'https', 'ftp', 'ftps'] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 73 | |
| 74 | def recommends_checksum(self, urldata): |
| 75 | return True |
| 76 | |
| 77 | def urldata_init(self, ud, d): |
| 78 | if 'protocol' in ud.parm: |
| 79 | if ud.parm['protocol'] == 'git': |
| 80 | raise bb.fetch2.ParameterError("Invalid protocol - if you wish to fetch from a git repository using http, you need to instead use the git:// prefix with protocol=http", ud.url) |
| 81 | |
| 82 | if 'downloadfilename' in ud.parm: |
| 83 | ud.basename = ud.parm['downloadfilename'] |
| 84 | else: |
| 85 | ud.basename = os.path.basename(ud.path) |
| 86 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 87 | ud.localfile = d.expand(urllib.parse.unquote(ud.basename)) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 88 | if not ud.localfile: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 89 | ud.localfile = d.expand(urllib.parse.unquote(ud.host + ud.path).replace("/", ".")) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 90 | |
Patrick Williams | 0ca19cc | 2021-08-16 14:03:13 -0500 | [diff] [blame] | 91 | self.basecmd = d.getVar("FETCHCMD_wget") or "/usr/bin/env wget -t 2 -T 30 --passive-ftp" |
| 92 | |
| 93 | if not self.check_certs(d): |
| 94 | self.basecmd += " --no-check-certificate" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 95 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 96 | def _runwget(self, ud, d, command, quiet, workdir=None): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 97 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 98 | progresshandler = WgetProgressHandler(d) |
| 99 | |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 100 | logger.debug2("Fetching %s using command '%s'" % (ud.url, command)) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 101 | bb.fetch2.check_network_access(d, command, ud.url) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 102 | runfetchcmd(command + ' --progress=dot -v', d, quiet, log=progresshandler, workdir=workdir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 103 | |
| 104 | def download(self, ud, d): |
| 105 | """Fetch urls""" |
| 106 | |
| 107 | fetchcmd = self.basecmd |
| 108 | |
Andrew Geissler | 78b7279 | 2022-06-14 06:47:25 -0500 | [diff] [blame] | 109 | localpath = os.path.join(d.getVar("DL_DIR"), ud.localfile) + ".tmp" |
| 110 | bb.utils.mkdirhier(os.path.dirname(localpath)) |
| 111 | fetchcmd += " -O %s" % shlex.quote(localpath) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 112 | |
Brad Bishop | 37a0e4d | 2017-12-04 01:01:44 -0500 | [diff] [blame] | 113 | if ud.user and ud.pswd: |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 114 | fetchcmd += " --auth-no-challenge" |
| 115 | if ud.parm.get("redirectauth", "1") == "1": |
| 116 | # An undocumented feature of wget is that if the |
| 117 | # username/password are specified on the URI, wget will only |
| 118 | # send the Authorization header to the first host and not to |
| 119 | # any hosts that it is redirected to. With the increasing |
| 120 | # usage of temporary AWS URLs, this difference now matters as |
| 121 | # AWS will reject any request that has authentication both in |
| 122 | # the query parameters (from the redirect) and in the |
| 123 | # Authorization header. |
| 124 | fetchcmd += " --user=%s --password=%s" % (ud.user, ud.pswd) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 125 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 126 | uri = ud.url.split(";")[0] |
| 127 | if os.path.exists(ud.localpath): |
| 128 | # file exists, but we didnt complete it.. trying again.. |
| 129 | fetchcmd += d.expand(" -c -P ${DL_DIR} '%s'" % uri) |
| 130 | else: |
| 131 | fetchcmd += d.expand(" -P ${DL_DIR} '%s'" % uri) |
| 132 | |
| 133 | self._runwget(ud, d, fetchcmd, False) |
| 134 | |
Andrew Geissler | 78b7279 | 2022-06-14 06:47:25 -0500 | [diff] [blame] | 135 | # Remove the ".tmp" and move the file into position atomically |
| 136 | # Our lock prevents multiple writers but mirroring code may grab incomplete files |
| 137 | os.rename(localpath, localpath[:-4]) |
| 138 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 139 | # Sanity check since wget can pretend it succeed when it didn't |
| 140 | # Also, this used to happen if sourceforge sent us to the mirror page |
| 141 | if not os.path.exists(ud.localpath): |
| 142 | raise FetchError("The fetch command returned success for url %s but %s doesn't exist?!" % (uri, ud.localpath), uri) |
| 143 | |
| 144 | if os.path.getsize(ud.localpath) == 0: |
| 145 | os.remove(ud.localpath) |
| 146 | raise FetchError("The fetch of %s resulted in a zero size file?! Deleting and failing since this isn't right." % (uri), uri) |
| 147 | |
| 148 | return True |
| 149 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 150 | def checkstatus(self, fetch, ud, d, try_again=True): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 151 | class HTTPConnectionCache(http.client.HTTPConnection): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 152 | if fetch.connection_cache: |
| 153 | def connect(self): |
| 154 | """Connect to the host and port specified in __init__.""" |
| 155 | |
| 156 | sock = fetch.connection_cache.get_connection(self.host, self.port) |
| 157 | if sock: |
| 158 | self.sock = sock |
| 159 | else: |
| 160 | self.sock = socket.create_connection((self.host, self.port), |
| 161 | self.timeout, self.source_address) |
| 162 | fetch.connection_cache.add_connection(self.host, self.port, self.sock) |
| 163 | |
| 164 | if self._tunnel_host: |
| 165 | self._tunnel() |
| 166 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 167 | class CacheHTTPHandler(urllib.request.HTTPHandler): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 168 | def http_open(self, req): |
| 169 | return self.do_open(HTTPConnectionCache, req) |
| 170 | |
| 171 | def do_open(self, http_class, req): |
| 172 | """Return an addinfourl object for the request, using http_class. |
| 173 | |
| 174 | http_class must implement the HTTPConnection API from httplib. |
| 175 | The addinfourl return value is a file-like object. It also |
| 176 | has methods and attributes including: |
| 177 | - info(): return a mimetools.Message object for the headers |
| 178 | - geturl(): return the original request URL |
| 179 | - code: HTTP status code |
| 180 | """ |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 181 | host = req.host |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 182 | if not host: |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 183 | raise urllib.error.URLError('no host given') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 184 | |
| 185 | h = http_class(host, timeout=req.timeout) # will parse host:port |
| 186 | h.set_debuglevel(self._debuglevel) |
| 187 | |
| 188 | headers = dict(req.unredirected_hdrs) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 189 | headers.update(dict((k, v) for k, v in list(req.headers.items()) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 190 | if k not in headers)) |
| 191 | |
| 192 | # We want to make an HTTP/1.1 request, but the addinfourl |
| 193 | # class isn't prepared to deal with a persistent connection. |
| 194 | # It will try to read all remaining data from the socket, |
| 195 | # which will block while the server waits for the next request. |
| 196 | # So make sure the connection gets closed after the (only) |
| 197 | # request. |
| 198 | |
| 199 | # Don't close connection when connection_cache is enabled, |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 200 | if fetch.connection_cache is None: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 201 | headers["Connection"] = "close" |
| 202 | else: |
| 203 | headers["Connection"] = "Keep-Alive" # Works for HTTP/1.0 |
| 204 | |
| 205 | headers = dict( |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 206 | (name.title(), val) for name, val in list(headers.items())) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 207 | |
| 208 | if req._tunnel_host: |
| 209 | tunnel_headers = {} |
| 210 | proxy_auth_hdr = "Proxy-Authorization" |
| 211 | if proxy_auth_hdr in headers: |
| 212 | tunnel_headers[proxy_auth_hdr] = headers[proxy_auth_hdr] |
| 213 | # Proxy-Authorization should not be sent to origin |
| 214 | # server. |
| 215 | del headers[proxy_auth_hdr] |
| 216 | h.set_tunnel(req._tunnel_host, headers=tunnel_headers) |
| 217 | |
| 218 | try: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 219 | h.request(req.get_method(), req.selector, req.data, headers) |
| 220 | except socket.error as err: # XXX what error? |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 221 | # Don't close connection when cache is enabled. |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 222 | # Instead, try to detect connections that are no longer |
| 223 | # usable (for example, closed unexpectedly) and remove |
| 224 | # them from the cache. |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 225 | if fetch.connection_cache is None: |
| 226 | h.close() |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 227 | elif isinstance(err, OSError) and err.errno == errno.EBADF: |
| 228 | # This happens when the server closes the connection despite the Keep-Alive. |
| 229 | # Apparently urllib then uses the file descriptor, expecting it to be |
| 230 | # connected, when in reality the connection is already gone. |
| 231 | # We let the request fail and expect it to be |
| 232 | # tried once more ("try_again" in check_status()), |
| 233 | # with the dead connection removed from the cache. |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 234 | # If it still fails, we give up, which can happen for bad |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 235 | # HTTP proxy settings. |
| 236 | fetch.connection_cache.remove_connection(h.host, h.port) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 237 | raise urllib.error.URLError(err) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 238 | else: |
Andrew Geissler | c9f7865 | 2020-09-18 14:11:35 -0500 | [diff] [blame] | 239 | r = h.getresponse() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 240 | |
| 241 | # Pick apart the HTTPResponse object to get the addinfourl |
| 242 | # object initialized properly. |
| 243 | |
| 244 | # Wrap the HTTPResponse object in socket's file object adapter |
| 245 | # for Windows. That adapter calls recv(), so delegate recv() |
| 246 | # to read(). This weird wrapping allows the returned object to |
| 247 | # have readline() and readlines() methods. |
| 248 | |
| 249 | # XXX It might be better to extract the read buffering code |
| 250 | # out of socket._fileobject() and into a base class. |
| 251 | r.recv = r.read |
| 252 | |
| 253 | # no data, just have to read |
| 254 | r.read() |
| 255 | class fp_dummy(object): |
| 256 | def read(self): |
| 257 | return "" |
| 258 | def readline(self): |
| 259 | return "" |
| 260 | def close(self): |
| 261 | pass |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 262 | closed = False |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 263 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 264 | resp = urllib.response.addinfourl(fp_dummy(), r.msg, req.get_full_url()) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 265 | resp.code = r.status |
| 266 | resp.msg = r.reason |
| 267 | |
| 268 | # Close connection when server request it. |
| 269 | if fetch.connection_cache is not None: |
| 270 | if 'Connection' in r.msg and r.msg['Connection'] == 'close': |
| 271 | fetch.connection_cache.remove_connection(h.host, h.port) |
| 272 | |
| 273 | return resp |
| 274 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 275 | class HTTPMethodFallback(urllib.request.BaseHandler): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 276 | """ |
| 277 | Fallback to GET if HEAD is not allowed (405 HTTP error) |
| 278 | """ |
| 279 | def http_error_405(self, req, fp, code, msg, headers): |
| 280 | fp.read() |
| 281 | fp.close() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 282 | |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 283 | if req.get_method() != 'GET': |
| 284 | newheaders = dict((k, v) for k, v in list(req.headers.items()) |
| 285 | if k.lower() not in ("content-length", "content-type")) |
| 286 | return self.parent.open(urllib.request.Request(req.get_full_url(), |
| 287 | headers=newheaders, |
| 288 | origin_req_host=req.origin_req_host, |
| 289 | unverifiable=True)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 290 | |
Brad Bishop | 08902b0 | 2019-08-20 09:16:51 -0400 | [diff] [blame] | 291 | raise urllib.request.HTTPError(req, code, msg, headers, None) |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 292 | |
| 293 | # Some servers (e.g. GitHub archives, hosted on Amazon S3) return 403 |
| 294 | # Forbidden when they actually mean 405 Method Not Allowed. |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 295 | http_error_403 = http_error_405 |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 296 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 297 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 298 | class FixedHTTPRedirectHandler(urllib.request.HTTPRedirectHandler): |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 299 | """ |
| 300 | urllib2.HTTPRedirectHandler resets the method to GET on redirect, |
| 301 | when we want to follow redirects using the original method. |
| 302 | """ |
| 303 | def redirect_request(self, req, fp, code, msg, headers, newurl): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 304 | newreq = urllib.request.HTTPRedirectHandler.redirect_request(self, req, fp, code, msg, headers, newurl) |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 305 | newreq.get_method = req.get_method |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 306 | return newreq |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 307 | |
Patrick Williams | 0ca19cc | 2021-08-16 14:03:13 -0500 | [diff] [blame] | 308 | # We need to update the environment here as both the proxy and HTTPS |
| 309 | # handlers need variables set. The proxy needs http_proxy and friends to |
| 310 | # be set, and HTTPSHandler ends up calling into openssl to load the |
| 311 | # certificates. In buildtools configurations this will be looking at the |
| 312 | # wrong place for certificates by default: we set SSL_CERT_FILE to the |
| 313 | # right location in the buildtools environment script but as BitBake |
| 314 | # prunes prunes the environment this is lost. When binaries are executed |
| 315 | # runfetchcmd ensures these values are in the environment, but this is |
| 316 | # pure Python so we need to update the environment. |
| 317 | # |
| 318 | # Avoid tramping the environment too much by using bb.utils.environment |
| 319 | # to scope the changes to the build_opener request, which is when the |
| 320 | # environment lookups happen. |
Andrew Geissler | 7e0e3c0 | 2022-02-25 20:34:39 +0000 | [diff] [blame] | 321 | newenv = bb.fetch2.get_fetcher_environment(d) |
Patrick Williams | 0ca19cc | 2021-08-16 14:03:13 -0500 | [diff] [blame] | 322 | |
| 323 | with bb.utils.environment(**newenv): |
| 324 | import ssl |
| 325 | |
| 326 | if self.check_certs(d): |
| 327 | context = ssl.create_default_context() |
| 328 | else: |
| 329 | context = ssl._create_unverified_context() |
| 330 | |
| 331 | handlers = [FixedHTTPRedirectHandler, |
| 332 | HTTPMethodFallback, |
| 333 | urllib.request.ProxyHandler(), |
| 334 | CacheHTTPHandler(), |
| 335 | urllib.request.HTTPSHandler(context=context)] |
| 336 | opener = urllib.request.build_opener(*handlers) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 337 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 338 | try: |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 339 | uri = ud.url.split(";")[0] |
| 340 | r = urllib.request.Request(uri) |
| 341 | r.get_method = lambda: "HEAD" |
| 342 | # Some servers (FusionForge, as used on Alioth) require that the |
| 343 | # optional Accept header is set. |
| 344 | r.add_header("Accept", "*/*") |
| 345 | r.add_header("User-Agent", self.user_agent) |
| 346 | def add_basic_auth(login_str, request): |
| 347 | '''Adds Basic auth to http request, pass in login:password as string''' |
| 348 | import base64 |
| 349 | encodeuser = base64.b64encode(login_str.encode('utf-8')).decode("utf-8") |
| 350 | authheader = "Basic %s" % encodeuser |
| 351 | r.add_header("Authorization", authheader) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 352 | |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 353 | if ud.user and ud.pswd: |
| 354 | add_basic_auth(ud.user + ':' + ud.pswd, r) |
| 355 | |
| 356 | try: |
| 357 | import netrc |
| 358 | n = netrc.netrc() |
| 359 | login, unused, password = n.authenticators(urllib.parse.urlparse(uri).hostname) |
| 360 | add_basic_auth("%s:%s" % (login, password), r) |
| 361 | except (TypeError, ImportError, IOError, netrc.NetrcParseError): |
| 362 | pass |
| 363 | |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 364 | with opener.open(r, timeout=30) as response: |
Andrew Geissler | d159c7f | 2021-09-02 21:05:58 -0500 | [diff] [blame] | 365 | pass |
| 366 | except urllib.error.URLError as e: |
| 367 | if try_again: |
| 368 | logger.debug2("checkstatus: trying again") |
| 369 | return self.checkstatus(fetch, ud, d, False) |
| 370 | else: |
| 371 | # debug for now to avoid spamming the logs in e.g. remote sstate searches |
| 372 | logger.debug2("checkstatus() urlopen failed: %s" % e) |
| 373 | return False |
| 374 | except ConnectionResetError as e: |
| 375 | if try_again: |
| 376 | logger.debug2("checkstatus: trying again") |
| 377 | return self.checkstatus(fetch, ud, d, False) |
| 378 | else: |
| 379 | # debug for now to avoid spamming the logs in e.g. remote sstate searches |
| 380 | logger.debug2("checkstatus() urlopen failed: %s" % e) |
| 381 | return False |
| 382 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 383 | return True |
| 384 | |
| 385 | def _parse_path(self, regex, s): |
| 386 | """ |
| 387 | Find and group name, version and archive type in the given string s |
| 388 | """ |
| 389 | |
| 390 | m = regex.search(s) |
| 391 | if m: |
| 392 | pname = '' |
| 393 | pver = '' |
| 394 | ptype = '' |
| 395 | |
| 396 | mdict = m.groupdict() |
| 397 | if 'name' in mdict.keys(): |
| 398 | pname = mdict['name'] |
| 399 | if 'pver' in mdict.keys(): |
| 400 | pver = mdict['pver'] |
| 401 | if 'type' in mdict.keys(): |
| 402 | ptype = mdict['type'] |
| 403 | |
| 404 | bb.debug(3, "_parse_path: %s, %s, %s" % (pname, pver, ptype)) |
| 405 | |
| 406 | return (pname, pver, ptype) |
| 407 | |
| 408 | return None |
| 409 | |
| 410 | def _modelate_version(self, version): |
| 411 | if version[0] in ['.', '-']: |
| 412 | if version[1].isdigit(): |
| 413 | version = version[1] + version[0] + version[2:len(version)] |
| 414 | else: |
| 415 | version = version[1:len(version)] |
| 416 | |
| 417 | version = re.sub('-', '.', version) |
| 418 | version = re.sub('_', '.', version) |
| 419 | version = re.sub('(rc)+', '.1000.', version) |
| 420 | version = re.sub('(beta)+', '.100.', version) |
| 421 | version = re.sub('(alpha)+', '.10.', version) |
| 422 | if version[0] == 'v': |
| 423 | version = version[1:len(version)] |
| 424 | return version |
| 425 | |
| 426 | def _vercmp(self, old, new): |
| 427 | """ |
| 428 | Check whether 'new' is newer than 'old' version. We use existing vercmp() for the |
| 429 | purpose. PE is cleared in comparison as it's not for build, and PR is cleared too |
| 430 | for simplicity as it's somehow difficult to get from various upstream format |
| 431 | """ |
| 432 | |
| 433 | (oldpn, oldpv, oldsuffix) = old |
| 434 | (newpn, newpv, newsuffix) = new |
| 435 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 436 | # Check for a new suffix type that we have never heard of before |
| 437 | if newsuffix: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 438 | m = self.suffix_regex_comp.search(newsuffix) |
| 439 | if not m: |
| 440 | bb.warn("%s has a possible unknown suffix: %s" % (newpn, newsuffix)) |
| 441 | return False |
| 442 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 443 | # Not our package so ignore it |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 444 | if oldpn != newpn: |
| 445 | return False |
| 446 | |
| 447 | oldpv = self._modelate_version(oldpv) |
| 448 | newpv = self._modelate_version(newpv) |
| 449 | |
| 450 | return bb.utils.vercmp(("0", oldpv, ""), ("0", newpv, "")) |
| 451 | |
| 452 | def _fetch_index(self, uri, ud, d): |
| 453 | """ |
| 454 | Run fetch checkstatus to get directory information |
| 455 | """ |
| 456 | f = tempfile.NamedTemporaryFile() |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 457 | with tempfile.TemporaryDirectory(prefix="wget-index-") as workdir, tempfile.NamedTemporaryFile(dir=workdir, prefix="wget-listing-") as f: |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 458 | fetchcmd = self.basecmd |
Andrew Geissler | d1e8949 | 2021-02-12 15:35:20 -0600 | [diff] [blame] | 459 | fetchcmd += " -O " + f.name + " --user-agent='" + self.user_agent + "' '" + uri + "'" |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 460 | try: |
| 461 | self._runwget(ud, d, fetchcmd, True, workdir=workdir) |
| 462 | fetchresult = f.read() |
| 463 | except bb.fetch2.BBFetchException: |
| 464 | fetchresult = "" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 465 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 466 | return fetchresult |
| 467 | |
| 468 | def _check_latest_version(self, url, package, package_regex, current_version, ud, d): |
| 469 | """ |
| 470 | Return the latest version of a package inside a given directory path |
| 471 | If error or no version, return "" |
| 472 | """ |
| 473 | valid = 0 |
| 474 | version = ['', '', ''] |
| 475 | |
| 476 | bb.debug(3, "VersionURL: %s" % (url)) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 477 | soup = BeautifulSoup(self._fetch_index(url, ud, d), "html.parser", parse_only=SoupStrainer("a")) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 478 | if not soup: |
| 479 | bb.debug(3, "*** %s NO SOUP" % (url)) |
| 480 | return "" |
| 481 | |
| 482 | for line in soup.find_all('a', href=True): |
| 483 | bb.debug(3, "line['href'] = '%s'" % (line['href'])) |
| 484 | bb.debug(3, "line = '%s'" % (str(line))) |
| 485 | |
| 486 | newver = self._parse_path(package_regex, line['href']) |
| 487 | if not newver: |
| 488 | newver = self._parse_path(package_regex, str(line)) |
| 489 | |
| 490 | if newver: |
| 491 | bb.debug(3, "Upstream version found: %s" % newver[1]) |
| 492 | if valid == 0: |
| 493 | version = newver |
| 494 | valid = 1 |
| 495 | elif self._vercmp(version, newver) < 0: |
| 496 | version = newver |
| 497 | |
| 498 | pupver = re.sub('_', '.', version[1]) |
| 499 | |
| 500 | bb.debug(3, "*** %s -> UpstreamVersion = %s (CurrentVersion = %s)" % |
| 501 | (package, pupver or "N/A", current_version[1])) |
| 502 | |
| 503 | if valid: |
| 504 | return pupver |
| 505 | |
| 506 | return "" |
| 507 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 508 | def _check_latest_version_by_dir(self, dirver, package, package_regex, current_version, ud, d): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 509 | """ |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 510 | Scan every directory in order to get upstream version. |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 511 | """ |
| 512 | version_dir = ['', '', ''] |
| 513 | version = ['', '', ''] |
| 514 | |
William A. Kennington III | ac69b48 | 2021-06-02 12:28:27 -0700 | [diff] [blame] | 515 | dirver_regex = re.compile(r"(?P<pfx>\D*)(?P<ver>(\d+[\.\-_])*(\d+))") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 516 | s = dirver_regex.search(dirver) |
| 517 | if s: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 518 | version_dir[1] = s.group('ver') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 519 | else: |
| 520 | version_dir[1] = dirver |
| 521 | |
| 522 | dirs_uri = bb.fetch.encodeurl([ud.type, ud.host, |
| 523 | ud.path.split(dirver)[0], ud.user, ud.pswd, {}]) |
| 524 | bb.debug(3, "DirURL: %s, %s" % (dirs_uri, package)) |
| 525 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 526 | soup = BeautifulSoup(self._fetch_index(dirs_uri, ud, d), "html.parser", parse_only=SoupStrainer("a")) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 527 | if not soup: |
| 528 | return version[1] |
| 529 | |
| 530 | for line in soup.find_all('a', href=True): |
| 531 | s = dirver_regex.search(line['href'].strip("/")) |
| 532 | if s: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 533 | sver = s.group('ver') |
| 534 | |
| 535 | # When prefix is part of the version directory it need to |
| 536 | # ensure that only version directory is used so remove previous |
| 537 | # directories if exists. |
| 538 | # |
| 539 | # Example: pfx = '/dir1/dir2/v' and version = '2.5' the expected |
| 540 | # result is v2.5. |
| 541 | spfx = s.group('pfx').split('/')[-1] |
| 542 | |
| 543 | version_dir_new = ['', sver, ''] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 544 | if self._vercmp(version_dir, version_dir_new) <= 0: |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 545 | dirver_new = spfx + sver |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 546 | path = ud.path.replace(dirver, dirver_new, True) \ |
| 547 | .split(package)[0] |
| 548 | uri = bb.fetch.encodeurl([ud.type, ud.host, path, |
| 549 | ud.user, ud.pswd, {}]) |
| 550 | |
| 551 | pupver = self._check_latest_version(uri, |
| 552 | package, package_regex, current_version, ud, d) |
| 553 | if pupver: |
| 554 | version[1] = pupver |
| 555 | |
| 556 | version_dir = version_dir_new |
| 557 | |
| 558 | return version[1] |
| 559 | |
| 560 | def _init_regexes(self, package, ud, d): |
| 561 | """ |
| 562 | Match as many patterns as possible such as: |
| 563 | gnome-common-2.20.0.tar.gz (most common format) |
| 564 | gtk+-2.90.1.tar.gz |
| 565 | xf86-input-synaptics-12.6.9.tar.gz |
| 566 | dri2proto-2.3.tar.gz |
| 567 | blktool_4.orig.tar.gz |
| 568 | libid3tag-0.15.1b.tar.gz |
| 569 | unzip552.tar.gz |
| 570 | icu4c-3_6-src.tgz |
| 571 | genext2fs_1.3.orig.tar.gz |
| 572 | gst-fluendo-mp3 |
| 573 | """ |
| 574 | # match most patterns which uses "-" as separator to version digits |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 575 | pn_prefix1 = r"[a-zA-Z][a-zA-Z0-9]*([-_][a-zA-Z]\w+)*\+?[-_]" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 576 | # a loose pattern such as for unzip552.tar.gz |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 577 | pn_prefix2 = r"[a-zA-Z]+" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 578 | # a loose pattern such as for 80325-quicky-0.4.tar.gz |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 579 | pn_prefix3 = r"[0-9]+[-]?[a-zA-Z]+" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 580 | # Save the Package Name (pn) Regex for use later |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 581 | pn_regex = r"(%s|%s|%s)" % (pn_prefix1, pn_prefix2, pn_prefix3) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 582 | |
| 583 | # match version |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 584 | pver_regex = r"(([A-Z]*\d+[a-zA-Z]*[\.\-_]*)+)" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 585 | |
| 586 | # match arch |
| 587 | parch_regex = "-source|_all_" |
| 588 | |
| 589 | # src.rpm extension was added only for rpm package. Can be removed if the rpm |
| 590 | # packaged will always be considered as having to be manually upgraded |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 591 | psuffix_regex = r"(tar\.\w+|tgz|zip|xz|rpm|bz2|orig\.tar\.\w+|src\.tar\.\w+|src\.tgz|svnr\d+\.tar\.\w+|stable\.tar\.\w+|src\.rpm)" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 592 | |
| 593 | # match name, version and archive type of a package |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 594 | package_regex_comp = re.compile(r"(?P<name>%s?\.?v?)(?P<pver>%s)(?P<arch>%s)?[\.-](?P<type>%s$)" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 595 | % (pn_regex, pver_regex, parch_regex, psuffix_regex)) |
| 596 | self.suffix_regex_comp = re.compile(psuffix_regex) |
| 597 | |
| 598 | # compile regex, can be specific by package or generic regex |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 599 | pn_regex = d.getVar('UPSTREAM_CHECK_REGEX') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 600 | if pn_regex: |
| 601 | package_custom_regex_comp = re.compile(pn_regex) |
| 602 | else: |
| 603 | version = self._parse_path(package_regex_comp, package) |
| 604 | if version: |
| 605 | package_custom_regex_comp = re.compile( |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 606 | r"(?P<name>%s)(?P<pver>%s)(?P<arch>%s)?[\.-](?P<type>%s)" % |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 607 | (re.escape(version[0]), pver_regex, parch_regex, psuffix_regex)) |
| 608 | else: |
| 609 | package_custom_regex_comp = None |
| 610 | |
| 611 | return package_custom_regex_comp |
| 612 | |
| 613 | def latest_versionstring(self, ud, d): |
| 614 | """ |
| 615 | Manipulate the URL and try to obtain the latest package version |
| 616 | |
| 617 | sanity check to ensure same name and type. |
| 618 | """ |
| 619 | package = ud.path.split("/")[-1] |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 620 | current_version = ['', d.getVar('PV'), ''] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 621 | |
| 622 | """possible to have no version in pkg name, such as spectrum-fw""" |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 623 | if not re.search(r"\d+", package): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 624 | current_version[1] = re.sub('_', '.', current_version[1]) |
| 625 | current_version[1] = re.sub('-', '.', current_version[1]) |
| 626 | return (current_version[1], '') |
| 627 | |
| 628 | package_regex = self._init_regexes(package, ud, d) |
| 629 | if package_regex is None: |
| 630 | bb.warn("latest_versionstring: package %s don't match pattern" % (package)) |
| 631 | return ('', '') |
| 632 | bb.debug(3, "latest_versionstring, regex: %s" % (package_regex.pattern)) |
| 633 | |
| 634 | uri = "" |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 635 | regex_uri = d.getVar("UPSTREAM_CHECK_URI") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 636 | if not regex_uri: |
| 637 | path = ud.path.split(package)[0] |
| 638 | |
| 639 | # search for version matches on folders inside the path, like: |
| 640 | # "5.7" in http://download.gnome.org/sources/${PN}/5.7/${PN}-${PV}.tar.gz |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 641 | dirver_regex = re.compile(r"(?P<dirver>[^/]*(\d+\.)*\d+([-_]r\d+)*)/") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 642 | m = dirver_regex.search(path) |
| 643 | if m: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 644 | pn = d.getVar('PN') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 645 | dirver = m.group('dirver') |
| 646 | |
Brad Bishop | 1932369 | 2019-04-05 15:28:33 -0400 | [diff] [blame] | 647 | dirver_pn_regex = re.compile(r"%s\d?" % (re.escape(pn))) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 648 | if not dirver_pn_regex.search(dirver): |
| 649 | return (self._check_latest_version_by_dir(dirver, |
| 650 | package, package_regex, current_version, ud, d), '') |
| 651 | |
| 652 | uri = bb.fetch.encodeurl([ud.type, ud.host, path, ud.user, ud.pswd, {}]) |
| 653 | else: |
| 654 | uri = regex_uri |
| 655 | |
| 656 | return (self._check_latest_version(uri, package, package_regex, |
| 657 | current_version, ud, d), '') |