blob: a114ac12e57d262f3fd6a09aac84217b28ab11fb [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001# 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
Patrick Williamsc0f7c042017-02-23 20:41:17 -060029import urllib.request, urllib.parse, urllib.error
Patrick Williamsc124f4f2015-09-15 14:41:29 -050030import bb
31import bb.utils
Patrick Williamsc124f4f2015-09-15 14:41:29 -050032from 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!
Patrick Williamsc0f7c042017-02-23 20:41:17 -060044 ud.decodedurl = urllib.parse.unquote(ud.url.split("://")[1].split(";")[0])
Patrick Williamsc124f4f2015-09-15 14:41:29 -050045 ud.basename = os.path.basename(ud.decodedurl)
46 ud.basepath = ud.decodedurl
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050047 ud.needdonestamp = False
Patrick Williamsc124f4f2015-09-15 14:41:29 -050048 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 Bishop6e60e8b2018-02-01 10:27:11 -050065 filespath = d.getVar('FILESPATH')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050066 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 Williamsc124f4f2015-09-15 14:41:29 -050070 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 Bishop6e60e8b2018-02-01 10:27:11 -050077 dldirfile = os.path.join(d.getVar("DL_DIR"), path)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050078 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 Bishop6e60e8b2018-02-01 10:27:11 -050096 filespath = d.getVar('FILESPATH')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050097 if filespath:
98 locations = filespath.split(":")
Brad Bishop6e60e8b2018-02-01 10:27:11 -050099 locations.append(d.getVar("DL_DIR"))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500100
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