local.py 3.7 KB

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