xref: /openbmc/openbmc/poky/bitbake/lib/bb/fetch2/local.py (revision 8c226234)
1"""
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#
11# SPDX-License-Identifier: GPL-2.0-only
12#
13# Based on functions from the base bb module, Copyright 2003 Holger Schurig
14#
15
16import os
17import urllib.request, urllib.parse, urllib.error
18import bb
19import bb.utils
20from   bb.fetch2 import FetchMethod, FetchError, ParameterError
21from   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!
32        ud.decodedurl = urllib.parse.unquote(ud.url.split("://")[1].split(";")[0])
33        ud.basename = os.path.basename(ud.decodedurl)
34        ud.basepath = ud.decodedurl
35        ud.needdonestamp = False
36        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)
38        return
39
40    def localpath(self, urldata, d):
41        """
42        Return the local filename of a given url assuming a successful fetch.
43        """
44        return self.localpaths(urldata, d)[-1]
45
46    def localpaths(self, urldata, d):
47        """
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] == "/":
54            return [path]
55        filespath = d.getVar('FILESPATH')
56        if filespath:
57            logger.debug2("Searching for %s in paths:\n    %s" % (path, "\n    ".join(filespath.split(":"))))
58            newpath, hist = bb.utils.which(filespath, path, history=True)
59            searched.extend(hist)
60        return searched
61
62    def need_update(self, ud, d):
63        if os.path.exists(ud.localpath):
64            return False
65        return True
66
67    def download(self, urldata, d):
68        """Fetch urls (no-op for Local method)"""
69        # no need to fetch local files, we'll deal with them in place.
70        if self.supports_checksum(urldata) and not os.path.exists(urldata.localpath):
71            locations = []
72            filespath = d.getVar('FILESPATH')
73            if filespath:
74                locations = filespath.split(":")
75            msg = "Unable to find file " + urldata.url + " anywhere to download to " + urldata.localpath + ". The paths that were searched were:\n    " + "\n    ".join(locations)
76            raise FetchError(msg)
77
78        return True
79
80    def checkstatus(self, fetch, urldata, d):
81        """
82        Check the status of the url
83        """
84        if os.path.exists(urldata.localpath):
85            return True
86        return False
87
88    def clean(self, urldata, d):
89        return
90
91