From dd421288b94939f01b36c7fe9efe07f31f1f3e8b Mon Sep 17 00:00:00 2001 From: John Sirois Date: Wed, 8 Jan 2025 14:10:21 -0800 Subject: [PATCH] Try a different Windows workaround. --- science/cache.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/science/cache.py b/science/cache.py index 9717ca7..0370237 100644 --- a/science/cache.py +++ b/science/cache.py @@ -78,6 +78,17 @@ def work_aux_dir(self) -> Path: _TTL_EXPIRY_FORMAT = "%m/%d/%y %H:%M:%S" +def windows_dirname_safe_version(version: int) -> str: + # N.B.: This insanity is needed for Windows where 1-31 cannot be used as dir names: + # https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file#naming-conventions + if version < 1 or version > 31: + return str(version) + zero = ord("a") - 1 + if version <= 26: + return chr(zero + version) + return f"a{chr(zero + version - 26)}" + + @dataclass(frozen=True) class DownloadCache: # Bump this when changing download cache on-disk structure. @@ -113,9 +124,8 @@ def get_or_create(self, url: Url, ttl: timedelta | None = None) -> Iterator[Cach # _/file # aux/ (Again, only present if Missing.work_aux_dir is used by caller.) - # N.B.: The v1 (instead of just 1) is needed for Windows where 1-31 cannot be used: - # https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file#naming-conventions - cache_dir = self.base_dir / f"v{self._VERSION}" / hashlib.sha256(url.encode()).hexdigest() + url_hash = hashlib.sha256(url.encode()).hexdigest() + cache_dir = self.base_dir / windows_dirname_safe_version(self._VERSION) / url_hash ttl_file = cache_dir.with_suffix(".ttl") if ttl else None if ttl_file and not ttl_file.exists():