From 4bbfe9e086eec5d31165a6c8d931c2b53c642420 Mon Sep 17 00:00:00 2001 From: Danilo Horta Date: Tue, 13 Feb 2024 10:32:59 +0000 Subject: [PATCH] Remove snap archive creation in scan-run --- python-core/deciphon_core/scan.py | 2 -- python-core/deciphon_core/schema.py | 18 ++++++++++++++++++ python-core/pyproject.toml | 2 +- python-core/tests/test_scan.py | 2 ++ python-core/tests/test_snap.py | 25 +++++++++++++++++++++++++ 5 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 python-core/tests/test_snap.py diff --git a/python-core/deciphon_core/scan.py b/python-core/deciphon_core/scan.py index 52bf7b1f..35bc81c5 100644 --- a/python-core/deciphon_core/scan.py +++ b/python-core/deciphon_core/scan.py @@ -40,8 +40,6 @@ def run(self, snap: NewSnapFile): if rc := lib.scan_run(self._cscan, str(snap.basename).encode()): raise DeciphonError(rc) - snap.make_archive() - def interrupted(self) -> bool: return lib.scan_interrupted(self._cscan) diff --git a/python-core/deciphon_core/schema.py b/python-core/deciphon_core/schema.py index c32ee2f6..b0c4e05d 100644 --- a/python-core/deciphon_core/schema.py +++ b/python-core/deciphon_core/schema.py @@ -120,6 +120,24 @@ def must_have_extension(cls, x: FilePath): class NewSnapFile(BaseModel): path: Path + @classmethod + def create_from_prefix(cls, prefix: str): + try: + x = cls(path=Path(f"{prefix}.dcs").absolute()) + except ValueError: + for i in range(1, 1001): + try: + x = cls(path=Path(f"{prefix}.{i}.dcs").absolute()) + except ValueError: + continue + else: + break + else: + raise ValueError( + f"failed to find a noncolliding filename for prefix {prefix}" + ) + return x + @field_validator("path") def must_have_extension(cls, x: Path): if x.suffix != ".dcs": diff --git a/python-core/pyproject.toml b/python-core/pyproject.toml index 057091e0..15ee3f63 100644 --- a/python-core/pyproject.toml +++ b/python-core/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "deciphon-core" -version = "0.20.17" +version = "0.21.0" description = "Python wrapper around the Deciphon C library" authors = ["Danilo Horta "] license = "MIT" diff --git a/python-core/tests/test_scan.py b/python-core/tests/test_scan.py index 611616ed..2dbfc25b 100644 --- a/python-core/tests/test_scan.py +++ b/python-core/tests/test_scan.py @@ -46,6 +46,8 @@ def test_scan(tmp_path, files_path: Path): for seq in sequences: scan.add(seq) scan.run(snapfile) + assert not scan.interrupted() + snapfile.make_archive() assert scan.progress() == 100 shutil.unpack_archive(snapfile.path, format="zip") diff --git a/python-core/tests/test_snap.py b/python-core/tests/test_snap.py new file mode 100644 index 00000000..50e456ef --- /dev/null +++ b/python-core/tests/test_snap.py @@ -0,0 +1,25 @@ +import os +from pathlib import Path + +from deciphon_core.schema import NewSnapFile + + +def test_collision(tmp_path: Path): + os.chdir(tmp_path) + + x0 = NewSnapFile.create_from_prefix("snap") + x0.basename.mkdir() + + x1 = NewSnapFile.create_from_prefix("snap") + x1.basename.mkdir() + + x2 = NewSnapFile.create_from_prefix("snap") + x2.basename.mkdir() + + x0.make_archive() + x1.make_archive() + x2.make_archive() + + assert x0.basename.name == "snap" + assert x1.basename.name == "snap.1" + assert x2.basename.name == "snap.2"