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 |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame^] | 14 | # |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 15 | |
| 16 | import os |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 17 | import urllib.request, urllib.parse, urllib.error |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 18 | import bb |
| 19 | import bb.utils |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 20 | from bb.fetch2 import FetchMethod, FetchError |
| 21 | from bb.fetch2 import logger |
| 22 | |
| 23 | class Local(FetchMethod): |
| 24 | def supports(self, urldata, d): |
| 25 | """ |
| 26 | Check to see if a given url represents a local fetch. |
| 27 | """ |
| 28 | return urldata.type in ['file'] |
| 29 | |
| 30 | def urldata_init(self, ud, d): |
| 31 | # We don't set localfile as for this fetcher the file is already local! |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 32 | ud.decodedurl = urllib.parse.unquote(ud.url.split("://")[1].split(";")[0]) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 33 | ud.basename = os.path.basename(ud.decodedurl) |
| 34 | ud.basepath = ud.decodedurl |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 35 | ud.needdonestamp = False |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 36 | return |
| 37 | |
| 38 | def localpath(self, urldata, d): |
| 39 | """ |
| 40 | Return the local filename of a given url assuming a successful fetch. |
| 41 | """ |
| 42 | return self.localpaths(urldata, d)[-1] |
| 43 | |
| 44 | def localpaths(self, urldata, d): |
| 45 | """ |
| 46 | Return the local filename of a given url assuming a successful fetch. |
| 47 | """ |
| 48 | searched = [] |
| 49 | path = urldata.decodedurl |
| 50 | newpath = path |
| 51 | if path[0] == "/": |
| 52 | return [path] |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 53 | filespath = d.getVar('FILESPATH') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 54 | if filespath: |
| 55 | logger.debug(2, "Searching for %s in paths:\n %s" % (path, "\n ".join(filespath.split(":")))) |
| 56 | newpath, hist = bb.utils.which(filespath, path, history=True) |
| 57 | searched.extend(hist) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 58 | if (not newpath or not os.path.exists(newpath)) and path.find("*") != -1: |
| 59 | # For expressions using '*', best we can do is take the first directory in FILESPATH that exists |
| 60 | newpath, hist = bb.utils.which(filespath, ".", history=True) |
| 61 | searched.extend(hist) |
| 62 | logger.debug(2, "Searching for %s in path: %s" % (path, newpath)) |
| 63 | return searched |
| 64 | if not os.path.exists(newpath): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 65 | dldirfile = os.path.join(d.getVar("DL_DIR"), path) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 66 | logger.debug(2, "Defaulting to %s for %s" % (dldirfile, path)) |
| 67 | bb.utils.mkdirhier(os.path.dirname(dldirfile)) |
| 68 | searched.append(dldirfile) |
| 69 | return searched |
| 70 | return searched |
| 71 | |
| 72 | def need_update(self, ud, d): |
| 73 | if ud.url.find("*") != -1: |
| 74 | return False |
| 75 | if os.path.exists(ud.localpath): |
| 76 | return False |
| 77 | return True |
| 78 | |
| 79 | def download(self, urldata, d): |
| 80 | """Fetch urls (no-op for Local method)""" |
| 81 | # no need to fetch local files, we'll deal with them in place. |
| 82 | if self.supports_checksum(urldata) and not os.path.exists(urldata.localpath): |
| 83 | locations = [] |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 84 | filespath = d.getVar('FILESPATH') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 85 | if filespath: |
| 86 | locations = filespath.split(":") |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 87 | locations.append(d.getVar("DL_DIR")) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 88 | |
| 89 | msg = "Unable to find file " + urldata.url + " anywhere. The paths that were searched were:\n " + "\n ".join(locations) |
| 90 | raise FetchError(msg) |
| 91 | |
| 92 | return True |
| 93 | |
| 94 | def checkstatus(self, fetch, urldata, d): |
| 95 | """ |
| 96 | Check the status of the url |
| 97 | """ |
| 98 | if urldata.localpath.find("*") != -1: |
| 99 | logger.info("URL %s looks like a glob and was therefore not checked.", urldata.url) |
| 100 | return True |
| 101 | if os.path.exists(urldata.localpath): |
| 102 | return True |
| 103 | return False |
| 104 | |
| 105 | def clean(self, urldata, d): |
| 106 | return |
| 107 | |