moinsetup

moinsetup.py

0:0c1be594e7b0
2010-04-13 Paul Boddie Installation and initialisation of MoinMoin Wiki sites.
     1 #!/usr/bin/env python     2      3 from os.path import abspath, exists, join, split     4 import os     5 import sys     6 import shutil     7 import re     8      9 moin_cgi_prefix = re.compile("^#sys.path.insert\(0, 'PREFIX.*$", re.MULTILINE)    10 moin_cgi_wikiconfig = re.compile("^#sys.path.insert\(0, '/path/to/wikiconfigdir.*$", re.MULTILINE)    11 wikiconfig_py_site_name = re.compile(r"^(\s*sitename =).*$", re.MULTILINE)    12 wikiconfig_py_url_prefix_static = re.compile(r"^(\s*)#(url_prefix_static =).*$", re.MULTILINE)    13     14 apache_site = """    15 ScriptAlias %(url_path)s "%(web_app_dir)s/moin.cgi"    16 """    17     18 apache_site_extra_moin18 = """    19 Alias %(static_path)s "%(prefix)s/share/moin/htdocs/"    20 """    21     22 def readfile(filename):    23     f = open(filename)    24     try:    25         return f.read()    26     finally:    27         f.close()    28     29 def writefile(filename, s):    30     f = open(filename, "w")    31     try:    32         f.write(s)    33     finally:    34         f.close()    35     36 def status(message):    37     print message    38     39 def note(message):    40     print message    41     42 def setup(moin_distribution, prefix, web_app_dir, common_dir, url_path, site_name):    43     44     """    45     Set up a Wiki installation using the following:    46     47       * moin_distribution - the directory containing a MoinMoin source    48                             distribution    49       * prefix            - the installation prefix (equivalent to /usr)    50       * web_app_dir       - the directory where Web applications and scripts    51                             reside (such as /home/www-user/cgi-bin)    52       * common_dir        - the directory where the Wiki configuration,    53                             resources and instance will reside (such as    54                             /home/www-user/mywiki)    55       * url_path          - the URL path at which the Wiki will be made    56                             available (such as / or /mywiki)    57       * site_name         - the name of the site (such as My Wiki)    58     """    59     60     prefix, web_app_dir, common_dir = map(abspath, (prefix, web_app_dir, common_dir))    61     62     conf_dir = join(common_dir, "conf")    63     instance_dir = join(common_dir, "wikidata")    64     65     for d in (conf_dir, instance_dir, web_app_dir):    66         if not exists(d):    67             os.makedirs(d)    68     69     # Enter the distribution directory and run the setup script.    70     71     this_dir = os.getcwd()    72     os.chdir(moin_distribution)    73     74     log_filename = "install-%s.log" % split(common_dir)[-1]    75     76     status("Installing MoinMoin in %s..." % prefix)    77     78     os.system("python setup.py --quiet install --force --prefix='%s' --install-data='%s' --record='%s'" % (    79         prefix, instance_dir, log_filename))    80     81     os.chdir(this_dir)    82     83     # The default wikiconfig assumes data and underlay in the same directory.    84     85     status("Installing data and underlay in %s..." % conf_dir)    86     87     for d in ("data", "underlay"):    88         shutil.copytree(join(moin_distribution, "wiki", d), join(conf_dir, d))    89     90     # NOTE: Single Wiki only so far.    91     # NOTE: Static URLs seem to be different in MoinMoin 1.9.x.    92     93     wikiconfig_py = join(moin_distribution, "wiki", "config", "wikiconfig.py")    94     95     status("Editing configuration from %s..." % wikiconfig_py)    96     97     s = readfile(wikiconfig_py)    98     s = wikiconfig_py_site_name.sub(r"\1 %r" % site_name, s)    99     s = wikiconfig_py_url_prefix_static.sub(r"\1\2 %r + url_prefix_static" % url_path, s)   100     writefile(join(conf_dir, "wikiconfig.py"), s)   101    102     # Edit moin script.   103    104     moin_script = join(prefix, "bin", "moin")   105     prefix_site_packages = join(prefix, "lib", "python%s.%s" % sys.version_info[:2], "site-packages")   106    107     status("Editing moin script at %s..." % moin_script)   108    109     s = readfile(moin_script)   110     s = s.replace("#import sys", "import sys\nsys.path.insert(0, %r)" % prefix_site_packages)   111    112     writefile(moin_script, s)   113    114     # Edit and install CGI script.   115     # NOTE: CGI only so far.   116     # NOTE: Permissions should be checked.   117    118     moin_cgi = join(instance_dir, "share", "moin", "server", "moin.cgi")   119     moin_cgi_installed = join(web_app_dir, "moin.cgi")   120    121     status("Editing moin.cgi script from %s..." % moin_cgi)   122    123     s = readfile(moin_cgi)   124     s = moin_cgi_prefix.sub("sys.path.insert(0, %r)" % prefix_site_packages, s)   125     s = moin_cgi_wikiconfig.sub("sys.path.insert(0, %r)" % conf_dir, s)   126    127     writefile(moin_cgi_installed, s)   128     os.system("chmod a+rx '%s'" % moin_cgi_installed)   129    130     # Configure server.   131     # NOTE: Need to detect Apache sites directory.   132     # NOTE: Need to work with older MoinMoin versions.   133     # NOTE: Using local namespace for substitution.   134    135     status("Writing Apache site definition to %s..." % site_name)   136    137     writefile(site_name, apache_site % locals())   138    139     # General notes.   140    141     for d in ("data", "underlay"):   142         note("chown -R www-data.www-data '%s'" % join(conf_dir, d))   143    144 if __name__ == "__main__":   145     try:   146         n = 6   147         args = sys.argv[1:n+1]   148         args[n-1]   149     except (IndexError, ValueError):   150         print setup.__doc__   151         sys.exit(1)   152    153     setup(*args)   154    155 # vim: tabstop=4 expandtab shiftwidth=4