From dccf067299b704dd93e7019d028efdd6ac39d694 Mon Sep 17 00:00:00 2001 From: Bo Peng Date: Fri, 16 Feb 2024 05:34:39 +0000 Subject: [PATCH] Fix file_target._md4 (close #1539) --- src/sos/targets.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/sos/targets.py b/src/sos/targets.py index 3c33f3b10..b587b6993 100644 --- a/src/sos/targets.py +++ b/src/sos/targets.py @@ -21,12 +21,10 @@ import fasteners import pkg_resources -from .controller import (request_answer_from_controller, - send_message_to_controller) +from .controller import (request_answer_from_controller, send_message_to_controller) from .eval import get_config, interpolate from .pattern import extract_pattern -from .utils import (Error, env, fileMD5, objectMD5, pickleable, short_repr, - stable_repr, textMD5) +from .utils import (Error, env, fileMD5, objectMD5, pickleable, short_repr, stable_repr, textMD5) __all__ = ["dynamic", "executable", "env_variable", "sos_variable"] @@ -533,10 +531,12 @@ def __init__(self, *args, **kwargs): # this is path segments super().__init__(*args, **kwargs) if len(args) == 1 and isinstance(args[0], file_target): - self._md5 = args[0]._md5 + self._md5 = args[0]._md5 if hasattr(args[0], '_md5') else None else: self._md5 = None + # this only works for Python 3.9. Starting from 3.10, newly constructed file_target + # from calls such as file_target().with_suffix() will not have _md5 def _init(self, template=None): super()._init(template) self._md5 = None @@ -574,6 +574,8 @@ def target_name(self): def target_signature(self): """Return file signature""" + if not hasattr(self, '_md5'): + self._md5 = None full_md5 = env.config['sig_type'] == 'md5' if self.exists(): if full_md5: @@ -602,6 +604,8 @@ def sig_file(self): def validate(self, sig=None): """Check if file matches its signature""" + if not hasattr(self, '_md5'): + self._md5 = None if env.config['sig_type'] == 'md5': md5_file = self + '.md5' if md5_file.exists(): @@ -632,6 +636,8 @@ def validate(self, sig=None): def write_sig(self): """Write signature to sig store""" + if not hasattr(self, '_md5'): + self._md5 = None if not self._md5: self._md5 = fileMD5(self) with open(self.sig_file(), "w") as sig: @@ -657,7 +663,7 @@ def __reduce__(self): self.__class__, super().__reduce__()[1], { - "_md5": self._md5, + "_md5": self._md5 if hasattr(self, '_md5') else None, "_dict": self._dict }, ]) @@ -1245,7 +1251,6 @@ def remove_targets(self, type, kept=None): ).set(**grp._dict) return self - def group_with(self, name, properties): if not self._groups: self._group(by="all")