From 3e75519161619842466c044caf41cc06309c7849 Mon Sep 17 00:00:00 2001 From: Buddy Sandidge Date: Mon, 16 Dec 2013 16:54:01 -0800 Subject: [PATCH] Refactor autolink to handle symlinks some cases better, still bad --- autolink | 68 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/autolink b/autolink index 7f814c3..8e53b40 100755 --- a/autolink +++ b/autolink @@ -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 in this directory. @@ -7,32 +7,30 @@ For example: ~/.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): """Simple logging function""" - name = ("'%s':" % filename).ljust(30, ' ') - print "%s\t%s" % (name, message) + name = "'{0}':".format(filename).ljust(30, ' ') + 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(): """Create the symlinks""" - abs_path = os.path.abspath(os.path.dirname(__file__)) - home_dir = os.path.expanduser('~') - this_script = os.path.basename(__file__) - rel_path = abs_path.replace(home_dir, '.') - - for filename in os.listdir(abs_path): - if filename == this_script: - continue - if filename == 'README.md': - continue + dir_path = dirname(__file__) + base_name = dirname(normpath(join(getenv('PWD'), __file__))) + home_dir = expanduser('~') + rel_path = normpath(relpath(base_name, home_dir)) - 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 + for filename in filter(dir_filter, listdir(dirname(abspath(__file__)))): + config_path = join(home_dir, ".{0}".format(filename)) + file_path = join(rel_path, filename) # If the current file is going to be linked deeper in the # home directory, for example, we want to link @@ -40,22 +38,24 @@ def main(): if filename[0] == '_': deep_dirs = filename[1:].split('_') link_file = deep_dirs[-1] - dir_name = '/'.join(deep_dirs[:-1]) - config_path = os.path.join(home_dir, ".%s" % dir_name, link_file) - file_path = "../%s" % file_path - - if os.path.exists(config_path): - if os.path.islink(config_path): - if os.path.realpath(config_path) != file_path: - pass - elif os.path.isfile(config_path): + dir_name = join(*deep_dirs[:-1]) + config_path = join(home_dir, ".{0}".format(dir_name), link_file) + file_path = join("..", file_path) + + if lexists(config_path): + if not exists(config_path): + # If it does not exists but lexists is true, that means this + # is a broken symlink + 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') - continue - - os.symlink(file_path, config_path) - log(config_path, "now linking to '%s'" % file_path) + continue - return + log(config_path, "now linking to '{0}'".format(file_path)) + symlink(file_path, config_path) if __name__ == '__main__': main()