From 96cc85ea15558b226790309ea0404c712e3da5da Mon Sep 17 00:00:00 2001 From: Feth Arezki Date: Mon, 28 Feb 2011 23:07:44 +0100 Subject: [PATCH] I prefer Python to bash. It's not shorter It's a little safer, but the spec hasn't changed: original files are erased. --- conf.list | 20 +++++++ deploy | 165 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ deploy.sh | 49 ---------------- 3 files changed, 185 insertions(+), 49 deletions(-) create mode 100644 conf.list create mode 100755 deploy delete mode 100755 deploy.sh diff --git a/conf.list b/conf.list new file mode 100644 index 0000000..deb9517 --- /dev/null +++ b/conf.list @@ -0,0 +1,20 @@ +#csv -comma separated values)- +#empty lines ignored. Comment lines start with '#'. +# +#Format: +#Executable name, source directory (relative to this dir), config destination (relative to dest/home dir) + +awesome,awesome3/rc.lua,.config/awesome/rc.lua +awesome,awesome3/theme.lua,.config/awesome/theme.lua +bash,xmodmap,.xmodmap +git-core,gitconfig_home,.gitconfig +irssi,irssi,.irssi +lynx,lynxrc,.lynxrc +mercurial,hgrc,.hgrc +newsbeuter,newsbeuter,.newsbeuter +screen,screenrc,.screenrc +tmux,tmux.conf,.tmux.conf +vim,vimrc,.vimrc +vim,vim,.vim +Xorg,Xdefaults,.Xdefaults +zsh,zshrc,.zshrc diff --git a/deploy b/deploy new file mode 100755 index 0000000..93ecf5d --- /dev/null +++ b/deploy @@ -0,0 +1,165 @@ +#!/usr/bin/python + +""" +Dirty tool to deploy config scripts. +We use this in conjunction with a git repo where every machine has its branch +""" + +from argparse import ArgumentParser +from csv import reader +from os import X_OK, access, environ, makedirs, symlink, unlink +from os.path import dirname, expanduser, exists, islink, join, realpath +from shutil import rmtree +import sys +import logging + + +SRC = realpath(dirname(sys.argv[0])) +DST = expanduser("~") + + +def delconf(filename): + """ + deletes anything you ask + """ + if islink(filename): + unlink(filename) + else: + rmtree(filename) + + +class Deployer(object): + """ + This will deploy (or not) your config files + """ + def __init__(self, syspath, src, dst): + """ + The constructor wants: + syslog: the environment PATH variable + src: the dir where we pull files + dst: the base installation dir (likely home) + """ + self.syspath = syspath.split(':') + self.src = src + self.dst = dst + + def has_exe(self, exename): + """ + If this name in the PATH? + Is the file executable? + """ + for directory in self.syspath: + searched = join(directory, exename) + if exists(searched) and access(searched, X_OK): + return True + return False + + def deploy(self, exename, confsrc, confdst): + """ + unlinks previous install, which is rude. + installs new conf + + exename: the exe that must exist if you want config files installation + confsrc: the place + """ + logger = logging.getLogger('%s>%s' % (exename, confsrc)) + logger.debug("Processing exe: %s\tsrc: %s\tdst: %s" % + (exename, confsrc, confdst)) + if not self.has_exe(exename): + logger.info('[NOT INSTALLED] (skipping)') + return + + logger.info('[INSTALLED] (installing conf)') + + confsrc = join(self.src, confsrc) + confdst = join(self.dst, confdst) + + #wipe + if exists(confdst): + logger.debug('Removing %s' % confdst) + delconf(confdst) + + #prepare if dirs don't exist + basedir = dirname(confdst) + if not exists(basedir): + logger.debug("Creating %s" % basedir) + makedirs(basedir) + + #install + symlink(realpath(confsrc), confdst) + logger.debug('\t Copy done') + + +def parse_args(): + """ + returns parsed argv + """ + parser = ArgumentParser( + description='deploys your config files the dirty way') + parser.add_argument('--sourcedir', '-s', default=SRC, + help="Conf files repository, must hold a 'conf.list' file, " + "defaults to %s" % SRC) + parser.add_argument('--destdir', '-d', default=DST, + help="Where to install, defaults to %s" % DST) + parser.add_argument('--verbose', '-v', action='store_true', + help="be verbose") + return parser.parse_args() + + +def read_conflist(filename): + """ + reads supplied file and yields a list of records + """ + with open(filename, 'r') as conffd: + csvreader = reader(conffd) + for index, row in enumerate(csvreader): + if not row: + continue + if row[0].startswith('#'): + continue + if len(row) != 3: + raise ValueError("Row %d has incorrect formatting: %s" % + (index + 1, ','.join(row))) + yield row + + +def deploy(syspath, src, dst, records): + """ + Function that does the job + """ + deployer = Deployer(syspath, src, dst) + + for exename, confsrc, confdst in records: + deployer.deploy(exename, confsrc, confdst) + + +def main(): + """ + Main function, ran when the module is executed + """ + + parsed = parse_args() + + level = logging.DEBUG if parsed.verbose else logging.INFO + logging.basicConfig(format="[%(name)-27s] %(message)s", level=level) + + syspath = environ.get('PATH') + if not syspath: + logging.error('Needs PATH environment variable to seek exe') + sys.exit(2) + + src = parsed.sourcedir + dst = parsed.destdir + conflist = realpath(join(src, 'conf.list')) + + logarg = {'conflist': conflist} + + if not exists(conflist): + logging.error('%(conflist)s: file not found', logarg) + sys.exit(2) + + logging.debug('Reading %s', logarg) + deploy(syspath, src, dst, read_conflist(conflist)) + +if __name__ == '__main__': + main() diff --git a/deploy.sh b/deploy.sh deleted file mode 100755 index 10f1613..0000000 --- a/deploy.sh +++ /dev/null @@ -1,49 +0,0 @@ -SCRIPTS_DIR=$PWD - -check_installed(){ - is_installed=$(dpkg -l|grep " $1 "|wc -l) -} - -deploy(){ - # $1 is the program name (e.g. vim) - # $2 is the destination of the script (e.g. ~/.vimrc) - # $3 is the source of the script (e.g. ~/conffiles/vimrc) - check_installed $1 - if [ $is_installed ] && [ ! -L $2 ] - then - echo "$1 is installed, deploying $2" - rm -r $2 - ln -s $3 $2 - fi -} - - -deploy zsh ~/.zshrc $SCRIPTS_DIR/zshrc - -deploy vim ~/.vim $SCRIPTS_DIR/vim -deploy vim ~/.vimrc $SCRIPTS_DIR/vimrc - -deploy git-core ~/.gitconfig $SCRIPTS_DIR/gitconfig_home - -deploy lynx ~/.lynxrc $SCRIPTS_DIR/lynxrc - -deploy newsbeuter ~/.newsbeuter $SCRIPTS_DIR/newsbeuter - -deploy screen ~/.screenrc $SCRIPTS_DIR/screenrc - -deploy bash ~/.xmodmap $SCRIPTS_DIR/xmodmap - -deploy irssi ~/.irssi $SCRIPTS_DIR/irssi - -deploy xserver-xorg-core ~/.Xdefaults $SCRIPTS_DIR/Xdefaults - -is_installed_awesome=$(check_installed awesome) -if [ $is_installed ] -then - mkdir -p .config/awesome -fi -deploy awesome ~/.config/awesome/rc.lua $SCRIPTS_DIR/awesome3/rc.lua -deploy awesome ~/.config/awesome/theme.lua $SCRIPTS_DIR/awesome3/theme.lua - -deploy mercurial ~/.hgrc $SCRIPTS_DIR/hgrc -deploy tmux ~/.tmux.conf $SCRIPTS_DIR/tmux.conf