checksum.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. # Local file checksum cache implementation
  2. #
  3. # Copyright (C) 2012 Intel Corporation
  4. #
  5. # SPDX-License-Identifier: GPL-2.0-only
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License version 2 as
  9. # published by the Free Software Foundation.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License along
  17. # with this program; if not, write to the Free Software Foundation, Inc.,
  18. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. import glob
  20. import operator
  21. import os
  22. import stat
  23. import pickle
  24. import bb.utils
  25. import logging
  26. from bb.cache import MultiProcessCache
  27. logger = logging.getLogger("BitBake.Cache")
  28. # mtime cache (non-persistent)
  29. # based upon the assumption that files do not change during bitbake run
  30. class FileMtimeCache(object):
  31. cache = {}
  32. def cached_mtime(self, f):
  33. if f not in self.cache:
  34. self.cache[f] = os.stat(f)[stat.ST_MTIME]
  35. return self.cache[f]
  36. def cached_mtime_noerror(self, f):
  37. if f not in self.cache:
  38. try:
  39. self.cache[f] = os.stat(f)[stat.ST_MTIME]
  40. except OSError:
  41. return 0
  42. return self.cache[f]
  43. def update_mtime(self, f):
  44. self.cache[f] = os.stat(f)[stat.ST_MTIME]
  45. return self.cache[f]
  46. def clear(self):
  47. self.cache.clear()
  48. # Checksum + mtime cache (persistent)
  49. class FileChecksumCache(MultiProcessCache):
  50. cache_file_name = "local_file_checksum_cache.dat"
  51. CACHE_VERSION = 1
  52. def __init__(self):
  53. self.mtime_cache = FileMtimeCache()
  54. MultiProcessCache.__init__(self)
  55. def get_checksum(self, f):
  56. entry = self.cachedata[0].get(f)
  57. cmtime = self.mtime_cache.cached_mtime(f)
  58. if entry:
  59. (mtime, hashval) = entry
  60. if cmtime == mtime:
  61. return hashval
  62. else:
  63. bb.debug(2, "file %s changed mtime, recompute checksum" % f)
  64. hashval = bb.utils.md5_file(f)
  65. self.cachedata_extras[0][f] = (cmtime, hashval)
  66. return hashval
  67. def merge_data(self, source, dest):
  68. for h in source[0]:
  69. if h in dest:
  70. (smtime, _) = source[0][h]
  71. (dmtime, _) = dest[0][h]
  72. if smtime > dmtime:
  73. dest[0][h] = source[0][h]
  74. else:
  75. dest[0][h] = source[0][h]
  76. def get_checksums(self, filelist, pn):
  77. """Get checksums for a list of files"""
  78. def checksum_file(f):
  79. try:
  80. checksum = self.get_checksum(f)
  81. except OSError as e:
  82. bb.warn("Unable to get checksum for %s SRC_URI entry %s: %s" % (pn, os.path.basename(f), e))
  83. return None
  84. return checksum
  85. def checksum_dir(pth):
  86. # Handle directories recursively
  87. if pth == "/":
  88. bb.fatal("Refusing to checksum /")
  89. dirchecksums = []
  90. for root, dirs, files in os.walk(pth):
  91. for name in files:
  92. fullpth = os.path.join(root, name)
  93. checksum = checksum_file(fullpth)
  94. if checksum:
  95. dirchecksums.append((fullpth, checksum))
  96. return dirchecksums
  97. checksums = []
  98. for pth in filelist.split():
  99. exist = pth.split(":")[1]
  100. if exist == "False":
  101. continue
  102. pth = pth.split(":")[0]
  103. if '*' in pth:
  104. # Handle globs
  105. for f in glob.glob(pth):
  106. if os.path.isdir(f):
  107. if not os.path.islink(f):
  108. checksums.extend(checksum_dir(f))
  109. else:
  110. checksum = checksum_file(f)
  111. if checksum:
  112. checksums.append((f, checksum))
  113. elif os.path.isdir(pth):
  114. if not os.path.islink(pth):
  115. checksums.extend(checksum_dir(pth))
  116. else:
  117. checksum = checksum_file(pth)
  118. if checksum:
  119. checksums.append((pth, checksum))
  120. checksums.sort(key=operator.itemgetter(1))
  121. return checksums