Last active
December 21, 2015 16:09
-
-
Save patrickmclaren/6331972 to your computer and use it in GitHub Desktop.
Bootstrap dotfiles. Keep them symlinked to home directory, but store them elsewhere, without a dot (i.e. not hidden).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import os | |
import sys | |
import subprocess | |
from stat import * | |
############################################################################## | |
# Routine Functions | |
############################################################################## | |
def info(msg, prefix="", suffix=""): | |
msg = prefix + msg + suffix | |
print(msg) | |
def success(msg): | |
msg = "SUCCESS: " + msg | |
print(msg) | |
sys.exit(0) | |
def fail(msg): | |
msg = "FATAL: " + msg | |
print(msg) | |
sys.exit(-1) | |
def print_welcome_banner(): | |
welcome = "Dotfile installer" | |
border = "".join(['-' for x in welcome]) | |
info(border) | |
info(welcome) | |
info(border) | |
############################################################################## | |
# Logic | |
############################################################################## | |
def link_with_home(der, obj, home, link_name): | |
"""Create symbolic link. Remember, that we have ln -s TARGET LINK_NAME.""" | |
cmd = "ln -s {}/{} {}/{}".format(der, obj, home, link_name) | |
info(cmd, " * ") | |
subprocess.check_call(cmd.split()) | |
def remove_existing(name, der): | |
"""Remove file from directory.""" | |
filename = "{}/{}".format(der, name) | |
if os.path.exists(filename): | |
msg = "File exists, removing {}/{}".format(der, name) | |
info(msg, " * ") | |
cmd = "rm -rf {}/{}".format(der, name) | |
subprocess.check_call(cmd.split()) | |
def set_permissions(name, perms): | |
human_perms = oct(perms) | |
msg = "Setting permissions on {} to {}".format(name, human_perms) | |
info(msg, " * ") | |
os.chmod(name, perms) | |
############################################################################## | |
# Main | |
############################################################################## | |
def main(): | |
"""Link directories and files with the prefix ~. as a dotfile to the | |
user's home directory. If a file or directory does not contain ~. in it's | |
filename, then create the link with the same name.""" | |
print_welcome_banner() | |
info("Assuming we're running on a POSIX compliant system", " - ") | |
home = os.getenv("HOME") | |
os.chdir('../') | |
der = os.getcwd() | |
########################################################################## | |
# Special files to avoid | |
########################################################################## | |
exclude = [] | |
exclude.append('exclude') | |
exclude.append('.git') | |
objs = [] | |
for x in os.listdir(): | |
for y in exclude: | |
if y in x: | |
break | |
else: | |
objs.append(x) | |
########################################################################## | |
# Symlink | |
########################################################################## | |
info("Performing the following commands:", " - ") | |
for obj in objs: | |
if os.path.isfile(obj): | |
perms = S_IRWXU & (S_IRUSR | S_IWUSR) | |
else: | |
perms = S_IRWXU & (S_IRUSR | S_IWUSR | S_IXUSR) | |
set_permissions(obj, perms) | |
new_name = obj.replace('~.', '.') | |
remove_existing(new_name, home) | |
link_with_home(der, obj, home, new_name) | |
print() | |
success("Dotfiles ready.") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment