1
Fork 0

Updated swash run with log file

This commit is contained in:
Edgar P. Burkhart 2022-03-03 11:49:59 +01:00
parent c456d3d671
commit 1449b66917
Signed by: edpibu
GPG key ID: 9833D3C5A25BD227

View file

@ -1,3 +1,6 @@
import logging
import sys
import argparse
import pathlib
import subprocess
import configparser
@ -5,24 +8,41 @@ import shutil
import tempfile
parser = argparse.ArgumentParser(description="Run swash model")
parser.add_argument("-v", "--verbose", action="count", default=0)
args = parser.parse_args()
logging.basicConfig(level=max((10, 20 - 10 * args.verbose)))
log = logging.getLogger("swash")
log.info("Starting swash model")
config = configparser.ConfigParser()
config.read("config.ini")
inp = pathlib.Path(config.get("swash", "input"))
out = pathlib.Path(config.get("swash", "out"))
if out.exists():
log.error(f"Swash output '{out}' already exists")
sys.exit(1)
with tempfile.TemporaryDirectory(prefix="swash_", dir=".") as tmp_raw:
tmpdir = pathlib.Path(tmp_raw)
log.info(f"Copying files to '{tmpdir}'")
shutil.copy2(inp, tmpdir)
shutil.copytree(
pathlib.Path(config.get("data", "out")), tmpdir, dirs_exist_ok=True
)
with open(tmpdir.joinpath("sws.log"), "w") as logfile:
log.info(f"Runing swash in '{tmpdir}'")
subprocess.run(
(config.get("swash", "swashrun"), "-input", inp.name),
cwd=tmpdir,
stdout=logfile,
stderr=logfile,
)
subprocess.run(
(config.get("swash", "swashrun"), "-input", inp.name), cwd=tmpdir
)
if out.exists():
shutil.rmtree(out)
log.info(f"Moving swash output to '{out}'")
shutil.move(tmpdir, out)
log.info(f"Swash model finished successfully")