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