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