-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #171 from fmi-faim/pixi-helpers
Add pixi utility modules
- Loading branch information
Showing
8 changed files
with
228 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import json | ||
import shutil | ||
import subprocess | ||
import sys | ||
from inspect import cleandoc | ||
|
||
GB = 1024 * 1024 * 1024 | ||
|
||
|
||
def min_cache_size_gb(gb: int = 2): | ||
info = subprocess.run( | ||
["pixi", "info", "--json"], # noqa: S607 | ||
capture_output=True, | ||
text=True, | ||
check=False, | ||
) | ||
cache_dir = json.loads(info.stdout)["cache_dir"] | ||
|
||
if shutil.disk_usage(cache_dir).free < gb * GB: | ||
sys.tracebacklimit = 0 | ||
message = cleandoc( | ||
f""" | ||
Disk space in cache directory is below {gb} GB. | ||
PIXI_CACHE_DIR: {cache_dir} | ||
Did you initialize your session correctly ('source ./init.sh') ? | ||
""" | ||
) | ||
raise RuntimeError(message) | ||
sys.exit(0) | ||
|
||
|
||
if __name__ == "__main__": | ||
if len(sys.argv) > 1: | ||
min_cache_size_gb(gb=int(sys.argv[1])) | ||
min_cache_size_gb() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import os | ||
import subprocess | ||
import sys | ||
|
||
from faim_ipa.utils import create_logger | ||
|
||
|
||
def log_commit(task: str = ""): | ||
command = ["git", "rev-parse", "--verify", "HEAD"] | ||
info = subprocess.run(command, capture_output=True, text=True, check=False) | ||
hash_string = info.stdout.strip() | ||
|
||
os.chdir(path=os.getenv("WD", default=".")) | ||
logger = create_logger(name="githash", include_timestamp=False) | ||
logger.info("[Executing] %s [git-commit] %s", task, hash_string) | ||
|
||
|
||
if __name__ == "__main__": | ||
if len(sys.argv) > 1: | ||
log_commit(task=str(sys.argv[1])) | ||
else: | ||
log_commit() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import subprocess | ||
import sys | ||
from inspect import cleandoc | ||
|
||
|
||
def git_status_clean(src_dir: str = "src"): | ||
command = ["git", "status", "--porcelain", src_dir] | ||
info = subprocess.run(command, capture_output=True, text=True, check=False) | ||
changes = len(info.stdout.splitlines()) | ||
|
||
if changes > 0: | ||
sys.tracebacklimit = 0 | ||
message = cleandoc( | ||
f""" | ||
There are {changes} untracked changes in {src_dir}. | ||
Please commit or stash before proceeding. | ||
""" | ||
) | ||
raise RuntimeError(message) | ||
sys.exit(0) | ||
|
||
|
||
if __name__ == "__main__": | ||
if len(sys.argv) > 1: | ||
git_status_clean(src_dir=str(sys.argv[1])) | ||
git_status_clean() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import os | ||
import re | ||
from inspect import cleandoc | ||
from typing import NamedTuple | ||
|
||
import pytest | ||
|
||
from faim_ipa import pixi | ||
from faim_ipa.pixi.cache_status import min_cache_size_gb | ||
from faim_ipa.pixi.log_commit import log_commit | ||
from faim_ipa.pixi.src_status import git_status_clean | ||
|
||
|
||
def test_src_status_fail(mocker): | ||
mock_result = mocker.Mock() | ||
mock_result.stdout = cleandoc( | ||
""" | ||
A some/file | ||
M some/other/file | ||
""" | ||
) | ||
mock_result.returncode = 1 | ||
|
||
mocker.patch("faim_ipa.pixi.src_status.subprocess.run", return_value=mock_result) | ||
|
||
with pytest.raises( | ||
RuntimeError, match="There are 2 untracked changes in mock_source" | ||
): | ||
git_status_clean(src_dir="mock_source") | ||
|
||
pixi.src_status.subprocess.run.assert_called_once_with( | ||
["git", "status", "--porcelain", "mock_source"], | ||
text=True, | ||
capture_output=True, | ||
check=False, | ||
) | ||
|
||
|
||
def test_src_status_succeed(mocker): | ||
mock_result = mocker.Mock() | ||
mock_result.stdout = "" | ||
mock_result.returncode = 0 | ||
|
||
mocker.patch("faim_ipa.pixi.src_status.subprocess.run", return_value=mock_result) | ||
|
||
with pytest.raises(SystemExit) as excinfo: | ||
git_status_clean(src_dir="mock_source") | ||
assert excinfo.value.code == 0 | ||
|
||
pixi.src_status.subprocess.run.assert_called_once_with( | ||
["git", "status", "--porcelain", "mock_source"], | ||
text=True, | ||
capture_output=True, | ||
check=False, | ||
) | ||
|
||
|
||
def test_cache_status_fail(mocker): | ||
mock_result_pixi = mocker.Mock() | ||
mock_result_pixi.stdout = '{"cache_dir": "/some/folder"}' | ||
mock_result_pixi.returncode = 0 | ||
|
||
class Usage(NamedTuple): | ||
free: int | ||
|
||
mock_usage = Usage | ||
mock_disk_usage = mocker.Mock(return_value=mock_usage(free=0)) | ||
|
||
mocker.patch( | ||
"faim_ipa.pixi.cache_status.subprocess.run", return_value=mock_result_pixi | ||
) | ||
mocker.patch( | ||
"faim_ipa.pixi.cache_status.shutil.disk_usage", side_effect=mock_disk_usage | ||
) | ||
|
||
with pytest.raises( | ||
RuntimeError, match="Disk space in cache directory is below 1 GB" | ||
): | ||
min_cache_size_gb(gb=1) | ||
|
||
pixi.cache_status.subprocess.run.assert_called_once_with( | ||
["pixi", "info", "--json"], text=True, capture_output=True, check=False | ||
) | ||
pixi.cache_status.shutil.disk_usage.assert_called_once_with("/some/folder") | ||
|
||
|
||
def test_cache_status_succeed(mocker): | ||
mock_result_pixi = mocker.Mock() | ||
mock_result_pixi.stdout = '{"cache_dir": "/some/folder"}' | ||
mock_result_pixi.returncode = 0 | ||
|
||
class Usage(NamedTuple): | ||
free: int | ||
|
||
mock_usage = Usage | ||
mock_disk_usage = mocker.Mock(return_value=mock_usage(free=2 * 1024 * 1024 * 1024)) | ||
|
||
mocker.patch( | ||
"faim_ipa.pixi.cache_status.subprocess.run", return_value=mock_result_pixi | ||
) | ||
mocker.patch( | ||
"faim_ipa.pixi.cache_status.shutil.disk_usage", side_effect=mock_disk_usage | ||
) | ||
|
||
with pytest.raises(SystemExit) as excinfo: | ||
min_cache_size_gb() | ||
assert excinfo.value.code == 0 | ||
|
||
pixi.cache_status.subprocess.run.assert_called_once_with( | ||
["pixi", "info", "--json"], text=True, capture_output=True, check=False | ||
) | ||
pixi.cache_status.shutil.disk_usage.assert_called_once_with("/some/folder") | ||
|
||
|
||
def test_log_commit(tmp_path): | ||
os.environ["WD"] = str(tmp_path) | ||
log_commit(task="TESTING") | ||
log_path = tmp_path / "githash.log" | ||
assert log_path.exists() | ||
pattern = r"^\d{4}-\d{2}-\d{2}.*githash.*INFO.*Executing.*TESTING.*git-commit\] [0-9a-f]*$" | ||
with open(log_path) as log: | ||
assert re.match(pattern=pattern, string=log.readline()) | ||
assert not log.readline() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters