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