#!/usr/bin/env python3 import errno import os import pathlib import sys dirname = pathlib.Path(__file__).parent if dirname != pathlib.Path().cwd(): print('This script must be run from the dotfiles directory.') sys.exit(1) script = os.path.basename(__file__) def link_file(target, name): try: os.symlink(target, name) print('\033[36;1m' + name + '\033[0m ->', target) except OSError as e: if e.errno != errno.EEXIST: raise try: if os.readlink(name) == target: print(name, 'is already linked') else: print(name, '\033[31;1mexists\033[0m (symlink)') except OSError as e: if e.errno == errno.EINVAL: print(name, '\033[31;1mexists\033[0m') else: raise for f in os.listdir('.'): if f.startswith('.') or f == script: continue name = '../.' + f target = 'dotfiles/' + f if f in ['vim', 'zsh_fast_syntax_highlighting'] or not os.path.isdir(f): # special cases where we symlink the whole directory link_file(target, name) else: # everywhere else, we create the directory and symlink the contents for dirpath, _, filenames in os.walk(f): try: dirname = '../.' + dirpath os.mkdir(dirname) print('Created \033[34;1m' + dirname + '\033[0m') except OSError as e: if e.errno == errno.EEXIST: print('No need to create', dirname) else: raise prefix = '../' * (dirpath.count('/') + 1) for f in filenames: link_file(prefix + 'dotfiles/' + dirpath + '/' + f, '../.' + dirpath + '/' + f) # config/nvim is skipped because we ignore dirs from os.walk link_file('../dotfiles/vim', '../.config/nvim') vim_swap = '../.vim_swap' if os.path.exists(vim_swap): print('No need to create', vim_swap) else: os.mkdir(vim_swap, 0o700) print('Created \033[34;1m' + vim_swap + '\033[0m')