Refactor autolink to handle symlinks some cases better, still bad

This commit is contained in:
Buddy Sandidge 2013-12-16 16:54:01 -08:00
parent 0190b8beca
commit 3e75519161

View File

@ -1,4 +1,4 @@
#!/usr/bin/env python #!/usr/bin/env python2
""" """
A simple script to create a symlinks in your home directory for every filename A simple script to create a symlinks in your home directory for every filename
in this directory. in this directory.
@ -7,32 +7,30 @@ For example:
~/.bashrc -> dot-files/bashrc ~/.bashrc -> dot-files/bashrc
""" """
import os from os import getcwd, listdir, getenv, symlink, remove
from os.path import (dirname, basename, join, expanduser, lexists, exists,
relpath, normpath, isfile, islink, realpath, abspath)
def log(filename, message): def log(filename, message):
"""Simple logging function""" """Simple logging function"""
name = ("'%s':" % filename).ljust(30, ' ') name = "'{0}':".format(filename).ljust(30, ' ')
print "%s\t%s" % (name, message) print "{0}\t{0}".format(name, message)
def dir_filter(file_name):
return file_name != basename(__file__) and \
file_name != 'README.md' and \
file_name[0] != '.'
def main(): def main():
"""Create the symlinks""" """Create the symlinks"""
abs_path = os.path.abspath(os.path.dirname(__file__)) dir_path = dirname(__file__)
home_dir = os.path.expanduser('~') base_name = dirname(normpath(join(getenv('PWD'), __file__)))
this_script = os.path.basename(__file__) home_dir = expanduser('~')
rel_path = abs_path.replace(home_dir, '.') rel_path = normpath(relpath(base_name, home_dir))
for filename in os.listdir(abs_path): for filename in filter(dir_filter, listdir(dirname(abspath(__file__)))):
if filename == this_script: config_path = join(home_dir, ".{0}".format(filename))
continue file_path = join(rel_path, filename)
if filename == 'README.md':
continue
config_path = os.path.join(home_dir, ".%s" % filename)
file_path = os.path.join(rel_path, filename)
# If the current file is a hidden file, ignore it
if filename[0] == '.':
continue
# If the current file is going to be linked deeper in the # If the current file is going to be linked deeper in the
# home directory, for example, we want to link # home directory, for example, we want to link
@ -40,22 +38,24 @@ def main():
if filename[0] == '_': if filename[0] == '_':
deep_dirs = filename[1:].split('_') deep_dirs = filename[1:].split('_')
link_file = deep_dirs[-1] link_file = deep_dirs[-1]
dir_name = '/'.join(deep_dirs[:-1]) dir_name = join(*deep_dirs[:-1])
config_path = os.path.join(home_dir, ".%s" % dir_name, link_file) config_path = join(home_dir, ".{0}".format(dir_name), link_file)
file_path = "../%s" % file_path file_path = join("..", file_path)
if os.path.exists(config_path): if lexists(config_path):
if os.path.islink(config_path): if not exists(config_path):
if os.path.realpath(config_path) != file_path: # If it does not exists but lexists is true, that means this
pass # is a broken symlink
elif os.path.isfile(config_path): remove(config_path)
elif islink(config_path):
if realpath(config_path) != file_path:
continue
elif isfile(config_path):
log(config_path, 'existing file, consider merge') log(config_path, 'existing file, consider merge')
continue continue
os.symlink(file_path, config_path) log(config_path, "now linking to '{0}'".format(file_path))
log(config_path, "now linking to '%s'" % file_path) symlink(file_path, config_path)
return
if __name__ == '__main__': if __name__ == '__main__':
main() main()