Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes to get chandra_models and paths working on Windows #34

Merged
merged 1 commit into from
Jun 5, 2023
Merged
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
49 changes: 36 additions & 13 deletions ska_helpers/chandra_models.py
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@
import contextlib
import hashlib
import os
import shutil
import tempfile
import warnings
from pathlib import Path
@@ -26,6 +27,34 @@
)


@contextlib.contextmanager
def get_local_repo(repo_path, version):
"""Get local version of ``repo_path`` and ensure correct clean-up."""

def onerror(func, path, exc_info):
os.chmod(path, 0o0777)
try:
func(path)
except Exception as exc:
print(f"Warning: temp_dir() could not remove {path} because of {exc}")

clone = str(repo_path).startswith("https://github.com") or version is not None
if clone:
repo_path_local = tempfile.mkdtemp()
repo = git.Repo.clone_from(repo_path, repo_path_local)
if version is not None:
repo.git.checkout(version)
else:
repo = git.Repo(repo_path)
repo_path_local = repo_path

yield repo, repo_path_local

repo.close()
if clone:
shutil.rmtree(repo_path_local, onerror=onerror)


def get_data(
file_path: str | Path,
version: Optional[str] = None,
@@ -159,18 +188,11 @@ def get_data(
# - If the repo is remote on GitHub then we always clone to a temp dir
# - If the repo is local and the version is not the default then we clone to a temp
# to allow checking out at the specified version.
with contextlib.ExitStack() as stack:
if str(repo_path).startswith("https://github.com") or version is not None:
# For a remote GitHub repo or non-default version, clone to a temp dir
repo_path_local = stack.enter_context(tempfile.TemporaryDirectory())
repo = git.Repo.clone_from(repo_path, repo_path_local)
if version is not None:
repo.git.checkout(version)
else:
# For a local repo at the default version use the existing directory
repo = git.Repo(repo_path)
repo_path_local = repo_path

# This is all done with a context manager that ensure the repo object is
# properly closed and that all temporary files are cleaned up. Doing this
# on Windows was challenging. Search on slack:
# "The process cannot access the file because it is being used"
with get_local_repo(repo_path, version) as (repo, repo_path_local):
repo_file_path = Path(repo_path_local) / file_path
if not repo_file_path.exists():
raise FileNotFoundError(f"chandra_models {file_path=} does not exist")
@@ -193,7 +215,8 @@ def get_data(
data, repo_file_path = read_func(repo_file_path, **read_func_kwargs)

# Compute the MD5 sum of repo_file_path.
md5 = hashlib.md5(repo_file_path.read_bytes()).hexdigest()
file_bytes = repo_file_path.read_bytes().replace(b"\r", b"")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why .replace(b"\r", b"")?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The file in the windows repo has been "fixed" by git to have windows line endings, so the MD5 sum doesn't match Linux.

md5 = hashlib.md5(file_bytes).hexdigest()

# Store some info about this request in the cache.
info.update(
3 changes: 3 additions & 0 deletions ska_helpers/tests/test_paths.py
Original file line number Diff line number Diff line change
@@ -16,6 +16,9 @@ def test_chandra_models_paths(repo_source, chandra_models_repo_dir, monkeypatch)
monkeypatch.setenv(chandra_models_repo_dir, str(root))
kwargs = {}
elif repo_source == "default":
# Make sure no path-changing env vars are set
for env_var in paths.CHANDRA_MODELS_ROOT_ENV_VARS:
monkeypatch.delenv(env_var, raising=False)
root = Path(os.environ["SKA"]) / "data" / "chandra_models"
kwargs = {}
elif repo_source == "kwargs":