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