|
| 1 | +import inspect |
| 2 | +import os |
| 3 | +import subprocess |
| 4 | +from typing import Dict |
| 5 | + |
| 6 | +from piper.base.virtualenv.utils import VenvPython |
| 7 | +from piper.configurations import get_configuration |
| 8 | +from piper.utils.logger_utils import logger |
| 9 | + |
| 10 | + |
| 11 | +def copy_piper(path: str): |
| 12 | + cfg = get_configuration() |
| 13 | + from distutils.dir_util import copy_tree |
| 14 | + copy_tree(cfg.piper_path, f"{path}/piper") |
| 15 | + |
| 16 | + |
| 17 | +def copy_scripts(path: str, scripts: Dict[str, str]): |
| 18 | + for script_name, script_path in scripts.items(): |
| 19 | + with open(f"{path}/{script_name}.py", "w") as output: |
| 20 | + with open(script_path, "r") as current_file: |
| 21 | + output.write(current_file.read()) |
| 22 | + |
| 23 | + |
| 24 | +def write_requirements(path, requirements): |
| 25 | + with open(f"{path}/requirements.txt", "w") as output: |
| 26 | + output.write("\n".join(requirements)) |
| 27 | + |
| 28 | + |
| 29 | +class VirtualEnvExecutor: |
| 30 | + requirements = ["gunicorn", "fastapi", "uvicorn", "aiohttp", "docker", "Jinja2", "pydantic", "loguru"] |
| 31 | + |
| 32 | + def __init__(self): |
| 33 | + logger.info('VirtualEnvExecutor init with is_virtual_env()') |
| 34 | + |
| 35 | + cfg = get_configuration() |
| 36 | + self.project_output_path = cfg.path |
| 37 | + self.name_venv = cfg.name_venv |
| 38 | + self.number = cfg.number |
| 39 | + |
| 40 | + def scripts(self): |
| 41 | + return {"service": inspect.getfile(self.__class__)} |
| 42 | + |
| 43 | + def copy_struct_project(self): |
| 44 | + copy_piper(self.project_output_path) |
| 45 | + copy_scripts(self.project_output_path, self.scripts()) |
| 46 | + |
| 47 | + def create_files_for_venv(self): |
| 48 | + logger.info('VirtualEnvExecutor create_fast_api_files_venv()') |
| 49 | + |
| 50 | + venv_python_image = VenvPython( |
| 51 | + name_path=self.project_output_path, |
| 52 | + name_venv=self.name_venv, |
| 53 | + number=self.number, |
| 54 | + ) |
| 55 | + |
| 56 | + venv_main = venv_python_image.render_venv_python() |
| 57 | + with open(f"{self.project_output_path}/main.py", "w") as output: |
| 58 | + output.write(venv_main) |
| 59 | + |
| 60 | + venv_bash = venv_python_image.render_venv_bash() |
| 61 | + with open(f"{self.project_output_path}/create_venv.sh", "w") as output: |
| 62 | + output.write(venv_bash) |
| 63 | + |
| 64 | + write_requirements(self.project_output_path, self.requirements) |
| 65 | + |
| 66 | + process_chmod = subprocess.run(f'chmod +x {self.project_output_path}create_venv.sh', shell=True) |
| 67 | + process_run = subprocess.run(f'{self.project_output_path}create_venv.sh', shell=True) |
| 68 | + |
| 69 | + def create_files_for_tests(self): |
| 70 | + logger.info('VirtualEnvExecutor create_files_for_tests()') |
| 71 | + |
| 72 | + with open(f"{self.project_output_path}/__init__.py", "w") as output: |
| 73 | + pass |
| 74 | + |
| 75 | + tests_directory = f"{self.project_output_path}/tests" |
| 76 | + if not os.path.exists(tests_directory): |
| 77 | + os.makedirs(tests_directory) |
| 78 | + |
| 79 | + with open(f"{self.project_output_path}/tests/__init__.py", "w") as output: |
| 80 | + pass |
| 81 | + |
| 82 | + venv_python_image = VenvPython( |
| 83 | + name_path=self.project_output_path, |
| 84 | + name_venv=self.name_venv, |
| 85 | + number=self.number, |
| 86 | + ) |
| 87 | + |
| 88 | + test_main = venv_python_image.render_tests_python() |
| 89 | + with open(f"{self.project_output_path}/tests/test_main.py", "w") as output: |
| 90 | + output.write(test_main) |
| 91 | + |
| 92 | + test_bash = venv_python_image.render_tests_bash() |
| 93 | + with open(f"{self.project_output_path}/test_venv.sh", "w") as output: |
| 94 | + output.write(test_bash) |
0 commit comments