Generates control scripts from a given container config. For now the actual generate.py does nothing. Signed-off-by: Enno Tensing <tenno@suij.in>
77 lines
2.2 KiB
Python
77 lines
2.2 KiB
Python
#!/usr/bin/env python3
|
|
"""Generate the control files.."""
|
|
|
|
import json
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
from container import ConfigError, Container
|
|
from log import Log
|
|
|
|
|
|
def load_container_config(file: Path, log: Log) -> dict | None:
|
|
"""Load a container config."""
|
|
if not file.exists():
|
|
log.log_error(f"{file.name} does not exist!")
|
|
return None
|
|
data = {}
|
|
with file.open("r", encoding="utf-8") as fp:
|
|
try:
|
|
data = json.load(fp)
|
|
except json.JSONDecodeError as e:
|
|
log.log_error(f"{file.name} is not a valid JSON file!")
|
|
log.log_debug(f"Exception: {e}")
|
|
return None
|
|
except UnicodeDecodeError as e:
|
|
log.log_error(f"{file.name} is not a valid UTF8 file!")
|
|
log.log_debug(f"Exception: {e}")
|
|
return None
|
|
except OSError as e:
|
|
log.log_error(f"{file.name} could not be read!")
|
|
log.log_debug(f"Exception: {e}")
|
|
return None
|
|
return data
|
|
|
|
|
|
def create_container_from_config(data: dict, log: Log) -> Container | None:
|
|
"""Create a container object."""
|
|
log.log_info("Creating container...")
|
|
log.log_debug(f"Container config is:\n{data}")
|
|
ct: Container | None = None
|
|
try:
|
|
ct = Container(data)
|
|
except ConfigError as e:
|
|
log.log_error(f"{e}")
|
|
return None
|
|
return ct
|
|
|
|
|
|
def main() -> None:
|
|
"""Run the program."""
|
|
argc_threshold = 2
|
|
if len(sys.argv) < argc_threshold:
|
|
logger = Log("/dev/stdout")
|
|
logger.log_error("No arguments passed!")
|
|
sys.exit(1)
|
|
config_file = ""
|
|
log_file = ""
|
|
if len(sys.argv) >= argc_threshold:
|
|
config_file = sys.argv[1]
|
|
if len(sys.argv) > argc_threshold:
|
|
log_file = sys.argv[2]
|
|
logger = Log(log_file)
|
|
data = load_container_config(Path(config_file), logger)
|
|
if data is None:
|
|
logger.log_error(f"{config_file} is invalid, aborting!")
|
|
sys.exit(1)
|
|
try:
|
|
ct = create_container_from_config(data, logger)
|
|
except Exception as e: # noqa: BLE001
|
|
logger.log_error(e)
|
|
sys.exit(1)
|
|
print(ct)
|
|
print(vars(ct))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|