xref: /openbmc/openbmc/poky/bitbake/lib/bb/fetch2/local.py (revision 43a6b7c2a48b0cb1381af4d3192d22a12ead65f0)
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.basename = os.path.basename(ud.path)
33        ud.basepath = ud.path
34        ud.needdonestamp = False
35        if "*" in ud.path:
36            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)
37        return
38
39    def localpath(self, urldata, d):
40        """
41        Return the local filename of a given url assuming a successful fetch.
42        """
43        return self.localfile_searchpaths(urldata, d)[-1]
44
45    def localfile_searchpaths(self, urldata, d):
46        """
47        Return the local filename of a given url assuming a successful fetch.
48        """
49        searched = []
50        path = urldata.path
51        newpath = path
52        if path[0] == "/":
53            logger.debug2("Using absolute %s" % (path))
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            logger.debug2("Using %s for %s" % (newpath, path))
60            searched.extend(hist)
61        return searched
62
63    def need_update(self, ud, d):
64        if os.path.exists(ud.localpath):
65            return False
66        return True
67
68    def download(self, urldata, d):
69        """Fetch urls (no-op for Local method)"""
70        # no need to fetch local files, we'll deal with them in place.
71        if self.supports_checksum(urldata) and not os.path.exists(urldata.localpath):
72            locations = []
73            filespath = d.getVar('FILESPATH')
74            if filespath:
75                locations = filespath.split(":")
76            msg = "Unable to find file " + urldata.url + " anywhere to download to " + urldata.localpath + ". The paths that were searched were:\n    " + "\n    ".join(locations)
77            raise FetchError(msg)
78
79        return True
80
81    def checkstatus(self, fetch, urldata, d):
82        """
83        Check the status of the url
84        """
85        if os.path.exists(urldata.localpath):
86            return True
87        return False
88
89    def clean(self, urldata, d):
90        return
91
92