dotfiles/sync.py

63 lines
1.4 KiB
Python
Raw Normal View History

2012-10-10 12:51:59 +00:00
#!/usr/bin/env python
"""
Dotfiles syncronization.
2012-11-28 08:15:02 +00:00
Makes symlinks for all files: ~/dotfiles/tilde/bashrc.bash => ~/.bashrc.
2012-10-22 08:01:42 +00:00
Based on https://gist.github.com/490016
2012-10-10 12:51:59 +00:00
"""
import os
import glob
2012-10-22 08:01:42 +00:00
import shutil
2012-11-28 08:51:21 +00:00
SOURCE_DIR = '~/dotfiles/tilde'
2012-10-22 08:01:42 +00:00
EXCLUDE = []
NO_DOT_PREFIX = []
2013-10-11 13:16:12 +00:00
PRESERVE_EXTENSION = [
'slate.js'
]
2012-10-10 12:51:59 +00:00
def force_remove(path):
if os.path.isdir(path) and not os.path.islink(path):
2012-10-22 08:01:42 +00:00
shutil.rmtree(path, False)
2012-10-10 12:51:59 +00:00
else:
os.unlink(path)
2012-10-22 08:01:42 +00:00
def is_link_to(link, dest):
2012-10-10 12:51:59 +00:00
is_link = os.path.islink(link)
is_link = is_link and os.readlink(link).rstrip('/') == dest.rstrip('/')
return is_link
2012-10-22 08:01:42 +00:00
2012-10-10 12:51:59 +00:00
def main():
2012-11-28 08:51:21 +00:00
os.chdir(os.path.expanduser(SOURCE_DIR))
2012-10-10 12:51:59 +00:00
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
2013-10-11 13:16:12 +00:00
if filename not in PRESERVE_EXTENSION:
dotfile = os.path.splitext(dotfile)[0]
dotfile = os.path.join(os.path.expanduser('~'), dotfile)
2012-11-28 08:51:21 +00:00
source = os.path.join(SOURCE_DIR, filename).replace('~', '.')
2012-10-10 12:51:59 +00:00
# Check that we aren't overwriting anything
2012-10-22 08:32:31 +00:00
if os.path.lexists(dotfile):
2012-10-10 12:51:59 +00:00
if is_link_to(dotfile, source):
continue
response = raw_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)
2012-10-22 08:01:42 +00:00
2012-10-10 12:51:59 +00:00
if __name__ == '__main__':
2012-10-22 08:01:42 +00:00
main()