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