from pydantic import AnyHttpUrl from pathlib import Path from git import Repo import logging import shutil import os from configparser import ConfigParser from runner_script import RunnerScript # load default values config = ConfigParser() config.read(Path(__file__).parent / 'defaults.ini') REPO_DIR = config.get('main', 'repo_dir') def clone_repository( repo_url: AnyHttpUrl, repo_dir: str = REPO_DIR ) -> Path: # extract repo name repo_name = repo_url.split('/')[-1].replace('.git', '') 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 execute_script( local_repo_dir: Path ) -> None: # locate the script script_path = ( local_repo_dir / '.gitea' / 'gpu_runner' / 'script.yml' ) # determine original working dir org_dir = os.getcwd() # initialise command list cmd_list = [ f'cd {local_repo_dir.resolve()}', f'virtualenv -p python3.10 venv', f'source venv/bin/activate', ] # add in runner script rs = RunnerScript.from_file(path=script_path) for cmd in rs.script: cmd_list.append(cmd) # append cleanup commands cmd_list.append('deactivate') cmd_list.append(f'cd {org_dir}') cmd_list.append(f'rm -rf {local_repo_dir.resolve()}') # build command string cmd_str = ' && '.join(cmd_list) os.system(cmd_str) logging.debug('finished') if __name__ == '__main__': local_repo_dir = Path(__file__).parent.parent / 'repo' / 'gpu_runner' execute_script( local_repo_dir=local_repo_dir )