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@30 | 332 | web_site_dir=None, web_static_dir=None, theme_default=None): |
paul@2 | 333 | |
paul@2 | 334 | """ |
paul@2 | 335 | Initialise a Wiki installation using the following: |
paul@2 | 336 | |
paul@2 | 337 | * moin_distribution - the directory containing a MoinMoin source |
paul@2 | 338 | distribution |
paul@40 | 339 | * moin_data - the directory containing MoinMoin shared data |
paul@40 | 340 | (can be omitted if 'moin_distribution' is given) |
paul@2 | 341 | * prefix - the installation prefix (equivalent to /usr) |
paul@2 | 342 | * web_app_dir - the directory where Web applications and scripts |
paul@2 | 343 | reside (such as /home/www-user/cgi-bin) |
paul@2 | 344 | * common_dir - the directory where the Wiki configuration, |
paul@2 | 345 | resources and instance will reside (such as |
paul@2 | 346 | /home/www-user/mywiki) |
paul@2 | 347 | * url_path - the URL path at which the Wiki will be made |
paul@2 | 348 | available (such as / or /mywiki) |
paul@9 | 349 | * superuser - the name of the site's superuser (such as |
paul@9 | 350 | "AdminUser") |
paul@2 | 351 | * site_name - the name of the site (such as "My Wiki") |
paul@2 | 352 | * front_page_name - the front page name for the site (such as |
paul@2 | 353 | "FrontPage" or a specific name for the site) |
paul@30 | 354 | * web_site_dir - optional: the directory where Web site |
paul@30 | 355 | definitions reside (such as |
paul@30 | 356 | /etc/apache2/sites-available) |
paul@30 | 357 | * web_static_dir - optional: the directory where static Web |
paul@30 | 358 | resources reside (such as /home/www-user/htdocs) |
paul@9 | 359 | * theme_default - optional: the default theme (such as modern) |
paul@2 | 360 | """ |
paul@2 | 361 | |
paul@2 | 362 | self.moin_distribution = moin_distribution |
paul@40 | 363 | self.moin_data = moin_data |
paul@9 | 364 | self.superuser = superuser |
paul@2 | 365 | self.site_name = site_name |
paul@2 | 366 | self.front_page_name = front_page_name |
paul@9 | 367 | self.theme_default = theme_default |
paul@2 | 368 | |
paul@2 | 369 | # NOTE: Support the detection of the Apache sites directory. |
paul@2 | 370 | |
paul@30 | 371 | self.prefix, self.web_app_dir, self.web_site_dir, self.web_static_dir, self.common_dir = \ |
paul@30 | 372 | map(self._get_abspath, (prefix, web_app_dir, web_site_dir, web_static_dir, common_dir)) |
paul@2 | 373 | |
paul@2 | 374 | # Strip any trailing "/" from the URL path. |
paul@2 | 375 | |
paul@15 | 376 | if url_path != "/" and url_path.endswith("/"): |
paul@2 | 377 | self.url_path = url_path[:-1] |
paul@2 | 378 | else: |
paul@2 | 379 | self.url_path = url_path |
paul@2 | 380 | |
paul@2 | 381 | # Define and create specific directories. |
paul@25 | 382 | # Here are the configuration and actual Wiki data directories. |
paul@2 | 383 | |
paul@2 | 384 | self.conf_dir = join(self.common_dir, "conf") |
paul@2 | 385 | self.instance_dir = join(self.common_dir, "wikidata") |
paul@2 | 386 | |
paul@25 | 387 | # Define the place where the MoinMoin package will actually reside. |
paul@2 | 388 | |
paul@9 | 389 | self.prefix_site_packages = join(self.prefix, "lib", "python%s.%s" % sys.version_info[:2], "site-packages") |
paul@2 | 390 | |
paul@2 | 391 | # Find the version. |
paul@2 | 392 | |
paul@2 | 393 | self.moin_version = self.get_moin_version() |
paul@2 | 394 | |
paul@14 | 395 | # The static resources reside in different locations depending on the |
paul@14 | 396 | # version of MoinMoin. Moreover, these resources may end up in a |
paul@14 | 397 | # published directory for 1.8 installations where the Web server cannot |
paul@14 | 398 | # be instructed to fetch the content from outside certain designated |
paul@14 | 399 | # locations. |
paul@14 | 400 | |
paul@9 | 401 | # 1.9: moin/lib/python2.x/site-packages/MoinMoin/web/static/htdocs |
paul@9 | 402 | |
paul@9 | 403 | if self.moin_version.startswith("1.9"): |
paul@40 | 404 | |
paul@40 | 405 | # A shared data directory may be in use. |
paul@40 | 406 | |
paul@40 | 407 | if self.moin_data: |
paul@40 | 408 | self.htdocs_dir = self.htdocs_dir_source = join(self.moin_data, "htdocs") |
paul@40 | 409 | else: |
paul@40 | 410 | self.htdocs_dir = self.htdocs_dir_source = join(self.prefix_site_packages, "MoinMoin", "web", "static", "htdocs") |
paul@40 | 411 | |
paul@30 | 412 | self.static_url_path = self.url_path |
paul@14 | 413 | |
paul@14 | 414 | # 1.8: moin/share/moin/htdocs (optionally copied to a Web directory) |
paul@14 | 415 | |
paul@9 | 416 | else: |
paul@40 | 417 | |
paul@40 | 418 | # A shared data directory may be in use. |
paul@40 | 419 | |
paul@40 | 420 | if self.moin_data: |
paul@40 | 421 | self.htdocs_dir_source = join(self.moin_data, "htdocs") |
paul@40 | 422 | else: |
paul@40 | 423 | self.htdocs_dir_source = join(self.instance_dir, "share", "moin", "htdocs") |
paul@14 | 424 | |
paul@30 | 425 | # Add the static identifier to the URL path. For example: |
paul@30 | 426 | # / -> /moin_static187 |
paul@30 | 427 | # /hgwiki -> /hgwiki-moin_static187 |
paul@30 | 428 | |
paul@30 | 429 | self.static_url_path = self.url_path + (self.url_path != "/" and "-" or "") + self.get_static_identifier() |
paul@30 | 430 | |
paul@30 | 431 | # In limited hosting, the static resources directory is related to |
paul@30 | 432 | # the URL path. |
paul@30 | 433 | |
paul@14 | 434 | if self.limited_hosting(): |
paul@30 | 435 | self.htdocs_dir = join(self.web_static_dir or self.web_app_dir, self.static_url_path.lstrip("/")) |
paul@30 | 436 | |
paul@30 | 437 | # Otherwise, a mapping is made to the directory. |
paul@30 | 438 | |
paul@14 | 439 | else: |
paul@14 | 440 | self.htdocs_dir = self.htdocs_dir_source |
paul@9 | 441 | |
paul@30 | 442 | def _get_abspath(self, d): |
paul@30 | 443 | return d and abspath(d) or None |
paul@30 | 444 | |
paul@2 | 445 | def get_moin_version(self): |
paul@2 | 446 | |
paul@2 | 447 | "Inspect the MoinMoin package information, returning the version." |
paul@2 | 448 | |
paul@40 | 449 | if self.moin_distribution: |
paul@5 | 450 | try: |
paul@40 | 451 | this_dir = os.getcwd() |
paul@5 | 452 | try: |
paul@40 | 453 | os.chdir(self.moin_distribution) |
paul@40 | 454 | f = open("PKG-INFO") |
paul@40 | 455 | try: |
paul@40 | 456 | for line in f.xreadlines(): |
paul@40 | 457 | columns = line.split() |
paul@40 | 458 | if columns[0] == "Version:": |
paul@40 | 459 | return columns[1] |
paul@5 | 460 | |
paul@40 | 461 | return None |
paul@40 | 462 | |
paul@40 | 463 | finally: |
paul@40 | 464 | f.close() |
paul@2 | 465 | |
paul@5 | 466 | finally: |
paul@40 | 467 | os.chdir(this_dir) |
paul@2 | 468 | |
paul@5 | 469 | except IOError: |
paul@40 | 470 | pass |
paul@40 | 471 | |
paul@40 | 472 | # Where no distribution information can be read, try and import an |
paul@40 | 473 | # installed version module. |
paul@40 | 474 | |
paul@40 | 475 | f = os.popen("%s -c 'from MoinMoin.version import release; print release'" % sys.executable) |
paul@40 | 476 | try: |
paul@40 | 477 | return f.read().strip() |
paul@2 | 478 | finally: |
paul@40 | 479 | f.close() |
paul@40 | 480 | |
paul@40 | 481 | def get_moin_data(self): |
paul@40 | 482 | |
paul@40 | 483 | "Return the location of MoinMoin data." |
paul@40 | 484 | |
paul@40 | 485 | return self.moin_data or self.moin_distribution and join(self.moin_distribution, "wiki") or None |
paul@2 | 486 | |
paul@14 | 487 | def get_static_identifier(self): |
paul@14 | 488 | |
paul@14 | 489 | "Return the static URL/directory identifier for the Wiki." |
paul@14 | 490 | |
paul@14 | 491 | return "moin_static%s" % self.moin_version.replace(".", "") |
paul@14 | 492 | |
paul@18 | 493 | def get_plugin_directory(self, plugin_type): |
paul@18 | 494 | |
paul@18 | 495 | "Return the directory for plugins of the given 'plugin_type'." |
paul@18 | 496 | |
paul@18 | 497 | data_dir = join(self.conf_dir, "data") |
paul@18 | 498 | return join(data_dir, "plugin", plugin_type) |
paul@18 | 499 | |
paul@14 | 500 | def limited_hosting(self): |
paul@14 | 501 | |
paul@14 | 502 | "Return whether limited Web hosting is being used." |
paul@14 | 503 | |
paul@30 | 504 | return not self.web_site_dir |
paul@14 | 505 | |
paul@8 | 506 | def ensure_directories(self): |
paul@2 | 507 | |
paul@8 | 508 | "Make sure that all the directories are available." |
paul@2 | 509 | |
paul@30 | 510 | for d in (self.conf_dir, self.instance_dir, self.web_app_dir, self.web_static_dir, self.web_site_dir): |
paul@30 | 511 | if d is not None and not exists(d): |
paul@2 | 512 | os.makedirs(d) |
paul@2 | 513 | |
paul@20 | 514 | def get_theme_directories(self, theme_name=None): |
paul@20 | 515 | |
paul@20 | 516 | """ |
paul@20 | 517 | Return tuples of the form (theme name, theme directory) for all themes, |
paul@20 | 518 | or for a single theme if the optional 'theme_name' is specified. |
paul@20 | 519 | """ |
paul@20 | 520 | |
paul@20 | 521 | filenames = theme_name and [theme_name] or os.listdir(self.htdocs_dir) |
paul@20 | 522 | directories = [] |
paul@20 | 523 | |
paul@20 | 524 | for filename in filenames: |
paul@20 | 525 | theme_dir = join(self.htdocs_dir, filename) |
paul@20 | 526 | |
paul@20 | 527 | if not exists(theme_dir) or not isdir(theme_dir): |
paul@20 | 528 | continue |
paul@20 | 529 | |
paul@20 | 530 | directories.append((filename, theme_dir)) |
paul@20 | 531 | |
paul@20 | 532 | return directories |
paul@20 | 533 | |
paul@9 | 534 | # Main methods. |
paul@9 | 535 | |
paul@8 | 536 | def setup(self): |
paul@8 | 537 | |
paul@8 | 538 | "Set up the installation." |
paul@8 | 539 | |
paul@8 | 540 | self.ensure_directories() |
paul@2 | 541 | self.install_moin() |
paul@12 | 542 | self._setup_wiki() |
paul@12 | 543 | |
paul@12 | 544 | def setup_wiki(self): |
paul@12 | 545 | |
paul@12 | 546 | "Set up a Wiki without installing MoinMoin." |
paul@12 | 547 | |
paul@12 | 548 | self.ensure_directories() |
paul@40 | 549 | |
paul@40 | 550 | try: |
paul@40 | 551 | self.install_moin(data_only=1) |
paul@40 | 552 | except SetupException: |
paul@40 | 553 | if not self.moin_data: |
paul@40 | 554 | raise |
paul@40 | 555 | |
paul@12 | 556 | self._setup_wiki() |
paul@12 | 557 | |
paul@12 | 558 | def _setup_wiki(self): |
paul@12 | 559 | |
paul@12 | 560 | "Set up a Wiki without installing MoinMoin." |
paul@12 | 561 | |
paul@2 | 562 | self.install_data() |
paul@2 | 563 | self.configure_moin() |
paul@13 | 564 | self.edit_moin_script() |
paul@4 | 565 | self.add_superuser() |
paul@30 | 566 | self.edit_moin_web_script(self.make_site_files()) |
paul@2 | 567 | self.make_post_install_script() |
paul@2 | 568 | |
paul@12 | 569 | def install_moin(self, data_only=0): |
paul@2 | 570 | |
paul@2 | 571 | "Enter the distribution directory and run the setup script." |
paul@2 | 572 | |
paul@2 | 573 | # NOTE: Possibly check for an existing installation and skip repeated |
paul@2 | 574 | # NOTE: installation attempts. |
paul@2 | 575 | |
paul@40 | 576 | if not self.moin_distribution: |
paul@40 | 577 | raise SetupException, "Cannot install MoinMoin without a 'moin_distribution' setting being defined." |
paul@40 | 578 | |
paul@2 | 579 | this_dir = os.getcwd() |
paul@2 | 580 | os.chdir(self.moin_distribution) |
paul@2 | 581 | |
paul@2 | 582 | log_filename = "install-%s.log" % split(self.common_dir)[-1] |
paul@2 | 583 | |
paul@27 | 584 | status("Installing MoinMoin%s in %s..." % (data_only and " (data only)" or "", self.prefix)) |
paul@2 | 585 | |
paul@12 | 586 | if data_only: |
paul@12 | 587 | install_cmd = "install_data" |
paul@12 | 588 | options = "--install-dir='%s'" % self.instance_dir |
paul@12 | 589 | else: |
paul@12 | 590 | install_cmd = "install" |
paul@12 | 591 | options = "--prefix='%s' --install-data='%s' --record='%s'" % (self.prefix, self.instance_dir, log_filename) |
paul@12 | 592 | |
paul@12 | 593 | os.system("python setup.py --quiet %s %s --force" % (install_cmd, options)) |
paul@2 | 594 | |
paul@2 | 595 | os.chdir(this_dir) |
paul@2 | 596 | |
paul@2 | 597 | def install_data(self): |
paul@2 | 598 | |
paul@2 | 599 | "Install Wiki data." |
paul@2 | 600 | |
paul@40 | 601 | moin_data = self.get_moin_data() |
paul@40 | 602 | |
paul@40 | 603 | if not moin_data: |
paul@40 | 604 | raise SetupException, \ |
paul@40 | 605 | "Cannot install MoinMoin data without either a 'moin_distribution' or a 'moin_data' setting being defined." |
paul@40 | 606 | |
paul@2 | 607 | # The default wikiconfig assumes data and underlay in the same directory. |
paul@2 | 608 | |
paul@2 | 609 | status("Installing data and underlay in %s..." % self.conf_dir) |
paul@2 | 610 | |
paul@2 | 611 | for d in ("data", "underlay"): |
paul@40 | 612 | source = join(moin_data, d) |
paul@5 | 613 | source_tar = source + os.path.extsep + "tar" |
paul@5 | 614 | d_tar = source + os.path.extsep + "tar" |
paul@5 | 615 | |
paul@5 | 616 | if os.path.exists(source): |
paul@5 | 617 | shutil.copytree(source, join(self.conf_dir, d)) |
paul@5 | 618 | elif os.path.exists(source_tar): |
paul@5 | 619 | shutil.copy(source_tar, self.conf_dir) |
paul@5 | 620 | os.system("tar xf %s -C %s" % (d_tar, self.conf_dir)) |
paul@5 | 621 | else: |
paul@5 | 622 | status("Could not copy %s into installed Wiki." % d) |
paul@2 | 623 | |
paul@14 | 624 | # Copy static Web data if appropriate. |
paul@14 | 625 | |
paul@14 | 626 | if not self.moin_version.startswith("1.9") and self.limited_hosting(): |
paul@14 | 627 | |
paul@14 | 628 | if not exists(self.htdocs_dir): |
paul@14 | 629 | os.mkdir(self.htdocs_dir) |
paul@14 | 630 | |
paul@14 | 631 | for item in os.listdir(self.htdocs_dir_source): |
paul@14 | 632 | path = join(self.htdocs_dir_source, item) |
paul@14 | 633 | if isdir(path): |
paul@14 | 634 | shutil.copytree(path, join(self.htdocs_dir, item)) |
paul@14 | 635 | else: |
paul@14 | 636 | shutil.copy(path, join(self.htdocs_dir, item)) |
paul@14 | 637 | |
paul@2 | 638 | def configure_moin(self): |
paul@2 | 639 | |
paul@2 | 640 | "Edit the Wiki configuration file." |
paul@2 | 641 | |
paul@40 | 642 | moin_data = self.get_moin_data() |
paul@40 | 643 | |
paul@40 | 644 | if not moin_data: |
paul@40 | 645 | raise SetupException, \ |
paul@40 | 646 | "Cannot configure MoinMoin without either a 'moin_distribution' or a 'moin_data' setting being defined." |
paul@40 | 647 | |
paul@2 | 648 | # NOTE: Single Wiki only so far. |
paul@2 | 649 | |
paul@2 | 650 | # Static URLs seem to be different in MoinMoin 1.9.x. |
paul@2 | 651 | # For earlier versions, reserve URL space alongside the Wiki. |
paul@2 | 652 | # NOTE: MoinMoin usually uses an apparently common URL space associated |
paul@2 | 653 | # NOTE: with the version, but more specific locations are probably |
paul@2 | 654 | # NOTE: acceptable if less efficient. |
paul@2 | 655 | |
paul@2 | 656 | if self.moin_version.startswith("1.9"): |
paul@12 | 657 | url_prefix_static = "%r + url_prefix_static" % self.static_url_path |
paul@2 | 658 | else: |
paul@12 | 659 | url_prefix_static = "%r" % self.static_url_path |
paul@2 | 660 | |
paul@12 | 661 | # Copy the Wiki configuration file from the distribution. |
paul@2 | 662 | |
paul@12 | 663 | wikiconfig_py = join(self.conf_dir, "wikiconfig.py") |
paul@40 | 664 | shutil.copyfile(join(moin_data, "config", "wikiconfig.py"), wikiconfig_py) |
paul@2 | 665 | |
paul@2 | 666 | status("Editing configuration from %s..." % wikiconfig_py) |
paul@2 | 667 | |
paul@12 | 668 | # Edit the Wiki configuration file. |
paul@12 | 669 | |
paul@12 | 670 | wikiconfig = Configuration(wikiconfig_py) |
paul@2 | 671 | |
paul@12 | 672 | try: |
paul@12 | 673 | wikiconfig.set("url_prefix_static", url_prefix_static, raw=1) |
paul@12 | 674 | wikiconfig.set("superuser", [self.superuser]) |
paul@12 | 675 | wikiconfig.set("acl_rights_before", u"%s:read,write,delete,revert,admin" % self.superuser) |
paul@2 | 676 | |
paul@12 | 677 | if not self.moin_version.startswith("1.9"): |
paul@12 | 678 | data_dir = join(self.conf_dir, "data") |
paul@12 | 679 | data_underlay_dir = join(self.conf_dir, "underlay") |
paul@12 | 680 | |
paul@12 | 681 | wikiconfig.set("data_dir", data_dir) |
paul@12 | 682 | wikiconfig.set("data_underlay_dir", data_underlay_dir) |
paul@2 | 683 | |
paul@12 | 684 | self._configure_moin(wikiconfig) |
paul@9 | 685 | |
paul@12 | 686 | finally: |
paul@12 | 687 | wikiconfig.close() |
paul@2 | 688 | |
paul@12 | 689 | def _configure_moin(self, wikiconfig): |
paul@9 | 690 | |
paul@9 | 691 | """ |
paul@12 | 692 | Configure Moin, accessing the configuration file using 'wikiconfig'. |
paul@9 | 693 | """ |
paul@9 | 694 | |
paul@12 | 695 | wikiconfig.set("site_name", self.site_name) |
paul@12 | 696 | wikiconfig.set("page_front_page", self.front_page_name, count=1) |
paul@9 | 697 | |
paul@9 | 698 | if self.theme_default is not None: |
paul@12 | 699 | wikiconfig.set("theme_default", self.theme_default) |
paul@9 | 700 | |
paul@13 | 701 | def edit_moin_script(self): |
paul@2 | 702 | |
paul@13 | 703 | "Edit the moin script." |
paul@2 | 704 | |
paul@2 | 705 | moin_script = join(self.prefix, "bin", "moin") |
paul@2 | 706 | |
paul@2 | 707 | status("Editing moin script at %s..." % moin_script) |
paul@2 | 708 | |
paul@2 | 709 | s = readfile(moin_script) |
paul@13 | 710 | s = s.replace("#import sys", "import sys\nsys.path.insert(0, %r)" % self.prefix_site_packages) |
paul@2 | 711 | |
paul@2 | 712 | writefile(moin_script, s) |
paul@2 | 713 | |
paul@30 | 714 | def edit_moin_web_script(self, site_file_configured=1): |
paul@13 | 715 | |
paul@13 | 716 | "Edit and install CGI script." |
paul@13 | 717 | |
paul@2 | 718 | # NOTE: CGI only so far. |
paul@2 | 719 | # NOTE: Permissions should be checked. |
paul@2 | 720 | |
paul@40 | 721 | moin_data = self.get_moin_data() |
paul@40 | 722 | |
paul@14 | 723 | if self.moin_version.startswith("1.9"): |
paul@40 | 724 | moin_cgi_script = "moin.fcgi" |
paul@14 | 725 | else: |
paul@40 | 726 | moin_cgi_script = "moin.cgi" |
paul@14 | 727 | |
paul@40 | 728 | moin_cgi = join(moin_data, "server", moin_cgi_script) |
paul@2 | 729 | moin_cgi_installed = join(self.web_app_dir, "moin.cgi") |
paul@2 | 730 | |
paul@40 | 731 | status("Editing moin.cgi script from %s, writing to %s..." % (moin_cgi, moin_cgi_installed)) |
paul@2 | 732 | |
paul@2 | 733 | s = readfile(moin_cgi) |
paul@13 | 734 | s = moin_cgi_prefix.sub("sys.path.insert(0, %r)" % self.prefix_site_packages, s) |
paul@2 | 735 | s = moin_cgi_wikiconfig.sub("sys.path.insert(0, %r)" % self.conf_dir, s) |
paul@2 | 736 | |
paul@14 | 737 | # Handle differences in script names when using limited hosting with |
paul@14 | 738 | # URL rewriting. |
paul@14 | 739 | |
paul@14 | 740 | if self.limited_hosting(): |
paul@30 | 741 | if not site_file_configured: |
paul@30 | 742 | note("Site file not configured: script name not changed.") |
paul@14 | 743 | else: |
paul@30 | 744 | if self.moin_version.startswith("1.9"): |
paul@30 | 745 | s = moin_cgi_fix_script_name.sub(r"\1\2 %r" % self.url_path, s) |
paul@30 | 746 | else: |
paul@30 | 747 | s = moin_cgi_properties.sub(r"\1\2 %r" % {"script_name" : self.url_path}, s) |
paul@14 | 748 | |
paul@24 | 749 | # NOTE: Use CGI for now. |
paul@24 | 750 | |
paul@24 | 751 | if self.moin_version.startswith("1.9"): |
paul@24 | 752 | s = moin_cgi_force_cgi.sub(r"\1", s) |
paul@24 | 753 | |
paul@2 | 754 | writefile(moin_cgi_installed, s) |
paul@2 | 755 | os.system("chmod a+rx '%s'" % moin_cgi_installed) |
paul@2 | 756 | |
paul@4 | 757 | def add_superuser(self): |
paul@4 | 758 | |
paul@4 | 759 | "Add the superuser account." |
paul@4 | 760 | |
paul@4 | 761 | moin_script = join(self.prefix, "bin", "moin") |
paul@4 | 762 | |
paul@4 | 763 | print "Creating superuser", self.superuser, "using..." |
paul@4 | 764 | email = raw_input("E-mail address: ") |
paul@4 | 765 | password = getpass("Password: ") |
paul@4 | 766 | |
paul@4 | 767 | path = os.environ.get("PYTHONPATH", "") |
paul@4 | 768 | |
paul@4 | 769 | if path: |
paul@4 | 770 | os.environ["PYTHONPATH"] = path + ":" + self.conf_dir |
paul@4 | 771 | else: |
paul@4 | 772 | os.environ["PYTHONPATH"] = self.conf_dir |
paul@4 | 773 | |
paul@4 | 774 | os.system(moin_script + " account create --name='%s' --email='%s' --password='%s'" % (self.superuser, email, password)) |
paul@4 | 775 | |
paul@4 | 776 | if path: |
paul@4 | 777 | os.environ["PYTHONPATH"] = path |
paul@4 | 778 | else: |
paul@4 | 779 | del os.environ["PYTHONPATH"] |
paul@4 | 780 | |
paul@2 | 781 | def make_site_files(self): |
paul@2 | 782 | |
paul@2 | 783 | "Make the Apache site files." |
paul@2 | 784 | |
paul@2 | 785 | # NOTE: Using local namespace for substitution. |
paul@2 | 786 | |
paul@14 | 787 | # Where the site definitions and applications directories are different, |
paul@14 | 788 | # use a normal site definition. |
paul@2 | 789 | |
paul@14 | 790 | if not self.limited_hosting(): |
paul@14 | 791 | |
paul@14 | 792 | site_def = join(self.web_site_dir, self.site_name) |
paul@14 | 793 | |
paul@14 | 794 | s = apache_site % self.__dict__ |
paul@2 | 795 | |
paul@14 | 796 | if not self.moin_version.startswith("1.9"): |
paul@14 | 797 | s += apache_site_extra_moin18 % self.__dict__ |
paul@14 | 798 | |
paul@30 | 799 | status("Writing Apache site definitions to %s..." % site_def) |
paul@30 | 800 | writefile(site_def, s) |
paul@30 | 801 | |
paul@30 | 802 | note("Copy the site definitions to the appropriate sites directory if appropriate.") |
paul@30 | 803 | note("Then, make sure that the site is enabled by running the appropriate tools (such as a2ensite).") |
paul@30 | 804 | |
paul@30 | 805 | return 1 |
paul@30 | 806 | |
paul@14 | 807 | # Otherwise, use an .htaccess file. |
paul@2 | 808 | |
paul@14 | 809 | else: |
paul@30 | 810 | site_def = join(self.web_app_dir, ".htaccess") |
paul@14 | 811 | |
paul@14 | 812 | s = apache_htaccess_combined_mod_rewrite % self.__dict__ |
paul@14 | 813 | |
paul@30 | 814 | status("Writing .htaccess file to %s..." % site_def) |
paul@30 | 815 | try: |
paul@30 | 816 | writefile(site_def, s) |
paul@30 | 817 | except IOError: |
paul@30 | 818 | note("The .htaccess file could not be written. This will also affect the script name setting.") |
paul@30 | 819 | return 0 |
paul@30 | 820 | else: |
paul@30 | 821 | return 1 |
paul@2 | 822 | |
paul@2 | 823 | def make_post_install_script(self): |
paul@2 | 824 | |
paul@2 | 825 | "Write a post-install script with additional actions." |
paul@2 | 826 | |
paul@33 | 827 | # Work out whether setfacl works. |
paul@33 | 828 | |
paul@33 | 829 | fd, temp_filename = tempfile.mkstemp(dir=self.conf_dir) |
paul@33 | 830 | os.close(fd) |
paul@33 | 831 | |
paul@33 | 832 | have_setfacl = os.system("setfacl -m user:%(web_user)s:r %(file)s > /dev/null 2>&1" % { |
paul@33 | 833 | "web_user" : self.web_user, "file" : temp_filename}) == 0 |
paul@33 | 834 | |
paul@33 | 835 | os.remove(temp_filename) |
paul@33 | 836 | |
paul@33 | 837 | # Create the scripts. |
paul@33 | 838 | |
paul@2 | 839 | this_user = os.environ["USER"] |
paul@33 | 840 | postinst_scripts = "moinsetup-post-chown.sh", "moinsetup-post-setfacl.sh" |
paul@2 | 841 | |
paul@33 | 842 | vars = {} |
paul@33 | 843 | vars.update(Installation.__dict__) |
paul@33 | 844 | vars.update(self.__dict__) |
paul@33 | 845 | vars.update(locals()) |
paul@33 | 846 | |
paul@33 | 847 | for postinst_script, start, extra in [ |
paul@33 | 848 | (postinst_scripts[0], postsetup_chown_chmod, postsetup_chown_moin18_extra), |
paul@33 | 849 | (postinst_scripts[1], postsetup_setfacl, postsetup_setfacl_moin18_extra) |
paul@33 | 850 | ]: |
paul@33 | 851 | |
paul@33 | 852 | s = start % vars |
paul@2 | 853 | |
paul@33 | 854 | if not self.moin_version.startswith("1.9"): |
paul@33 | 855 | s += extra % vars |
paul@33 | 856 | |
paul@33 | 857 | writefile(postinst_script, s) |
paul@33 | 858 | os.chmod(postinst_script, 0755) |
paul@2 | 859 | |
paul@33 | 860 | if have_setfacl: |
paul@33 | 861 | note("Run %s to set file ownership and permissions." % postinst_scripts[1]) |
paul@33 | 862 | note("If this somehow fails...") |
paul@33 | 863 | |
paul@33 | 864 | note("Run %s as root to set file ownership and permissions." % postinst_scripts[0]) |
paul@1 | 865 | |
paul@9 | 866 | # Accessory methods. |
paul@9 | 867 | |
paul@12 | 868 | def reconfigure_moin(self, name=None, value=None, raw=0): |
paul@9 | 869 | |
paul@26 | 870 | """ |
paul@26 | 871 | Edit the installed Wiki configuration file, setting a parameter with any |
paul@26 | 872 | given 'name' to the given 'value', treating the value as a raw |
paul@26 | 873 | expression (not a string) if 'raw' is set to a true value. |
paul@26 | 874 | |
paul@26 | 875 | If 'name' and the remaining parameters are omitted, the default |
paul@26 | 876 | configuration activity is performed. |
paul@26 | 877 | """ |
paul@9 | 878 | |
paul@9 | 879 | wikiconfig_py = join(self.conf_dir, "wikiconfig.py") |
paul@9 | 880 | |
paul@9 | 881 | status("Editing configuration from %s..." % wikiconfig_py) |
paul@9 | 882 | |
paul@12 | 883 | wikiconfig = Configuration(wikiconfig_py) |
paul@12 | 884 | |
paul@12 | 885 | try: |
paul@12 | 886 | # Perform default configuration. |
paul@12 | 887 | |
paul@12 | 888 | if name is None and value is None: |
paul@12 | 889 | self._configure_moin(wikiconfig) |
paul@12 | 890 | else: |
paul@12 | 891 | wikiconfig.set(name, value, raw=raw) |
paul@12 | 892 | |
paul@12 | 893 | finally: |
paul@12 | 894 | wikiconfig.close() |
paul@9 | 895 | |
paul@26 | 896 | def set_auth_method(self, method_name): |
paul@26 | 897 | |
paul@26 | 898 | """ |
paul@26 | 899 | Edit the installed Wiki configuration file, configuring the |
paul@26 | 900 | authentication method having the given 'method_name'. |
paul@26 | 901 | """ |
paul@26 | 902 | |
paul@26 | 903 | wikiconfig_py = join(self.conf_dir, "wikiconfig.py") |
paul@26 | 904 | |
paul@26 | 905 | status("Editing configuration from %s..." % wikiconfig_py) |
paul@26 | 906 | |
paul@26 | 907 | wikiconfig = Configuration(wikiconfig_py) |
paul@26 | 908 | |
paul@26 | 909 | try: |
paul@34 | 910 | # OpenID authentication. |
paul@34 | 911 | |
paul@26 | 912 | if method_name.lower() == "openid": |
paul@34 | 913 | wikiconfig.set_import("MoinMoin.auth.openidrp", ["OpenIDAuth"]) |
paul@9 | 914 | |
paul@34 | 915 | if self.moin_version.startswith("1.9"): |
paul@34 | 916 | if wikiconfig.get("cookie_lifetime"): |
paul@34 | 917 | wikiconfig.replace("cookie_lifetime", "(12, 12)", raw=1) |
paul@34 | 918 | else: |
paul@34 | 919 | wikiconfig.set("cookie_lifetime", "(12, 12)", raw=1) |
paul@26 | 920 | else: |
paul@34 | 921 | if wikiconfig.get("anonymous_session_lifetime"): |
paul@34 | 922 | wikiconfig.replace("anonymous_session_lifetime", "1000", raw=1) |
paul@34 | 923 | else: |
paul@34 | 924 | wikiconfig.set("anonymous_session_lifetime", "1000", raw=1) |
paul@34 | 925 | |
paul@34 | 926 | auth_object = "OpenIDAuth()" |
paul@34 | 927 | |
paul@34 | 928 | # Default Moin authentication. |
paul@34 | 929 | |
paul@34 | 930 | elif method_name.lower() in ("moin", "default"): |
paul@34 | 931 | wikiconfig.set_import("MoinMoin.auth", ["MoinAuth"]) |
paul@34 | 932 | auth_object = "MoinAuth()" |
paul@34 | 933 | |
paul@34 | 934 | # REMOTE_USER authentication. |
paul@26 | 935 | |
paul@34 | 936 | elif method_name.lower() in ("given", "remote-user"): |
paul@34 | 937 | wikiconfig.set_import("MoinMoin.auth.http", ["HTTPAuth"]) |
paul@34 | 938 | auth_object = "HTTPAuth(autocreate=True)" |
paul@34 | 939 | |
paul@34 | 940 | # Other methods are not currently supported. |
paul@34 | 941 | |
paul@34 | 942 | else: |
paul@34 | 943 | return |
paul@34 | 944 | |
paul@34 | 945 | # Edit the authentication setting. |
paul@34 | 946 | |
paul@34 | 947 | auth = wikiconfig.get("auth") |
paul@34 | 948 | if auth: |
paul@34 | 949 | wikiconfig.replace("auth", "%s + [%s]" % (auth, auth_object), raw=1) |
paul@34 | 950 | else: |
paul@34 | 951 | wikiconfig.set("auth", "[%s]" % auth_object, raw=1) |
paul@26 | 952 | |
paul@26 | 953 | finally: |
paul@26 | 954 | wikiconfig.close() |
paul@26 | 955 | |
paul@26 | 956 | def install_theme(self, theme_dir, theme_name=None): |
paul@26 | 957 | |
paul@26 | 958 | """ |
paul@26 | 959 | Install Wiki theme provided in the given 'theme_dir' having the given |
paul@26 | 960 | optional 'theme_name' (if different from the 'theme_dir' name). |
paul@26 | 961 | """ |
paul@9 | 962 | |
paul@9 | 963 | theme_dir = normpath(theme_dir) |
paul@26 | 964 | theme_name = theme_name or split(theme_dir)[-1] |
paul@9 | 965 | theme_module = join(theme_dir, theme_name + extsep + "py") |
paul@9 | 966 | |
paul@18 | 967 | plugin_theme_dir = self.get_plugin_directory("theme") |
paul@9 | 968 | |
paul@9 | 969 | # Copy the theme module. |
paul@9 | 970 | |
paul@9 | 971 | status("Copying theme module to %s..." % plugin_theme_dir) |
paul@9 | 972 | |
paul@9 | 973 | shutil.copy(theme_module, plugin_theme_dir) |
paul@9 | 974 | |
paul@9 | 975 | # Copy the resources. |
paul@9 | 976 | |
paul@9 | 977 | resources_dir = join(self.htdocs_dir, theme_name) |
paul@9 | 978 | |
paul@16 | 979 | if not exists(resources_dir): |
paul@16 | 980 | os.mkdir(resources_dir) |
paul@16 | 981 | |
paul@9 | 982 | status("Copying theme resources to %s..." % resources_dir) |
paul@9 | 983 | |
paul@9 | 984 | for d in ("css", "img"): |
paul@9 | 985 | target_dir = join(resources_dir, d) |
paul@9 | 986 | if exists(target_dir): |
paul@9 | 987 | status("Replacing %s..." % target_dir) |
paul@9 | 988 | shutil.rmtree(target_dir) |
paul@9 | 989 | shutil.copytree(join(theme_dir, d), target_dir) |
paul@9 | 990 | |
paul@11 | 991 | # Copy additional resources from other themes. |
paul@11 | 992 | |
paul@11 | 993 | resources_source_dir = join(self.htdocs_dir, self.theme_master) |
paul@11 | 994 | target_dir = join(resources_dir, "css") |
paul@11 | 995 | |
paul@11 | 996 | status("Copying resources from %s..." % resources_source_dir) |
paul@11 | 997 | |
paul@11 | 998 | for css_file in self.extra_theme_css_files: |
paul@11 | 999 | css_file_path = join(resources_source_dir, "css", css_file) |
paul@11 | 1000 | if exists(css_file_path): |
paul@11 | 1001 | shutil.copy(css_file_path, target_dir) |
paul@11 | 1002 | |
paul@21 | 1003 | note("Don't forget to add theme resources for extensions for this theme.") |
paul@29 | 1004 | note("Don't forget to edit this theme's stylesheets for extensions.") |
paul@21 | 1005 | |
paul@20 | 1006 | def install_extension_package(self, extension_dir): |
paul@20 | 1007 | |
paul@20 | 1008 | "Install any libraries from 'extension_dir' using a setup script." |
paul@20 | 1009 | |
paul@20 | 1010 | this_dir = os.getcwd() |
paul@20 | 1011 | os.chdir(extension_dir) |
paul@20 | 1012 | os.system("python setup.py install --prefix=%s" % self.prefix) |
paul@20 | 1013 | os.chdir(this_dir) |
paul@20 | 1014 | |
paul@18 | 1015 | def install_plugins(self, plugins_dir, plugin_type): |
paul@18 | 1016 | |
paul@18 | 1017 | """ |
paul@18 | 1018 | Install Wiki actions provided in the given 'plugins_dir' of the |
paul@18 | 1019 | specified 'plugin_type'. |
paul@18 | 1020 | """ |
paul@18 | 1021 | |
paul@18 | 1022 | plugin_target_dir = self.get_plugin_directory(plugin_type) |
paul@18 | 1023 | |
paul@18 | 1024 | # Copy the modules. |
paul@18 | 1025 | |
paul@18 | 1026 | status("Copying %s modules to %s..." % (plugin_type, plugin_target_dir)) |
paul@18 | 1027 | |
paul@18 | 1028 | for module in glob(join(plugins_dir, "*%spy" % extsep)): |
paul@18 | 1029 | shutil.copy(module, plugin_target_dir) |
paul@18 | 1030 | |
paul@18 | 1031 | def install_actions(self, actions_dir): |
paul@18 | 1032 | |
paul@18 | 1033 | "Install Wiki actions provided in the given 'actions_dir'." |
paul@18 | 1034 | |
paul@18 | 1035 | self.install_plugins(actions_dir, "action") |
paul@18 | 1036 | |
paul@18 | 1037 | def install_macros(self, macros_dir): |
paul@18 | 1038 | |
paul@18 | 1039 | "Install Wiki macros provided in the given 'macros_dir'." |
paul@18 | 1040 | |
paul@18 | 1041 | self.install_plugins(macros_dir, "macro") |
paul@18 | 1042 | |
paul@35 | 1043 | def install_parsers(self, parsers_dir): |
paul@35 | 1044 | |
paul@35 | 1045 | "Install Wiki macros provided in the given 'parsers_dir'." |
paul@35 | 1046 | |
paul@35 | 1047 | self.install_plugins(parsers_dir, "parser") |
paul@35 | 1048 | |
paul@18 | 1049 | def install_theme_resources(self, theme_resources_dir, theme_name=None): |
paul@18 | 1050 | |
paul@18 | 1051 | """ |
paul@18 | 1052 | Install theme resources provided in the given 'theme_resources_dir'. If |
paul@18 | 1053 | a specific 'theme_name' is given, only that theme will be given the |
paul@18 | 1054 | specified resources. |
paul@18 | 1055 | """ |
paul@18 | 1056 | |
paul@20 | 1057 | for theme_name, theme_dir in self.get_theme_directories(theme_name): |
paul@18 | 1058 | |
paul@20 | 1059 | # Copy the resources. |
paul@18 | 1060 | |
paul@18 | 1061 | copied = 0 |
paul@18 | 1062 | |
paul@18 | 1063 | for d in ("css", "img"): |
paul@18 | 1064 | source_dir = join(theme_resources_dir, d) |
paul@18 | 1065 | target_dir = join(theme_dir, d) |
paul@18 | 1066 | |
paul@18 | 1067 | if not exists(target_dir): |
paul@18 | 1068 | continue |
paul@18 | 1069 | |
paul@18 | 1070 | for resource in glob(join(source_dir, "*%s*" % extsep)): |
paul@18 | 1071 | shutil.copy(resource, target_dir) |
paul@18 | 1072 | copied = 1 |
paul@18 | 1073 | |
paul@18 | 1074 | if copied: |
paul@18 | 1075 | status("Copied theme resources into %s..." % theme_dir) |
paul@18 | 1076 | |
paul@21 | 1077 | note("Don't forget to edit theme stylesheets for any extensions.") |
paul@21 | 1078 | |
paul@20 | 1079 | def edit_theme_stylesheet(self, theme_stylesheet, imported_stylesheet, action="ensure", theme_name=None): |
paul@20 | 1080 | |
paul@20 | 1081 | """ |
paul@20 | 1082 | Edit the given 'theme_stylesheet', ensuring (or removing) a reference to |
paul@20 | 1083 | the 'imported_stylesheet' according to the given 'action' (optional, |
paul@20 | 1084 | defaulting to "ensure"). If a specific 'theme_name' is given, only that |
paul@20 | 1085 | theme will be affected. |
paul@20 | 1086 | """ |
paul@20 | 1087 | |
paul@20 | 1088 | if action == "ensure": |
paul@20 | 1089 | ensure = 1 |
paul@20 | 1090 | elif action == "remove": |
paul@20 | 1091 | ensure = 0 |
paul@20 | 1092 | else: |
paul@20 | 1093 | error("Action %s not valid: it must be given as either 'ensure' or 'remove'." % action) |
paul@20 | 1094 | return |
paul@20 | 1095 | |
paul@20 | 1096 | for theme_name, theme_dir in self.get_theme_directories(theme_name): |
paul@20 | 1097 | |
paul@20 | 1098 | # Locate the resources. |
paul@20 | 1099 | |
paul@20 | 1100 | css_dir = join(theme_dir, "css") |
paul@20 | 1101 | |
paul@20 | 1102 | if not exists(css_dir): |
paul@20 | 1103 | continue |
paul@20 | 1104 | |
paul@20 | 1105 | theme_stylesheet_filename = join(css_dir, theme_stylesheet) |
paul@20 | 1106 | imported_stylesheet_filename = join(css_dir, imported_stylesheet) |
paul@20 | 1107 | |
paul@20 | 1108 | if not exists(theme_stylesheet_filename): |
paul@20 | 1109 | error("Stylesheet %s not defined in theme %s." % (theme_stylesheet, theme_name)) |
paul@20 | 1110 | continue |
paul@20 | 1111 | |
paul@20 | 1112 | if not exists(imported_stylesheet_filename): |
paul@20 | 1113 | error("Stylesheet %s not defined in theme %s." % (imported_stylesheet, theme_name)) |
paul@20 | 1114 | continue |
paul@20 | 1115 | |
paul@20 | 1116 | # Edit the resources. |
paul@20 | 1117 | |
paul@20 | 1118 | s = readfile(theme_stylesheet_filename) |
paul@20 | 1119 | after_point = 0 |
paul@20 | 1120 | |
paul@20 | 1121 | for stylesheet_import in css_import_stylesheet.finditer(s): |
paul@20 | 1122 | before, filename, after = stylesheet_import.groups() |
paul@20 | 1123 | before_point, after_point = stylesheet_import.span() |
paul@20 | 1124 | |
paul@20 | 1125 | # Test the import for a reference to the requested imported |
paul@20 | 1126 | # stylesheet. |
paul@20 | 1127 | |
paul@20 | 1128 | if filename == imported_stylesheet: |
paul@20 | 1129 | if ensure: |
paul@20 | 1130 | break |
paul@20 | 1131 | else: |
paul@20 | 1132 | if s[after_point:after_point+1] == "\n": |
paul@20 | 1133 | after_point += 1 |
paul@20 | 1134 | s = "%s%s" % (s[:before_point], s[after_point:]) |
paul@20 | 1135 | |
paul@20 | 1136 | status("Removing %s from %s in theme %s..." % (imported_stylesheet, theme_stylesheet, theme_name)) |
paul@20 | 1137 | writefile(theme_stylesheet_filename, s) |
paul@20 | 1138 | break |
paul@20 | 1139 | |
paul@20 | 1140 | # Where no import references the imported stylesheet, insert a |
paul@20 | 1141 | # reference into the theme stylesheet. |
paul@20 | 1142 | |
paul@20 | 1143 | else: |
paul@20 | 1144 | if ensure: |
paul@20 | 1145 | |
paul@20 | 1146 | # Assume that the stylesheet can follow other imports. |
paul@20 | 1147 | |
paul@20 | 1148 | if s[after_point:after_point+1] == "\n": |
paul@20 | 1149 | after_point += 1 |
paul@20 | 1150 | s = "%s%s\n%s" % (s[:after_point], '@import "%s";' % imported_stylesheet, s[after_point:]) |
paul@20 | 1151 | |
paul@20 | 1152 | status("Adding %s to %s in theme %s..." % (imported_stylesheet, theme_stylesheet, theme_name)) |
paul@20 | 1153 | writefile(theme_stylesheet_filename, s) |
paul@20 | 1154 | |
paul@25 | 1155 | def make_page_package(self, page_directory, package_filename): |
paul@25 | 1156 | |
paul@25 | 1157 | """ |
paul@25 | 1158 | Make a package containing the pages in 'page_directory', using the |
paul@25 | 1159 | filenames as the page names, and writing the package to a file with the |
paul@25 | 1160 | given 'package_filename'. |
paul@25 | 1161 | """ |
paul@25 | 1162 | |
paul@25 | 1163 | package = ZipFile(package_filename, "w") |
paul@25 | 1164 | |
paul@25 | 1165 | try: |
paul@25 | 1166 | script = ["MoinMoinPackage|1"] |
paul@25 | 1167 | |
paul@25 | 1168 | for filename in os.listdir(page_directory): |
paul@35 | 1169 | pathname = join(page_directory, filename) |
paul@35 | 1170 | |
paul@35 | 1171 | # Add files as pages having the filename as page name. |
paul@35 | 1172 | |
paul@35 | 1173 | if os.path.isfile(pathname): |
paul@35 | 1174 | package.write(pathname, filename) |
paul@35 | 1175 | script.append("AddRevision|%s|%s" % (filename, filename)) |
paul@35 | 1176 | |
paul@35 | 1177 | # Add directories ending with "-attachments" as collections of |
paul@35 | 1178 | # attachments for a particular page. |
paul@35 | 1179 | |
paul@35 | 1180 | elif os.path.isdir(pathname) and filename.endswith("-attachments"): |
paul@35 | 1181 | parent = filename[:-len("-attachments")] |
paul@35 | 1182 | |
paul@35 | 1183 | # Add each file as an attachment. |
paul@35 | 1184 | |
paul@35 | 1185 | for attachment in os.listdir(pathname): |
paul@35 | 1186 | zipname = "%s_%s" % (filename, attachment) |
paul@35 | 1187 | package.write(join(pathname, attachment), zipname) |
paul@35 | 1188 | script.append("AddAttachment|%s|%s|%s||" % (zipname, attachment, parent)) |
paul@25 | 1189 | |
paul@25 | 1190 | package.writestr("MOIN_PACKAGE", "\n".join(script)) |
paul@25 | 1191 | |
paul@25 | 1192 | finally: |
paul@25 | 1193 | package.close() |
paul@25 | 1194 | |
paul@25 | 1195 | def install_page_package(self, package_filename): |
paul@25 | 1196 | |
paul@25 | 1197 | """ |
paul@25 | 1198 | Install a package from the file with the given 'package_filename'. |
paul@25 | 1199 | """ |
paul@25 | 1200 | |
paul@25 | 1201 | path = os.environ.get("PYTHONPATH", "") |
paul@25 | 1202 | |
paul@25 | 1203 | if path: |
paul@25 | 1204 | os.environ["PYTHONPATH"] = path + ":" + self.prefix_site_packages + ":" + self.conf_dir |
paul@25 | 1205 | else: |
paul@25 | 1206 | os.environ["PYTHONPATH"] = self.prefix_site_packages + ":" + self.conf_dir |
paul@25 | 1207 | |
paul@25 | 1208 | installer = join(self.prefix_site_packages, "MoinMoin", "packages.py") |
paul@25 | 1209 | os.system("python %s i %s" % (installer, package_filename)) |
paul@25 | 1210 | |
paul@25 | 1211 | if path: |
paul@25 | 1212 | os.environ["PYTHONPATH"] = path |
paul@25 | 1213 | else: |
paul@25 | 1214 | del os.environ["PYTHONPATH"] |
paul@25 | 1215 | |
paul@24 | 1216 | def show_methods(): |
paul@24 | 1217 | print "Methods:" |
paul@24 | 1218 | print |
paul@24 | 1219 | for method_name in Installation.method_names: |
paul@24 | 1220 | doc = getattr(Installation, method_name).__doc__.strip() |
paul@24 | 1221 | print "%-30s%-s" % (method_name, format(doc, 30)) |
paul@24 | 1222 | print |
paul@24 | 1223 | |
paul@7 | 1224 | # Command line option syntax. |
paul@7 | 1225 | |
paul@25 | 1226 | syntax_description = "[ -f <config-filename> ] ( <method> | --method=METHOD ) [ <method-argument> ... ]" |
paul@7 | 1227 | |
paul@1 | 1228 | # Main program. |
paul@0 | 1229 | |
paul@0 | 1230 | if __name__ == "__main__": |
paul@24 | 1231 | from ConfigParser import ConfigParser |
paul@7 | 1232 | import sys, cmdsyntax |
paul@1 | 1233 | |
paul@7 | 1234 | # Check the command syntax. |
paul@1 | 1235 | |
paul@7 | 1236 | syntax = cmdsyntax.Syntax(syntax_description) |
paul@0 | 1237 | try: |
paul@7 | 1238 | matches = syntax.get_args(sys.argv[1:]) |
paul@7 | 1239 | args = matches[0] |
paul@24 | 1240 | except IndexError: |
paul@24 | 1241 | print "Syntax:" |
paul@24 | 1242 | print sys.argv[0], syntax_description |
paul@24 | 1243 | print |
paul@24 | 1244 | show_methods() |
paul@24 | 1245 | sys.exit(1) |
paul@8 | 1246 | |
paul@24 | 1247 | # Obtain configuration details. |
paul@8 | 1248 | |
paul@24 | 1249 | try: |
paul@24 | 1250 | config_filename = args.get("config-filename", "moinsetup.cfg") |
paul@24 | 1251 | config = ConfigParser() |
paul@24 | 1252 | config.read(config_filename) |
paul@24 | 1253 | |
paul@24 | 1254 | # Obtain as many arguments as needed from the configuration. |
paul@24 | 1255 | |
paul@24 | 1256 | config_arguments = dict(config.items("installation") + config.items("site")) |
paul@9 | 1257 | method_arguments = args.get("method-argument", []) |
paul@0 | 1258 | |
paul@8 | 1259 | # Attempt to initialise the configuration. |
paul@7 | 1260 | |
paul@24 | 1261 | installation = Installation(**config_arguments) |
paul@8 | 1262 | |
paul@24 | 1263 | except TypeError: |
paul@24 | 1264 | print "Configuration settings:" |
paul@7 | 1265 | print |
paul@7 | 1266 | print Installation.__init__.__doc__ |
paul@22 | 1267 | print |
paul@7 | 1268 | sys.exit(1) |
paul@7 | 1269 | |
paul@24 | 1270 | # Obtain the method. |
paul@7 | 1271 | |
paul@24 | 1272 | try: |
paul@7 | 1273 | method = getattr(installation, args["method"]) |
paul@24 | 1274 | except AttributeError: |
paul@24 | 1275 | show_methods() |
paul@24 | 1276 | sys.exit(1) |
paul@7 | 1277 | |
paul@24 | 1278 | try: |
paul@24 | 1279 | method(*method_arguments) |
paul@24 | 1280 | except TypeError: |
paul@24 | 1281 | print "Method documentation:" |
paul@24 | 1282 | print |
paul@24 | 1283 | print method.__doc__ |
paul@24 | 1284 | print |
paul@24 | 1285 | raise |
paul@0 | 1286 | |
paul@0 | 1287 | # vim: tabstop=4 expandtab shiftwidth=4 |