Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Script to automatically create symlinks to config files
#!/usr/bin/env python3
from pathlib import Path
from subprocess import run
# DOTFILES is the path to where you store your dotfiles
DOTFILES = Path.home().joinpath(".dotfiles/")
# SHELL_CMD should output a list of paths to files in your dotfiles directory
# They should be separated by newlines (\ņ)
SHELL_CMD = r"fdfind --hidden --type f --exclude '\.git' --exclude '\#*' ."
# EXCLUDES is a list of strings (interpreted as paths) to ignore
EXCLUDES = [
".gitignore",
"halp",
"linker.py",
"linker.sh",
".",
]
excludes_paths = [Path(x) for x in EXCLUDES]
prcs = run(SHELL_CMD, shell=True, capture_output=True, cwd=DOTFILES)
path_str = prcs.stdout.decode("utf-8")
print("Found the following dotfiles:\n", path_str)
path_list = [Path(x) for x in path_str.split("\n")]
path_dirs = [x.parent for x in path_list]
def main():
for dir in path_dirs:
if not Path.home().joinpath(dir).is_dir():
print(f"Creating directory {dir}")
dir.mkdir(parents=True)
for pp in path_list:
home_path = Path.home().joinpath(pp)
df_path = DOTFILES.joinpath(pp)
if (not home_path.is_symlink()) and pp.name not in excludes_paths:
print(f"Linking file {df_path} to symlink {home_path}")
try:
home_path.symlink_to(df_path)
except FileExistsError as err:
print(f"File {pp} already exists at {home_path}")
print(err)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment