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