Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions packages/mecha/src/mecha/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import csv
import logging
import os
import hashlib
import pickle
import sys
from contextlib import contextmanager
Expand Down Expand Up @@ -376,6 +377,32 @@ def parse(

cache_miss = ast_path

if self.cache and (resource_location and not filename):
ast_path = self.cache.get_path(f"{resource_location}-ast")
# compile the hash of the file at resource_location
hash = hashlib.sha256(source.text.encode("utf-8")).hexdigest()
changed = False
known_hash = self.cache.json.get("resource_location_hashes", {}).get(
resource_location
)
if known_hash != hash:
changed = True
self.cache.json.setdefault("resource_location_hashes", {})[
resource_location
] = hash
if not changed:
try:
with ast_path.open("rb") as f:
ast = self.cache_backend.load(f)
logger.debug(
'Load cached ast for file "%s".', resource_location
)
return ast
except Exception:
pass

cache_miss = ast_path

stream = TokenStream(
source.text,
preprocessor=preprocessor or self.preprocessor,
Expand All @@ -388,6 +415,10 @@ def parse(
except InvalidSyntax as exc:
if self.cache and filename and cache_miss:
self.cache.invalidate_changes(self.directory / filename)
if self.cache and (resource_location and not filename) and cache_miss:
self.cache.json.get("resource_location_hashes", {}).pop(
resource_location, None
)
d = Diagnostic(
level="error",
message=str(exc),
Expand All @@ -398,11 +429,14 @@ def parse(
)
raise DiagnosticError(DiagnosticCollection([set_location(d, exc)])) from exc
else:
if self.cache and filename and cache_miss:
if self.cache and (filename or resource_location) and cache_miss:
try:
with cache_miss.open("wb") as f:
self.cache_backend.dump(ast, f)
logger.debug('Update cached ast for file "%s".', filename)
logger.debug(
'Update cached ast for file "%s".',
filename or resource_location,
)
except Exception:
pass
return ast
Expand Down