Commit 031929a4 authored by Eric Duminil's avatar Eric Duminil
Browse files

Removing old projects during tests

parent 0473e661
...@@ -9,10 +9,10 @@ from .results import ( ...@@ -9,10 +9,10 @@ from .results import (
create_simstadt_results, create_simstadt_results,
) )
from .runner import run_workflow_with_citygml from .runner import run_workflow_with_citygml
from .utils import clean_old_workflows
from .workflows import greenwater_simulation, heatdemand_simulation, photovoltaic_simulation from .workflows import greenwater_simulation, heatdemand_simulation, photovoltaic_simulation
# TODO: Define Template class, with already defined Workflows? # TODO: Define Template class, with already defined Workflows?
# TODO: Define clean_old_files, and use it on Testrepo and TempRepo
__all__ = [ __all__ = [
"KPI", "KPI",
...@@ -23,6 +23,7 @@ __all__ = [ ...@@ -23,6 +23,7 @@ __all__ = [
"GreenWaterResults", "GreenWaterResults",
"SolarPotentialResults", "SolarPotentialResults",
"create_simstadt_results", "create_simstadt_results",
"clean_old_workflows",
"run_workflow_with_citygml", "run_workflow_with_citygml",
"photovoltaic_simulation", "photovoltaic_simulation",
"heatdemand_simulation", "heatdemand_simulation",
......
...@@ -3,12 +3,15 @@ import logging ...@@ -3,12 +3,15 @@ import logging
import os import os
import random import random
import re import re
import shutil
import string import string
from contextlib import contextmanager from contextlib import contextmanager
from pathlib import Path from pathlib import Path
import numpy as np import numpy as np
WORKFLOW_FOLDER_PATTERN = re.compile(r"(20\d{6}_\d{4})_[a-z0-9]{4}")
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
...@@ -34,6 +37,19 @@ def remove_random_id(text: str) -> str: ...@@ -34,6 +37,19 @@ def remove_random_id(text: str) -> str:
return re.sub(r"^(20\d{6}_\d{4}_)?[a-z0-9]{4}_", "", text) return re.sub(r"^(20\d{6}_\d{4}_)?[a-z0-9]{4}_", "", text)
def clean_old_workflows(folder: Path, pattern: str = "20*.flow", hours: int = 1) -> None:
"""Remove timestamped workflow folders older than `hours` from `folder`.
Only deletes paths whose names match the random_id timestamp pattern.
"""
cutoff = (datetime.datetime.now() - datetime.timedelta(hours=hours)).strftime("%Y%m%d_%H%M_")
for path in sorted(folder.glob(pattern)):
if m := WORKFLOW_FOLDER_PATTERN.match(path.name):
if m.group(1) < cutoff:
log.info("Removing old workflow: %s", path.name)
shutil.rmtree(path) if path.is_dir() else path.unlink()
@contextmanager @contextmanager
def chdir(path: Path): def chdir(path: Path):
"""Context manager that temporarily changes the working directory.""" """Context manager that temporarily changes the working directory."""
......
...@@ -8,6 +8,18 @@ TEST_REPOSITORY = Path(__file__).parent / "data" / "TestRepo" ...@@ -8,6 +8,18 @@ TEST_REPOSITORY = Path(__file__).parent / "data" / "TestRepo"
TEST_PROJECT_PATH = TEST_REPOSITORY / "CGSC.proj" TEST_PROJECT_PATH = TEST_REPOSITORY / "CGSC.proj"
@pytest.fixture(scope="session", autouse=True)
def clean_old_test_workflows():
"""Remove stale workflow folders from test and temp repositories before the session."""
from simstadt.runner import SIMSTADT_TEMP_REPO
from simstadt.utils import clean_old_workflows
for proj in TEST_REPOSITORY.glob("*.proj"):
clean_old_workflows(proj)
for proj in SIMSTADT_TEMP_REPO.glob("*.proj"):
clean_old_workflows(proj)
@pytest.fixture(scope="session") @pytest.fixture(scope="session")
def test_repository() -> Path: def test_repository() -> Path:
return TEST_REPOSITORY return TEST_REPOSITORY
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment