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