paul@0 | 1 | #!/usr/bin/env python |
paul@0 | 2 | |
paul@27 | 3 | """ |
paul@27 | 4 | A setup and configuration script for MoinMoin. |
paul@27 | 5 | |
paul@70 | 6 | Copyright (C) 2010, 2011, 2012, 2013 Paul Boddie <paul@boddie.org.uk> |
paul@27 | 7 | |
paul@27 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@27 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@27 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@27 | 11 | version. |
paul@27 | 12 | |
paul@27 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@27 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@27 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@27 | 16 | details. |
paul@27 | 17 | |
paul@27 | 18 | You should have received a copy of the GNU General Public License along with |
paul@27 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@27 | 20 | """ |
paul@27 | 21 | |
paul@47 | 22 | from os.path import abspath, exists, extsep, isdir, islink, join, normpath, split |
paul@47 | 23 | from os import chdir, chmod, listdir, mkdir, makedirs, remove, rename, rmdir |
paul@4 | 24 | from getpass import getpass |
paul@18 | 25 | from glob import glob |
paul@25 | 26 | from zipfile import ZipFile |
paul@0 | 27 | import os |
paul@70 | 28 | import py_compile |
paul@70 | 29 | import re |
paul@0 | 30 | import shutil |
paul@70 | 31 | import sys |
paul@33 | 32 | import tempfile |
paul@0 | 33 | |
paul@59 | 34 | __version__ = "0.4" |
paul@27 | 35 | |
paul@1 | 36 | # Regular expressions for editing MoinMoin scripts and configuration files. |
paul@1 | 37 | |
paul@2 | 38 | def compile_definition(name): |
paul@26 | 39 | return re.compile(r"^(\s*)#*\s*(%s =)\s*(.*)$" % name, re.MULTILINE) |
paul@2 | 40 | |
paul@34 | 41 | wikiconfig_import = re.compile(r"^(\s*)#*\s*" |
paul@34 | 42 | r"(from\s+)(?P<module>\S+)" |
paul@34 | 43 | r"(\s+import\s+)(?P<names>(?:\S|[^,\s])+(?:\s*,\s*(?:\S|[^,\s])+)*)" |
paul@34 | 44 | r"(\s*)$", re.MULTILINE) |
paul@34 | 45 | |
paul@34 | 46 | moin_cgi_prefix = re.compile(r"^#sys\.path\.insert\(0, 'PREFIX.*$", re.MULTILINE) |
paul@34 | 47 | moin_cgi_wikiconfig = re.compile(r"^#sys\.path\.insert\(0, '/path/to/wikiconfigdir.*$", re.MULTILINE) |
paul@14 | 48 | moin_cgi_properties = compile_definition("properties") |
paul@14 | 49 | moin_cgi_fix_script_name = compile_definition("fix_script_name") |
paul@34 | 50 | moin_cgi_force_cgi = re.compile(r"^#(os.environ\['FCGI_FORCE_CGI'\].*)$", re.MULTILINE) |
paul@9 | 51 | |
paul@34 | 52 | css_import_stylesheet = re.compile(r"(\s*@import\s+[\"'])(.*?)([\"']\s*;)") |
paul@19 | 53 | |
paul@1 | 54 | # Templates for Apache site definitions. |
paul@0 | 55 | |
paul@62 | 56 | apache_site = """\ |
paul@0 | 57 | ScriptAlias %(url_path)s "%(web_app_dir)s/moin.cgi" |
paul@0 | 58 | """ |
paul@0 | 59 | |
paul@62 | 60 | apache_site_extra = """\ |
paul@58 | 61 | Alias %(static_url_resources_path)s "%(htdocs_dir)s/" |
paul@2 | 62 | """ |
paul@2 | 63 | |
paul@14 | 64 | # Limited hosting .htaccess definitions require the following settings to be |
paul@58 | 65 | # configured in the main Apache configuration files for the directory where |
paul@58 | 66 | # .htaccess is to be deployed: |
paul@58 | 67 | # |
paul@59 | 68 | # AllowOverride FileInfo Indexes |
paul@14 | 69 | # |
paul@58 | 70 | # The following settings are required for the directory where the moin.cgi |
paul@58 | 71 | # script is to be deployed: |
paul@58 | 72 | # |
paul@58 | 73 | # Options ExecCGI FollowSymLinks SymLinksIfOwnerMatch |
paul@14 | 74 | # AddHandler cgi-script .cgi |
paul@58 | 75 | # |
paul@58 | 76 | # If a DirectoryIndex directive is also desired, the Indexes option must be set. |
paul@58 | 77 | # Such a directive is not desirable where the static and dynamic resources are |
paul@58 | 78 | # in different places, however. |
paul@74 | 79 | # |
paul@74 | 80 | # The first three blocks of conditions and rules are used to work around a bug |
paul@74 | 81 | # in mod_rewrite, supporting up to three question marks in URL paths. |
paul@74 | 82 | # See: http://moinmo.in/MoinMoinBugs/CannotAccessPagesEndingWithAQuestionMark |
paul@74 | 83 | # See: https://issues.apache.org/bugzilla/show_bug.cgi?id=49642 |
paul@14 | 84 | |
paul@62 | 85 | apache_htaccess_combined_mod_rewrite = """\ |
paul@68 | 86 | DirectoryIndex %(url_path_tr)s/moin.cgi/ |
paul@14 | 87 | RewriteEngine On |
paul@58 | 88 | RewriteBase %(final_url_path)s |
paul@74 | 89 | |
paul@74 | 90 | RewriteCond %%{REQUEST_FILENAME} !-f |
paul@74 | 91 | RewriteCond %%{REQUEST_FILENAME} !-d |
paul@74 | 92 | RewriteRule ^(.*)\?(.*)\?(.*)\?(.*) %(url_path_tr)s/moin.cgi/$1\%3f$2\%3f$3\%3f$4 [PT,L,QSA] |
paul@74 | 93 | |
paul@74 | 94 | RewriteCond %%{REQUEST_FILENAME} !-f |
paul@74 | 95 | RewriteCond %%{REQUEST_FILENAME} !-d |
paul@74 | 96 | RewriteRule ^(.*)\?(.*)\?(.*) %(url_path_tr)s/moin.cgi/$1\%3f$2\%3f$3 [PT,L,QSA] |
paul@74 | 97 | |
paul@74 | 98 | RewriteCond %%{REQUEST_FILENAME} !-f |
paul@74 | 99 | RewriteCond %%{REQUEST_FILENAME} !-d |
paul@74 | 100 | RewriteRule ^(.*)\?(.*) %(url_path_tr)s/moin.cgi/$1\%3f$2 [PT,L,QSA] |
paul@74 | 101 | |
paul@14 | 102 | RewriteCond %%{REQUEST_FILENAME} !-f |
paul@14 | 103 | RewriteCond %%{REQUEST_FILENAME} !-d |
paul@68 | 104 | RewriteRule ^(.*) %(url_path_tr)s/moin.cgi/$1 [PT,L,QSA] |
paul@14 | 105 | """ |
paul@14 | 106 | |
paul@72 | 107 | |
paul@72 | 108 | |
paul@33 | 109 | # Post-setup templates. |
paul@33 | 110 | |
paul@72 | 111 | # ACL-capable filesystem modifications. |
paul@72 | 112 | |
paul@62 | 113 | postsetup_setfacl = """\ |
paul@62 | 114 | #!/bin/sh |
paul@33 | 115 | |
paul@73 | 116 | find '%(common_dir)s/data' -type f -print0 | xargs -0 setfacl -m u:%(web_user)s:rw |
paul@73 | 117 | find '%(common_dir)s/data' -type d -print0 | xargs -0 setfacl -m u:%(web_user)s:rwx |
paul@73 | 118 | find '%(common_dir)s/data/plugin' -type f -print0 | xargs -0 setfacl -m u:%(web_user)s:r |
paul@73 | 119 | find '%(common_dir)s/data/plugin' -type d -print0 | xargs -0 setfacl -m u:%(web_user)s:rx |
paul@73 | 120 | find '%(common_dir)s/underlay' -type f -print0 | xargs -0 setfacl -m u:%(web_user)s:rw |
paul@73 | 121 | find '%(common_dir)s/underlay' -type d -print0 | xargs -0 setfacl -m u:%(web_user)s:rwx |
paul@62 | 122 | if [ -e "%(common_dir)s/wikiconfig.py" ]; then |
paul@62 | 123 | setfacl -m u:%(web_user)s:r '%(common_dir)s/wikiconfig.py' |
paul@62 | 124 | fi |
paul@33 | 125 | """ |
paul@33 | 126 | |
paul@43 | 127 | postsetup_setfacl_extra = """ |
paul@73 | 128 | find '%(htdocs_dir)s' -type f -print0 | xargs -0 setfacl -m u:%(web_user)s:r |
paul@73 | 129 | find '%(htdocs_dir)s' -type d -print0 | xargs -0 setfacl -m u:%(web_user)s:rx |
paul@33 | 130 | """ |
paul@33 | 131 | |
paul@58 | 132 | postsetup_setfacl_logs = """ |
paul@58 | 133 | if [ -e "%(common_dir)s/data/*-log" ]; then |
paul@63 | 134 | setfacl -m u:%(web_user)s:rw %(common_dir)s/data/*-log |
paul@58 | 135 | fi |
paul@58 | 136 | """ |
paul@58 | 137 | |
paul@72 | 138 | # Conventional owner/group permissions. |
paul@72 | 139 | |
paul@62 | 140 | postsetup_chown_chmod = """\ |
paul@62 | 141 | #!/bin/sh |
paul@33 | 142 | |
paul@43 | 143 | chown -R %(this_user)s.%(web_group)s '%(common_dir)s/data' |
paul@43 | 144 | chown -R %(this_user)s.%(web_group)s '%(common_dir)s/underlay' |
paul@73 | 145 | find '%(common_dir)s/data' -type f -print0 | xargs -0 chmod g=rw |
paul@73 | 146 | find '%(common_dir)s/data' -type d -print0 | xargs -0 chmod g=rwx |
paul@73 | 147 | find '%(common_dir)s/data/plugin' -type f -print0 | xargs -0 chmod g=r |
paul@73 | 148 | find '%(common_dir)s/data/plugin' -type d -print0 | xargs -0 chmod g=rx |
paul@73 | 149 | find '%(common_dir)s/underlay' -type f -print0 | xargs -0 chmod g=rw |
paul@73 | 150 | find '%(common_dir)s/underlay' -type d -print0 | xargs -0 chmod g=rwx |
paul@62 | 151 | if [ -e "%(common_dir)s/wikiconfig.py" ]; then |
paul@62 | 152 | chown %(this_user)s.%(web_group)s '%(common_dir)s/wikiconfig.py' |
paul@70 | 153 | chmod g=r '%(common_dir)s/wikiconfig.py' |
paul@62 | 154 | fi |
paul@33 | 155 | """ |
paul@33 | 156 | |
paul@43 | 157 | postsetup_chown_extra = """ |
paul@33 | 158 | chown -R %(this_user)s.%(web_group)s '%(htdocs_dir)s' |
paul@33 | 159 | """ |
paul@33 | 160 | |
paul@58 | 161 | postsetup_chown_logs = """ |
paul@58 | 162 | if [ -e "%(common_dir)s/data/*-log" ]; then |
paul@58 | 163 | chown %(this_user)s.%(web_group)s %(common_dir)s/data/*-log |
paul@58 | 164 | fi |
paul@58 | 165 | """ |
paul@58 | 166 | |
paul@72 | 167 | # SELinux file type modifications. |
paul@72 | 168 | |
paul@72 | 169 | postsetup_semanage = """\ |
paul@72 | 170 | #!/bin/sh |
paul@72 | 171 | |
paul@72 | 172 | semanage fcontext -a -t httpd_sys_content_t "%(common_dir)s(/.*)?" |
paul@72 | 173 | """ |
paul@72 | 174 | |
paul@72 | 175 | postsetup_semanage_extra = """\ |
paul@72 | 176 | semanage fcontext -a -t httpd_sys_content_t "%(htdocs_dir)s(/.*)?" |
paul@72 | 177 | """ |
paul@72 | 178 | |
paul@72 | 179 | postsetup_semanage_logs = """\ |
paul@72 | 180 | """ |
paul@72 | 181 | |
paul@72 | 182 | postinst_scripts = { |
paul@72 | 183 | "chown" : ("moinsetup-post-chown.sh", postsetup_chown_chmod, postsetup_chown_extra, postsetup_chown_logs), |
paul@72 | 184 | "setfacl" : ("moinsetup-post-setfacl.sh", postsetup_setfacl, postsetup_setfacl_extra, postsetup_setfacl_logs), |
paul@72 | 185 | "semanage" : ("moinsetup-post-semanage.sh", postsetup_semanage, postsetup_semanage_extra, postsetup_semanage_logs), |
paul@72 | 186 | } |
paul@72 | 187 | |
paul@1 | 188 | # Utility functions. |
paul@1 | 189 | |
paul@0 | 190 | def readfile(filename): |
paul@0 | 191 | f = open(filename) |
paul@0 | 192 | try: |
paul@0 | 193 | return f.read() |
paul@0 | 194 | finally: |
paul@0 | 195 | f.close() |
paul@0 | 196 | |
paul@0 | 197 | def writefile(filename, s): |
paul@0 | 198 | f = open(filename, "w") |
paul@0 | 199 | try: |
paul@0 | 200 | f.write(s) |
paul@0 | 201 | finally: |
paul@0 | 202 | f.close() |
paul@0 | 203 | |
paul@0 | 204 | def status(message): |
paul@0 | 205 | print message |
paul@0 | 206 | |
paul@20 | 207 | note = status |
paul@20 | 208 | error = status |
paul@0 | 209 | |
paul@22 | 210 | def format(s, indent): |
paul@22 | 211 | return re.sub("\n\s+", "\n" + " " * indent, s) |
paul@22 | 212 | |
paul@22 | 213 | # Classes. |
paul@22 | 214 | |
paul@40 | 215 | class SetupException(Exception): |
paul@40 | 216 | |
paul@40 | 217 | "An exception indicating a problem with a setup action." |
paul@40 | 218 | |
paul@40 | 219 | pass |
paul@40 | 220 | |
paul@12 | 221 | class Configuration: |
paul@12 | 222 | |
paul@12 | 223 | "A class representing the configuration." |
paul@12 | 224 | |
paul@12 | 225 | special_names = ["site_name"] |
paul@12 | 226 | |
paul@12 | 227 | def __init__(self, filename): |
paul@12 | 228 | self.content = readfile(filename) |
paul@12 | 229 | self.filename = filename |
paul@12 | 230 | |
paul@12 | 231 | def get_pattern(self, name): |
paul@12 | 232 | |
paul@12 | 233 | # Make underscores optional for certain names. |
paul@12 | 234 | |
paul@12 | 235 | if name in self.special_names: |
paul@12 | 236 | name = name.replace("_", "_?") |
paul@12 | 237 | |
paul@12 | 238 | return compile_definition(name) |
paul@12 | 239 | |
paul@26 | 240 | def get(self, name): |
paul@26 | 241 | |
paul@26 | 242 | """ |
paul@26 | 243 | Return the raw value of the last definition having the given 'name'. |
paul@26 | 244 | """ |
paul@26 | 245 | |
paul@26 | 246 | pattern = self.get_pattern(name) |
paul@26 | 247 | results = [match.group(3) for match in pattern.finditer(self.content)] |
paul@26 | 248 | if results: |
paul@26 | 249 | return results[-1] |
paul@26 | 250 | else: |
paul@26 | 251 | return None |
paul@26 | 252 | |
paul@12 | 253 | def set(self, name, value, count=None, raw=0): |
paul@12 | 254 | |
paul@12 | 255 | """ |
paul@12 | 256 | Set the configuration parameter having the given 'name' with the given |
paul@12 | 257 | 'value', limiting the number of appropriately named parameters changed |
paul@12 | 258 | to 'count', if specified. |
paul@12 | 259 | |
paul@12 | 260 | If the configuration parameter of the given 'name' does not exist, |
paul@12 | 261 | insert such a parameter at the end of the file. |
paul@12 | 262 | |
paul@12 | 263 | If the optional 'raw' parameter is specified and set to a true value, |
paul@12 | 264 | the provided 'value' is inserted directly into the configuration file. |
paul@12 | 265 | """ |
paul@12 | 266 | |
paul@12 | 267 | if not self.replace(name, value, count, raw): |
paul@12 | 268 | self.insert(name, value, raw) |
paul@12 | 269 | |
paul@12 | 270 | def replace(self, name, value, count=None, raw=0): |
paul@12 | 271 | |
paul@12 | 272 | """ |
paul@12 | 273 | Replace configuration parameters having the given 'name' with the given |
paul@12 | 274 | 'value', limiting the number of appropriately named parameters changed |
paul@12 | 275 | to 'count', if specified. |
paul@12 | 276 | |
paul@12 | 277 | If the optional 'raw' parameter is specified and set to a true value, |
paul@12 | 278 | the provided 'value' is inserted directly into the configuration file. |
paul@12 | 279 | |
paul@12 | 280 | Return the number of substitutions made. |
paul@12 | 281 | """ |
paul@12 | 282 | |
paul@12 | 283 | if raw: |
paul@12 | 284 | substitution = r"\1\2 %s" % value |
paul@12 | 285 | else: |
paul@12 | 286 | substitution = r"\1\2 %r" % value |
paul@12 | 287 | |
paul@12 | 288 | pattern = self.get_pattern(name) |
paul@12 | 289 | |
paul@12 | 290 | if count is None: |
paul@12 | 291 | self.content, n = pattern.subn(substitution, self.content) |
paul@12 | 292 | else: |
paul@12 | 293 | self.content, n = pattern.subn(substitution, self.content, count=count) |
paul@12 | 294 | |
paul@12 | 295 | return n |
paul@12 | 296 | |
paul@12 | 297 | def insert(self, name, value, raw=0): |
paul@12 | 298 | |
paul@12 | 299 | """ |
paul@12 | 300 | Insert the configuration parameter having the given 'name' and 'value'. |
paul@12 | 301 | |
paul@12 | 302 | If the optional 'raw' parameter is specified and set to a true value, |
paul@12 | 303 | the provided 'value' is inserted directly into the configuration file. |
paul@12 | 304 | """ |
paul@12 | 305 | |
paul@12 | 306 | if raw: |
paul@26 | 307 | insertion = "%s = %s" |
paul@12 | 308 | else: |
paul@26 | 309 | insertion = "%s = %r" |
paul@26 | 310 | |
paul@26 | 311 | self.insert_text(insertion % (name, value)) |
paul@26 | 312 | |
paul@26 | 313 | def insert_text(self, text): |
paul@12 | 314 | |
paul@26 | 315 | "Insert the given 'text' at the end of the configuration." |
paul@26 | 316 | |
paul@26 | 317 | if not self.content.endswith("\n"): |
paul@26 | 318 | self.content += "\n" |
paul@26 | 319 | self.content += " %s\n" % text |
paul@12 | 320 | |
paul@34 | 321 | def set_import(self, imported_module, imported_names): |
paul@34 | 322 | |
paul@34 | 323 | """ |
paul@34 | 324 | Set up an import of the given 'imported_module' exposing the given |
paul@34 | 325 | 'imported_names'. |
paul@34 | 326 | """ |
paul@34 | 327 | |
paul@34 | 328 | s = self.content |
paul@34 | 329 | after_point = 0 |
paul@34 | 330 | first_point = None |
paul@34 | 331 | |
paul@34 | 332 | for module_import in wikiconfig_import.finditer(s): |
paul@34 | 333 | before, from_keyword, module, import_keyword, names, after = module_import.groups() |
paul@34 | 334 | before_point, after_point = module_import.span() |
paul@34 | 335 | |
paul@34 | 336 | if first_point is None: |
paul@34 | 337 | first_point = after_point |
paul@34 | 338 | |
paul@34 | 339 | names = [name.strip() for name in names.split(",")] |
paul@34 | 340 | |
paul@34 | 341 | # Test the import for a reference to the requested imported module. |
paul@34 | 342 | |
paul@34 | 343 | if imported_module == module: |
paul@34 | 344 | for name in imported_names: |
paul@34 | 345 | if name not in names: |
paul@34 | 346 | names.append(name) |
paul@34 | 347 | |
paul@34 | 348 | self.content = s[:before_point] + ( |
paul@34 | 349 | "%s%s%s%s%s%s" % (before, from_keyword, module, import_keyword, ", ".join(names), after) |
paul@34 | 350 | ) + s[after_point:] |
paul@34 | 351 | break |
paul@34 | 352 | |
paul@34 | 353 | # Where no import references the imported module, insert a reference |
paul@34 | 354 | # into the configuration. |
paul@34 | 355 | |
paul@34 | 356 | else: |
paul@34 | 357 | # Add the import after the first one. |
paul@34 | 358 | |
paul@34 | 359 | if first_point is not None: |
paul@34 | 360 | self.content = s[:first_point] + ("\nfrom %s import %s" % (imported_module, ", ".join(imported_names))) + s[first_point:] |
paul@34 | 361 | |
paul@12 | 362 | def close(self): |
paul@12 | 363 | |
paul@12 | 364 | "Close the file, writing the content." |
paul@12 | 365 | |
paul@12 | 366 | writefile(self.filename, self.content) |
paul@12 | 367 | |
paul@2 | 368 | class Installation: |
paul@0 | 369 | |
paul@2 | 370 | "A class for installing and initialising MoinMoin." |
paul@0 | 371 | |
paul@22 | 372 | method_names = ( |
paul@45 | 373 | "show_config", |
paul@22 | 374 | "setup", |
paul@22 | 375 | "setup_wiki", |
paul@22 | 376 | "install_moin", |
paul@22 | 377 | "install_data", |
paul@43 | 378 | "install_static_data", |
paul@22 | 379 | "configure_moin", |
paul@22 | 380 | "edit_moin_script", |
paul@22 | 381 | "edit_moin_web_script", |
paul@22 | 382 | "add_superuser", |
paul@22 | 383 | "make_site_files", |
paul@22 | 384 | "make_post_install_script", |
paul@25 | 385 | |
paul@25 | 386 | # Post-installation activities. |
paul@25 | 387 | |
paul@47 | 388 | "reconfigure_moin", |
paul@47 | 389 | "set_auth_method", |
paul@47 | 390 | "migrate_instance", |
paul@22 | 391 | "install_theme", |
paul@22 | 392 | "install_extension_package", |
paul@22 | 393 | "install_plugins", |
paul@22 | 394 | "install_actions", |
paul@22 | 395 | "install_macros", |
paul@35 | 396 | "install_parsers", |
paul@52 | 397 | "install_event_handlers", |
paul@22 | 398 | "install_theme_resources", |
paul@25 | 399 | "edit_theme_stylesheet", |
paul@25 | 400 | |
paul@25 | 401 | # Other activities. |
paul@25 | 402 | |
paul@25 | 403 | "make_page_package", |
paul@25 | 404 | "install_page_package", |
paul@22 | 405 | ) |
paul@22 | 406 | |
paul@45 | 407 | source_config_names = ( |
paul@46 | 408 | "moin_distribution", "prefix", "site_packages", "prefix_site_packages", |
paul@46 | 409 | "htdocs_dir_source" |
paul@45 | 410 | ) |
paul@45 | 411 | |
paul@45 | 412 | instance_config_names = ( |
paul@45 | 413 | "common_dir", "farm_config", "site_config", |
paul@45 | 414 | "site_name", "site_identifier", |
paul@45 | 415 | "front_page_name", "superuser", "theme_default", "htdocs_dir" |
paul@45 | 416 | ) |
paul@45 | 417 | |
paul@45 | 418 | site_config_names = ( |
paul@45 | 419 | "web_app_dir", "web_site_dir", "web_static_dir", |
paul@45 | 420 | "url_path", "static_url_path" |
paul@45 | 421 | ) |
paul@45 | 422 | |
paul@63 | 423 | # NOTE: Potentially detect Web server credentials. |
paul@1 | 424 | |
paul@63 | 425 | web_user_default = "www-data" |
paul@63 | 426 | web_group_default = "www-data" |
paul@0 | 427 | |
paul@11 | 428 | # MoinMoin resources. |
paul@11 | 429 | |
paul@11 | 430 | theme_master = "modernized" |
paul@11 | 431 | extra_theme_css_files = ["SlideShow.css"] |
paul@11 | 432 | |
paul@62 | 433 | def __init__(self, |
paul@62 | 434 | moin_distribution=None, |
paul@62 | 435 | prefix=None, site_packages=None, |
paul@62 | 436 | web_app_dir=None, web_static_dir=None, web_site_dir=None, |
paul@63 | 437 | web_user=None, web_group=None, |
paul@58 | 438 | common_dir=None, farm_config=None, site_config=None, |
paul@58 | 439 | url_path=None, static_url_path=None, |
paul@43 | 440 | superuser=None, site_name=None, site_identifier=None, front_page_name=None, |
paul@43 | 441 | theme_default=None): |
paul@2 | 442 | |
paul@2 | 443 | """ |
paul@43 | 444 | Initialise a Wiki installation using the following installation |
paul@43 | 445 | settings: |
paul@2 | 446 | |
paul@2 | 447 | * moin_distribution - the directory containing a MoinMoin source |
paul@43 | 448 | distribution (can be omitted) |
paul@2 | 449 | * prefix - the installation prefix (equivalent to /usr) |
paul@43 | 450 | * site_packages - optional: the location of the Python |
paul@43 | 451 | site-packages directory if outside the 'prefix' |
paul@43 | 452 | (overrides the path calculated using 'prefix') |
paul@2 | 453 | * web_app_dir - the directory where Web applications and scripts |
paul@2 | 454 | reside (such as /home/www-user/cgi-bin) |
paul@43 | 455 | * web_static_dir - optional: the directory where static Web |
paul@43 | 456 | resources reside (such as /home/www-user/htdocs) |
paul@43 | 457 | * web_site_dir - optional: the directory where Web site |
paul@43 | 458 | definitions reside (such as |
paul@43 | 459 | /etc/apache2/sites-available) |
paul@63 | 460 | * web_user - optional: the user operating the Web server |
paul@63 | 461 | * web_group - optional: the group of the Web server user |
paul@43 | 462 | |
paul@43 | 463 | The following site settings are also applicable: |
paul@43 | 464 | |
paul@2 | 465 | * common_dir - the directory where the Wiki configuration, |
paul@2 | 466 | resources and instance will reside (such as |
paul@2 | 467 | /home/www-user/mywiki) |
paul@43 | 468 | * farm_config - optional: any Wiki farm configuration file for |
paul@43 | 469 | multiple Wiki deployments (overrides the |
paul@43 | 470 | 'common_dir' setting) |
paul@43 | 471 | * site_config - optional: a specific configuration file location |
paul@43 | 472 | (overrides the 'common_dir' setting) |
paul@2 | 473 | * url_path - the URL path at which the Wiki will be made |
paul@2 | 474 | available (such as / or /mywiki) |
paul@58 | 475 | * static_url_path - optional: the URL path at which static resources |
paul@58 | 476 | will be made available (such as / or /mywiki) |
paul@9 | 477 | * superuser - the name of the site's superuser (such as |
paul@50 | 478 | "AdminUser", can be omitted) |
paul@2 | 479 | * site_name - the name of the site (such as "My Wiki") |
paul@43 | 480 | * site_identifier - optional: an identifier used to refer to the |
paul@43 | 481 | site, typically derived from 'site_name' |
paul@2 | 482 | * front_page_name - the front page name for the site (such as |
paul@2 | 483 | "FrontPage" or a specific name for the site) |
paul@9 | 484 | * theme_default - optional: the default theme (such as modern) |
paul@2 | 485 | """ |
paul@2 | 486 | |
paul@2 | 487 | self.moin_distribution = moin_distribution |
paul@9 | 488 | self.superuser = superuser |
paul@2 | 489 | self.site_name = site_name |
paul@43 | 490 | self.site_identifier = site_identifier or site_name.replace(" ", "").lower() |
paul@2 | 491 | self.front_page_name = front_page_name |
paul@41 | 492 | self.farm_config = farm_config |
paul@41 | 493 | self.site_config = site_config |
paul@9 | 494 | self.theme_default = theme_default |
paul@2 | 495 | |
paul@63 | 496 | self.web_user = web_user or self.web_user_default |
paul@63 | 497 | self.web_group = web_group or self.web_group_default |
paul@63 | 498 | |
paul@2 | 499 | # NOTE: Support the detection of the Apache sites directory. |
paul@2 | 500 | |
paul@43 | 501 | self.prefix, self.site_packages, self.web_app_dir, self.web_site_dir, self.web_static_dir, self.common_dir = \ |
paul@43 | 502 | map(self._get_abspath, (prefix, site_packages, web_app_dir, web_site_dir, web_static_dir, common_dir)) |
paul@43 | 503 | |
paul@43 | 504 | if not self.web_app_dir: |
paul@43 | 505 | raise TypeError, "The 'web_app_dir' setting must be specified." |
paul@2 | 506 | |
paul@2 | 507 | # Strip any trailing "/" from the URL path. |
paul@2 | 508 | |
paul@43 | 509 | if not url_path: |
paul@43 | 510 | raise TypeError, "The 'url_path' setting must be specified." |
paul@43 | 511 | |
paul@58 | 512 | self.url_path = self._tidy_url_path(url_path) |
paul@61 | 513 | self.static_url_path = static_url_path and self._tidy_url_path(static_url_path) |
paul@2 | 514 | |
paul@68 | 515 | self.url_path_tr = self._truncate_url_path(url_path) |
paul@68 | 516 | |
paul@2 | 517 | # Define and create specific directories. |
paul@25 | 518 | # Here are the configuration and actual Wiki data directories. |
paul@2 | 519 | |
paul@43 | 520 | if not self.common_dir: |
paul@43 | 521 | raise TypeError, "The 'common_dir' setting must be specified." |
paul@2 | 522 | |
paul@25 | 523 | # Define the place where the MoinMoin package will actually reside. |
paul@2 | 524 | |
paul@43 | 525 | if not self.prefix and not self.site_packages: |
paul@43 | 526 | raise TypeError, "Either the 'prefix' or the 'site_packages' setting must be specified." |
paul@43 | 527 | |
paul@43 | 528 | self.prefix_site_packages = self.site_packages or \ |
paul@42 | 529 | join(self.prefix, "lib", "python%s.%s" % sys.version_info[:2], "site-packages") |
paul@2 | 530 | |
paul@2 | 531 | # Find the version. |
paul@2 | 532 | |
paul@2 | 533 | self.moin_version = self.get_moin_version() |
paul@2 | 534 | |
paul@14 | 535 | # The static resources reside in different locations depending on the |
paul@43 | 536 | # version of MoinMoin, but the Web server is used to serve static |
paul@43 | 537 | # resources in both cases, even though MoinMoin 1.9 can serve static |
paul@43 | 538 | # files itself. |
paul@43 | 539 | |
paul@43 | 540 | # A shared data directory may be in use. |
paul@14 | 541 | |
paul@43 | 542 | self.htdocs_dir_source = join(self.get_moin_data(), "htdocs") |
paul@9 | 543 | |
paul@43 | 544 | if self.htdocs_dir_source is None or not exists(self.htdocs_dir_source): |
paul@43 | 545 | |
paul@43 | 546 | # 1.9: moin/lib/python2.x/site-packages/MoinMoin/web/static/htdocs |
paul@40 | 547 | |
paul@43 | 548 | if self.moin_version.startswith("1.9"): |
paul@43 | 549 | self.htdocs_dir_source = join(self.prefix_site_packages, "MoinMoin", "web", "static", "htdocs") |
paul@40 | 550 | else: |
paul@43 | 551 | raise SetupException, "The static resources could not be found." |
paul@43 | 552 | |
paul@43 | 553 | # Add the static identifier to the URL path. For example: |
paul@50 | 554 | # |
paul@43 | 555 | # / -> /moin_static187 |
paul@43 | 556 | # /hgwiki -> /hgwiki-moin_static187 |
paul@50 | 557 | # |
paul@50 | 558 | # This allows multiple Wiki instances to have their own static resources |
paul@50 | 559 | # in the same hosting area. |
paul@58 | 560 | # |
paul@58 | 561 | # Where a separate static URL path has been given, the resources are |
paul@58 | 562 | # located under that path: |
paul@58 | 563 | # |
paul@58 | 564 | # / -> /moin_static187 |
paul@58 | 565 | # /hgwiki -> /hgwiki/moin_static187 |
paul@43 | 566 | |
paul@58 | 567 | # The final URL path is the principal location of the Wiki. |
paul@58 | 568 | |
paul@58 | 569 | self.final_url_path = self.static_url_path or self.url_path |
paul@58 | 570 | |
paul@58 | 571 | # The static URL resources path is the specific location of static |
paul@58 | 572 | # resources. |
paul@58 | 573 | |
paul@58 | 574 | self.static_url_resources_path = \ |
paul@58 | 575 | self.final_url_path + \ |
paul@58 | 576 | (self.static_url_path |
paul@58 | 577 | and (self.static_url_path != "/" |
paul@58 | 578 | and "/" |
paul@58 | 579 | or "") |
paul@58 | 580 | or (self.final_url_path != "/" |
paul@58 | 581 | and "-" |
paul@58 | 582 | or "")) + \ |
paul@58 | 583 | self.get_static_identifier() |
paul@58 | 584 | |
paul@58 | 585 | self.static_dir_name = self.static_url_resources_path.split("/")[-1] |
paul@40 | 586 | |
paul@43 | 587 | # In limited hosting, the static resources directory is related to |
paul@58 | 588 | # the URL path, either a specified static URL path or the general path. |
paul@14 | 589 | |
paul@43 | 590 | if self.limited_hosting(): |
paul@58 | 591 | self.htdocs_dir = join(self.web_static_dir or self.web_app_dir, self.static_dir_name) |
paul@43 | 592 | |
paul@43 | 593 | # Otherwise, a mapping is made to the directory. |
paul@44 | 594 | # This may be placed in a special static directory if desired. |
paul@14 | 595 | |
paul@9 | 596 | else: |
paul@44 | 597 | self.htdocs_dir = join(self.web_static_dir or self.common_dir, "htdocs") |
paul@9 | 598 | |
paul@45 | 599 | def show_config(self): |
paul@45 | 600 | |
paul@45 | 601 | "Show the configuration." |
paul@45 | 602 | |
paul@45 | 603 | print |
paul@64 | 604 | if self.limited_hosting(): |
paul@64 | 605 | print "Limited hosting configuration detected." |
paul@64 | 606 | print "Published resources will be configured using .htaccess." |
paul@64 | 607 | else: |
paul@64 | 608 | print "Privileged hosting configuration detected..." |
paul@64 | 609 | print "Published resources will be configured using site definition files." |
paul@64 | 610 | print |
paul@45 | 611 | for section in ("source", "instance", "site"): |
paul@45 | 612 | print section.title() |
paul@45 | 613 | print "-" * len(section) |
paul@45 | 614 | print |
paul@45 | 615 | for setting in getattr(self, "%s_config_names" % section): |
paul@50 | 616 | print "%-24s%s" % (setting, getattr(self, setting)) |
paul@45 | 617 | print |
paul@45 | 618 | |
paul@55 | 619 | print "Configuration locations" |
paul@55 | 620 | print "-----------------------" |
paul@55 | 621 | print |
paul@55 | 622 | print "%-24s%s" % ("site-level", self.get_site_config()) |
paul@55 | 623 | print "%-24s%s" % ("global", self.get_global_config()) |
paul@56 | 624 | print |
paul@56 | 625 | print "Theme directories" |
paul@56 | 626 | print "-----------------" |
paul@56 | 627 | print |
paul@56 | 628 | |
paul@57 | 629 | try: |
paul@57 | 630 | for theme in self.get_theme_directories(): |
paul@57 | 631 | print "%-24s%s" % theme |
paul@57 | 632 | except OSError, exc: |
paul@57 | 633 | print "Not shown:", str(exc) |
paul@55 | 634 | |
paul@30 | 635 | def _get_abspath(self, d): |
paul@30 | 636 | return d and abspath(d) or None |
paul@30 | 637 | |
paul@58 | 638 | def _tidy_url_path(self, url_path): |
paul@58 | 639 | if url_path != "/" and url_path.endswith("/"): |
paul@58 | 640 | return url_path[:-1] |
paul@58 | 641 | else: |
paul@58 | 642 | return url_path |
paul@58 | 643 | |
paul@68 | 644 | def _truncate_url_path(self, url_path): |
paul@68 | 645 | if url_path.endswith("/"): |
paul@68 | 646 | return url_path[:-1] |
paul@68 | 647 | else: |
paul@68 | 648 | return url_path |
paul@68 | 649 | |
paul@67 | 650 | def _set_pythonpath(self): |
paul@67 | 651 | path = os.environ.get("PYTHONPATH", "") |
paul@67 | 652 | |
paul@67 | 653 | conf_dir = self.get_wikiconfig_directory() |
paul@67 | 654 | |
paul@67 | 655 | if path: |
paul@67 | 656 | os.environ["PYTHONPATH"] = path + ":" + self.prefix_site_packages + ":" + conf_dir |
paul@67 | 657 | else: |
paul@67 | 658 | os.environ["PYTHONPATH"] = self.prefix_site_packages + ":" + conf_dir |
paul@67 | 659 | |
paul@67 | 660 | return path |
paul@67 | 661 | |
paul@67 | 662 | def _reset_pythonpath(self, path): |
paul@67 | 663 | if path: |
paul@67 | 664 | os.environ["PYTHONPATH"] = path |
paul@67 | 665 | else: |
paul@67 | 666 | del os.environ["PYTHONPATH"] |
paul@67 | 667 | |
paul@2 | 668 | def get_moin_version(self): |
paul@2 | 669 | |
paul@43 | 670 | "Return the MoinMoin version." |
paul@2 | 671 | |
paul@54 | 672 | this_dir = os.getcwd() |
paul@43 | 673 | |
paul@54 | 674 | for dir in [self.moin_distribution, self.prefix_site_packages]: |
paul@54 | 675 | if dir: |
paul@54 | 676 | try: |
paul@54 | 677 | chdir(dir) |
paul@54 | 678 | version = self.get_moin_version_from_package_info() or \ |
paul@54 | 679 | self.get_moin_version_from_import() |
paul@54 | 680 | if version: |
paul@54 | 681 | return version |
paul@43 | 682 | |
paul@54 | 683 | finally: |
paul@54 | 684 | chdir(this_dir) |
paul@43 | 685 | else: |
paul@43 | 686 | return self.get_moin_version_from_import() |
paul@43 | 687 | |
paul@43 | 688 | def get_moin_version_from_package_info(self): |
paul@43 | 689 | |
paul@43 | 690 | "Inspect the MoinMoin package information, returning the version." |
paul@5 | 691 | |
paul@43 | 692 | try: |
paul@43 | 693 | f = open("PKG-INFO") |
paul@43 | 694 | try: |
paul@43 | 695 | for line in f.xreadlines(): |
paul@43 | 696 | columns = line.split() |
paul@43 | 697 | if columns[0] == "Version:": |
paul@43 | 698 | return columns[1] |
paul@43 | 699 | finally: |
paul@43 | 700 | f.close() |
paul@2 | 701 | |
paul@43 | 702 | except IOError: |
paul@43 | 703 | pass |
paul@2 | 704 | |
paul@43 | 705 | return None |
paul@43 | 706 | |
paul@43 | 707 | def get_moin_version_from_import(self): |
paul@43 | 708 | |
paul@43 | 709 | "Return the MoinMoin version from an import of the package itself." |
paul@40 | 710 | |
paul@40 | 711 | # Where no distribution information can be read, try and import an |
paul@40 | 712 | # installed version module. |
paul@40 | 713 | |
paul@40 | 714 | f = os.popen("%s -c 'from MoinMoin.version import release; print release'" % sys.executable) |
paul@40 | 715 | try: |
paul@40 | 716 | return f.read().strip() |
paul@2 | 717 | finally: |
paul@40 | 718 | f.close() |
paul@40 | 719 | |
paul@40 | 720 | def get_moin_data(self): |
paul@40 | 721 | |
paul@43 | 722 | "Return the exact location of MoinMoin data." |
paul@40 | 723 | |
paul@43 | 724 | return self.moin_distribution and join(self.moin_distribution, "wiki") or \ |
paul@43 | 725 | self.prefix and join(self.prefix, "share", "moin") or None |
paul@2 | 726 | |
paul@50 | 727 | def get_moin_script(self): |
paul@50 | 728 | |
paul@50 | 729 | "Return the location of the general-purpose moin script." |
paul@50 | 730 | |
paul@50 | 731 | return join(self.prefix, "bin", "moin") |
paul@50 | 732 | |
paul@46 | 733 | def get_wikiconfig_directory(self): |
paul@46 | 734 | |
paul@46 | 735 | "Return the location of the Wiki configuration." |
paul@46 | 736 | |
paul@46 | 737 | if self.site_config: |
paul@46 | 738 | return split(self.site_config)[0] |
paul@46 | 739 | else: |
paul@46 | 740 | return self.common_dir |
paul@46 | 741 | |
paul@55 | 742 | def get_site_config(self): |
paul@55 | 743 | |
paul@55 | 744 | "Return the file providing the site-level configuration." |
paul@55 | 745 | |
paul@55 | 746 | if self.site_config: |
paul@55 | 747 | return self.site_config |
paul@55 | 748 | else: |
paul@55 | 749 | return join(self.common_dir, "wikiconfig.py") |
paul@55 | 750 | |
paul@55 | 751 | def get_global_config(self): |
paul@55 | 752 | |
paul@55 | 753 | "Return the file providing the global MoinMoin configuration." |
paul@55 | 754 | |
paul@55 | 755 | if self.farm_config: |
paul@55 | 756 | return self.farm_config |
paul@55 | 757 | else: |
paul@55 | 758 | return join(self.common_dir, "wikiconfig.py") |
paul@55 | 759 | |
paul@14 | 760 | def get_static_identifier(self): |
paul@14 | 761 | |
paul@14 | 762 | "Return the static URL/directory identifier for the Wiki." |
paul@14 | 763 | |
paul@14 | 764 | return "moin_static%s" % self.moin_version.replace(".", "") |
paul@14 | 765 | |
paul@18 | 766 | def get_plugin_directory(self, plugin_type): |
paul@18 | 767 | |
paul@18 | 768 | "Return the directory for plugins of the given 'plugin_type'." |
paul@18 | 769 | |
paul@43 | 770 | data_dir = join(self.common_dir, "data") |
paul@18 | 771 | return join(data_dir, "plugin", plugin_type) |
paul@18 | 772 | |
paul@14 | 773 | def limited_hosting(self): |
paul@14 | 774 | |
paul@14 | 775 | "Return whether limited Web hosting is being used." |
paul@14 | 776 | |
paul@30 | 777 | return not self.web_site_dir |
paul@14 | 778 | |
paul@8 | 779 | def ensure_directories(self): |
paul@2 | 780 | |
paul@8 | 781 | "Make sure that all the directories are available." |
paul@2 | 782 | |
paul@43 | 783 | for d in (self.common_dir, self.web_app_dir, self.web_static_dir, self.web_site_dir): |
paul@30 | 784 | if d is not None and not exists(d): |
paul@47 | 785 | makedirs(d) |
paul@2 | 786 | |
paul@20 | 787 | def get_theme_directories(self, theme_name=None): |
paul@20 | 788 | |
paul@20 | 789 | """ |
paul@20 | 790 | Return tuples of the form (theme name, theme directory) for all themes, |
paul@20 | 791 | or for a single theme if the optional 'theme_name' is specified. |
paul@20 | 792 | """ |
paul@20 | 793 | |
paul@47 | 794 | filenames = theme_name and [theme_name] or listdir(self.htdocs_dir) |
paul@20 | 795 | directories = [] |
paul@20 | 796 | |
paul@20 | 797 | for filename in filenames: |
paul@20 | 798 | theme_dir = join(self.htdocs_dir, filename) |
paul@20 | 799 | |
paul@20 | 800 | if not exists(theme_dir) or not isdir(theme_dir): |
paul@20 | 801 | continue |
paul@20 | 802 | |
paul@20 | 803 | directories.append((filename, theme_dir)) |
paul@20 | 804 | |
paul@20 | 805 | return directories |
paul@20 | 806 | |
paul@72 | 807 | def _get_temp_filename(self): |
paul@72 | 808 | fd, temp_filename = tempfile.mkstemp(dir=self.common_dir) |
paul@72 | 809 | os.close(fd) |
paul@72 | 810 | return temp_filename |
paul@72 | 811 | |
paul@72 | 812 | def have_setfacl(self): |
paul@72 | 813 | |
paul@72 | 814 | "Work out whether setfacl works." |
paul@72 | 815 | |
paul@72 | 816 | temp_filename = self._get_temp_filename() |
paul@72 | 817 | |
paul@72 | 818 | try: |
paul@72 | 819 | return os.system("setfacl -m user:%(web_user)s:r %(file)s > /dev/null 2>&1" % { |
paul@72 | 820 | "web_user" : self.web_user, "file" : temp_filename}) == 0 |
paul@72 | 821 | finally: |
paul@72 | 822 | remove(temp_filename) |
paul@72 | 823 | |
paul@9 | 824 | # Main methods. |
paul@9 | 825 | |
paul@8 | 826 | def setup(self): |
paul@8 | 827 | |
paul@8 | 828 | "Set up the installation." |
paul@8 | 829 | |
paul@8 | 830 | self.ensure_directories() |
paul@2 | 831 | self.install_moin() |
paul@43 | 832 | self.edit_moin_script() |
paul@12 | 833 | self._setup_wiki() |
paul@12 | 834 | |
paul@12 | 835 | def setup_wiki(self): |
paul@12 | 836 | |
paul@12 | 837 | "Set up a Wiki without installing MoinMoin." |
paul@12 | 838 | |
paul@12 | 839 | self.ensure_directories() |
paul@12 | 840 | self._setup_wiki() |
paul@12 | 841 | |
paul@12 | 842 | def _setup_wiki(self): |
paul@12 | 843 | |
paul@12 | 844 | "Set up a Wiki without installing MoinMoin." |
paul@12 | 845 | |
paul@2 | 846 | self.install_data() |
paul@43 | 847 | self.install_static_data() |
paul@2 | 848 | self.configure_moin() |
paul@4 | 849 | self.add_superuser() |
paul@30 | 850 | self.edit_moin_web_script(self.make_site_files()) |
paul@2 | 851 | self.make_post_install_script() |
paul@2 | 852 | |
paul@43 | 853 | if self.moin_version.startswith("1.9"): |
paul@43 | 854 | note("You may need to visit the LanguageSetup page in the Wiki to create the standard set of pages.") |
paul@43 | 855 | |
paul@43 | 856 | def install_moin(self): |
paul@2 | 857 | |
paul@2 | 858 | "Enter the distribution directory and run the setup script." |
paul@2 | 859 | |
paul@2 | 860 | # NOTE: Possibly check for an existing installation and skip repeated |
paul@2 | 861 | # NOTE: installation attempts. |
paul@2 | 862 | |
paul@40 | 863 | if not self.moin_distribution: |
paul@40 | 864 | raise SetupException, "Cannot install MoinMoin without a 'moin_distribution' setting being defined." |
paul@40 | 865 | |
paul@2 | 866 | this_dir = os.getcwd() |
paul@47 | 867 | chdir(self.moin_distribution) |
paul@2 | 868 | |
paul@2 | 869 | log_filename = "install-%s.log" % split(self.common_dir)[-1] |
paul@2 | 870 | |
paul@43 | 871 | status("Installing MoinMoin in %s..." % self.prefix) |
paul@2 | 872 | |
paul@43 | 873 | install_cmd = "install" |
paul@43 | 874 | options = "--prefix='%s' --record='%s'" % (self.prefix, log_filename) |
paul@12 | 875 | |
paul@66 | 876 | os.system("%s setup.py --quiet %s %s --force" % (sys.executable, install_cmd, options)) |
paul@2 | 877 | |
paul@47 | 878 | chdir(this_dir) |
paul@2 | 879 | |
paul@2 | 880 | def install_data(self): |
paul@2 | 881 | |
paul@43 | 882 | "Install Wiki data into an instance." |
paul@2 | 883 | |
paul@40 | 884 | moin_data = self.get_moin_data() |
paul@40 | 885 | |
paul@40 | 886 | if not moin_data: |
paul@40 | 887 | raise SetupException, \ |
paul@43 | 888 | "Cannot install MoinMoin data without either a 'moin_distribution' or a 'prefix' setting being defined." |
paul@40 | 889 | |
paul@2 | 890 | # The default wikiconfig assumes data and underlay in the same directory. |
paul@2 | 891 | |
paul@43 | 892 | status("Installing data and underlay in %s..." % self.common_dir) |
paul@2 | 893 | |
paul@2 | 894 | for d in ("data", "underlay"): |
paul@40 | 895 | source = join(moin_data, d) |
paul@47 | 896 | source_tar = source + extsep + "tar" |
paul@5 | 897 | |
paul@47 | 898 | if exists(source): |
paul@43 | 899 | shutil.copytree(source, join(self.common_dir, d)) |
paul@47 | 900 | elif exists(source_tar): |
paul@43 | 901 | |
paul@43 | 902 | note("Copying archive %s instead of directory %s. Running...\n" |
paul@43 | 903 | "make pagepacks\n" |
paul@43 | 904 | "in the distribution directory should rectify this." % (source_tar, source)) |
paul@43 | 905 | |
paul@43 | 906 | shutil.copy(source_tar, self.common_dir) |
paul@43 | 907 | os.system("tar xf %s -C %s" % (source_tar, self.common_dir)) |
paul@5 | 908 | else: |
paul@5 | 909 | status("Could not copy %s into installed Wiki." % d) |
paul@2 | 910 | |
paul@43 | 911 | def install_static_data(self): |
paul@14 | 912 | |
paul@43 | 913 | "Install static Web data if appropriate." |
paul@14 | 914 | |
paul@43 | 915 | if not exists(self.htdocs_dir): |
paul@47 | 916 | mkdir(self.htdocs_dir) |
paul@14 | 917 | |
paul@47 | 918 | for item in listdir(self.htdocs_dir_source): |
paul@43 | 919 | path = join(self.htdocs_dir_source, item) |
paul@43 | 920 | if isdir(path): |
paul@43 | 921 | shutil.copytree(path, join(self.htdocs_dir, item)) |
paul@43 | 922 | else: |
paul@43 | 923 | shutil.copy(path, join(self.htdocs_dir, item)) |
paul@14 | 924 | |
paul@47 | 925 | def configure_moin(self, reset=0): |
paul@2 | 926 | |
paul@47 | 927 | """ |
paul@47 | 928 | Edit the Wiki configuration file. If the optional 'reset' parameter is |
paul@47 | 929 | specified as a true value, a default configuration will be copied from |
paul@47 | 930 | the distribution if appropriate. |
paul@47 | 931 | """ |
paul@2 | 932 | |
paul@40 | 933 | moin_data = self.get_moin_data() |
paul@40 | 934 | |
paul@40 | 935 | if not moin_data: |
paul@40 | 936 | raise SetupException, \ |
paul@43 | 937 | "Cannot configure MoinMoin without either a 'moin_distribution' or a 'prefix' setting being defined." |
paul@40 | 938 | |
paul@2 | 939 | # NOTE: MoinMoin usually uses an apparently common URL space associated |
paul@2 | 940 | # NOTE: with the version, but more specific locations are probably |
paul@2 | 941 | # NOTE: acceptable if less efficient. |
paul@2 | 942 | |
paul@58 | 943 | url_prefix_static = "%r" % self.static_url_resources_path |
paul@2 | 944 | |
paul@41 | 945 | # Use a farm configuration file. |
paul@41 | 946 | |
paul@41 | 947 | if self.farm_config: |
paul@41 | 948 | wikiconfig_py = self.farm_config |
paul@2 | 949 | |
paul@41 | 950 | # Or copy the Wiki configuration file from the distribution. |
paul@41 | 951 | |
paul@41 | 952 | else: |
paul@43 | 953 | wikiconfig_py = join(self.common_dir, "wikiconfig.py") |
paul@47 | 954 | |
paul@47 | 955 | if not exists(wikiconfig_py) or reset: |
paul@47 | 956 | shutil.copyfile(join(moin_data, "config", "wikiconfig.py"), wikiconfig_py) |
paul@2 | 957 | |
paul@2 | 958 | status("Editing configuration from %s..." % wikiconfig_py) |
paul@2 | 959 | |
paul@12 | 960 | # Edit the Wiki configuration file. |
paul@12 | 961 | |
paul@12 | 962 | wikiconfig = Configuration(wikiconfig_py) |
paul@2 | 963 | |
paul@12 | 964 | try: |
paul@12 | 965 | wikiconfig.set("url_prefix_static", url_prefix_static, raw=1) |
paul@50 | 966 | if self.superuser: |
paul@50 | 967 | wikiconfig.set("superuser", [self.superuser]) |
paul@50 | 968 | wikiconfig.set("acl_rights_before", u"%s:read,write,delete,revert,admin" % self.superuser) |
paul@50 | 969 | else: |
paul@50 | 970 | note("Superuser not defined. The ACL rules should be fixed in the configuration.") |
paul@2 | 971 | |
paul@41 | 972 | # Edit any created Wiki configuration. |
paul@12 | 973 | |
paul@41 | 974 | if not self.site_config: |
paul@41 | 975 | self._configure_moin(wikiconfig) |
paul@9 | 976 | |
paul@12 | 977 | finally: |
paul@12 | 978 | wikiconfig.close() |
paul@2 | 979 | |
paul@41 | 980 | # Edit any separate site configuration file. |
paul@41 | 981 | |
paul@41 | 982 | if self.site_config: |
paul@41 | 983 | status("Editing configuration from %s..." % self.site_config) |
paul@41 | 984 | |
paul@41 | 985 | wikiconfig = Configuration(self.site_config) |
paul@41 | 986 | |
paul@41 | 987 | try: |
paul@41 | 988 | self._configure_moin(wikiconfig) |
paul@41 | 989 | finally: |
paul@41 | 990 | wikiconfig.close() |
paul@41 | 991 | |
paul@12 | 992 | def _configure_moin(self, wikiconfig): |
paul@9 | 993 | |
paul@9 | 994 | """ |
paul@12 | 995 | Configure Moin, accessing the configuration file using 'wikiconfig'. |
paul@9 | 996 | """ |
paul@9 | 997 | |
paul@43 | 998 | # Specific site configurations also appear to need 'data_dir', even in |
paul@43 | 999 | # 1.9. |
paul@43 | 1000 | |
paul@43 | 1001 | if not self.moin_version.startswith("1.9") or self.site_config: |
paul@43 | 1002 | data_dir = join(self.common_dir, "data") |
paul@43 | 1003 | data_underlay_dir = join(self.common_dir, "underlay") |
paul@41 | 1004 | |
paul@41 | 1005 | wikiconfig.set("data_dir", data_dir) |
paul@41 | 1006 | wikiconfig.set("data_underlay_dir", data_underlay_dir) |
paul@41 | 1007 | |
paul@12 | 1008 | wikiconfig.set("site_name", self.site_name) |
paul@12 | 1009 | wikiconfig.set("page_front_page", self.front_page_name, count=1) |
paul@9 | 1010 | |
paul@9 | 1011 | if self.theme_default is not None: |
paul@12 | 1012 | wikiconfig.set("theme_default", self.theme_default) |
paul@9 | 1013 | |
paul@13 | 1014 | def edit_moin_script(self): |
paul@2 | 1015 | |
paul@13 | 1016 | "Edit the moin script." |
paul@2 | 1017 | |
paul@50 | 1018 | moin_script = self.get_moin_script() |
paul@2 | 1019 | status("Editing moin script at %s..." % moin_script) |
paul@2 | 1020 | |
paul@2 | 1021 | s = readfile(moin_script) |
paul@69 | 1022 | s = s.replace("#!/usr/bin/env python", "#!%s" % sys.executable) |
paul@13 | 1023 | s = s.replace("#import sys", "import sys\nsys.path.insert(0, %r)" % self.prefix_site_packages) |
paul@2 | 1024 | |
paul@2 | 1025 | writefile(moin_script, s) |
paul@2 | 1026 | |
paul@30 | 1027 | def edit_moin_web_script(self, site_file_configured=1): |
paul@13 | 1028 | |
paul@13 | 1029 | "Edit and install CGI script." |
paul@13 | 1030 | |
paul@2 | 1031 | # NOTE: CGI only so far. |
paul@2 | 1032 | # NOTE: Permissions should be checked. |
paul@2 | 1033 | |
paul@40 | 1034 | moin_data = self.get_moin_data() |
paul@40 | 1035 | |
paul@14 | 1036 | if self.moin_version.startswith("1.9"): |
paul@40 | 1037 | moin_cgi_script = "moin.fcgi" |
paul@14 | 1038 | else: |
paul@40 | 1039 | moin_cgi_script = "moin.cgi" |
paul@14 | 1040 | |
paul@40 | 1041 | moin_cgi = join(moin_data, "server", moin_cgi_script) |
paul@2 | 1042 | moin_cgi_installed = join(self.web_app_dir, "moin.cgi") |
paul@2 | 1043 | |
paul@40 | 1044 | status("Editing moin.cgi script from %s, writing to %s..." % (moin_cgi, moin_cgi_installed)) |
paul@2 | 1045 | |
paul@2 | 1046 | s = readfile(moin_cgi) |
paul@69 | 1047 | s = s.replace("#!/usr/bin/env python", "#!%s" % sys.executable) |
paul@13 | 1048 | s = moin_cgi_prefix.sub("sys.path.insert(0, %r)" % self.prefix_site_packages, s) |
paul@43 | 1049 | s = moin_cgi_wikiconfig.sub("sys.path.insert(0, %r)" % self.common_dir, s) |
paul@2 | 1050 | |
paul@14 | 1051 | # Handle differences in script names when using limited hosting with |
paul@14 | 1052 | # URL rewriting. |
paul@14 | 1053 | |
paul@14 | 1054 | if self.limited_hosting(): |
paul@30 | 1055 | if not site_file_configured: |
paul@30 | 1056 | note("Site file not configured: script name not changed.") |
paul@14 | 1057 | else: |
paul@58 | 1058 | url_path = self.final_url_path |
paul@58 | 1059 | |
paul@30 | 1060 | if self.moin_version.startswith("1.9"): |
paul@58 | 1061 | s = moin_cgi_fix_script_name.sub(r"\1\2 %r" % url_path, s) |
paul@30 | 1062 | else: |
paul@58 | 1063 | s = moin_cgi_properties.sub(r"\1\2 %r" % {"script_name" : url_path}, s) |
paul@14 | 1064 | |
paul@24 | 1065 | # NOTE: Use CGI for now. |
paul@24 | 1066 | |
paul@24 | 1067 | if self.moin_version.startswith("1.9"): |
paul@24 | 1068 | s = moin_cgi_force_cgi.sub(r"\1", s) |
paul@24 | 1069 | |
paul@2 | 1070 | writefile(moin_cgi_installed, s) |
paul@2 | 1071 | os.system("chmod a+rx '%s'" % moin_cgi_installed) |
paul@2 | 1072 | |
paul@43 | 1073 | # Fix the cause of opaque errors in some Apache environments. |
paul@43 | 1074 | |
paul@43 | 1075 | os.system("chmod go-w '%s'" % moin_cgi_installed) |
paul@43 | 1076 | |
paul@4 | 1077 | def add_superuser(self): |
paul@4 | 1078 | |
paul@4 | 1079 | "Add the superuser account." |
paul@4 | 1080 | |
paul@50 | 1081 | if not self.superuser: |
paul@50 | 1082 | return |
paul@4 | 1083 | |
paul@4 | 1084 | print "Creating superuser", self.superuser, "using..." |
paul@4 | 1085 | email = raw_input("E-mail address: ") |
paul@4 | 1086 | password = getpass("Password: ") |
paul@4 | 1087 | |
paul@67 | 1088 | path = self._set_pythonpath() |
paul@4 | 1089 | |
paul@50 | 1090 | cmd = "%s --config-dir='%s' account create --name='%s' --email='%s' --password='%s'" % ( |
paul@50 | 1091 | self.get_moin_script(), self.common_dir, self.superuser, email, password) |
paul@41 | 1092 | os.system(cmd) |
paul@4 | 1093 | |
paul@67 | 1094 | self._reset_pythonpath(path) |
paul@4 | 1095 | |
paul@2 | 1096 | def make_site_files(self): |
paul@2 | 1097 | |
paul@2 | 1098 | "Make the Apache site files." |
paul@2 | 1099 | |
paul@2 | 1100 | # NOTE: Using local namespace for substitution. |
paul@2 | 1101 | |
paul@14 | 1102 | # Where the site definitions and applications directories are different, |
paul@14 | 1103 | # use a normal site definition. |
paul@2 | 1104 | |
paul@14 | 1105 | if not self.limited_hosting(): |
paul@14 | 1106 | |
paul@43 | 1107 | site_def = join(self.web_site_dir, self.site_identifier) |
paul@14 | 1108 | |
paul@14 | 1109 | s = apache_site % self.__dict__ |
paul@43 | 1110 | s += apache_site_extra % self.__dict__ |
paul@14 | 1111 | |
paul@30 | 1112 | status("Writing Apache site definitions to %s..." % site_def) |
paul@30 | 1113 | writefile(site_def, s) |
paul@30 | 1114 | |
paul@43 | 1115 | note("Copy the site definitions to the appropriate sites directory if appropriate.\n" |
paul@43 | 1116 | "Then, make sure that the site is enabled by running the appropriate tools (such as a2ensite).") |
paul@30 | 1117 | |
paul@30 | 1118 | return 1 |
paul@30 | 1119 | |
paul@14 | 1120 | # Otherwise, use an .htaccess file. |
paul@2 | 1121 | |
paul@14 | 1122 | else: |
paul@58 | 1123 | site_def = join(self.web_static_dir or self.web_app_dir, ".htaccess") |
paul@14 | 1124 | |
paul@14 | 1125 | s = apache_htaccess_combined_mod_rewrite % self.__dict__ |
paul@14 | 1126 | |
paul@30 | 1127 | status("Writing .htaccess file to %s..." % site_def) |
paul@30 | 1128 | try: |
paul@30 | 1129 | writefile(site_def, s) |
paul@30 | 1130 | except IOError: |
paul@30 | 1131 | note("The .htaccess file could not be written. This will also affect the script name setting.") |
paul@30 | 1132 | return 0 |
paul@30 | 1133 | else: |
paul@30 | 1134 | return 1 |
paul@2 | 1135 | |
paul@2 | 1136 | def make_post_install_script(self): |
paul@2 | 1137 | |
paul@2 | 1138 | "Write a post-install script with additional actions." |
paul@2 | 1139 | |
paul@33 | 1140 | # Create the scripts. |
paul@33 | 1141 | |
paul@2 | 1142 | this_user = os.environ["USER"] |
paul@2 | 1143 | |
paul@33 | 1144 | vars = {} |
paul@33 | 1145 | vars.update(Installation.__dict__) |
paul@33 | 1146 | vars.update(self.__dict__) |
paul@33 | 1147 | vars.update(locals()) |
paul@33 | 1148 | |
paul@72 | 1149 | for postinst_script, start, extra, logs in postinst_scripts.values(): |
paul@33 | 1150 | |
paul@33 | 1151 | s = start % vars |
paul@43 | 1152 | s += extra % vars |
paul@58 | 1153 | s += logs % vars |
paul@33 | 1154 | |
paul@33 | 1155 | writefile(postinst_script, s) |
paul@47 | 1156 | chmod(postinst_script, 0755) |
paul@2 | 1157 | |
paul@72 | 1158 | if self.have_setfacl(): |
paul@72 | 1159 | note("Run %s to set file access permissions.\n" |
paul@72 | 1160 | "If this somehow fails..." % postinst_scripts["setfacl"][0]) |
paul@33 | 1161 | |
paul@72 | 1162 | note("Run %s as root to set file ownership and permissions." % postinst_scripts["chown"][0]) |
paul@72 | 1163 | |
paul@72 | 1164 | note("Run %s as root to set SELinux permissions, if applicable." % postinst_scripts["semanage"][0]) |
paul@1 | 1165 | |
paul@9 | 1166 | # Accessory methods. |
paul@9 | 1167 | |
paul@12 | 1168 | def reconfigure_moin(self, name=None, value=None, raw=0): |
paul@9 | 1169 | |
paul@26 | 1170 | """ |
paul@26 | 1171 | Edit the installed Wiki configuration file, setting a parameter with any |
paul@26 | 1172 | given 'name' to the given 'value', treating the value as a raw |
paul@26 | 1173 | expression (not a string) if 'raw' is set to a true value. |
paul@26 | 1174 | |
paul@26 | 1175 | If 'name' and the remaining parameters are omitted, the default |
paul@26 | 1176 | configuration activity is performed. |
paul@41 | 1177 | |
paul@41 | 1178 | If the 'site_config' setting is defined, the specific site configuration |
paul@41 | 1179 | will be changed. |
paul@26 | 1180 | """ |
paul@9 | 1181 | |
paul@55 | 1182 | wikiconfig_py = self.get_site_config() |
paul@9 | 1183 | |
paul@9 | 1184 | status("Editing configuration from %s..." % wikiconfig_py) |
paul@9 | 1185 | |
paul@12 | 1186 | wikiconfig = Configuration(wikiconfig_py) |
paul@12 | 1187 | |
paul@12 | 1188 | try: |
paul@12 | 1189 | # Perform default configuration. |
paul@12 | 1190 | |
paul@12 | 1191 | if name is None and value is None: |
paul@12 | 1192 | self._configure_moin(wikiconfig) |
paul@12 | 1193 | else: |
paul@12 | 1194 | wikiconfig.set(name, value, raw=raw) |
paul@12 | 1195 | |
paul@12 | 1196 | finally: |
paul@12 | 1197 | wikiconfig.close() |
paul@9 | 1198 | |
paul@26 | 1199 | def set_auth_method(self, method_name): |
paul@26 | 1200 | |
paul@26 | 1201 | """ |
paul@26 | 1202 | Edit the installed Wiki configuration file, configuring the |
paul@26 | 1203 | authentication method having the given 'method_name'. |
paul@41 | 1204 | |
paul@64 | 1205 | Currently recognised authentication methods are: |
paul@64 | 1206 | |
paul@64 | 1207 | * openid (uses OpenIDAuth to access OpenID providers) |
paul@64 | 1208 | * moin, default (use MoinAuth to provide a login form) |
paul@64 | 1209 | * given, remote-user (use HTTPAuth to obtain Web server credentials) |
paul@64 | 1210 | |
paul@41 | 1211 | If the 'farm_config' setting is defined, the Wiki farm configuration |
paul@41 | 1212 | will be changed. |
paul@26 | 1213 | """ |
paul@26 | 1214 | |
paul@55 | 1215 | wikiconfig_py = self.get_global_config() |
paul@26 | 1216 | |
paul@26 | 1217 | status("Editing configuration from %s..." % wikiconfig_py) |
paul@26 | 1218 | |
paul@26 | 1219 | wikiconfig = Configuration(wikiconfig_py) |
paul@26 | 1220 | |
paul@26 | 1221 | try: |
paul@34 | 1222 | # OpenID authentication. |
paul@34 | 1223 | |
paul@26 | 1224 | if method_name.lower() == "openid": |
paul@34 | 1225 | wikiconfig.set_import("MoinMoin.auth.openidrp", ["OpenIDAuth"]) |
paul@9 | 1226 | |
paul@34 | 1227 | if self.moin_version.startswith("1.9"): |
paul@34 | 1228 | if wikiconfig.get("cookie_lifetime"): |
paul@34 | 1229 | wikiconfig.replace("cookie_lifetime", "(12, 12)", raw=1) |
paul@34 | 1230 | else: |
paul@34 | 1231 | wikiconfig.set("cookie_lifetime", "(12, 12)", raw=1) |
paul@26 | 1232 | else: |
paul@34 | 1233 | if wikiconfig.get("anonymous_session_lifetime"): |
paul@34 | 1234 | wikiconfig.replace("anonymous_session_lifetime", "1000", raw=1) |
paul@34 | 1235 | else: |
paul@34 | 1236 | wikiconfig.set("anonymous_session_lifetime", "1000", raw=1) |
paul@34 | 1237 | |
paul@34 | 1238 | auth_object = "OpenIDAuth()" |
paul@34 | 1239 | |
paul@34 | 1240 | # Default Moin authentication. |
paul@34 | 1241 | |
paul@34 | 1242 | elif method_name.lower() in ("moin", "default"): |
paul@34 | 1243 | wikiconfig.set_import("MoinMoin.auth", ["MoinAuth"]) |
paul@34 | 1244 | auth_object = "MoinAuth()" |
paul@34 | 1245 | |
paul@34 | 1246 | # REMOTE_USER authentication. |
paul@26 | 1247 | |
paul@34 | 1248 | elif method_name.lower() in ("given", "remote-user"): |
paul@34 | 1249 | wikiconfig.set_import("MoinMoin.auth.http", ["HTTPAuth"]) |
paul@34 | 1250 | auth_object = "HTTPAuth(autocreate=True)" |
paul@34 | 1251 | |
paul@34 | 1252 | # Other methods are not currently supported. |
paul@34 | 1253 | |
paul@34 | 1254 | else: |
paul@34 | 1255 | return |
paul@34 | 1256 | |
paul@34 | 1257 | # Edit the authentication setting. |
paul@34 | 1258 | |
paul@34 | 1259 | auth = wikiconfig.get("auth") |
paul@34 | 1260 | if auth: |
paul@34 | 1261 | wikiconfig.replace("auth", "%s + [%s]" % (auth, auth_object), raw=1) |
paul@34 | 1262 | else: |
paul@34 | 1263 | wikiconfig.set("auth", "[%s]" % auth_object, raw=1) |
paul@26 | 1264 | |
paul@26 | 1265 | finally: |
paul@26 | 1266 | wikiconfig.close() |
paul@26 | 1267 | |
paul@49 | 1268 | def migrate_instance(self, test=0, change_site=0): |
paul@47 | 1269 | |
paul@47 | 1270 | """ |
paul@47 | 1271 | Migrate the Wiki to the currently supported layout. If 'test' is |
paul@49 | 1272 | specified and set to a non-empty or true value, only print whether the |
paul@49 | 1273 | migration can be performed. |
paul@49 | 1274 | |
paul@49 | 1275 | If 'change_site' is specified and set to a non-empty or true value, the |
paul@49 | 1276 | site definitions will be updated; this will overwrite any changes made |
paul@49 | 1277 | to the site definitions after they were last produced by moinsetup, and |
paul@49 | 1278 | care must be taken to ensure that things like access controls are |
paul@49 | 1279 | re-added to the definitions after this action is performed. |
paul@47 | 1280 | """ |
paul@47 | 1281 | |
paul@47 | 1282 | conf_dir = join(self.common_dir, "conf") |
paul@47 | 1283 | if exists(conf_dir): |
paul@47 | 1284 | for filename in listdir(conf_dir): |
paul@47 | 1285 | pathname = join(conf_dir, filename) |
paul@48 | 1286 | target = join(self.common_dir, filename) |
paul@48 | 1287 | if not exists(target): |
paul@48 | 1288 | print "Move", filename, "from conf directory." |
paul@48 | 1289 | if not test: |
paul@48 | 1290 | rename(pathname, target) |
paul@47 | 1291 | else: |
paul@47 | 1292 | print "No conf directory." |
paul@47 | 1293 | |
paul@47 | 1294 | wikidata = join(self.common_dir, "wikidata") |
paul@47 | 1295 | if exists(wikidata): |
paul@47 | 1296 | htdocs = join(wikidata, "share", "moin", "htdocs") |
paul@47 | 1297 | if exists(htdocs): |
paul@48 | 1298 | target = join(self.common_dir, "htdocs") |
paul@48 | 1299 | if not exists(target): |
paul@48 | 1300 | print "Move htdocs from wikidata directory." |
paul@48 | 1301 | if not test: |
paul@48 | 1302 | rename(htdocs, target) |
paul@47 | 1303 | else: |
paul@47 | 1304 | print "No wikidata directory." |
paul@47 | 1305 | |
paul@47 | 1306 | # Remove links and directories. |
paul@47 | 1307 | |
paul@47 | 1308 | for name in ("conf", "wikidata"): |
paul@47 | 1309 | d = join(self.common_dir, name) |
paul@47 | 1310 | if islink(d): |
paul@47 | 1311 | print "Remove %s symbolic link." % name |
paul@47 | 1312 | if not test: |
paul@47 | 1313 | remove(d) |
paul@47 | 1314 | |
paul@47 | 1315 | if isdir(conf_dir): |
paul@47 | 1316 | print "Remove conf directory." |
paul@47 | 1317 | if not test: |
paul@47 | 1318 | rmdir(conf_dir) |
paul@47 | 1319 | |
paul@47 | 1320 | # Add any missing htdocs directory. |
paul@47 | 1321 | |
paul@47 | 1322 | if not exists(self.htdocs_dir): |
paul@47 | 1323 | print "Copy htdocs into the instance." |
paul@47 | 1324 | if not test: |
paul@47 | 1325 | self.install_static_data() |
paul@47 | 1326 | |
paul@47 | 1327 | # Now attempt to reconfigure the Wiki. |
paul@47 | 1328 | |
paul@49 | 1329 | print "Reconfigure the Wiki, the Web script%s." % (change_site and " and the site files" or "") |
paul@47 | 1330 | if not test: |
paul@47 | 1331 | self.configure_moin() |
paul@47 | 1332 | self.edit_moin_web_script() |
paul@49 | 1333 | if change_site: |
paul@49 | 1334 | self.make_site_files() |
paul@47 | 1335 | |
paul@26 | 1336 | def install_theme(self, theme_dir, theme_name=None): |
paul@26 | 1337 | |
paul@26 | 1338 | """ |
paul@26 | 1339 | Install Wiki theme provided in the given 'theme_dir' having the given |
paul@26 | 1340 | optional 'theme_name' (if different from the 'theme_dir' name). |
paul@26 | 1341 | """ |
paul@9 | 1342 | |
paul@9 | 1343 | theme_dir = normpath(theme_dir) |
paul@26 | 1344 | theme_name = theme_name or split(theme_dir)[-1] |
paul@9 | 1345 | theme_module = join(theme_dir, theme_name + extsep + "py") |
paul@9 | 1346 | |
paul@18 | 1347 | plugin_theme_dir = self.get_plugin_directory("theme") |
paul@9 | 1348 | |
paul@9 | 1349 | # Copy the theme module. |
paul@9 | 1350 | |
paul@9 | 1351 | status("Copying theme module to %s..." % plugin_theme_dir) |
paul@9 | 1352 | |
paul@9 | 1353 | shutil.copy(theme_module, plugin_theme_dir) |
paul@9 | 1354 | |
paul@9 | 1355 | # Copy the resources. |
paul@9 | 1356 | |
paul@9 | 1357 | resources_dir = join(self.htdocs_dir, theme_name) |
paul@9 | 1358 | |
paul@16 | 1359 | if not exists(resources_dir): |
paul@47 | 1360 | mkdir(resources_dir) |
paul@16 | 1361 | |
paul@9 | 1362 | status("Copying theme resources to %s..." % resources_dir) |
paul@9 | 1363 | |
paul@9 | 1364 | for d in ("css", "img"): |
paul@9 | 1365 | target_dir = join(resources_dir, d) |
paul@9 | 1366 | if exists(target_dir): |
paul@9 | 1367 | status("Replacing %s..." % target_dir) |
paul@9 | 1368 | shutil.rmtree(target_dir) |
paul@9 | 1369 | shutil.copytree(join(theme_dir, d), target_dir) |
paul@9 | 1370 | |
paul@11 | 1371 | # Copy additional resources from other themes. |
paul@11 | 1372 | |
paul@11 | 1373 | resources_source_dir = join(self.htdocs_dir, self.theme_master) |
paul@11 | 1374 | target_dir = join(resources_dir, "css") |
paul@11 | 1375 | |
paul@11 | 1376 | status("Copying resources from %s..." % resources_source_dir) |
paul@11 | 1377 | |
paul@11 | 1378 | for css_file in self.extra_theme_css_files: |
paul@11 | 1379 | css_file_path = join(resources_source_dir, "css", css_file) |
paul@11 | 1380 | if exists(css_file_path): |
paul@11 | 1381 | shutil.copy(css_file_path, target_dir) |
paul@11 | 1382 | |
paul@43 | 1383 | note("Don't forget to add theme resources for extensions for this theme.\n" |
paul@43 | 1384 | "Don't forget to edit this theme's stylesheets for extensions.") |
paul@21 | 1385 | |
paul@20 | 1386 | def install_extension_package(self, extension_dir): |
paul@20 | 1387 | |
paul@20 | 1388 | "Install any libraries from 'extension_dir' using a setup script." |
paul@20 | 1389 | |
paul@20 | 1390 | this_dir = os.getcwd() |
paul@47 | 1391 | chdir(extension_dir) |
paul@46 | 1392 | |
paul@56 | 1393 | try: |
paul@56 | 1394 | options = "install --install-lib=%s" % self.prefix_site_packages |
paul@66 | 1395 | os.system("%s setup.py %s" % (sys.executable, options)) |
paul@56 | 1396 | finally: |
paul@56 | 1397 | chdir(this_dir) |
paul@20 | 1398 | |
paul@18 | 1399 | def install_plugins(self, plugins_dir, plugin_type): |
paul@18 | 1400 | |
paul@18 | 1401 | """ |
paul@18 | 1402 | Install Wiki actions provided in the given 'plugins_dir' of the |
paul@18 | 1403 | specified 'plugin_type'. |
paul@18 | 1404 | """ |
paul@18 | 1405 | |
paul@18 | 1406 | plugin_target_dir = self.get_plugin_directory(plugin_type) |
paul@18 | 1407 | |
paul@18 | 1408 | # Copy the modules. |
paul@18 | 1409 | |
paul@18 | 1410 | status("Copying %s modules to %s..." % (plugin_type, plugin_target_dir)) |
paul@18 | 1411 | |
paul@65 | 1412 | # If a setup file is detected, abandon the installation. |
paul@65 | 1413 | |
paul@65 | 1414 | if exists(join(plugins_dir, "setup%spy" % extsep)): |
paul@65 | 1415 | print "Plugins not installed: setup%spy was detected." % extsep |
paul@65 | 1416 | return |
paul@65 | 1417 | |
paul@18 | 1418 | for module in glob(join(plugins_dir, "*%spy" % extsep)): |
paul@18 | 1419 | shutil.copy(module, plugin_target_dir) |
paul@18 | 1420 | |
paul@70 | 1421 | # Compile the plugin. |
paul@70 | 1422 | |
paul@70 | 1423 | plugin = join(plugins_dir, split(module)[-1]) |
paul@70 | 1424 | py_compile.compile(plugin) |
paul@70 | 1425 | |
paul@18 | 1426 | def install_actions(self, actions_dir): |
paul@18 | 1427 | |
paul@18 | 1428 | "Install Wiki actions provided in the given 'actions_dir'." |
paul@18 | 1429 | |
paul@18 | 1430 | self.install_plugins(actions_dir, "action") |
paul@18 | 1431 | |
paul@18 | 1432 | def install_macros(self, macros_dir): |
paul@18 | 1433 | |
paul@18 | 1434 | "Install Wiki macros provided in the given 'macros_dir'." |
paul@18 | 1435 | |
paul@18 | 1436 | self.install_plugins(macros_dir, "macro") |
paul@18 | 1437 | |
paul@35 | 1438 | def install_parsers(self, parsers_dir): |
paul@35 | 1439 | |
paul@52 | 1440 | "Install Wiki parsers provided in the given 'parsers_dir'." |
paul@35 | 1441 | |
paul@35 | 1442 | self.install_plugins(parsers_dir, "parser") |
paul@35 | 1443 | |
paul@52 | 1444 | def install_event_handlers(self, events_dir): |
paul@52 | 1445 | |
paul@52 | 1446 | "Install Wiki event handlers provided in the given 'events_dir'." |
paul@52 | 1447 | |
paul@52 | 1448 | self.install_plugins(events_dir, "events") |
paul@52 | 1449 | |
paul@18 | 1450 | def install_theme_resources(self, theme_resources_dir, theme_name=None): |
paul@18 | 1451 | |
paul@18 | 1452 | """ |
paul@18 | 1453 | Install theme resources provided in the given 'theme_resources_dir'. If |
paul@18 | 1454 | a specific 'theme_name' is given, only that theme will be given the |
paul@18 | 1455 | specified resources. |
paul@18 | 1456 | """ |
paul@18 | 1457 | |
paul@20 | 1458 | for theme_name, theme_dir in self.get_theme_directories(theme_name): |
paul@18 | 1459 | |
paul@20 | 1460 | # Copy the resources. |
paul@18 | 1461 | |
paul@18 | 1462 | copied = 0 |
paul@18 | 1463 | |
paul@18 | 1464 | for d in ("css", "img"): |
paul@18 | 1465 | source_dir = join(theme_resources_dir, d) |
paul@18 | 1466 | target_dir = join(theme_dir, d) |
paul@18 | 1467 | |
paul@18 | 1468 | if not exists(target_dir): |
paul@18 | 1469 | continue |
paul@18 | 1470 | |
paul@18 | 1471 | for resource in glob(join(source_dir, "*%s*" % extsep)): |
paul@18 | 1472 | shutil.copy(resource, target_dir) |
paul@18 | 1473 | copied = 1 |
paul@18 | 1474 | |
paul@18 | 1475 | if copied: |
paul@18 | 1476 | status("Copied theme resources into %s..." % theme_dir) |
paul@18 | 1477 | |
paul@21 | 1478 | note("Don't forget to edit theme stylesheets for any extensions.") |
paul@21 | 1479 | |
paul@20 | 1480 | def edit_theme_stylesheet(self, theme_stylesheet, imported_stylesheet, action="ensure", theme_name=None): |
paul@20 | 1481 | |
paul@20 | 1482 | """ |
paul@20 | 1483 | Edit the given 'theme_stylesheet', ensuring (or removing) a reference to |
paul@20 | 1484 | the 'imported_stylesheet' according to the given 'action' (optional, |
paul@20 | 1485 | defaulting to "ensure"). If a specific 'theme_name' is given, only that |
paul@20 | 1486 | theme will be affected. |
paul@20 | 1487 | """ |
paul@20 | 1488 | |
paul@20 | 1489 | if action == "ensure": |
paul@20 | 1490 | ensure = 1 |
paul@20 | 1491 | elif action == "remove": |
paul@20 | 1492 | ensure = 0 |
paul@20 | 1493 | else: |
paul@20 | 1494 | error("Action %s not valid: it must be given as either 'ensure' or 'remove'." % action) |
paul@20 | 1495 | return |
paul@20 | 1496 | |
paul@20 | 1497 | for theme_name, theme_dir in self.get_theme_directories(theme_name): |
paul@20 | 1498 | |
paul@20 | 1499 | # Locate the resources. |
paul@20 | 1500 | |
paul@20 | 1501 | css_dir = join(theme_dir, "css") |
paul@20 | 1502 | |
paul@20 | 1503 | if not exists(css_dir): |
paul@20 | 1504 | continue |
paul@20 | 1505 | |
paul@20 | 1506 | theme_stylesheet_filename = join(css_dir, theme_stylesheet) |
paul@20 | 1507 | imported_stylesheet_filename = join(css_dir, imported_stylesheet) |
paul@20 | 1508 | |
paul@20 | 1509 | if not exists(theme_stylesheet_filename): |
paul@20 | 1510 | error("Stylesheet %s not defined in theme %s." % (theme_stylesheet, theme_name)) |
paul@20 | 1511 | continue |
paul@20 | 1512 | |
paul@20 | 1513 | if not exists(imported_stylesheet_filename): |
paul@20 | 1514 | error("Stylesheet %s not defined in theme %s." % (imported_stylesheet, theme_name)) |
paul@20 | 1515 | continue |
paul@20 | 1516 | |
paul@20 | 1517 | # Edit the resources. |
paul@20 | 1518 | |
paul@20 | 1519 | s = readfile(theme_stylesheet_filename) |
paul@20 | 1520 | after_point = 0 |
paul@20 | 1521 | |
paul@20 | 1522 | for stylesheet_import in css_import_stylesheet.finditer(s): |
paul@20 | 1523 | before, filename, after = stylesheet_import.groups() |
paul@20 | 1524 | before_point, after_point = stylesheet_import.span() |
paul@20 | 1525 | |
paul@20 | 1526 | # Test the import for a reference to the requested imported |
paul@20 | 1527 | # stylesheet. |
paul@20 | 1528 | |
paul@20 | 1529 | if filename == imported_stylesheet: |
paul@20 | 1530 | if ensure: |
paul@20 | 1531 | break |
paul@20 | 1532 | else: |
paul@20 | 1533 | if s[after_point:after_point+1] == "\n": |
paul@20 | 1534 | after_point += 1 |
paul@20 | 1535 | s = "%s%s" % (s[:before_point], s[after_point:]) |
paul@20 | 1536 | |
paul@20 | 1537 | status("Removing %s from %s in theme %s..." % (imported_stylesheet, theme_stylesheet, theme_name)) |
paul@20 | 1538 | writefile(theme_stylesheet_filename, s) |
paul@20 | 1539 | break |
paul@20 | 1540 | |
paul@20 | 1541 | # Where no import references the imported stylesheet, insert a |
paul@20 | 1542 | # reference into the theme stylesheet. |
paul@20 | 1543 | |
paul@20 | 1544 | else: |
paul@20 | 1545 | if ensure: |
paul@20 | 1546 | |
paul@20 | 1547 | # Assume that the stylesheet can follow other imports. |
paul@20 | 1548 | |
paul@20 | 1549 | if s[after_point:after_point+1] == "\n": |
paul@20 | 1550 | after_point += 1 |
paul@20 | 1551 | s = "%s%s\n%s" % (s[:after_point], '@import "%s";' % imported_stylesheet, s[after_point:]) |
paul@20 | 1552 | |
paul@20 | 1553 | status("Adding %s to %s in theme %s..." % (imported_stylesheet, theme_stylesheet, theme_name)) |
paul@20 | 1554 | writefile(theme_stylesheet_filename, s) |
paul@20 | 1555 | |
paul@25 | 1556 | def make_page_package(self, page_directory, package_filename): |
paul@25 | 1557 | |
paul@25 | 1558 | """ |
paul@25 | 1559 | Make a package containing the pages in 'page_directory', using the |
paul@25 | 1560 | filenames as the page names, and writing the package to a file with the |
paul@25 | 1561 | given 'package_filename'. |
paul@25 | 1562 | """ |
paul@25 | 1563 | |
paul@25 | 1564 | package = ZipFile(package_filename, "w") |
paul@25 | 1565 | |
paul@25 | 1566 | try: |
paul@25 | 1567 | script = ["MoinMoinPackage|1"] |
paul@65 | 1568 | self._add_page_package_files(page_directory, "", package, script) |
paul@65 | 1569 | package.writestr("MOIN_PACKAGE", "\n".join(script) + "\n") |
paul@25 | 1570 | |
paul@65 | 1571 | finally: |
paul@65 | 1572 | package.close() |
paul@65 | 1573 | |
paul@65 | 1574 | def _add_page_package_files(self, page_directory, prefix, package, script): |
paul@65 | 1575 | |
paul@65 | 1576 | """ |
paul@65 | 1577 | Add files in the given 'page_directory' as pages with the given 'prefix' |
paul@65 | 1578 | to the given 'package', recording them in the given 'script'. |
paul@65 | 1579 | """ |
paul@35 | 1580 | |
paul@65 | 1581 | filenames = listdir(page_directory) |
paul@65 | 1582 | filenames.sort() |
paul@65 | 1583 | |
paul@65 | 1584 | for filename in filenames: |
paul@65 | 1585 | pathname = join(page_directory, filename) |
paul@65 | 1586 | |
paul@65 | 1587 | # Add files as pages having the filename as page name. |
paul@35 | 1588 | |
paul@65 | 1589 | if os.path.isfile(pathname): |
paul@65 | 1590 | pagename = prefix + filename |
paul@65 | 1591 | zipname = pagename.replace("/", "__") |
paul@65 | 1592 | package.write(pathname, zipname) |
paul@65 | 1593 | script.append("AddRevision|%s|%s" % (zipname, pagename)) |
paul@65 | 1594 | |
paul@65 | 1595 | elif os.path.isdir(pathname): |
paul@35 | 1596 | |
paul@35 | 1597 | # Add directories ending with "-attachments" as collections of |
paul@35 | 1598 | # attachments for a particular page. |
paul@35 | 1599 | |
paul@65 | 1600 | if filename.endswith("-attachments"): |
paul@65 | 1601 | pagename = prefix + filename[:-len("-attachments")] |
paul@65 | 1602 | zipname = pagename.replace("/", "__") |
paul@35 | 1603 | |
paul@35 | 1604 | # Add each file as an attachment. |
paul@35 | 1605 | |
paul@47 | 1606 | for attachment in listdir(pathname): |
paul@65 | 1607 | azipname = "%s_%s" % (zipname, attachment) |
paul@65 | 1608 | package.write(join(pathname, attachment), azipname) |
paul@65 | 1609 | script.append("AddAttachment|%s|%s|%s||" % ( |
paul@65 | 1610 | azipname, attachment, pagename)) |
paul@25 | 1611 | |
paul@65 | 1612 | # Descend into other directories. |
paul@25 | 1613 | |
paul@65 | 1614 | else: |
paul@65 | 1615 | pagename = prefix + filename |
paul@65 | 1616 | self._add_page_package_files(pathname, "%s/" % pagename, package, script) |
paul@25 | 1617 | |
paul@25 | 1618 | def install_page_package(self, package_filename): |
paul@25 | 1619 | |
paul@25 | 1620 | """ |
paul@25 | 1621 | Install a package from the file with the given 'package_filename'. |
paul@25 | 1622 | """ |
paul@25 | 1623 | |
paul@67 | 1624 | path = self._set_pythonpath() |
paul@25 | 1625 | installer = join(self.prefix_site_packages, "MoinMoin", "packages.py") |
paul@66 | 1626 | cmd = "%s %s i %s" % (sys.executable, installer, package_filename) |
paul@42 | 1627 | os.system(cmd) |
paul@67 | 1628 | self._reset_pythonpath(path) |
paul@25 | 1629 | |
paul@24 | 1630 | def show_methods(): |
paul@24 | 1631 | print "Methods:" |
paul@24 | 1632 | print |
paul@24 | 1633 | for method_name in Installation.method_names: |
paul@24 | 1634 | doc = getattr(Installation, method_name).__doc__.strip() |
paul@24 | 1635 | print "%-30s%-s" % (method_name, format(doc, 30)) |
paul@24 | 1636 | print |
paul@24 | 1637 | |
paul@7 | 1638 | # Command line option syntax. |
paul@7 | 1639 | |
paul@43 | 1640 | syntax_description = "[ -f <config-filename> ] ( -m <method> | --method=METHOD ) [ <method-argument> ... ]" |
paul@7 | 1641 | |
paul@1 | 1642 | # Main program. |
paul@0 | 1643 | |
paul@0 | 1644 | if __name__ == "__main__": |
paul@24 | 1645 | from ConfigParser import ConfigParser |
paul@7 | 1646 | import sys, cmdsyntax |
paul@1 | 1647 | |
paul@7 | 1648 | # Check the command syntax. |
paul@1 | 1649 | |
paul@7 | 1650 | syntax = cmdsyntax.Syntax(syntax_description) |
paul@0 | 1651 | try: |
paul@7 | 1652 | matches = syntax.get_args(sys.argv[1:]) |
paul@7 | 1653 | args = matches[0] |
paul@24 | 1654 | except IndexError: |
paul@24 | 1655 | print "Syntax:" |
paul@24 | 1656 | print sys.argv[0], syntax_description |
paul@24 | 1657 | print |
paul@24 | 1658 | show_methods() |
paul@24 | 1659 | sys.exit(1) |
paul@8 | 1660 | |
paul@24 | 1661 | # Obtain configuration details. |
paul@8 | 1662 | |
paul@24 | 1663 | try: |
paul@24 | 1664 | config_filename = args.get("config-filename", "moinsetup.cfg") |
paul@43 | 1665 | |
paul@43 | 1666 | if not exists(config_filename): |
paul@43 | 1667 | print "Configuration", config_filename, "not found." |
paul@43 | 1668 | sys.exit(1) |
paul@43 | 1669 | |
paul@24 | 1670 | config = ConfigParser() |
paul@24 | 1671 | config.read(config_filename) |
paul@24 | 1672 | |
paul@24 | 1673 | # Obtain as many arguments as needed from the configuration. |
paul@24 | 1674 | |
paul@24 | 1675 | config_arguments = dict(config.items("installation") + config.items("site")) |
paul@9 | 1676 | method_arguments = args.get("method-argument", []) |
paul@0 | 1677 | |
paul@8 | 1678 | # Attempt to initialise the configuration. |
paul@7 | 1679 | |
paul@24 | 1680 | installation = Installation(**config_arguments) |
paul@8 | 1681 | |
paul@42 | 1682 | except TypeError, exc: |
paul@43 | 1683 | print "Error:" |
paul@43 | 1684 | print |
paul@42 | 1685 | print exc.args[0] |
paul@42 | 1686 | print |
paul@24 | 1687 | print "Configuration settings:" |
paul@7 | 1688 | print |
paul@7 | 1689 | print Installation.__init__.__doc__ |
paul@22 | 1690 | print |
paul@7 | 1691 | sys.exit(1) |
paul@7 | 1692 | |
paul@24 | 1693 | # Obtain the method. |
paul@7 | 1694 | |
paul@24 | 1695 | try: |
paul@7 | 1696 | method = getattr(installation, args["method"]) |
paul@24 | 1697 | except AttributeError: |
paul@24 | 1698 | show_methods() |
paul@24 | 1699 | sys.exit(1) |
paul@7 | 1700 | |
paul@24 | 1701 | try: |
paul@24 | 1702 | method(*method_arguments) |
paul@43 | 1703 | except TypeError, exc: |
paul@43 | 1704 | print "Error:" |
paul@43 | 1705 | print |
paul@42 | 1706 | print exc.args[0] |
paul@42 | 1707 | print |
paul@24 | 1708 | print "Method documentation:" |
paul@24 | 1709 | print |
paul@24 | 1710 | print method.__doc__ |
paul@24 | 1711 | print |
paul@43 | 1712 | sys.exit(1) |
paul@0 | 1713 | |
paul@0 | 1714 | # vim: tabstop=4 expandtab shiftwidth=4 |