From 255961b944a638e648aef2b29f2d12ba1e13ca80 Mon Sep 17 00:00:00 2001 From: Santiago Lo Coco Date: Thu, 21 Nov 2024 00:30:00 +0100 Subject: [PATCH] Reformat files --- src/config.py | 29 ++++++++++++++++++----------- src/generate.py | 13 ++++++++++--- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/src/config.py b/src/config.py index 259bb7f..1073b03 100644 --- a/src/config.py +++ b/src/config.py @@ -1,6 +1,7 @@ import os import re + def load_env(filename=".env"): env_vars = {} with open(filename) as f: @@ -10,14 +11,16 @@ def load_env(filename=".env"): 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) + def generate_docker_compose(env_vars, num_servers): ip = env_vars.get("IP") services = [] - + for i in range(1, num_servers + 1): port = env_vars.get(f"PORT_{i}") listener_port = env_vars.get(f"LISTENER_PORT_{i}") @@ -34,32 +37,35 @@ def generate_docker_compose(env_vars, num_servers): - ./mediamtx/mediamtx_{i}.yml:/mediamtx.yml """ services.append(service) - + compose_content = f""" name: streaming services: {''.join(services)} """ - + with open("docker-compose.yml", "w") as f: f.write(compose_content) + 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) - + with open("templates/mediamtx.yml", "r") as template_file: template_content = template_file.read() - + for i in range(1, num_servers + 1): port = env_vars.get(f"PORT_{i}") listener_port = env_vars.get(f"LISTENER_PORT_{i}") - - generated_content = template_content.replace("{{WEBRTC_PORT}}", port).replace("{{WEBRTC_LISTENER_PORT}}", listener_port) - + + generated_content = template_content.replace("{{WEBRTC_PORT}}", port).replace( + "{{WEBRTC_LISTENER_PORT}}", listener_port + ) + with open(f"mediamtx/mediamtx_{i}.yml", "w") as f: f.write(generated_content) @@ -71,20 +77,21 @@ def generate_mediamtx_files(env_vars, num_servers): os.remove(f"mediamtx/{file}") print(f"Removed extra file: {file}") + def generate_start_bat(num_servers): with open("templates/start.bat", "r") as template_file: bat_template = template_file.read() obs_instances = "" for i in range(1, num_servers + 1): - obs_instance = f""" + obs_instance = f""" # noqa: E501 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 """ obs_instances += obs_instance - + bat_content = bat_template.replace("{{OBS_INSTANCES}}", obs_instances) - + with open("start.bat", "w") as f: f.write(bat_content) diff --git a/src/generate.py b/src/generate.py index 9b520bd..33952b9 100644 --- a/src/generate.py +++ b/src/generate.py @@ -1,13 +1,20 @@ -import os -from config import * +from config import ( + count_servers, + generate_docker_compose, + generate_mediamtx_files, + generate_start_bat, + load_env, +) + def main(): env_vars = load_env(".env") num_servers = count_servers(env_vars) - + generate_docker_compose(env_vars, num_servers) generate_mediamtx_files(env_vars, num_servers) generate_start_bat(num_servers) + if __name__ == "__main__": main()