101 lines
2.8 KiB
Python
Executable File
101 lines
2.8 KiB
Python
Executable File
#!/bin/python3
|
|
|
|
import os
|
|
import re
|
|
import shutil
|
|
from toml import load
|
|
from glob import glob
|
|
from argparse import ArgumentParser
|
|
|
|
FG_GREEN = "\x1b[32m"
|
|
FG_RED = "\x1b[31m"
|
|
BACKUP_FOLDER = "./backup"
|
|
|
|
|
|
def is_root() -> bool:
|
|
"""
|
|
Not the most "correct" way but since this is for personal usage I don't care
|
|
"""
|
|
return os.geteuid() == 0
|
|
|
|
|
|
def manage_dot_files(dots, deploy=False, wipe=False):
|
|
if wipe:
|
|
# Nuke everything and bring the new stuff in
|
|
# Easy way to remove old stuff thats no longer linked in dot.toml
|
|
if not deploy:
|
|
shutil.rmtree(BACKUP_FOLDER)
|
|
|
|
os.makedirs(BACKUP_FOLDER, exist_ok=True)
|
|
|
|
for dot in dots:
|
|
path = os.path.expanduser(dot["path"])
|
|
if deploy:
|
|
path = BACKUP_FOLDER + path
|
|
|
|
ignore_list = dot["ignore"] if "ignore" in dot else []
|
|
for file in glob(path, recursive=True):
|
|
exp_pass = True
|
|
for exp in ignore_list:
|
|
match = re.search(exp, file)
|
|
if match is not None:
|
|
exp_pass = False
|
|
break
|
|
|
|
if exp_pass and os.path.isfile(file):
|
|
if deploy:
|
|
dest_path = file.replace(BACKUP_FOLDER, "")
|
|
else:
|
|
dest_path = BACKUP_FOLDER + file
|
|
os.makedirs(os.path.dirname(dest_path), exist_ok=True)
|
|
try:
|
|
shutil.copy(file, dest_path)
|
|
print(f"{FG_GREEN}{file} -> {dest_path}")
|
|
except OSError:
|
|
print(f"{FG_RED}{file} requires root access to write, skipping")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
config = load("./dot.toml")["dots"]
|
|
|
|
parser = ArgumentParser()
|
|
parser.add_argument(
|
|
"--init",
|
|
action="store_true",
|
|
help="Copies all the specified files in the current directory",
|
|
required=False,
|
|
default=False,
|
|
dest="init",
|
|
)
|
|
parser.add_argument(
|
|
"--deploy",
|
|
action="store_true",
|
|
help="Deploys all dot files in the current directory to their locations",
|
|
required=False,
|
|
default=False,
|
|
dest="deploy",
|
|
)
|
|
parser.add_argument(
|
|
"--wipe",
|
|
action="store_true",
|
|
help="Wipes the backup folder before bringing in the new backups, only works with --init flag",
|
|
required=False,
|
|
default=True,
|
|
dest="wipe",
|
|
)
|
|
parser.add_argument(
|
|
"--no-wipe",
|
|
action="store_false",
|
|
required=False,
|
|
default=False,
|
|
dest="wipe",
|
|
)
|
|
args = parser.parse_args()
|
|
|
|
if args.init:
|
|
manage_dot_files(config, False, args.wipe)
|
|
elif args.deploy:
|
|
manage_dot_files(config, True)
|
|
else:
|
|
print("No argument passed (--init or --deploy), closing without any changes.")
|