2021-04-09 22:54:20 +02:00
|
|
|
# Copyright 2021 MongoDB Inc.
|
|
|
|
#
|
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
# a copy of this software and associated documentation files (the
|
|
|
|
# "Software"), to deal in the Software without restriction, including
|
|
|
|
# without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
# distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
# permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
# the following conditions:
|
|
|
|
#
|
|
|
|
# The above copyright notice and this permission notice shall be included
|
|
|
|
# in all copies or substantial portions of the Software.
|
|
|
|
#
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
|
|
|
|
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
|
|
|
|
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
#
|
|
|
|
|
2021-06-23 17:51:53 +02:00
|
|
|
import datetime
|
2021-05-04 19:27:01 +02:00
|
|
|
import json
|
2021-04-09 22:54:20 +02:00
|
|
|
import os
|
2022-01-19 23:32:33 +01:00
|
|
|
import sys
|
2021-04-09 22:54:20 +02:00
|
|
|
import pathlib
|
|
|
|
import shutil
|
2022-01-14 23:31:22 +01:00
|
|
|
import tempfile
|
2021-06-23 17:51:53 +02:00
|
|
|
import traceback
|
2021-04-09 22:54:20 +02:00
|
|
|
|
2021-06-23 19:10:47 +02:00
|
|
|
|
2021-04-09 22:54:20 +02:00
|
|
|
import SCons
|
|
|
|
|
|
|
|
cache_debug_suffix = " (target: %s, cachefile: %s) "
|
|
|
|
|
2022-01-18 19:00:26 +01:00
|
|
|
class InvalidChecksum(SCons.Errors.BuildError):
|
2021-05-04 19:27:01 +02:00
|
|
|
def __init__(self, src, dst, reason, cache_csig='', computed_csig=''):
|
2021-04-09 22:54:20 +02:00
|
|
|
self.message = f"ERROR: md5 checksum {reason} for {src} ({dst})"
|
2021-05-04 19:27:01 +02:00
|
|
|
self.cache_csig = cache_csig
|
|
|
|
self.computed_csig = computed_csig
|
2021-04-09 22:54:20 +02:00
|
|
|
def __str__(self):
|
|
|
|
return self.message
|
|
|
|
|
2022-01-18 19:00:26 +01:00
|
|
|
class CacheTransferFailed(SCons.Errors.BuildError):
|
2021-04-09 22:54:20 +02:00
|
|
|
def __init__(self, src, dst, reason):
|
|
|
|
self.message = f"ERROR: cachedir transfer {reason} while transfering {src} to {dst}"
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.message
|
|
|
|
|
2022-01-18 19:00:26 +01:00
|
|
|
class UnsupportedError(SCons.Errors.BuildError):
|
2021-04-09 22:54:20 +02:00
|
|
|
def __init__(self, class_name, feature):
|
|
|
|
self.message = f"{class_name} does not support {feature}"
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.message
|
|
|
|
|
|
|
|
class CacheDirValidate(SCons.CacheDir.CacheDir):
|
|
|
|
|
2021-05-04 19:27:01 +02:00
|
|
|
def __init__(self, path):
|
|
|
|
self.json_log = None
|
|
|
|
super().__init__(path)
|
|
|
|
|
2021-04-09 22:54:20 +02:00
|
|
|
@staticmethod
|
|
|
|
def get_ext():
|
|
|
|
# Cache prune script is allowing only directories with this extension
|
|
|
|
# if this is changed, cache prune script should also be updated.
|
|
|
|
return '.cksum'
|
|
|
|
|
|
|
|
@staticmethod
|
2021-06-23 18:21:16 +02:00
|
|
|
def get_file_contents_path(default_cachefile_path):
|
2021-06-23 19:10:47 +02:00
|
|
|
return pathlib.Path(default_cachefile_path) / pathlib.Path(default_cachefile_path).name.split('.')[0]
|
2021-06-23 18:21:16 +02:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_bad_cachefile_path(cksum_cachefile_dir):
|
|
|
|
return pathlib.Path(cksum_cachefile_dir) / 'bad_cache_file'
|
2021-04-09 22:54:20 +02:00
|
|
|
|
|
|
|
@staticmethod
|
2021-06-23 18:21:16 +02:00
|
|
|
def get_hash_path(cksum_cachefile_path):
|
|
|
|
return pathlib.Path(cksum_cachefile_path).parent / 'content_hash'
|
2021-04-09 22:54:20 +02:00
|
|
|
|
2021-06-23 19:10:47 +02:00
|
|
|
@staticmethod
|
|
|
|
def get_cachedir_path(path):
|
|
|
|
return str(pathlib.Path(path + CacheDirValidate.get_ext()))
|
|
|
|
|
2021-04-09 22:54:20 +02:00
|
|
|
@classmethod
|
|
|
|
def copy_from_cache(cls, env, src, dst):
|
|
|
|
|
2021-06-23 19:10:47 +02:00
|
|
|
if not str(pathlib.Path(src)).endswith(cls.get_ext()):
|
2021-04-09 22:54:20 +02:00
|
|
|
return super().copy_from_cache(env, src, dst)
|
|
|
|
|
|
|
|
if env.cache_timestamp_newer:
|
|
|
|
raise UnsupportedError(cls.__name__, "timestamp-newer")
|
|
|
|
|
2021-06-23 19:10:47 +02:00
|
|
|
src_file = cls.get_file_contents_path(src)
|
2022-01-19 23:32:33 +01:00
|
|
|
# using os.path.exists here because: https://bugs.python.org/issue35306
|
|
|
|
if os.path.exists(str(cls.get_bad_cachefile_path(src))):
|
2021-06-23 19:10:47 +02:00
|
|
|
raise InvalidChecksum(cls.get_hash_path(src_file), dst, f"cachefile marked as bad checksum")
|
2021-06-23 18:21:16 +02:00
|
|
|
|
2021-04-09 22:54:20 +02:00
|
|
|
csig = None
|
|
|
|
try:
|
2021-06-23 19:10:47 +02:00
|
|
|
with open(cls.get_hash_path(src_file), 'rb') as f_out:
|
2021-04-09 22:54:20 +02:00
|
|
|
csig = f_out.read().decode().strip()
|
|
|
|
except OSError as ex:
|
2021-06-23 19:10:47 +02:00
|
|
|
raise InvalidChecksum(cls.get_hash_path(src_file), dst, f"failed to read hash file: {ex}") from ex
|
2021-11-10 22:57:39 +01:00
|
|
|
else:
|
2021-04-09 22:54:20 +02:00
|
|
|
if not csig:
|
2021-06-23 19:10:47 +02:00
|
|
|
raise InvalidChecksum(cls.get_hash_path(src_file), dst, f"no content_hash data found")
|
2021-04-09 22:54:20 +02:00
|
|
|
|
2022-01-14 23:31:22 +01:00
|
|
|
with tempfile.TemporaryDirectory() as tmpdirname:
|
|
|
|
dst_tmp = pathlib.Path(tmpdirname) / os.path.basename(dst)
|
|
|
|
try:
|
|
|
|
shutil.copy2(src_file, dst_tmp)
|
|
|
|
except OSError as ex:
|
|
|
|
raise CacheTransferFailed(src_file, dst, f"failed to copy from cache: {ex}") from ex
|
|
|
|
else:
|
|
|
|
shutil.move(dst_tmp, dst)
|
2021-04-09 22:54:20 +02:00
|
|
|
|
|
|
|
new_csig = SCons.Util.MD5filesignature(dst,
|
|
|
|
chunksize=SCons.Node.FS.File.md5_chunksize*1024)
|
|
|
|
|
|
|
|
if csig != new_csig:
|
|
|
|
raise InvalidChecksum(
|
2021-06-23 19:10:47 +02:00
|
|
|
cls.get_hash_path(src_file), dst, f"checksums don't match {csig} != {new_csig}", cache_csig=csig, computed_csig=new_csig)
|
2021-04-09 22:54:20 +02:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def copy_to_cache(cls, env, src, dst):
|
|
|
|
|
|
|
|
# dst is bsig/file from cachepath method, so
|
|
|
|
# we make sure to make the bsig dir first
|
2021-06-23 19:10:47 +02:00
|
|
|
dst = pathlib.Path(dst)
|
|
|
|
dst_file = dst / dst.name.split('.')[0]
|
2022-01-19 23:32:33 +01:00
|
|
|
|
2021-04-09 22:54:20 +02:00
|
|
|
try:
|
2022-01-19 23:32:33 +01:00
|
|
|
os.makedirs(dst, exist_ok=True)
|
2021-06-23 19:10:47 +02:00
|
|
|
super().copy_to_cache(env, src, dst_file)
|
2021-04-09 22:54:20 +02:00
|
|
|
except OSError as ex:
|
2021-06-23 19:10:47 +02:00
|
|
|
raise CacheTransferFailed(src, dst_file, f"failed to copy to cache: {ex}") from ex
|
2021-04-09 22:54:20 +02:00
|
|
|
|
|
|
|
try:
|
2021-06-23 19:10:47 +02:00
|
|
|
with open(cls.get_hash_path(dst_file), 'w') as f_out:
|
2021-04-09 22:54:20 +02:00
|
|
|
f_out.write(env.File(src).get_content_hash())
|
|
|
|
except OSError as ex:
|
2021-06-23 19:10:47 +02:00
|
|
|
raise CacheTransferFailed(src, dst_file, f"failed to create hash file: {ex}") from ex
|
2021-05-04 19:27:01 +02:00
|
|
|
|
|
|
|
def log_json_cachedebug(self, node, pushing=False):
|
|
|
|
if (pushing
|
|
|
|
and (node.nocache or SCons.CacheDir.cache_readonly or 'conftest' in str(node))):
|
|
|
|
return
|
|
|
|
|
2021-06-23 19:10:47 +02:00
|
|
|
cachefile = self.get_file_contents_path(self.cachepath(node)[1])
|
2021-05-04 19:27:01 +02:00
|
|
|
if node.fs.exists(cachefile):
|
|
|
|
cache_event = 'double_push' if pushing else 'hit'
|
|
|
|
else:
|
|
|
|
cache_event = 'push' if pushing else 'miss'
|
|
|
|
|
|
|
|
self.CacheDebugJson({'type': cache_event}, node, cachefile)
|
2021-04-09 22:54:20 +02:00
|
|
|
|
|
|
|
def retrieve(self, node):
|
2021-11-10 07:09:12 +01:00
|
|
|
if not self.is_enabled():
|
|
|
|
return False
|
|
|
|
|
2021-05-04 19:27:01 +02:00
|
|
|
self.log_json_cachedebug(node)
|
2021-04-09 22:54:20 +02:00
|
|
|
try:
|
|
|
|
return super().retrieve(node)
|
|
|
|
except InvalidChecksum as ex:
|
2021-06-23 17:51:53 +02:00
|
|
|
self.print_cache_issue(node, ex)
|
2021-05-04 19:27:01 +02:00
|
|
|
self.clean_bad_cachefile(node, ex.cache_csig, ex.computed_csig)
|
2021-04-09 22:54:20 +02:00
|
|
|
return False
|
|
|
|
except (UnsupportedError, CacheTransferFailed) as ex:
|
2021-06-23 17:51:53 +02:00
|
|
|
self.print_cache_issue(node, ex)
|
2021-04-09 22:54:20 +02:00
|
|
|
return False
|
|
|
|
|
|
|
|
def push(self, node):
|
2021-11-10 07:09:12 +01:00
|
|
|
if self.is_readonly() or not self.is_enabled():
|
|
|
|
return
|
2021-05-04 19:27:01 +02:00
|
|
|
self.log_json_cachedebug(node, pushing=True)
|
2021-04-09 22:54:20 +02:00
|
|
|
try:
|
|
|
|
return super().push(node)
|
|
|
|
except CacheTransferFailed as ex:
|
2022-01-18 19:00:26 +01:00
|
|
|
self.print_cache_issue(node, ex)
|
2021-04-09 22:54:20 +02:00
|
|
|
return False
|
|
|
|
|
2021-05-04 19:27:01 +02:00
|
|
|
def CacheDebugJson(self, json_data, target, cachefile):
|
|
|
|
|
|
|
|
if SCons.CacheDir.cache_debug and SCons.CacheDir.cache_debug != '-' and self.json_log is None:
|
|
|
|
self.json_log = open(SCons.CacheDir.cache_debug + '.json', 'w')
|
|
|
|
|
|
|
|
if self.json_log is not None:
|
|
|
|
cksum_cachefile = str(pathlib.Path(cachefile).parent)
|
|
|
|
if cksum_cachefile.endswith(self.get_ext()):
|
|
|
|
cachefile = cksum_cachefile
|
|
|
|
|
|
|
|
json_data.update({
|
2021-06-23 17:51:53 +02:00
|
|
|
'timestamp': str(datetime.datetime.now(datetime.timezone.utc)),
|
2021-05-04 19:27:01 +02:00
|
|
|
'realfile': str(target),
|
|
|
|
'cachefile': pathlib.Path(cachefile).name,
|
|
|
|
'cache_dir': str(pathlib.Path(cachefile).parent.parent),
|
|
|
|
})
|
|
|
|
|
2022-01-19 23:32:33 +01:00
|
|
|
# capture exception information
|
|
|
|
if sys.exc_info()[1]:
|
|
|
|
json_data.update({'error': self._format_exception_msg()})
|
|
|
|
|
2021-05-04 19:27:01 +02:00
|
|
|
self.json_log.write(json.dumps(json_data) + '\n')
|
|
|
|
|
2021-04-20 20:03:09 +02:00
|
|
|
def CacheDebug(self, fmt, target, cachefile):
|
|
|
|
|
2022-01-19 23:32:33 +01:00
|
|
|
super().CacheDebug(fmt, target, cachefile)
|
|
|
|
# Capture exception information into the cache debug log
|
|
|
|
if sys.exc_info()[1] and self.debugFP:
|
|
|
|
self.debugFP.write(self._format_exception_msg())
|
|
|
|
|
|
|
|
def _format_exception_msg(self):
|
|
|
|
return ('An exception was detected while using the cache:\n' +
|
|
|
|
' ' + "\n ".join("".join(traceback.format_exc()).split("\n"))) + '\n'
|
2022-01-18 17:50:03 +01:00
|
|
|
|
2022-01-19 23:32:33 +01:00
|
|
|
def _log(self, log_msg, json_info, realnode, cachefile):
|
|
|
|
self.CacheDebug(log_msg + cache_debug_suffix, realnode, cachefile)
|
2021-06-23 18:21:16 +02:00
|
|
|
self.CacheDebugJson(json_info, realnode, cachefile)
|
|
|
|
|
2021-06-23 17:51:53 +02:00
|
|
|
def print_cache_issue(self, node, ex):
|
2021-04-09 22:54:20 +02:00
|
|
|
|
2021-06-23 19:10:47 +02:00
|
|
|
cksum_dir = pathlib.Path(self.cachepath(node)[1])
|
2022-01-19 23:32:33 +01:00
|
|
|
self._log(str(ex), {'type': 'error'}, node, cksum_dir)
|
2021-04-09 22:54:20 +02:00
|
|
|
|
2021-05-04 19:27:01 +02:00
|
|
|
def clean_bad_cachefile(self, node, cache_csig, computed_csig):
|
2021-04-09 22:54:20 +02:00
|
|
|
|
2021-06-23 19:10:47 +02:00
|
|
|
cksum_dir = pathlib.Path(self.cachepath(node)[1])
|
2021-06-23 18:21:16 +02:00
|
|
|
rm_path = f"{cksum_dir}.{SCons.CacheDir.cache_tmp_uuid}.del"
|
|
|
|
try:
|
2021-11-10 22:57:39 +01:00
|
|
|
try:
|
|
|
|
pathlib.Path(self.get_bad_cachefile_path(cksum_dir)).touch()
|
|
|
|
except FileExistsError:
|
|
|
|
pass
|
2021-06-23 18:21:16 +02:00
|
|
|
cksum_dir.replace(rm_path)
|
|
|
|
except OSError as ex:
|
|
|
|
msg = f"Failed to rename {cksum_dir} to {rm_path}: {ex}"
|
2022-01-19 23:32:33 +01:00
|
|
|
self._log(msg, {'type': 'error'}, node, cksum_dir)
|
2021-06-23 18:21:16 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
msg = f"Removed bad cachefile {cksum_dir} found in cache."
|
2022-01-19 23:32:33 +01:00
|
|
|
self._log(msg, {
|
2021-06-23 18:21:16 +02:00
|
|
|
'type': 'invalid_checksum',
|
|
|
|
'cache_csig': cache_csig,
|
|
|
|
'computed_csig': computed_csig
|
|
|
|
}, node, cksum_dir)
|
2021-05-04 19:27:01 +02:00
|
|
|
|
2021-04-09 22:54:20 +02:00
|
|
|
|
|
|
|
def get_cachedir_csig(self, node):
|
|
|
|
cachedir, cachefile = self.cachepath(node)
|
|
|
|
if cachefile and os.path.exists(cachefile):
|
2021-06-23 19:10:47 +02:00
|
|
|
with open(self.get_hash_path(self.get_file_contents_path(cachefile)), 'rb') as f_out:
|
2021-04-09 22:54:20 +02:00
|
|
|
return f_out.read().decode()
|
|
|
|
|
|
|
|
def cachepath(self, node):
|
2021-11-10 07:09:12 +01:00
|
|
|
if not self.is_enabled():
|
|
|
|
return None, None
|
|
|
|
|
2021-04-09 22:54:20 +02:00
|
|
|
dir, path = super().cachepath(node)
|
|
|
|
if node.fs.exists(path):
|
|
|
|
return dir, path
|
2021-06-23 19:10:47 +02:00
|
|
|
return dir, str(self.get_cachedir_path(path))
|
2021-04-09 22:54:20 +02:00
|
|
|
|
|
|
|
def exists(env):
|
|
|
|
return True
|
|
|
|
|
|
|
|
def generate(env):
|
|
|
|
if not env.get('CACHEDIR_CLASS'):
|
|
|
|
env['CACHEDIR_CLASS'] = CacheDirValidate
|