.dotfiles/dot.py

63 lines
1.8 KiB
Python
Raw Permalink Normal View History

2024-05-24 16:07:28 +03:00
#!/bin/python3
import os
import re
from shutil import copyfile
from toml import load
from glob import glob
from argparse import ArgumentParser
def manage_dot_files(dots, deploy=False):
home_path = os.path.expanduser("~/")
for dot in dots:
path = os.path.expanduser(dot["path"])
if deploy:
path = str(path).replace(home_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("./", home_path)
else:
dest_path = file.replace(home_path, "./")
os.makedirs(os.path.dirname(dest_path), exist_ok=True)
copyfile(file, dest_path)
print(f"{file} -> {dest_path}")
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",
)
args = parser.parse_args()
if args.init:
manage_dot_files(config)
elif args.deploy:
manage_dot_files(config, True)
else:
print("No argument passed (--init or --deploy), closing without any changes.")