blob: a114ac12e57d262f3fd6a09aac84217b28ab11fb [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001# ex:ts=4:sw=4:sts=4:et
2# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
3"""
4BitBake 'Fetch' implementations
5
6Classes for obtaining upstream sources for the
7BitBake 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
28import os
29import urllib.request, urllib.parse, urllib.error
30import bb
31import bb.utils
32from bb.fetch2 import FetchMethod, FetchError
33from bb.fetch2 import logger
34
35class 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!
44 ud.decodedurl = urllib.parse.unquote(ud.url.split("://")[1].split(";")[0])
45 ud.basename = os.path.basename(ud.decodedurl)
46 ud.basepath = ud.decodedurl
47 ud.needdonestamp = False
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]
65 filespath = d.getVar('FILESPATH')
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)
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):
77 dldirfile = os.path.join(d.getVar("DL_DIR"), path)
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 = []
96 filespath = d.getVar('FILESPATH')
97 if filespath:
98 locations = filespath.split(":")
99 locations.append(d.getVar("DL_DIR"))
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