view python/install_config.py @ 577:9b9b66c2c1fc

syntax
author Jeff Hammel <k0scist@gmail.com>
date Mon, 20 Jan 2014 14:27:47 -0800
parents be90af6dfd47
children c16238544fca
line wrap: on
line source

#!/usr/bin/env python

"""
installs config to a user's home directory
this can be done with
curl http://k0s.org/hg/config/raw-file/tip/python/install_config.py | python
"""

import imp
import optparse
import os
import subprocess
import sys

# config repository
SRC='http://k0s.org/hg/config'

# go home
HOME=os.environ['HOME']
os.chdir(HOME)

### standalone functions

def execute(*commands):
    """execute a series of commands"""
    for command in commands:
        print ' '.join(command)
        code = subprocess.call(command)
        if code:
            sys.exit(code)

def install_develop(package):
    """install k0s.ware for development"""

    src = 'http://k0s.org/hg/%s' % package
    directory = '%s/src/%s' % (package, package)
    commands = [ ['virtualenv/virtualenv.py', package],
                 ['mkdir', '-p', directory ],
                 ['hg', 'clone', src, directory] ]
    execute(*commands)
    old_directory = os.getcwd()
    os.chdir(directory)
    command = ['../../bin/python',  'setup.py', 'develop']
    execute(command)
    os.chdir(old_directory)


### process steps

class Step(object):
    @classmethod
    def check(cls):
        """checks if the step may be run"""
    def __call__(self):
        execute(*self.commands)

class InitializeRepository(Step):
    """make the home directory a repository"""
    commands = [
        ['hg', 'init'],
        ['hg', 'pull', SRC],
        ['hg', 'update', '-C'],
        ]
    @classmethod
    def write_hgrc(self):
        hgrc = """[paths]
default = http://k0s.org/hg/config
default-push = ssh://k0s.org/hg/config
"""
        with file('.hg/hgrc', 'w') as f:
            f.write(hgrc)
    def __call__(self):
        Step.__call__(self)
        self.write_hgrc()

#
#
#@requires(Command('git'))
#class GitInstall

### legacy -v

commands = [
    ['hg', 'init'],
    ['hg', 'pull', SRC],
    ['hg', 'update', '-C'],
    ]

execute(*commands)


# make a (correct) .hg/hgrc file for $HOME
hgrc = """[paths]
default = http://k0s.org/hg/config
default-push = ssh://k0s.org/hg/config
"""
f = file('.hg/hgrc', 'w')
f.write(hgrc)
f.close()

# get the which command
sys.path.append(os.path.join(HOME, 'python'))
from which import which


# do git stuff
git = which('git')
if git:
    # get virtual env
    virtualenv_commands = [['git', 'clone', 'https://github.com/pypa/virtualenv.git'],
                           ['ln', '-s', HOME + '/virtualenv/virtualenv.py', HOME + '/bin/']]
    execute(*virtualenv_commands)

    # setup git's global ignore, since git is silly about this
    # and doesn't look for the file in the right place
    execute(['git', 'config', '--global', 'core.excludesfile', os.path.join(HOME, '.gitignore')])

    # install some python
    install_develop('smartopen')
    install_develop('silvermirror') # XXX this won't actually work since python-dev isn't installed; install it first

    postinstall_commands = [ ['ln', '-s', os.path.join(HOME, 'smartopen', 'bin', 'smartopen'), os.path.join(HOME, 'bin', 'smartopen') ],
                             ['ln', '-s', os.path.join(HOME, 'silvermirror', 'bin', 'silvermirror'), os.path.join(HOME, 'bin', 'silvermirror') ],
                             ]
    execute(*postinstall_commands)
else:
    print "git not installed"

# - ubuntu packages to install:
PACKAGES=["mercurial", "unison", "fluxbox", "antiword", "xclip",
          "graphviz", "python-dev", "python-lxml", "curl", "arandr",
          "git", "emacs", "irssi"]
print "Ensure the following packages are installed:"
print "sudo apt-get install %s" % ' '.join(PACKAGES)

# TODO:
# - dl and get ~/web/sync.ini :
#   ln -s /home/jhammel/web/sync.ini /home/jhammel/.silvermirror
# - handle cases where config is autogenerated BUT you still want
#   to have some base (e.g. .gkrellm2/user_config)

def main(args=sys.argv[1:]):
    usage = '%prog [options]'
    parser = optparse.OptionParser(usage=usage, description=__doc__)
    options, args = parser.parse_args()
    return

    steps = [InitializeRepository]
    for step in steps:
        pass # TODO

if __name__ == '__main__':
    main()