diff --git a/cassini/migrate/V0_1toV0_2.py b/cassini/migrate/V0_1toV0_2.py deleted file mode 100644 index a6da9e4..0000000 --- a/cassini/migrate/V0_1toV0_2.py +++ /dev/null @@ -1,95 +0,0 @@ -import nbformat -import re - -from .base import BaseMigrator, cell_processor - - -class V0_1toV0_2(BaseMigrator): - def __init__(self, project): - self.project = project - self.home = project.home - - @cell_processor - def fix_imports(cell): - text = cell["source"] - text = text.replace("from htools.exp import g", "from htools.scify import *") - cell["source"] = text - - @cell_processor - def fix_g_references(cell): - text = cell["source"] - - def replace(match): - return f"env.{match.group(1)}" - - cell["source"] = re.sub("g\.(.+)", replace, text) - - @cell_processor - def replace_mksmpl(cell): - pattern = "%%mksmpl (.+)" - text = cell["source"] - - match = re.search(pattern, text) - if match: - cell["source"] = f"smpl = env('{match.group(1)}')\nsmpl.header()" - - @cell_processor - def replace_smplconc(cell): - text = cell["source"] - cell["source"] = text.replace("%%smplconc", "%%conc") - - @cell_processor - def replace_expconc(cell): - text = cell["source"] - cell["source"] = text.replace("%%expconc", "%%conc") - - @cell_processor - def fix_meta_store(cell): - text = cell["source"] - pattern = "smpl\.write_meta\('(.+)', (.+)\)" - - def replace(match): - return f"smpl.{match.group(1)} = {match.group(2)}" - - cell["source"] = re.sub(pattern, replace, text) - - def walk_smpls(self): - for wp in self.home: - for exp in wp: - for smpl in exp: - yield smpl - - def migrate(self): - for smpl in self.walk_smpls(): - if not smpl.file.exists(): - print(f"No file found for {smpl} - skippiping") - continue - - print("Fixing", smpl) - with open(smpl.file, "rb") as f: - nb = nbformat.read(f, 4) - - print("Backing up old file") - try: - new_name = smpl.file.rename( - smpl.file.with_name(f"{smpl.name}-old.ipynb") - ) - except FileExistsError: - print(f"Already updated {smpl} - skipping") - continue - - try: - print("Applying processors") - for cell in nb["cells"]: - for processor in self.cell_processors: - processor(cell) - print("Success") - print("Errors: ", nbformat.validate(nb, version=4)) - - print("Writing new file") - with open(smpl.file, "w", encoding="utf-8") as f: - nbformat.write(nb, f, 4) - print("Success") - except Exception: - print("Exception occured, rolling back") - new_name.rename(smpl.file.name) diff --git a/cassini/migrate/__main__.py b/cassini/migrate/__main__.py index e3dbb0f..a6a51b2 100644 --- a/cassini/migrate/__main__.py +++ b/cassini/migrate/__main__.py @@ -5,11 +5,10 @@ from cassini.utils import find_project from .base import BaseMigrator -from .V0_1toV0_2 import V0_1toV0_2 from .V0_2toV0_3 import V0_2toV0_3 -migrators = {("0.1", "0.2"): V0_1toV0_2, ("0.2", "0.3"): V0_2toV0_3} +migrators = {("0.2", "0.3"): V0_2toV0_3} def main(args: List[str]) -> BaseMigrator: @@ -19,12 +18,8 @@ def main(args: List[str]) -> BaseMigrator: "It's not that fancy, so sequential updates may be needed." ) ) - parser.add_argument( - "old", choices=["0.1", "0.2"], help="which version to migrate from" - ) - parser.add_argument( - "new", choices=["0.2", "0.3"], help="which version to migrate to" - ) + parser.add_argument("old", choices=["0.2"], help="which version to migrate from") + parser.add_argument("new", choices=["0.3"], help="which version to migrate to") parser.add_argument( "--cassini-project", default="project.py:project", diff --git a/tests/migrate/test_cli.py b/tests/migrate/test_cli.py index 8d0c0e1..c8de7b4 100644 --- a/tests/migrate/test_cli.py +++ b/tests/migrate/test_cli.py @@ -1,7 +1,6 @@ from unittest.mock import Mock from cassini.migrate.__main__ import main -from cassini.migrate.V0_1toV0_2 import V0_1toV0_2 from cassini.migrate.V0_2toV0_3 import V0_2toV0_3 from cassini import env @@ -21,13 +20,12 @@ def mock_migrator(monkeypatch): def test_migrate_selection(mock_migrator): - migrator = main(['0.1', '0.2']) - - assert isinstance(migrator, V0_1toV0_2) - migrator = main(['0.2', '0.3']) assert isinstance(migrator, V0_2toV0_3) - with pytest.raises(ValueError): + # A bit tricky to test... + """ + with pytest.raises(Exception): migrator = main(['0.1', '0.3']) + """