blob: 7d7668110efa6a8f7e0442f4380f19a3ae9773e7 [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
9# Copyright (C) 2003, 2004 Chris Larson
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
Brad Bishopc342db32019-05-15 21:57:59 -040014#
Patrick Williamsc124f4f2015-09-15 14:41:29 -050015
16import os
Patrick Williamsc0f7c042017-02-23 20:41:17 -060017import urllib.request, urllib.parse, urllib.error
Patrick Williamsc124f4f2015-09-15 14:41:29 -050018import bb
19import bb.utils
Andrew Geisslerc9f78652020-09-18 14:11:35 -050020from bb.fetch2 import FetchMethod, FetchError, ParameterError
Patrick Williamsc124f4f2015-09-15 14:41:29 -050021from bb.fetch2 import logger
22
23class 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 Williamsc0f7c042017-02-23 20:41:17 -060032 ud.decodedurl = urllib.parse.unquote(ud.url.split("://")[1].split(";")[0])
Patrick Williamsc124f4f2015-09-15 14:41:29 -050033 ud.basename = os.path.basename(ud.decodedurl)
34 ud.basepath = ud.decodedurl
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050035 ud.needdonestamp = False
Andrew Geisslerc9f78652020-09-18 14:11:35 -050036 if "*" in ud.decodedurl:
37 raise bb.fetch2.ParameterError("file:// urls using globbing are no longer supported. Please place the files in a directory and reference that instead.", ud.url)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050038 return
39
40 def localpath(self, urldata, d):
41 """
42 Return the local filename of a given url assuming a successful fetch.
43 """
Patrick Williamse760df82023-05-26 11:10:49 -050044 return self.localfile_searchpaths(urldata, d)[-1]
Patrick Williamsc124f4f2015-09-15 14:41:29 -050045
Patrick Williamse760df82023-05-26 11:10:49 -050046 def localfile_searchpaths(self, urldata, d):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050047 """
48 Return the local filename of a given url assuming a successful fetch.
49 """
50 searched = []
51 path = urldata.decodedurl
52 newpath = path
53 if path[0] == "/":
Patrick Williamse760df82023-05-26 11:10:49 -050054 logger.debug2("Using absolute %s" % (path))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050055 return [path]
Brad Bishop6e60e8b2018-02-01 10:27:11 -050056 filespath = d.getVar('FILESPATH')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050057 if filespath:
Andrew Geisslerd1e89492021-02-12 15:35:20 -060058 logger.debug2("Searching for %s in paths:\n %s" % (path, "\n ".join(filespath.split(":"))))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050059 newpath, hist = bb.utils.which(filespath, path, history=True)
Patrick Williamse760df82023-05-26 11:10:49 -050060 logger.debug2("Using %s for %s" % (newpath, path))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050061 searched.extend(hist)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050062 return searched
63
64 def need_update(self, ud, d):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050065 if os.path.exists(ud.localpath):
66 return False
67 return True
68
69 def download(self, urldata, d):
70 """Fetch urls (no-op for Local method)"""
71 # no need to fetch local files, we'll deal with them in place.
72 if self.supports_checksum(urldata) and not os.path.exists(urldata.localpath):
73 locations = []
Brad Bishop6e60e8b2018-02-01 10:27:11 -050074 filespath = d.getVar('FILESPATH')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050075 if filespath:
76 locations = filespath.split(":")
Andrew Geisslerfc113ea2023-03-31 09:59:46 -050077 msg = "Unable to find file " + urldata.url + " anywhere to download to " + urldata.localpath + ". The paths that were searched were:\n " + "\n ".join(locations)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050078 raise FetchError(msg)
79
80 return True
81
82 def checkstatus(self, fetch, urldata, d):
83 """
84 Check the status of the url
85 """
Patrick Williamsc124f4f2015-09-15 14:41:29 -050086 if os.path.exists(urldata.localpath):
87 return True
88 return False
89
90 def clean(self, urldata, d):
91 return
92