77 lines
2.8 KiB
Python
77 lines
2.8 KiB
Python
import argparse
|
|
import logging
|
|
import logging.config
|
|
import pathlib
|
|
import shutil
|
|
import subprocess
|
|
import sys
|
|
import tomllib
|
|
|
|
from . import get_io
|
|
|
|
|
|
logging.config.fileConfig(pathlib.Path(__file__).with_name("log.conf"))
|
|
log = logging.getLogger("config")
|
|
|
|
_argp = argparse.ArgumentParser(
|
|
prog="Config",
|
|
description="Install custom configurations",
|
|
)
|
|
|
|
_argp.add_argument("-c", "--config", default="config.toml", type=pathlib.Path, help="Configuration file location")
|
|
|
|
args = _argp.parse_args()
|
|
|
|
log.info("Config installation starting")
|
|
|
|
log.info(f"Loading configuration file <{args.config}>.")
|
|
try:
|
|
with open(args.config, "rb") as _config_file:
|
|
config = tomllib.load(_config_file)
|
|
except FileNotFoundError:
|
|
log.error(f"Configuration file <{args.config}> does not exist.")
|
|
sys.exit(1)
|
|
|
|
for _name, _conf in config.items():
|
|
match _name.lower():
|
|
case "winget":
|
|
log.info("Installing packages using winget.")
|
|
for _package in _conf.get("packages"):
|
|
_test = subprocess.run(("winget", "list", "--id", _package))
|
|
if _test.returncode == 0:
|
|
_yn = input(f"<{_package}> is already installed; reinstall [Yn] ? ").strip().lower()
|
|
if _yn != "y" and _yn != "":
|
|
log.info(f"> Skipping <{_package}> install.")
|
|
continue
|
|
log.info(f"> Installing {_package} using winget.")
|
|
subprocess.run(("winget", "install", _package))
|
|
case _:
|
|
_in, _out = get_io(_conf)
|
|
log.info(f"Installing {_name} configuration from <{_in}> to <{_out}>.")
|
|
if not _in.is_file():
|
|
log.error(f"> {_name} configuration <{_in}> could not be found.")
|
|
sys.exit(1)
|
|
|
|
if _out.exists():
|
|
if _out.is_file():
|
|
_yn = input(f"<{_out}> already exists; replace [Yn] ? ").strip().lower()
|
|
if _yn != "y" and _yn != "":
|
|
log.info(f"> Skipping {_name} configuration.")
|
|
continue
|
|
else:
|
|
log.info(f"> Installing {_name} configuration.")
|
|
else:
|
|
log.error(f"> <{_out}> already exists and is not a file.")
|
|
sys.exit(1)
|
|
|
|
if not _out.parent.exists():
|
|
_out.parent.mkdir(parents=True)
|
|
elif not _out.parent.is_dir():
|
|
log.error(f"> {_name} configuration could not be installed as <{_out.parent}> is not a directory.")
|
|
sys.exit(1)
|
|
|
|
try:
|
|
shutil.copyfile(_in, _out)
|
|
except OSError:
|
|
log.error(f"> {_name} could not be configured: destination <{_out}> is not writable.")
|
|
sys.exit(1)
|