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

Skip is_dirty check from matlab tools #48

Merged
merged 7 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/psf/black
rev: 23.3.0
rev: 23.7.0
hooks:
- id: black
language_version: python3.10
Expand Down
20 changes: 17 additions & 3 deletions ska_helpers/chandra_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def get_data(
"""
Get data from chandra_models repository.

For testing purposes there are three environment variables that impact the behavior:
There are three environment variables that impact the behavior:

- ``CHANDRA_MODELS_REPO_DIR`` or ``THERMAL_MODELS_DIR_FOR_MATLAB_TOOLS_SW``:
override the default root for the chandra_models repository
Expand All @@ -123,6 +123,15 @@ def get_data(
developement branch to test a model file update with applications like yoshi where
specifying a version would require a long chain of API updates.

``THERMAL_MODELS_DIR_FOR_MATLAB_TOOLS_SW`` is used to define the chandra_models repository
location when running in the MATLAB tools software environment. If this environment
variable is set, then the git is_dirty() check of the chandra_models directory is skipped
as the chandra_models repository is verified via SVN in the MATLAB tools software environment.
Users in the FOT Matlab tools should exercise caution if using locally-modified files
for testing, as the version information reported by this function in that case will not
be correct.


Examples
--------
First we read the model specification for the ACA model. The ``get_data()`` function
Expand Down Expand Up @@ -310,8 +319,13 @@ def get_repo_version(
repo_path = chandra_models_repo_path()
repo = git.Repo(repo_path)

if repo.is_dirty():
raise ValueError("repo is dirty")
# Use the THERMAL_MODELS_DIR_FOR_MATLAB_TOOLS_SW environment variable as a proxy
# to determine if we are running in the MATLAB tools software environment. If so
# the repo will be checked via SVN and using is_dirty() would change the .git/index
# and cause SVN to mark the directory as modified. So skip is_dirty() in this case.
if os.environ.get("THERMAL_MODELS_DIR_FOR_MATLAB_TOOLS_SW") is None:
if repo.is_dirty():
raise ValueError("repo is dirty")

tags = sorted(repo.tags, key=lambda tag: tag.commit.committed_datetime)
tag_repo = tags[-1]
Expand Down
23 changes: 23 additions & 0 deletions ska_helpers/tests/test_chandra_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,29 @@ def test_get_model_file_fail():
chandra_models.get_data(ACA_SPEC_PATH, repo_path="__NOT_A_DIRECTORY__")


@pytest.mark.skipif(SOME_ENV_VAR_DEFINED, reason="Non flight repo is being used")
def test_repo_dirty_handling(monkeypatch, tmp_path):
repo_path = tmp_path / "chandra_models"
default_root = Path(os.environ["SKA"], "data", "chandra_models")
git.Repo.clone_from(default_root, repo_path)
# Make a change to the repo
with open(repo_path / "chandra_models/__init__.py", "a") as fh:
fh.write("# test\n")

repo = git.Repo(repo_path)
# Messing with the init should make the repo "dirty"
assert repo.is_dirty() is True

# And get_data should raise a ValueError as the repo is dirty
with pytest.raises(ValueError):
_, info = chandra_models.get_data(ACA_SPEC_PATH, repo_path=repo_path)

# But no value error if the THERMAL_MODELS_DIR_FOR_MATLAB_TOOLS_SW env var is set
monkeypatch.setenv("THERMAL_MODELS_DIR_FOR_MATLAB_TOOLS_SW", str(repo_path))
_, info = chandra_models.get_data(ACA_SPEC_PATH, repo_path=repo_path)
assert info["repo_path"] == str(repo_path)


def test_get_repo_version():
version = chandra_models.get_repo_version()
assert isinstance(version, str)
Expand Down
Loading