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