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