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