From 617ffc0b14bb30349f9cdd425e4e75d672b1028b Mon Sep 17 00:00:00 2001 From: Santiago Lo Coco Date: Thu, 21 Nov 2024 00:58:54 +0100 Subject: [PATCH] Fix more bugs and use dotenv library --- docker/Dockerfile | 4 ++++ src/config.py | 17 +++-------------- src/generate.py | 6 ++++-- 3 files changed, 11 insertions(+), 16 deletions(-) diff --git a/docker/Dockerfile b/docker/Dockerfile index 8abdc94..75a48c1 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -4,6 +4,10 @@ ENV PYTHONUNBUFFERED=1 WORKDIR /app/data +COPY requirements.txt . + +RUN pip install --no-cache-dir -r requirements.txt + COPY config.py /app/bin/ COPY generate.py /app/bin/ diff --git a/src/config.py b/src/config.py index 1073b03..e2a21db 100644 --- a/src/config.py +++ b/src/config.py @@ -2,16 +2,6 @@ import os import re -def load_env(filename=".env"): - env_vars = {} - with open(filename) as f: - for line in f: - if "=" in line: - key, value = line.strip().split("=", 1) - env_vars[key] = value - return env_vars - - def count_servers(env_vars): server_keys = [key for key in env_vars if re.match(r"PORT_\d+", key)] return len(server_keys) @@ -50,7 +40,6 @@ services: def generate_mediamtx_files(env_vars, num_servers): - # os.makedirs("mediamtx") mediamtx_dir = "mediamtx" if not os.path.exists(mediamtx_dir): os.makedirs(mediamtx_dir) @@ -69,7 +58,7 @@ def generate_mediamtx_files(env_vars, num_servers): with open(f"mediamtx/mediamtx_{i}.yml", "w") as f: f.write(generated_content) - existing_files = os.listdir("mediamtx") + existing_files = os.listdir(mediamtx_dir) for file in existing_files: if file.startswith("mediamtx_"): file_num = int(file.split("_")[1].split(".")[0]) @@ -84,11 +73,11 @@ def generate_start_bat(num_servers): obs_instances = "" for i in range(1, num_servers + 1): - obs_instance = f""" # noqa: E501 + obs_instance = f""" echo Launching OBS instance {i} start /MIN "OBS{i}" /D "C:\\Program Files\\obs-studio\\bin\\64bit" "obs64.exe" --profile "Profile{i}" --collection "Profile{i}" --multi --startstreaming timeout /t 1 >nul -""" +""" # noqa: E501 obs_instances += obs_instance bat_content = bat_template.replace("{{OBS_INSTANCES}}", obs_instances) diff --git a/src/generate.py b/src/generate.py index 33952b9..185ccaf 100644 --- a/src/generate.py +++ b/src/generate.py @@ -1,14 +1,16 @@ +from dotenv import dotenv_values + from config import ( count_servers, generate_docker_compose, generate_mediamtx_files, generate_start_bat, - load_env, ) def main(): - env_vars = load_env(".env") + env_vars = dotenv_values(".env") + num_servers = count_servers(env_vars) generate_docker_compose(env_vars, num_servers)