Skip to content

Commit

Permalink
Try a different Windows workaround.
Browse files Browse the repository at this point in the history
  • Loading branch information
jsirois committed Jan 8, 2025
1 parent 692fd74 commit dd42128
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions science/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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():
Expand Down

0 comments on commit dd42128

Please sign in to comment.