local.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # ex:ts=4:sw=4:sts=4:et
  2. # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
  3. """
  4. BitBake 'Fetch' implementations
  5. Classes for obtaining upstream sources for the
  6. BitBake build tools.
  7. """
  8. # Copyright (C) 2003, 2004 Chris Larson
  9. #
  10. # SPDX-License-Identifier: GPL-2.0-only
  11. #
  12. # This program is free software; you can redistribute it and/or modify
  13. # it under the terms of the GNU General Public License version 2 as
  14. # published by the Free Software Foundation.
  15. #
  16. # This program is distributed in the hope that it will be useful,
  17. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. # GNU General Public License for more details.
  20. #
  21. # You should have received a copy of the GNU General Public License along
  22. # with this program; if not, write to the Free Software Foundation, Inc.,
  23. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  24. #
  25. # Based on functions from the base bb module, Copyright 2003 Holger Schurig
  26. import os
  27. import urllib.request, urllib.parse, urllib.error
  28. import bb
  29. import bb.utils
  30. from bb.fetch2 import FetchMethod, FetchError
  31. from bb.fetch2 import logger
  32. class Local(FetchMethod):
  33. def supports(self, urldata, d):
  34. """
  35. Check to see if a given url represents a local fetch.
  36. """
  37. return urldata.type in ['file']
  38. def urldata_init(self, ud, d):
  39. # We don't set localfile as for this fetcher the file is already local!
  40. ud.decodedurl = urllib.parse.unquote(ud.url.split("://")[1].split(";")[0])
  41. ud.basename = os.path.basename(ud.decodedurl)
  42. ud.basepath = ud.decodedurl
  43. ud.needdonestamp = False
  44. return
  45. def localpath(self, urldata, d):
  46. """
  47. Return the local filename of a given url assuming a successful fetch.
  48. """
  49. return self.localpaths(urldata, d)[-1]
  50. def localpaths(self, urldata, d):
  51. """
  52. Return the local filename of a given url assuming a successful fetch.
  53. """
  54. searched = []
  55. path = urldata.decodedurl
  56. newpath = path
  57. if path[0] == "/":
  58. return [path]
  59. filespath = d.getVar('FILESPATH')
  60. if filespath:
  61. logger.debug(2, "Searching for %s in paths:\n %s" % (path, "\n ".join(filespath.split(":"))))
  62. newpath, hist = bb.utils.which(filespath, path, history=True)
  63. searched.extend(hist)
  64. if (not newpath or not os.path.exists(newpath)) and path.find("*") != -1:
  65. # For expressions using '*', best we can do is take the first directory in FILESPATH that exists
  66. newpath, hist = bb.utils.which(filespath, ".", history=True)
  67. searched.extend(hist)
  68. logger.debug(2, "Searching for %s in path: %s" % (path, newpath))
  69. return searched
  70. if not os.path.exists(newpath):
  71. dldirfile = os.path.join(d.getVar("DL_DIR"), path)
  72. logger.debug(2, "Defaulting to %s for %s" % (dldirfile, path))
  73. bb.utils.mkdirhier(os.path.dirname(dldirfile))
  74. searched.append(dldirfile)
  75. return searched
  76. return searched
  77. def need_update(self, ud, d):
  78. if ud.url.find("*") != -1:
  79. return False
  80. if os.path.exists(ud.localpath):
  81. return False
  82. return True
  83. def download(self, urldata, d):
  84. """Fetch urls (no-op for Local method)"""
  85. # no need to fetch local files, we'll deal with them in place.
  86. if self.supports_checksum(urldata) and not os.path.exists(urldata.localpath):
  87. locations = []
  88. filespath = d.getVar('FILESPATH')
  89. if filespath:
  90. locations = filespath.split(":")
  91. locations.append(d.getVar("DL_DIR"))
  92. msg = "Unable to find file " + urldata.url + " anywhere. The paths that were searched were:\n " + "\n ".join(locations)
  93. raise FetchError(msg)
  94. return True
  95. def checkstatus(self, fetch, urldata, d):
  96. """
  97. Check the status of the url
  98. """
  99. if urldata.localpath.find("*") != -1:
  100. logger.info("URL %s looks like a glob and was therefore not checked.", urldata.url)
  101. return True
  102. if os.path.exists(urldata.localpath):
  103. return True
  104. return False
  105. def clean(self, urldata, d):
  106. return