Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upstreamable: I rewrote the logical part #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
I prefer Python to bash. It's not shorter
It's a little safer, but the spec hasn't changed: original files are erased.
  • Loading branch information
feth committed Feb 28, 2011
commit 96cc85ea15558b226790309ea0404c712e3da5da
20 changes: 20 additions & 0 deletions conf.list
Original file line number Diff line number Diff line change
@@ -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
165 changes: 165 additions & 0 deletions deploy
Original file line number Diff line number Diff line change
@@ -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()
49 changes: 0 additions & 49 deletions deploy.sh

This file was deleted.