From e2f6a0a995267b38369368bae7c315fe5d333085 Mon Sep 17 00:00:00 2001 From: Matthew Gordon Date: Sat, 2 Mar 2024 19:20:30 -0400 Subject: [PATCH] Break dev scripts into modules --- .gitignore | 3 +- dev.py | 79 +++++------------------------------ scripts/postgres_container.py | 48 +++++++++++++++++++++ scripts/run.py | 32 ++++++++++++++ 4 files changed, 92 insertions(+), 70 deletions(-) create mode 100644 scripts/postgres_container.py create mode 100644 scripts/run.py diff --git a/.gitignore b/.gitignore index 3d52885..ea79bb3 100644 --- a/.gitignore +++ b/.gitignore @@ -20,4 +20,5 @@ Cargo.lock # MSVC Windows builds of rustc generate these, which store debugging information *.pdb -/.python_venv \ No newline at end of file +/.python_venv +__pycache__ \ No newline at end of file diff --git a/dev.py b/dev.py index fa62fb1..2d8d9c8 100644 --- a/dev.py +++ b/dev.py @@ -1,84 +1,25 @@ import argparse import os import subprocess -import shutil import sys -import time root_dir = sys.path[0] def devupdate(args): - subprocess.check_call( - [ - sys.executable, - '-m', - 'pip', - 'install', - '-r', - os.path.join(root_dir, 'dev-python-requirements.txt'), - ] - ) + with subprocess.Popen( + [sys.executable, '-m', 'pip', 'install', '-r', + os.path.join(root_dir, 'dev-python-requirements.txt'), ], + stdout=subprocess.PIPE, stderr=subprocess.STDOUT, bufsize=1, + universal_newlines=True) as pip: + for line in pip.stdout: + print(line, end='', flush=True) def run(args): - import docker - - POSTGRES_USER = 'locality' - POSTGRES_PASSWORD = 'wkyhjofg2837f' - POSTGRES_DB = 'locality' - - postgres_env = { - 'POSTGRES_USER': POSTGRES_USER, - 'POSTGRES_PASSWORD': POSTGRES_PASSWORD, - 'POSTGRES_DB': POSTGRES_DB, - } - - locality_env = { - 'LOCALITY_DATABASE_URL': 'postgres://{}:{}@localhost/{}'.format( - POSTGRES_USER, - POSTGRES_PASSWORD, - POSTGRES_DB), - 'LOCALITY_STATIC_FILE_PATH': os.path.join( - root_dir, - 'static'), - 'LOCALITY_HMAC_SECRET': 'iknf4390-8guvmr3' - } - - docker_client = docker.from_env() - postgres_container = docker_client.containers.run( - 'postgres:14.11', - environment=postgres_env, - detach=True, - ports={'5432/tcp': ('127.0.0.1', 5432)}, - ) - try: - docker_stream = postgres_container.attach(stream=True) - for line_binary in docker_stream: - line = line_binary.decode('utf-8') - print(line) - if 'listening on IPv4 address "0.0.0.0", port 5432' in line: - break - for line_binary in docker_stream: - line = line_binary.decode('utf-8') - print(line) - if 'database system is ready to accept connections' in line: - break - - cargo_bin = shutil.which('cargo') - locality_process = subprocess.Popen( - [cargo_bin, 'run'], env=locality_env, cwd=root_dir - ) - - try: - while locality_process.poll() is None: - time.sleep(0.5) - finally: - if locality_process.poll() is None: - locality_process.terminate() - finally: - postgres_container.stop() - postgres_container.remove() + import scripts.run + scripts.run.ROOT_DIR = root_dir # TODO: FIXME + scripts.run.run(args) parser = argparse.ArgumentParser() diff --git a/scripts/postgres_container.py b/scripts/postgres_container.py new file mode 100644 index 0000000..7751450 --- /dev/null +++ b/scripts/postgres_container.py @@ -0,0 +1,48 @@ +import docker +import os + + +class PostgresContainer(): + def __init__(self): + self.postgres_user = 'locality' + self.postgres_password = 'wkyhjofg2837f' + self.postgres_db = 'locality' + + def get_url(self): + return 'postgres://{}:{}@localhost/{}'.format( + self.postgres_user, + self.postgres_password, + self.postgres_db) + + def __enter__(self): + postgres_env = { + 'POSTGRES_USER': self.postgres_user, + 'POSTGRES_PASSWORD': self.postgres_password, + 'POSTGRES_DB': self.postgres_db, + } + + self.docker_client = docker.from_env() + self.postgres_container = self.docker_client.containers.run( + 'postgres:14.11', + environment=postgres_env, + detach=True, + ports={'5432/tcp': ('127.0.0.1', 5432)}, + ) + + self.docker_stream = self.postgres_container.attach(stream=True) + for line_binary in self.docker_stream: + line = line_binary.decode('utf-8') + print(line) + if 'listening on IPv4 address "0.0.0.0", port 5432' in line: + break + for line_binary in self.docker_stream: + line = line_binary.decode('utf-8') + print(line) + if 'database system is ready to accept connections' in line: + break + + return self + + def __exit__(self, type, value, traceback): + self.postgres_container.stop() + self.postgres_container.remove() diff --git a/scripts/run.py b/scripts/run.py new file mode 100644 index 0000000..8ae177a --- /dev/null +++ b/scripts/run.py @@ -0,0 +1,32 @@ +import os +import shutil +import subprocess +import time + +from .postgres_container import PostgresContainer + +ROOT_DIR = None + + +def run(args): + global ROOT_DIR + with PostgresContainer() as postgres: + locality_env = { + 'LOCALITY_DATABASE_URL': postgres.get_url(), + 'LOCALITY_STATIC_FILE_PATH': os.path.join( + ROOT_DIR, + 'static'), + 'LOCALITY_HMAC_SECRET': 'iknf4390-8guvmr3' + } + + cargo_bin = shutil.which('cargo') + locality_process = subprocess.Popen( + [cargo_bin, 'run'], env=locality_env, cwd=ROOT_DIR + ) + + try: + while locality_process.poll() is None: + time.sleep(0.5) + finally: + if locality_process.poll() is None: + locality_process.terminate()