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