feat(projects): automate project infrastructure provisioning
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
from collections.abc import Iterator
|
||||
from contextlib import contextmanager
|
||||
from pathlib import Path
|
||||
from tempfile import NamedTemporaryFile
|
||||
|
||||
from app.native.wndb.inp.exporter import dump_inp
|
||||
|
||||
|
||||
PROJECT_INP_DIRECTORY = Path("db_inp")
|
||||
|
||||
|
||||
@contextmanager
|
||||
def temporary_project_inp(
|
||||
project_code: str,
|
||||
*,
|
||||
purpose: str,
|
||||
version: str = "2",
|
||||
) -> Iterator[Path]:
|
||||
"""Export one project model to an isolated INP and remove it afterwards."""
|
||||
PROJECT_INP_DIRECTORY.mkdir(parents=True, exist_ok=True)
|
||||
with NamedTemporaryFile(
|
||||
dir=PROJECT_INP_DIRECTORY,
|
||||
prefix=f"{purpose}_",
|
||||
suffix=".inp",
|
||||
delete=False,
|
||||
) as temporary_file:
|
||||
path = Path(temporary_file.name).resolve()
|
||||
|
||||
try:
|
||||
dump_inp(project_code, str(path), version)
|
||||
if not path.is_file() or path.stat().st_size == 0:
|
||||
raise ValueError(f"项目 {project_code!r} 的 INP 导出失败")
|
||||
yield path
|
||||
finally:
|
||||
path.unlink(missing_ok=True)
|
||||
Reference in New Issue
Block a user