view python/install_config.py @ 910:98bcf207cda3

remove -dev from k0s-requirements.txt; not compatible with new pip
author Jeff Hammel <k0scist@gmail.com>
date Fri, 12 Apr 2024 07:32:37 -0700
parents 92f181cb772c
children
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
"""

# TODO:
# - install silvermirror:
#   ```
#   python -m venv "${HOME}"/k0s
#   mkdir -p "${HOME}"/k0s/src
#   "${HOME}"/k0s/bin/pip install -r "${HOME}"/k0s/k0s-requirements.txt
#   ```
# - dl and get ~/web/sync.ini :
#   ```
#   mkdir -p "${HOME}/web"
#   scp jhammel@k0s.org:~/web/sync.ini "${HOME}"/web/sync.ini
#   ln -s /home/jhammel/web/sync.ini /home/jhammel/.silvermirror
#   ```
# - scp k0s.org:~/web/sync.ini ~/.silvermirror

# imports
import imp
import optparse
import os
import subprocess
import sys

# globals
SRC = 'http://k0s.org/hg/config' # config repository
HOME = os.environ['HOME'] # home directory


### standalone functions

def class_mapping(_type):
    """
    returns a dict of (name, class) for objects
    in the current globals() of _type
    """

    return dict([(name,cls)
                 for name, cls in globals().items() if
                 cls.issubclass(_type)])


def execute(*commands, **kwargs):
    """execute a series of commands"""
    for command in commands:
        print (subprocess.list2cmdline(command))
        code = subprocess.call(command, **kwargs)
        if code:
            raise subprocess.CalledProcessError(code, command)


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)


### generic step framework

class StepGraph(object):
    """a bunch of steps"""

class Step(object):
    @classmethod
    def check(cls):
        """checks if the step may be run"""
    @classmethod
    def name(cls):
        return cls.__name__
    __str__ = name # XXX does not work! :(
    def __call__(self):
        execute(*self.commands)


class Command(object):
    """require a command"""

#@require(Virtualenv)
class InstallVirtualenv(Step):
    """ABC for installing packages in a virtualenv"""
    # TODO: move install_develop sctuff to here


### process steps

class InitializeRepository(Step):
    """make the home directory a repository"""
    commands = [
        ['hg', 'init'],
        ['hg', 'pull', SRC],
        ['hg', 'update', '-C'],
        ]
    @classmethod
    def write_hgrc(self):
        """make a (correct) .hg/hgrc file for $HOME"""

        hgrc = """[paths]
default = http://k0s.org/hg/config
default-push = ssh://k0s.org/hg/config
"""
        with open('.hg/hgrc', 'w') as f:
            f.write(hgrc)

    def __call__(self):
        Step.__call__(self)
        self.write_hgrc()

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

#@requires(Command('git'))
class ConfigureGit(Step):
    """configure git"""
    commands = [
        # setup git's global ignore, since git is silly about this
        # and doesn't look for the file in the right place
        ['git', 'config', '--global', 'core.excludesfile', os.path.join(HOME, '.gitignore')]
    ]

#@requires(Command('git'))
class InstallVirtualenv(Step):
    commands = [['git', 'clone', 'https://github.com/pypa/virtualenv.git'],
                ['ln', '-s',
                 os.path.join(HOME, 'virtualenv/virtualenv.py'),
                 os.path.join(HOME, 'bin', 'virtualenv.py')]
    ]

class InstallKWare(Step):
    """install k0s.ware"""
    # TODO
    # from legacy
        # # 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)


class DebianPackages(Step):
    """ubuntu packages to install"""

    PACKAGES=["antiword",
              "arandr",
              "curl",
              "emacs",
              "fluxbox",
              "git",
              "gkrellm",
              "graphviz",
              "graphviz-dev",
              "irssi",
              "mercurial",
              "pkg-config",
              "python3-dev",
              "python3-lxml",
              "libxml2",
              "libxml2-dev",
              "libxslt1-dev",
              "unison",
              "xclip",
    ]

    def __call__(self):
        # TODO: actually install these packages
        print ("Ensure the following packages are installed:")
        print ("sudo apt install -y %s" % ' '.join(self.PACKAGES))


### CLI

def main(args=sys.argv[1:]):

    # go home
    os.chdir(HOME)

    # parse command line
    usage = '%prog [options]'
    parser = optparse.OptionParser(usage=usage, description=__doc__)
    parser.add_option('--deb', '--dpkg', '--debian-packages',
                      dest='debian_packages',
                      action='store_true', default=False,
                      help="display debian packages to install")
    parser.add_option('-l', '--list', '--list-steps',
                      dest='list_steps',
                      action='store_true', default=False,
                      help="list steps to be run and exit")
    parser.add_option('--all', dest='all',
                      action='store_true', default=False,
                      help="list all actions")
    parser.add_option('--run', dest='run',
                      action='append',
                      help="run particular actions, in order")
    options, args = parser.parse_args()

    # plan steps
    steps = [InitializeRepository, DebianPackages]
    if options.debian_packages:
        steps = [DebianPackages]

    if options.list_steps:
        # list steps if specified
        for step in steps:
            print(step.name())
        parser.exit()

    if options.all:
        raise NotImplementedError("TODO")

    # execute steps
    for step in steps:
        instance = step()
        instance()

if __name__ == '__main__':
    main()