standard-build/scripts/setup-generator.py

52 lines
2.0 KiB
Python
Raw Normal View History

import glob
import importlib
import sys
import subprocess
2022-09-23 21:57:08 +01:00
with open("setup-template.py", "r") as f:
setup_template = f.read()
directories = glob.glob("*/", recursive=False)
2022-09-26 14:35:52 +01:00
if len(sys.argv) >= 2:
directories = sys.argv[1:]
2022-09-23 21:57:08 +01:00
for directory in directories:
if ".." in directory:
raise OSError("Illegal directory path")
2022-09-27 18:33:20 +01:00
if not glob.glob(f"{directory}/manifest.dat"):
continue
2022-09-23 21:57:08 +01:00
current_setup_template = setup_template
2022-09-27 18:33:20 +01:00
2022-09-23 21:57:08 +01:00
with open(f"{directory}/manifest.dat", "r") as f:
2022-09-26 14:47:46 +01:00
manifest_info = [line_info.split("\n@ ")[1:] for line_info in f.read().split("%")[1:]]
2022-09-23 21:57:08 +01:00
2022-09-26 14:49:33 +01:00
for replacement in manifest_info:
2022-09-26 14:46:19 +01:00
if len(replacement) == 2:
2022-09-26 14:43:44 +01:00
replacement.append("-1")
2022-09-26 14:49:33 +01:00
2022-09-26 14:53:02 +01:00
print(replacement[0].rstrip(), replacement[1].rstrip(), int(replacement[2].rstrip().rstrip("\n")))
2022-09-26 15:04:24 +01:00
current_setup_template = current_setup_template.replace(replacement[0].rstrip(), replacement[1].rstrip(), int(replacement[2].rstrip().rstrip("\n")))
2022-09-23 21:57:08 +01:00
2023-08-24 16:12:45 +01:00
if replacement[0] == "#service":
with open(f"{directory}/relaunch-docker.sh", "w") as f:
f.write("#!/bin/sh\n")
f.write("cd \"${0%/*}\"\n")
f.write(f"docker pull $(docker inspect {replacement[1].strip()} | jq -r '.[0].Config.Image')\n")
f.write(f"docker stop {replacement[1].strip()}\n")
f.write(f"docker rm {replacement[1].strip()}\n")
2023-08-24 16:12:45 +01:00
f.write(f"sh launch-docker.sh\n")
subprocess.run(["chmod", "+x", f"{directory}/relaunch-docker.sh"])
2023-08-24 16:12:45 +01:00
2022-09-23 21:57:08 +01:00
if glob.glob(f"{directory}/parser.py"):
should_continue = input(f"Custom instructions found in {directory}, continue? (Y/N) ")
if should_continue == "Y":
parser = importlib.import_module(f"{directory}/parser.py")
current_setup_template = parser.parse_template(current_setup_template)
else:
continue
with open(f"{directory}/setup.py", "w") as f:
f.write(current_setup_template)