checksum.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. # Local file checksum cache implementation
  2. #
  3. # Copyright (C) 2012 Intel Corporation
  4. #
  5. # SPDX-License-Identifier: GPL-2.0-only
  6. #
  7. import glob
  8. import operator
  9. import os
  10. import stat
  11. import pickle
  12. import bb.utils
  13. import logging
  14. from bb.cache import MultiProcessCache
  15. logger = logging.getLogger("BitBake.Cache")
  16. # mtime cache (non-persistent)
  17. # based upon the assumption that files do not change during bitbake run
  18. class FileMtimeCache(object):
  19. cache = {}
  20. def cached_mtime(self, f):
  21. if f not in self.cache:
  22. self.cache[f] = os.stat(f)[stat.ST_MTIME]
  23. return self.cache[f]
  24. def cached_mtime_noerror(self, f):
  25. if f not in self.cache:
  26. try:
  27. self.cache[f] = os.stat(f)[stat.ST_MTIME]
  28. except OSError:
  29. return 0
  30. return self.cache[f]
  31. def update_mtime(self, f):
  32. self.cache[f] = os.stat(f)[stat.ST_MTIME]
  33. return self.cache[f]
  34. def clear(self):
  35. self.cache.clear()
  36. # Checksum + mtime cache (persistent)
  37. class FileChecksumCache(MultiProcessCache):
  38. cache_file_name = "local_file_checksum_cache.dat"
  39. CACHE_VERSION = 1
  40. def __init__(self):
  41. self.mtime_cache = FileMtimeCache()
  42. MultiProcessCache.__init__(self)
  43. def get_checksum(self, f):
  44. entry = self.cachedata[0].get(f)
  45. cmtime = self.mtime_cache.cached_mtime(f)
  46. if entry:
  47. (mtime, hashval) = entry
  48. if cmtime == mtime:
  49. return hashval
  50. else:
  51. bb.debug(2, "file %s changed mtime, recompute checksum" % f)
  52. hashval = bb.utils.md5_file(f)
  53. self.cachedata_extras[0][f] = (cmtime, hashval)
  54. return hashval
  55. def merge_data(self, source, dest):
  56. for h in source[0]:
  57. if h in dest:
  58. (smtime, _) = source[0][h]
  59. (dmtime, _) = dest[0][h]
  60. if smtime > dmtime:
  61. dest[0][h] = source[0][h]
  62. else:
  63. dest[0][h] = source[0][h]
  64. def get_checksums(self, filelist, pn):
  65. """Get checksums for a list of files"""
  66. def checksum_file(f):
  67. try:
  68. checksum = self.get_checksum(f)
  69. except OSError as e:
  70. bb.warn("Unable to get checksum for %s SRC_URI entry %s: %s" % (pn, os.path.basename(f), e))
  71. return None
  72. return checksum
  73. def checksum_dir(pth):
  74. # Handle directories recursively
  75. if pth == "/":
  76. bb.fatal("Refusing to checksum /")
  77. dirchecksums = []
  78. for root, dirs, files in os.walk(pth):
  79. for name in files:
  80. fullpth = os.path.join(root, name)
  81. checksum = checksum_file(fullpth)
  82. if checksum:
  83. dirchecksums.append((fullpth, checksum))
  84. return dirchecksums
  85. checksums = []
  86. for pth in filelist.split():
  87. exist = pth.split(":")[1]
  88. if exist == "False":
  89. continue
  90. pth = pth.split(":")[0]
  91. if '*' in pth:
  92. # Handle globs
  93. for f in glob.glob(pth):
  94. if os.path.isdir(f):
  95. if not os.path.islink(f):
  96. checksums.extend(checksum_dir(f))
  97. else:
  98. checksum = checksum_file(f)
  99. if checksum:
  100. checksums.append((f, checksum))
  101. elif os.path.isdir(pth):
  102. if not os.path.islink(pth):
  103. checksums.extend(checksum_dir(pth))
  104. else:
  105. checksum = checksum_file(pth)
  106. if checksum:
  107. checksums.append((pth, checksum))
  108. checksums.sort(key=operator.itemgetter(1))
  109. return checksums