From 493dc874f08544b473dea267dc07b79c70d0dc0f Mon Sep 17 00:00:00 2001 From: brb Date: Thu, 13 Jul 2023 11:29:29 +0200 Subject: [PATCH] added definition --- code/main.py | 17 +++++++++++++++++ code/runner_script.py | 16 ++++++++++++++++ code/utils.py | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 code/main.py create mode 100644 code/runner_script.py create mode 100644 code/utils.py diff --git a/code/main.py b/code/main.py new file mode 100644 index 0000000..ed0d272 --- /dev/null +++ b/code/main.py @@ -0,0 +1,17 @@ +import uvicorn +from setup_logging import setup_logging +from app import app +import logging +import os + +if __name__ == '__main__': + # setup + setup_logging() + listen_ip = os.getenv('LISTEN_IP', default='0.0.0.0') + listen_port = int(os.getenv('LISTEN_PORT', default=8000)) + logging.info(f'starting FastAPI server ({listen_ip}:{listen_port})') + uvicorn.run( + app=app, + host=listen_ip, + port=listen_port + ) \ No newline at end of file diff --git a/code/runner_script.py b/code/runner_script.py new file mode 100644 index 0000000..2fd6c55 --- /dev/null +++ b/code/runner_script.py @@ -0,0 +1,16 @@ +from pydantic import BaseModel +from typing import List +import yaml +from pathlib import Path + + +class RunnerScript(BaseModel): + script: List[str] + + +if __name__ == '__main__': + path = Path(__file__).parent.parent / 'runner_script.yml' + with open(path, 'r') as fh: + script = yaml.safe_load(fh) + runner_script = RunnerScript.parse_obj(script) + print(runner_script) \ No newline at end of file diff --git a/code/utils.py b/code/utils.py new file mode 100644 index 0000000..b80555d --- /dev/null +++ b/code/utils.py @@ -0,0 +1,33 @@ +from pydantic import AnyHttpUrl +from typing import List +from pathlib import Path +from git import Repo +import logging +import shutil +import os +import yaml +from runner_script import RunnerScript + +def clone_repository(repo_url: AnyHttpUrl) -> Path: + # extract repo name + repo_name = repo_url.split('/')[-1].replace('.git', '') + repo_dir = os.getenv('REPO_DIR', default=None) + assert repo_dir is not None + dst_dir = Path(repo_dir) / repo_name + # prepare repo download dir + if dst_dir.exists(): + shutil.rmtree(dst_dir) + dst_dir.mkdir() + # git clone repo + Repo.clone_from(url=repo_url, to_path=dst_dir) + logging.debug('finished') + return dst_dir + +def load_runner_script(path: Path) -> RunnerScript: + assert path.exists() + with open(path, 'r') as fh: + script = yaml.safe_load(fh) + runner_script = RunnerScript.parse_obj(script) + logging.debug('finished') + return runner_script + \ No newline at end of file