1eb8dc403SDave Cobbley""" 2eb8dc403SDave CobbleyBitBake 'Fetch' implementations 3eb8dc403SDave Cobbley 4eb8dc403SDave CobbleyClasses for obtaining upstream sources for the 5eb8dc403SDave CobbleyBitBake build tools. 6eb8dc403SDave Cobbley 7eb8dc403SDave Cobbley""" 8eb8dc403SDave Cobbley 9eb8dc403SDave Cobbley# Copyright (C) 2003, 2004 Chris Larson 10eb8dc403SDave Cobbley# 11c342db35SBrad Bishop# SPDX-License-Identifier: GPL-2.0-only 12eb8dc403SDave Cobbley# 13eb8dc403SDave Cobbley# Based on functions from the base bb module, Copyright 2003 Holger Schurig 14c342db35SBrad Bishop# 15eb8dc403SDave Cobbley 16eb8dc403SDave Cobbleyimport os 17eb8dc403SDave Cobbleyimport urllib.request, urllib.parse, urllib.error 18eb8dc403SDave Cobbleyimport bb 19eb8dc403SDave Cobbleyimport bb.utils 20c9f7865aSAndrew Geisslerfrom bb.fetch2 import FetchMethod, FetchError, ParameterError 21eb8dc403SDave Cobbleyfrom bb.fetch2 import logger 22eb8dc403SDave Cobbley 23eb8dc403SDave Cobbleyclass Local(FetchMethod): 24eb8dc403SDave Cobbley def supports(self, urldata, d): 25eb8dc403SDave Cobbley """ 26eb8dc403SDave Cobbley Check to see if a given url represents a local fetch. 27eb8dc403SDave Cobbley """ 28eb8dc403SDave Cobbley return urldata.type in ['file'] 29eb8dc403SDave Cobbley 30eb8dc403SDave Cobbley def urldata_init(self, ud, d): 31eb8dc403SDave Cobbley # We don't set localfile as for this fetcher the file is already local! 32eb8dc403SDave Cobbley ud.decodedurl = urllib.parse.unquote(ud.url.split("://")[1].split(";")[0]) 33eb8dc403SDave Cobbley ud.basename = os.path.basename(ud.decodedurl) 34eb8dc403SDave Cobbley ud.basepath = ud.decodedurl 35eb8dc403SDave Cobbley ud.needdonestamp = False 36c9f7865aSAndrew Geissler if "*" in ud.decodedurl: 37c9f7865aSAndrew Geissler 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) 38eb8dc403SDave Cobbley return 39eb8dc403SDave Cobbley 40eb8dc403SDave Cobbley def localpath(self, urldata, d): 41eb8dc403SDave Cobbley """ 42eb8dc403SDave Cobbley Return the local filename of a given url assuming a successful fetch. 43eb8dc403SDave Cobbley """ 44*e760df85SPatrick Williams return self.localfile_searchpaths(urldata, d)[-1] 45eb8dc403SDave Cobbley 46*e760df85SPatrick Williams def localfile_searchpaths(self, urldata, d): 47eb8dc403SDave Cobbley """ 48eb8dc403SDave Cobbley Return the local filename of a given url assuming a successful fetch. 49eb8dc403SDave Cobbley """ 50eb8dc403SDave Cobbley searched = [] 51eb8dc403SDave Cobbley path = urldata.decodedurl 52eb8dc403SDave Cobbley newpath = path 53eb8dc403SDave Cobbley if path[0] == "/": 54*e760df85SPatrick Williams logger.debug2("Using absolute %s" % (path)) 55eb8dc403SDave Cobbley return [path] 56eb8dc403SDave Cobbley filespath = d.getVar('FILESPATH') 57eb8dc403SDave Cobbley if filespath: 58d1e89497SAndrew Geissler logger.debug2("Searching for %s in paths:\n %s" % (path, "\n ".join(filespath.split(":")))) 59eb8dc403SDave Cobbley newpath, hist = bb.utils.which(filespath, path, history=True) 60*e760df85SPatrick Williams logger.debug2("Using %s for %s" % (newpath, path)) 61eb8dc403SDave Cobbley searched.extend(hist) 62eb8dc403SDave Cobbley return searched 63eb8dc403SDave Cobbley 64eb8dc403SDave Cobbley def need_update(self, ud, d): 65eb8dc403SDave Cobbley if os.path.exists(ud.localpath): 66eb8dc403SDave Cobbley return False 67eb8dc403SDave Cobbley return True 68eb8dc403SDave Cobbley 69eb8dc403SDave Cobbley def download(self, urldata, d): 70eb8dc403SDave Cobbley """Fetch urls (no-op for Local method)""" 71eb8dc403SDave Cobbley # no need to fetch local files, we'll deal with them in place. 72eb8dc403SDave Cobbley if self.supports_checksum(urldata) and not os.path.exists(urldata.localpath): 73eb8dc403SDave Cobbley locations = [] 74eb8dc403SDave Cobbley filespath = d.getVar('FILESPATH') 75eb8dc403SDave Cobbley if filespath: 76eb8dc403SDave Cobbley locations = filespath.split(":") 77fc113eadSAndrew Geissler msg = "Unable to find file " + urldata.url + " anywhere to download to " + urldata.localpath + ". The paths that were searched were:\n " + "\n ".join(locations) 78eb8dc403SDave Cobbley raise FetchError(msg) 79eb8dc403SDave Cobbley 80eb8dc403SDave Cobbley return True 81eb8dc403SDave Cobbley 82eb8dc403SDave Cobbley def checkstatus(self, fetch, urldata, d): 83eb8dc403SDave Cobbley """ 84eb8dc403SDave Cobbley Check the status of the url 85eb8dc403SDave Cobbley """ 86eb8dc403SDave Cobbley if os.path.exists(urldata.localpath): 87eb8dc403SDave Cobbley return True 88eb8dc403SDave Cobbley return False 89eb8dc403SDave Cobbley 90eb8dc403SDave Cobbley def clean(self, urldata, d): 91eb8dc403SDave Cobbley return 92eb8dc403SDave Cobbley 93