dotfiles/sync.py

58 lines
1.3 KiB
Python
Raw Normal View History

2012-10-10 12:51:59 +00:00
#!/usr/bin/env python
"""
Dotfiles syncronization.
2012-10-22 08:01:42 +00:00
Makes symlinks for all files: ./tilde/bashrc.bash => ~/.bashrc.
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
SOURCE_DIR = 'tilde'
EXCLUDE = []
NO_DOT_PREFIX = []
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-10-22 08:01:42 +00:00
os.chdir(os.path.join(os.path.dirname(os.path.abspath(__file__)), 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
2012-10-22 08:01:42 +00:00
dotfile = os.path.join(os.path.expanduser('~'), os.path.splitext(dotfile)[0])
2012-10-10 12:51:59 +00:00
source = os.path.relpath(filename, os.path.dirname(dotfile))
# 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()