hgaxsq
Last Updated: April 28, 2019
·
1.863K
· vimalkumarvelay

Fabric - install Python, Zope 2 and create a Zope instance (Linux)

About
A script I wrote recently to automate installation of Python, Zope 2, setuptools, psycopg2 and PyXML.

Usage
Install Fabric. Save code below to a file called fabfile.py. To list all available functions, use

fab -l 

To run the entire setup procedure (i.e., install Python, Zope, psycopg2, PyXML and create a zope instance), use

fab run_setup

Test
If there are no errors, a zope instance would have been created under the zope directory. Start the instance

cd zope\bin
./zopectl start

Navigate to

http://localhost:8080/manage [Admin interface]
user name: admin, password: admin

OR

http://localhost:8080 [website]

"""fabfile.py - Fabric script to setup Python and Zope2 (Linux)"""
import os
import tarfile
from fabric.api import local, lcd

### CONFIGURATION ###

# By default, the current directory is used
PREFIX = os.getcwd()

# Python and Zope scripts will be installed under EXEC_PREFIX/bin
EXEC_PREFIX = os.path.join(PREFIX, "Programs")

# Zope instance will be created under ZOPE_HOME
ZOPE_HOME = os.path.join(PREFIX, "zope")

### END CONFIGURATION ###

PKGS = {
    "python": [
        "Python-2.3.7.tar.bz2",
        "http://www.python.org/ftp/python/2.3.7/Python-2.3.7.tar.bz2#fa73476c5214c57d0751fae527f991e1"
    ],
    "setuptools": [
        "setuptools-0.6c11.tar.gz",
        "http://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c11.tar.gz#md5=7df2a529a074f613b509fb44feefe74e"
    ],
    "zope": [
        "Zope-2.8.7-final.tgz",
        "http://old.zope.org/Products/Zope/2.8.7/Zope-2.8.7-final.tgz#1b5cfce7bf70ac877c3662441d08dfb9"
    ]
}

# Additional dependencies - installed with easy_install
DEPS = ["psycopg2==2.0.8", "PyXML"]

# Use python 2.3 to compile everything else
PYTHON = os.path.join(EXEC_PREFIX, "bin", "python")


def _make_install():
    """Run make and make install"""
    local("make -s")
    local("make -s install")


def _prepare(pkg):
    """Download and extract the given archive"""
    fname, url = PKGS[pkg]
    if not os.path.exists(fname):
        local("wget -nv {0}".format(url))
    else:
        print "Package exists: {0}".format(fname)

    archive = tarfile.open(fname)
    archdir = os.path.commonprefix(archive.getnames())
    archive.extractall()
    archive.close()

    # so we can next run configure
    return archdir


def install_python():
    """Intall Python 2.3 with zlib support"""
    pydir = _prepare("python")
    with lcd(pydir):
        print "Building and installing Python"
        local("./configure --prefix={0}".format(EXEC_PREFIX))

        print "Enable zlib support for Python"
        local(r"sed -i 's:#\(zlib.*\):\1:' Modules/Setup")
        _make_install()


def install_deps():
    """Install dependencies"""

    pkgdir = _prepare("setuptools")
    with lcd(pkgdir):
        print "Install setuptools"
        local("{0} setup.py -q install".format(PYTHON))

    # install additional dependencies
    easy_install = os.path.join(EXEC_PREFIX, "bin", "easy_install")
    for dep in DEPS:
        print "Install {0}".format(dep)
        local("{0} -q {1}".format(easy_install, dep))


def install_zope():
    """Install Zope"""
    zopedir = _prepare("zope")
    with lcd(zopedir):
        print "Build and install Zope"
        local("./configure --prefix={0} --with-python={1} "
                "--optimize".format(EXEC_PREFIX, PYTHON))
        _make_install()


def make_zope_instance():
    """Create Zope instance"""
    print "Creating Zope instance"
    local("{0} {1} -d {2} -u admin:admin".format(PYTHON,
        os.path.join(EXEC_PREFIX, "bin/mkzopeinstance.py"), ZOPE_HOME))


def run_setup():
    """Run all steps"""
    print "Install Python"
    install_python()

    print "Install dependencies"
    install_deps()

    print "Install Zope"
    install_zope()

    print "Create Zope instance"
    make_zope_instance()

    print "Finished"


if __name__ == "__main__":
    run_setup()