105 lines
2.8 KiB
Python
Executable file
105 lines
2.8 KiB
Python
Executable file
from pathlib import Path
|
|
|
|
import yaml
|
|
import subprocess
|
|
|
|
cwd = Path(__file__).parent.parent
|
|
|
|
variables = {
|
|
"$home": str(Path.home())
|
|
}
|
|
|
|
def replace_variables(string: str):
|
|
for variable in variables:
|
|
string = string.replace(variable, variables[variable])
|
|
return string
|
|
|
|
def get_files(folder: Path, files: list[str]) -> set[Path]:
|
|
result = set()
|
|
for file in files:
|
|
if not (folder / file).exists():
|
|
for d in (folder).glob(file):
|
|
result.add(d)
|
|
else:
|
|
result.add(folder / file)
|
|
return result
|
|
|
|
def is_enabled(config: dict, key: str) -> bool:
|
|
if "disabled" in config:
|
|
if config["disabled"] and key in config["disabled"]:
|
|
return False
|
|
key_parts = key.split(".")
|
|
parent = config
|
|
for part in key_parts:
|
|
if part in parent:
|
|
parent = parent[part]
|
|
else:
|
|
return False
|
|
return True
|
|
|
|
# Setup dotfiles
|
|
|
|
def set_dotfiles(folder: str, config: dict):
|
|
all_files = set()
|
|
for where in config:
|
|
files = get_files(cwd / folder, config[where]).difference(all_files)
|
|
for file in files:
|
|
where_file = Path(replace_variables(where)) / file.relative_to(cwd / folder)
|
|
if where_file.exists():
|
|
where_file.unlink()
|
|
where_file.symlink_to(file)
|
|
all_files.update(files)
|
|
|
|
# Install GNOME extensions
|
|
|
|
def install_gnome_extension(ext):
|
|
#p = subprocess.Popen(f"{cwd / '_scripts' / 'gnome-install-extension.sh'} {ext}", shell=True)
|
|
p = subprocess.Popen(f"busctl --user call org.gnome.Shell.Extensions /org/gnome/Shell/Extensions org.gnome.Shell.Extensions InstallRemoteExtension s {ext}", shell=True)
|
|
p.wait()
|
|
|
|
# Import / Export GNOME settings
|
|
|
|
def configure_gnome(file: str, path: str, export: bool = False):
|
|
dconf = cwd / "gnome" / (file+".dconf");
|
|
p = subprocess.Popen(f"dconf {'dump' if export else 'load'} {path} {'>' if export else '<'} {dconf}", shell=True)
|
|
p.wait()
|
|
|
|
# Main part
|
|
|
|
def main():
|
|
|
|
config_file = cwd / "config.yml"
|
|
if not config_file.exists():
|
|
return
|
|
config = yaml.safe_load(config_file.read_text())
|
|
if not config:
|
|
return
|
|
|
|
if is_enabled(config, "dotfiles"):
|
|
print("Installing dotfiles...")
|
|
for folder in config["dotfiles"]:
|
|
print(f" - {folder}")
|
|
set_dotfiles(folder, config["dotfiles"][folder])
|
|
|
|
if is_enabled(config, "gnome"):
|
|
|
|
if is_enabled(config, "gnome.extensions"):
|
|
print("Installing GNOME extensions...")
|
|
for ext in config["gnome"]["extensions"]:
|
|
print(f" - {ext}")
|
|
install_gnome_extension(ext)
|
|
|
|
if is_enabled(config, "gnome.import"):
|
|
print("Importing GNOME settings...")
|
|
for file in config["gnome"]["import"]:
|
|
print(f" - {file}")
|
|
configure_gnome(file, config["gnome"]["import"][file])
|
|
|
|
if is_enabled(config, "gnome.export"):
|
|
print("Exporting GNOME settings...")
|
|
for file in config["gnome"]["export"]:
|
|
print(f" - {file}")
|
|
configure_gnome(file, config["gnome"]["export"][file], True)
|
|
|
|
main()
|