Skip to content

Commit

Permalink
fix: cache busting
Browse files Browse the repository at this point in the history
  • Loading branch information
doctrino committed Nov 12, 2024
1 parent 15213b2 commit 542b7e8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 24 deletions.
55 changes: 32 additions & 23 deletions cognite/neat/_session/engine/_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,34 @@
def load_neat_engine(client: CogniteClient | None, location: Literal["newest", "cache"]) -> str | None:
if location not in ["newest", "cache"]:
raise NeatValueError(f"Cannot load engine from location: {location}")

if not __engine__.startswith("^"):
# Using value error as this is a developer error
raise ValueError(f"Invalid engine version: {__engine__}")

lower_bound = parse_version(__engine__[1:])
upper_bound = Version(f"{lower_bound.major + 1}.0.0")

cache_dir = Path(tempfile.gettempdir()) / PACKAGE_NAME
cache_dir.mkdir(exist_ok=True)
pattern = re.compile(rf"{PACKAGE_NAME}-(\d+\.\d+\.\d+)-{PYVERSION}.zip")

candidates: dict[Version, Callable[[], Path]] = {}
if location == "cache" and cache_dir.exists():
candidates = _load_from_path(cache_dir, pattern)
candidates = _load_from_path(cache_dir, pattern, lower_bound, upper_bound)

if location == "newest" or not candidates:
# Loading in revrse order of priority
# Loading in reverse order of priority
# 3. Downloads folder
candidates = _load_from_path(Path.home() / "Downloads", pattern)
candidates = _load_from_path(Path.home() / "Downloads", pattern, lower_bound, upper_bound)
# 2. CDF
if client:
candidates.update(_load_from_cdf(client, pattern, cache_dir))
candidates.update(_load_from_cdf(client, pattern, lower_bound, upper_bound, cache_dir))
# 1. Environment variable
if ENVIRONMENT_VARIABLE in os.environ:
environ_path = Path(os.environ[ENVIRONMENT_VARIABLE])
if environ_path.exists():
candidates.update(_load_from_path(environ_path, pattern))
candidates.update(_load_from_path(environ_path, pattern, lower_bound, upper_bound))
else:
warnings.warn(
f"Environment variable {ENVIRONMENT_VARIABLE} points to non-existing path: {environ_path}",
Expand All @@ -53,15 +61,7 @@ def load_neat_engine(client: CogniteClient | None, location: Literal["newest", "
if not candidates:
return None

if not __engine__.startswith("^"):
# Using value error as this is a developer error
raise ValueError(f"Invalid engine version: {__engine__}")

lower_bound = parse_version(__engine__[1:])
upper_bound = Version(f"{lower_bound.major+1}.0.0")
selected_version = max(
(version for version in candidates.keys() if lower_bound <= version < upper_bound), default=None
)
selected_version = max(candidates.keys(), default=None)
if not selected_version:
return None
source_path = candidates[selected_version]()
Expand All @@ -76,26 +76,33 @@ def load_neat_engine(client: CogniteClient | None, location: Literal["newest", "
return engine_version


def _load_from_path(path: Path, pattern) -> dict[Version, Callable[[], Path]]:
def _load_from_path(
path: Path, pattern: re.Pattern[str], lower_bound: Version, upper_bound: Version
) -> dict[Version, Callable[[], Path]]:
if path.is_file() and (match := pattern.match(path.name)):
return {parse_version(match.group(1)): lambda: path}
version = parse_version(match.group(1))
if lower_bound <= version < upper_bound:
return {parse_version(match.group(1)): lambda: path}
return {}
elif path.is_dir():
output: dict[Version, Callable[[], Path]] = {}
for candidate in path.iterdir():
if candidate.is_file() and (match := pattern.match(candidate.name)):
# Setting default value to ensure we use the candidate from the current iteration
# If not set, the function will use the last candidate from the loop
def return_path(the_path: Path = candidate) -> Path:
return the_path
version = parse_version(match.group(1))
if lower_bound <= version < upper_bound:
# Setting default value to ensure we use the candidate from the current iteration
# If not set, the function will use the last candidate from the loop
def return_path(the_path: Path = candidate) -> Path:
return the_path

output[parse_version(match.group(1))] = return_path
output[parse_version(match.group(1))] = return_path

return output
return {}


def _load_from_cdf(
client: CogniteClient, pattern: re.Pattern[str], cache_dir: Path
client: CogniteClient, pattern: re.Pattern[str], lower_bound: Version, upper_bound: Version, cache_dir: Path
) -> dict[Version, Callable[[], Path]]:
file_metadata = client.files.list(
limit=-1,
Expand All @@ -115,6 +122,8 @@ def download_file(file_id: int = file.id, filename: str = name) -> Path:
return cache_dir / filename

if match := pattern.match(name):
output[parse_version(match.group(1))] = download_file
version = parse_version(match.group(1))
if lower_bound <= version < upper_bound:
output[version] = download_file

return output
2 changes: 1 addition & 1 deletion cognite/neat/_version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__version__ = "0.96.6"
__engine__ = "^0.1.0"
__engine__ = "^1.0.0"
2 changes: 2 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Changes are grouped as follows:
### Added
- Added provenance on rules in NeatSession
- Option to move connections from reference to new model in DMS rules when generating Enterprise model
- Support for loading `NeatEngine`.

### Improved
- Case-insensitive "direct" connection type in DMS Rules
- Validation over view types for connections in DMS Rules
Expand Down

0 comments on commit 542b7e8

Please sign in to comment.