local.py 4.7 KB

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