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