local.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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
  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, url, 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.unquote(ud.url.split("://")[1].split(";")[0])
  40. ud.basename = os.path.basename(ud.decodedurl)
  41. ud.basepath = ud.decodedurl
  42. return
  43. def localpath(self, url, urldata, d):
  44. """
  45. Return the local filename of a given url assuming a successful fetch.
  46. """
  47. path = urldata.decodedurl
  48. newpath = path
  49. if path[0] != "/":
  50. filespath = data.getVar('FILESPATH', d, True)
  51. if filespath:
  52. logger.debug(2, "Searching for %s in paths: \n%s" % (path, "\n ".join(filespath.split(":"))))
  53. newpath = bb.utils.which(filespath, path)
  54. if not newpath:
  55. filesdir = data.getVar('FILESDIR', d, True)
  56. if filesdir:
  57. logger.debug(2, "Searching for %s in path: %s" % (path, filesdir))
  58. newpath = os.path.join(filesdir, path)
  59. if (not newpath or not os.path.exists(newpath)) and path.find("*") != -1:
  60. # For expressions using '*', best we can do is take the first directory in FILESPATH that exists
  61. newpath = bb.utils.which(filespath, ".")
  62. logger.debug(2, "Searching for %s in path: %s" % (path, newpath))
  63. return newpath
  64. if not os.path.exists(newpath):
  65. dldirfile = os.path.join(d.getVar("DL_DIR", True), path)
  66. logger.debug(2, "Defaulting to %s for %s" % (dldirfile, path))
  67. bb.utils.mkdirhier(os.path.dirname(dldirfile))
  68. return dldirfile
  69. return newpath
  70. def need_update(self, url, ud, d):
  71. if url.find("*") != -1:
  72. return False
  73. if os.path.exists(ud.localpath):
  74. return False
  75. return True
  76. def download(self, url, urldata, d):
  77. """Fetch urls (no-op for Local method)"""
  78. # no need to fetch local files, we'll deal with them in place.
  79. if self.supports_checksum(urldata) and not os.path.exists(urldata.localpath):
  80. locations = []
  81. filespath = data.getVar('FILESPATH', d, True)
  82. if filespath:
  83. locations = filespath.split(":")
  84. filesdir = data.getVar('FILESDIR', d, True)
  85. if filesdir:
  86. locations.append(filesdir)
  87. locations.append(d.getVar("DL_DIR", True))
  88. msg = "Unable to find file " + url + " anywhere. The paths that were searched were:\n " + "\n ".join(locations)
  89. raise FetchError(msg)
  90. return True
  91. def checkstatus(self, url, urldata, d):
  92. """
  93. Check the status of the url
  94. """
  95. if urldata.localpath.find("*") != -1:
  96. logger.info("URL %s looks like a glob and was therefore not checked.", url)
  97. return True
  98. if os.path.exists(urldata.localpath):
  99. return True
  100. return False
  101. def clean(self, urldata, d):
  102. return