Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | # ex:ts=4:sw=4:sts=4:et |
| 2 | # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- |
| 3 | """ |
| 4 | BitBake 'Fetch' implementations |
| 5 | |
| 6 | Classes for obtaining upstream sources for the |
| 7 | BitBake build tools. |
| 8 | |
| 9 | """ |
| 10 | |
| 11 | # Copyright (C) 2003, 2004 Chris Larson |
| 12 | # |
| 13 | # This program is free software; you can redistribute it and/or modify |
| 14 | # it under the terms of the GNU General Public License version 2 as |
| 15 | # published by the Free Software Foundation. |
| 16 | # |
| 17 | # This program is distributed in the hope that it will be useful, |
| 18 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | # GNU General Public License for more details. |
| 21 | # |
| 22 | # You should have received a copy of the GNU General Public License along |
| 23 | # with this program; if not, write to the Free Software Foundation, Inc., |
| 24 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 25 | # |
| 26 | # Based on functions from the base bb module, Copyright 2003 Holger Schurig |
| 27 | |
| 28 | import os |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 29 | import urllib.request, urllib.parse, urllib.error |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 30 | import bb |
| 31 | import bb.utils |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 32 | from bb.fetch2 import FetchMethod, FetchError |
| 33 | from bb.fetch2 import logger |
| 34 | |
| 35 | class Local(FetchMethod): |
| 36 | def supports(self, urldata, d): |
| 37 | """ |
| 38 | Check to see if a given url represents a local fetch. |
| 39 | """ |
| 40 | return urldata.type in ['file'] |
| 41 | |
| 42 | def urldata_init(self, ud, d): |
| 43 | # 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] | 44 | ud.decodedurl = urllib.parse.unquote(ud.url.split("://")[1].split(";")[0]) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 45 | ud.basename = os.path.basename(ud.decodedurl) |
| 46 | ud.basepath = ud.decodedurl |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 47 | ud.needdonestamp = False |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 48 | return |
| 49 | |
| 50 | def localpath(self, urldata, d): |
| 51 | """ |
| 52 | Return the local filename of a given url assuming a successful fetch. |
| 53 | """ |
| 54 | return self.localpaths(urldata, d)[-1] |
| 55 | |
| 56 | def localpaths(self, urldata, d): |
| 57 | """ |
| 58 | Return the local filename of a given url assuming a successful fetch. |
| 59 | """ |
| 60 | searched = [] |
| 61 | path = urldata.decodedurl |
| 62 | newpath = path |
| 63 | if path[0] == "/": |
| 64 | return [path] |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 65 | filespath = d.getVar('FILESPATH') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 66 | if filespath: |
| 67 | logger.debug(2, "Searching for %s in paths:\n %s" % (path, "\n ".join(filespath.split(":")))) |
| 68 | newpath, hist = bb.utils.which(filespath, path, history=True) |
| 69 | searched.extend(hist) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 70 | if (not newpath or not os.path.exists(newpath)) and path.find("*") != -1: |
| 71 | # For expressions using '*', best we can do is take the first directory in FILESPATH that exists |
| 72 | newpath, hist = bb.utils.which(filespath, ".", history=True) |
| 73 | searched.extend(hist) |
| 74 | logger.debug(2, "Searching for %s in path: %s" % (path, newpath)) |
| 75 | return searched |
| 76 | if not os.path.exists(newpath): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 77 | dldirfile = os.path.join(d.getVar("DL_DIR"), path) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 78 | logger.debug(2, "Defaulting to %s for %s" % (dldirfile, path)) |
| 79 | bb.utils.mkdirhier(os.path.dirname(dldirfile)) |
| 80 | searched.append(dldirfile) |
| 81 | return searched |
| 82 | return searched |
| 83 | |
| 84 | def need_update(self, ud, d): |
| 85 | if ud.url.find("*") != -1: |
| 86 | return False |
| 87 | if os.path.exists(ud.localpath): |
| 88 | return False |
| 89 | return True |
| 90 | |
| 91 | def download(self, urldata, d): |
| 92 | """Fetch urls (no-op for Local method)""" |
| 93 | # no need to fetch local files, we'll deal with them in place. |
| 94 | if self.supports_checksum(urldata) and not os.path.exists(urldata.localpath): |
| 95 | locations = [] |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 96 | filespath = d.getVar('FILESPATH') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 97 | if filespath: |
| 98 | locations = filespath.split(":") |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 99 | locations.append(d.getVar("DL_DIR")) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 100 | |
| 101 | msg = "Unable to find file " + urldata.url + " anywhere. The paths that were searched were:\n " + "\n ".join(locations) |
| 102 | raise FetchError(msg) |
| 103 | |
| 104 | return True |
| 105 | |
| 106 | def checkstatus(self, fetch, urldata, d): |
| 107 | """ |
| 108 | Check the status of the url |
| 109 | """ |
| 110 | if urldata.localpath.find("*") != -1: |
| 111 | logger.info("URL %s looks like a glob and was therefore not checked.", urldata.url) |
| 112 | return True |
| 113 | if os.path.exists(urldata.localpath): |
| 114 | return True |
| 115 | return False |
| 116 | |
| 117 | def clean(self, urldata, d): |
| 118 | return |
| 119 | |