Reformat files

This commit is contained in:
Santiago Lo Coco 2024-11-21 00:30:00 +01:00
parent 1ea363ecbd
commit 255961b944
2 changed files with 28 additions and 14 deletions

View File

@ -1,6 +1,7 @@
import os import os
import re import re
def load_env(filename=".env"): def load_env(filename=".env"):
env_vars = {} env_vars = {}
with open(filename) as f: with open(filename) as f:
@ -10,14 +11,16 @@ def load_env(filename=".env"):
env_vars[key] = value env_vars[key] = value
return env_vars return env_vars
def count_servers(env_vars): def count_servers(env_vars):
server_keys = [key for key in env_vars if re.match(r"PORT_\d+", key)] server_keys = [key for key in env_vars if re.match(r"PORT_\d+", key)]
return len(server_keys) return len(server_keys)
def generate_docker_compose(env_vars, num_servers): def generate_docker_compose(env_vars, num_servers):
ip = env_vars.get("IP") ip = env_vars.get("IP")
services = [] services = []
for i in range(1, num_servers + 1): for i in range(1, num_servers + 1):
port = env_vars.get(f"PORT_{i}") port = env_vars.get(f"PORT_{i}")
listener_port = env_vars.get(f"LISTENER_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 - ./mediamtx/mediamtx_{i}.yml:/mediamtx.yml
""" """
services.append(service) services.append(service)
compose_content = f""" compose_content = f"""
name: streaming name: streaming
services: services:
{''.join(services)} {''.join(services)}
""" """
with open("docker-compose.yml", "w") as f: with open("docker-compose.yml", "w") as f:
f.write(compose_content) f.write(compose_content)
def generate_mediamtx_files(env_vars, num_servers): def generate_mediamtx_files(env_vars, num_servers):
# os.makedirs("mediamtx") # os.makedirs("mediamtx")
mediamtx_dir = "mediamtx" mediamtx_dir = "mediamtx"
if not os.path.exists(mediamtx_dir): if not os.path.exists(mediamtx_dir):
os.makedirs(mediamtx_dir) os.makedirs(mediamtx_dir)
with open("templates/mediamtx.yml", "r") as template_file: with open("templates/mediamtx.yml", "r") as template_file:
template_content = template_file.read() template_content = template_file.read()
for i in range(1, num_servers + 1): for i in range(1, num_servers + 1):
port = env_vars.get(f"PORT_{i}") port = env_vars.get(f"PORT_{i}")
listener_port = env_vars.get(f"LISTENER_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: with open(f"mediamtx/mediamtx_{i}.yml", "w") as f:
f.write(generated_content) f.write(generated_content)
@ -71,20 +77,21 @@ def generate_mediamtx_files(env_vars, num_servers):
os.remove(f"mediamtx/{file}") os.remove(f"mediamtx/{file}")
print(f"Removed extra file: {file}") print(f"Removed extra file: {file}")
def generate_start_bat(num_servers): def generate_start_bat(num_servers):
with open("templates/start.bat", "r") as template_file: with open("templates/start.bat", "r") as template_file:
bat_template = template_file.read() bat_template = template_file.read()
obs_instances = "" obs_instances = ""
for i in range(1, num_servers + 1): for i in range(1, num_servers + 1):
obs_instance = f""" obs_instance = f""" # noqa: E501
echo Launching OBS instance {i} 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 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 timeout /t 1 >nul
""" """
obs_instances += obs_instance obs_instances += obs_instance
bat_content = bat_template.replace("{{OBS_INSTANCES}}", obs_instances) bat_content = bat_template.replace("{{OBS_INSTANCES}}", obs_instances)
with open("start.bat", "w") as f: with open("start.bat", "w") as f:
f.write(bat_content) f.write(bat_content)

View File

@ -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(): def main():
env_vars = load_env(".env") env_vars = load_env(".env")
num_servers = count_servers(env_vars) num_servers = count_servers(env_vars)
generate_docker_compose(env_vars, num_servers) generate_docker_compose(env_vars, num_servers)
generate_mediamtx_files(env_vars, num_servers) generate_mediamtx_files(env_vars, num_servers)
generate_start_bat(num_servers) generate_start_bat(num_servers)
if __name__ == "__main__": if __name__ == "__main__":
main() main()