-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync.py
executable file
·65 lines (50 loc) · 1.45 KB
/
sync.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python3
"""
Dotfiles syncronization.
Makes symlinks for all files: ~/dotfiles/tilde/bashrc.bash => ~/.bashrc.
Based on https://gist.github.com/490016
"""
import os
import glob
import shutil
from builtins import input
SOURCE_DIR = '~/dotfiles/tilde'
EXCLUDE = []
NO_DOT_PREFIX = []
PRESERVE_EXTENSION = [
'tmux.conf',
'doom.d',
'kitty.d'
]
def force_remove(path):
if os.path.isdir(path) and not os.path.islink(path):
shutil.rmtree(path, False)
else:
os.unlink(path)
def is_link_to(link, dest):
is_link = os.path.islink(link)
is_link = is_link and os.readlink(link).rstrip('/') == dest.rstrip('/')
return is_link
def main():
os.chdir(os.path.expanduser(SOURCE_DIR))
for filename in [file for file in glob.glob('*') if file not in EXCLUDE]:
dotfile = filename
if filename not in NO_DOT_PREFIX:
dotfile = '.' + dotfile
if filename not in PRESERVE_EXTENSION:
dotfile = os.path.splitext(dotfile)[0]
dotfile = os.path.join(os.path.expanduser('~'), dotfile)
source = os.path.join(SOURCE_DIR, filename).replace('~', '.')
# Check that we aren't overwriting anything
if os.path.lexists(dotfile):
if is_link_to(dotfile, source):
continue
response = input("Overwrite file `%s'? [y/N] " % dotfile)
if not response.lower().startswith('y'):
print("Skipping `%s'..." % dotfile)
continue
force_remove(dotfile)
os.symlink(source, dotfile)
print("%s => %s" % (dotfile, source))
if __name__ == '__main__':
main()