local.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. """
  2. BitBake 'Fetch' implementations
  3. Classes for obtaining upstream sources for the
  4. BitBake build tools.
  5. """
  6. # Copyright (C) 2003, 2004 Chris Larson
  7. #
  8. # SPDX-License-Identifier: GPL-2.0-only
  9. #
  10. # Based on functions from the base bb module, Copyright 2003 Holger Schurig
  11. #
  12. import os
  13. import urllib.request, urllib.parse, urllib.error
  14. import bb
  15. import bb.utils
  16. from bb.fetch2 import FetchMethod, FetchError
  17. from bb.fetch2 import logger
  18. class Local(FetchMethod):
  19. def supports(self, urldata, d):
  20. """
  21. Check to see if a given url represents a local fetch.
  22. """
  23. return urldata.type in ['file']
  24. def urldata_init(self, ud, d):
  25. # We don't set localfile as for this fetcher the file is already local!
  26. ud.decodedurl = urllib.parse.unquote(ud.url.split("://")[1].split(";")[0])
  27. ud.basename = os.path.basename(ud.decodedurl)
  28. ud.basepath = ud.decodedurl
  29. ud.needdonestamp = False
  30. return
  31. def localpath(self, urldata, d):
  32. """
  33. Return the local filename of a given url assuming a successful fetch.
  34. """
  35. return self.localpaths(urldata, d)[-1]
  36. def localpaths(self, urldata, d):
  37. """
  38. Return the local filename of a given url assuming a successful fetch.
  39. """
  40. searched = []
  41. path = urldata.decodedurl
  42. newpath = path
  43. if path[0] == "/":
  44. return [path]
  45. filespath = d.getVar('FILESPATH')
  46. if filespath:
  47. logger.debug(2, "Searching for %s in paths:\n %s" % (path, "\n ".join(filespath.split(":"))))
  48. newpath, hist = bb.utils.which(filespath, path, history=True)
  49. searched.extend(hist)
  50. if (not newpath or not os.path.exists(newpath)) and path.find("*") != -1:
  51. # For expressions using '*', best we can do is take the first directory in FILESPATH that exists
  52. newpath, hist = bb.utils.which(filespath, ".", history=True)
  53. searched.extend(hist)
  54. logger.debug(2, "Searching for %s in path: %s" % (path, newpath))
  55. return searched
  56. if not os.path.exists(newpath):
  57. dldirfile = os.path.join(d.getVar("DL_DIR"), path)
  58. logger.debug(2, "Defaulting to %s for %s" % (dldirfile, path))
  59. bb.utils.mkdirhier(os.path.dirname(dldirfile))
  60. searched.append(dldirfile)
  61. return searched
  62. return searched
  63. def need_update(self, ud, d):
  64. if ud.url.find("*") != -1:
  65. return False
  66. if os.path.exists(ud.localpath):
  67. return False
  68. return True
  69. def download(self, urldata, d):
  70. """Fetch urls (no-op for Local method)"""
  71. # no need to fetch local files, we'll deal with them in place.
  72. if self.supports_checksum(urldata) and not os.path.exists(urldata.localpath):
  73. locations = []
  74. filespath = d.getVar('FILESPATH')
  75. if filespath:
  76. locations = filespath.split(":")
  77. locations.append(d.getVar("DL_DIR"))
  78. msg = "Unable to find file " + urldata.url + " anywhere. The paths that were searched were:\n " + "\n ".join(locations)
  79. raise FetchError(msg)
  80. return True
  81. def checkstatus(self, fetch, urldata, d):
  82. """
  83. Check the status of the url
  84. """
  85. if urldata.localpath.find("*") != -1:
  86. logger.info("URL %s looks like a glob and was therefore not checked.", urldata.url)
  87. return True
  88. if os.path.exists(urldata.localpath):
  89. return True
  90. return False
  91. def clean(self, urldata, d):
  92. return