moinsetup

Changeset

0:0c1be594e7b0
2010-04-13 Paul Boddie raw files shortlog changelog graph Installation and initialisation of MoinMoin Wiki sites.
moinsetup.py (file)
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/moinsetup.py	Tue Apr 13 00:26:25 2010 +0200
     1.3 @@ -0,0 +1,155 @@
     1.4 +#!/usr/bin/env python
     1.5 +
     1.6 +from os.path import abspath, exists, join, split
     1.7 +import os
     1.8 +import sys
     1.9 +import shutil
    1.10 +import re
    1.11 +
    1.12 +moin_cgi_prefix = re.compile("^#sys.path.insert\(0, 'PREFIX.*$", re.MULTILINE)
    1.13 +moin_cgi_wikiconfig = re.compile("^#sys.path.insert\(0, '/path/to/wikiconfigdir.*$", re.MULTILINE)
    1.14 +wikiconfig_py_site_name = re.compile(r"^(\s*sitename =).*$", re.MULTILINE)
    1.15 +wikiconfig_py_url_prefix_static = re.compile(r"^(\s*)#(url_prefix_static =).*$", re.MULTILINE)
    1.16 +
    1.17 +apache_site = """
    1.18 +ScriptAlias %(url_path)s "%(web_app_dir)s/moin.cgi"
    1.19 +"""
    1.20 +
    1.21 +apache_site_extra_moin18 = """
    1.22 +Alias %(static_path)s "%(prefix)s/share/moin/htdocs/"
    1.23 +"""
    1.24 +
    1.25 +def readfile(filename):
    1.26 +    f = open(filename)
    1.27 +    try:
    1.28 +        return f.read()
    1.29 +    finally:
    1.30 +        f.close()
    1.31 +
    1.32 +def writefile(filename, s):
    1.33 +    f = open(filename, "w")
    1.34 +    try:
    1.35 +        f.write(s)
    1.36 +    finally:
    1.37 +        f.close()
    1.38 +
    1.39 +def status(message):
    1.40 +    print message
    1.41 +
    1.42 +def note(message):
    1.43 +    print message
    1.44 +
    1.45 +def setup(moin_distribution, prefix, web_app_dir, common_dir, url_path, site_name):
    1.46 +
    1.47 +    """
    1.48 +    Set up a Wiki installation using the following:
    1.49 +
    1.50 +      * moin_distribution - the directory containing a MoinMoin source
    1.51 +                            distribution
    1.52 +      * prefix            - the installation prefix (equivalent to /usr)
    1.53 +      * web_app_dir       - the directory where Web applications and scripts
    1.54 +                            reside (such as /home/www-user/cgi-bin)
    1.55 +      * common_dir        - the directory where the Wiki configuration,
    1.56 +                            resources and instance will reside (such as
    1.57 +                            /home/www-user/mywiki)
    1.58 +      * url_path          - the URL path at which the Wiki will be made
    1.59 +                            available (such as / or /mywiki)
    1.60 +      * site_name         - the name of the site (such as My Wiki)
    1.61 +    """
    1.62 +
    1.63 +    prefix, web_app_dir, common_dir = map(abspath, (prefix, web_app_dir, common_dir))
    1.64 +
    1.65 +    conf_dir = join(common_dir, "conf")
    1.66 +    instance_dir = join(common_dir, "wikidata")
    1.67 +
    1.68 +    for d in (conf_dir, instance_dir, web_app_dir):
    1.69 +        if not exists(d):
    1.70 +            os.makedirs(d)
    1.71 +
    1.72 +    # Enter the distribution directory and run the setup script.
    1.73 +
    1.74 +    this_dir = os.getcwd()
    1.75 +    os.chdir(moin_distribution)
    1.76 +
    1.77 +    log_filename = "install-%s.log" % split(common_dir)[-1]
    1.78 +
    1.79 +    status("Installing MoinMoin in %s..." % prefix)
    1.80 +
    1.81 +    os.system("python setup.py --quiet install --force --prefix='%s' --install-data='%s' --record='%s'" % (
    1.82 +        prefix, instance_dir, log_filename))
    1.83 +
    1.84 +    os.chdir(this_dir)
    1.85 +
    1.86 +    # The default wikiconfig assumes data and underlay in the same directory.
    1.87 +
    1.88 +    status("Installing data and underlay in %s..." % conf_dir)
    1.89 +
    1.90 +    for d in ("data", "underlay"):
    1.91 +        shutil.copytree(join(moin_distribution, "wiki", d), join(conf_dir, d))
    1.92 +
    1.93 +    # NOTE: Single Wiki only so far.
    1.94 +    # NOTE: Static URLs seem to be different in MoinMoin 1.9.x.
    1.95 +
    1.96 +    wikiconfig_py = join(moin_distribution, "wiki", "config", "wikiconfig.py")
    1.97 +
    1.98 +    status("Editing configuration from %s..." % wikiconfig_py)
    1.99 +
   1.100 +    s = readfile(wikiconfig_py)
   1.101 +    s = wikiconfig_py_site_name.sub(r"\1 %r" % site_name, s)
   1.102 +    s = wikiconfig_py_url_prefix_static.sub(r"\1\2 %r + url_prefix_static" % url_path, s)
   1.103 +    writefile(join(conf_dir, "wikiconfig.py"), s)
   1.104 +
   1.105 +    # Edit moin script.
   1.106 +
   1.107 +    moin_script = join(prefix, "bin", "moin")
   1.108 +    prefix_site_packages = join(prefix, "lib", "python%s.%s" % sys.version_info[:2], "site-packages")
   1.109 +
   1.110 +    status("Editing moin script at %s..." % moin_script)
   1.111 +
   1.112 +    s = readfile(moin_script)
   1.113 +    s = s.replace("#import sys", "import sys\nsys.path.insert(0, %r)" % prefix_site_packages)
   1.114 +
   1.115 +    writefile(moin_script, s)
   1.116 +
   1.117 +    # Edit and install CGI script.
   1.118 +    # NOTE: CGI only so far.
   1.119 +    # NOTE: Permissions should be checked.
   1.120 +
   1.121 +    moin_cgi = join(instance_dir, "share", "moin", "server", "moin.cgi")
   1.122 +    moin_cgi_installed = join(web_app_dir, "moin.cgi")
   1.123 +
   1.124 +    status("Editing moin.cgi script from %s..." % moin_cgi)
   1.125 +
   1.126 +    s = readfile(moin_cgi)
   1.127 +    s = moin_cgi_prefix.sub("sys.path.insert(0, %r)" % prefix_site_packages, s)
   1.128 +    s = moin_cgi_wikiconfig.sub("sys.path.insert(0, %r)" % conf_dir, s)
   1.129 +
   1.130 +    writefile(moin_cgi_installed, s)
   1.131 +    os.system("chmod a+rx '%s'" % moin_cgi_installed)
   1.132 +
   1.133 +    # Configure server.
   1.134 +    # NOTE: Need to detect Apache sites directory.
   1.135 +    # NOTE: Need to work with older MoinMoin versions.
   1.136 +    # NOTE: Using local namespace for substitution.
   1.137 +
   1.138 +    status("Writing Apache site definition to %s..." % site_name)
   1.139 +
   1.140 +    writefile(site_name, apache_site % locals())
   1.141 +
   1.142 +    # General notes.
   1.143 +
   1.144 +    for d in ("data", "underlay"):
   1.145 +        note("chown -R www-data.www-data '%s'" % join(conf_dir, d))
   1.146 +
   1.147 +if __name__ == "__main__":
   1.148 +    try:
   1.149 +        n = 6
   1.150 +        args = sys.argv[1:n+1]
   1.151 +        args[n-1]
   1.152 +    except (IndexError, ValueError):
   1.153 +        print setup.__doc__
   1.154 +        sys.exit(1)
   1.155 +
   1.156 +    setup(*args)
   1.157 +
   1.158 +# vim: tabstop=4 expandtab shiftwidth=4