Created
January 19, 2022 16:19
-
-
Save academo/db696829f1c6bceec1012a9a802bbbbc to your computer and use it in GitHub Desktop.
creates symlinks based on a dotfiles structure
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/pytho | |
# -*- coding: utf-8 -*- | |
import os | |
from os import path | |
from shutil import rmtree | |
homedir = os.environ["HOME"] | |
dotfiles_root = path.join(homedir, ".dotfiles") | |
dotfiles = path.join(homedir, '.dotfiles', 'dotfiles') | |
folders_whitelist = ['config', 'ssh'] | |
if (path.dirname(path.realpath(__file__)) != dotfiles_root): | |
exit("ERROR create-symlynks.py should be inside ~/.dotfiles") | |
def make_links(currentPath: str, targetFolder: str, dotsify:bool=True): | |
for el in os.listdir(currentPath): | |
fname = el | |
if dotsify: | |
fname = "." + fname | |
sourcePath = path.join(currentPath, el) | |
targetPath = path.join(targetFolder, fname) | |
if path.isfile(sourcePath): | |
pass | |
if path.isfile(targetPath): | |
print('delete '+targetPath) | |
os.remove(targetPath) | |
print("%s -> %s" % (sourcePath, targetPath)) | |
os.symlink(src=sourcePath,dst=targetPath) | |
else: | |
is_whitelisted = el in folders_whitelist | |
if path.isdir(targetPath) and not is_whitelisted: | |
print ('delete ' + targetPath) | |
if path.islink(targetPath): | |
os.unlink(targetPath) | |
else: | |
rmtree(targetPath) | |
if is_whitelisted: | |
make_links(currentPath=sourcePath, targetFolder=targetPath, dotsify=False) | |
pass | |
else: | |
print("%s -> %s" % (sourcePath, targetPath)) | |
os.symlink(src=sourcePath,dst=targetPath) | |
make_links(currentPath=dotfiles, targetFolder=homedir) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment