moinsetup

Change of moinsetup.py

9:18ce9b375d50
moinsetup.py
     1.1 --- a/moinsetup.py	Thu May 13 22:48:11 2010 +0200
     1.2 +++ b/moinsetup.py	Sat May 22 22:17:47 2010 +0200
     1.3 @@ -1,6 +1,6 @@
     1.4  #!/usr/bin/env python
     1.5  
     1.6 -from os.path import abspath, exists, join, split
     1.7 +from os.path import abspath, exists, extsep, join, normpath, split
     1.8  from getpass import getpass
     1.9  import os
    1.10  import sys
    1.11 @@ -14,13 +14,15 @@
    1.12  
    1.13  moin_cgi_prefix                 = re.compile("^#sys.path.insert\(0, 'PREFIX.*$", re.MULTILINE)
    1.14  moin_cgi_wikiconfig             = re.compile("^#sys.path.insert\(0, '/path/to/wikiconfigdir.*$", re.MULTILINE)
    1.15 -wikiconfig_py_site_name         = compile_definition("site_name")
    1.16 +
    1.17 +wikiconfig_py_site_name         = compile_definition("site_?name")
    1.18  wikiconfig_py_url_prefix_static = compile_definition("url_prefix_static")
    1.19  wikiconfig_py_superuser         = compile_definition("superuser")
    1.20  wikiconfig_py_acl_rights_before = compile_definition("acl_rights_before")
    1.21  wikiconfig_py_page_front_page   = compile_definition("page_front_page")
    1.22  wikiconfig_py_data_dir          = compile_definition("data_dir")
    1.23  wikiconfig_py_data_underlay_dir = compile_definition("data_underlay_dir")
    1.24 +wikiconfig_py_theme_default     = compile_definition("theme_default")
    1.25  
    1.26  # Templates for Apache site definitions.
    1.27  
    1.28 @@ -64,7 +66,8 @@
    1.29      web_group = "www-data"
    1.30  
    1.31      def __init__(self, moin_distribution, prefix, web_app_dir, web_site_dir,
    1.32 -        common_dir, url_path, site_name, front_page_name, superuser):
    1.33 +        common_dir, url_path, superuser, site_name, front_page_name,
    1.34 +        theme_default=None):
    1.35  
    1.36          """
    1.37          Initialise a Wiki installation using the following:
    1.38 @@ -81,17 +84,19 @@
    1.39                                  /home/www-user/mywiki)
    1.40            * url_path          - the URL path at which the Wiki will be made
    1.41                                  available (such as / or /mywiki)
    1.42 +          * superuser         - the name of the site's superuser (such as
    1.43 +                                "AdminUser")
    1.44            * site_name         - the name of the site (such as "My Wiki")
    1.45            * front_page_name   - the front page name for the site (such as
    1.46                                  "FrontPage" or a specific name for the site)
    1.47 -          * superuser         - the name of the site's superuser (such as
    1.48 -                                "AdminUser")
    1.49 +          * theme_default     - optional: the default theme (such as modern)
    1.50          """
    1.51  
    1.52          self.moin_distribution = moin_distribution
    1.53 +        self.superuser = superuser
    1.54          self.site_name = site_name
    1.55          self.front_page_name = front_page_name
    1.56 -        self.superuser = superuser
    1.57 +        self.theme_default = theme_default
    1.58  
    1.59          # NOTE: Support the detection of the Apache sites directory.
    1.60  
    1.61 @@ -110,14 +115,22 @@
    1.62          self.conf_dir = join(self.common_dir, "conf")
    1.63          self.instance_dir = join(self.common_dir, "wikidata")
    1.64  
    1.65 -        # For MoinMoin 1.8.x and earlier.
    1.66 +        # Define useful directories.
    1.67  
    1.68 -        self.htdocs_dir = join(self.instance_dir, "share", "moin", "htdocs")
    1.69 +        self.prefix_site_packages = join(self.prefix, "lib", "python%s.%s" % sys.version_info[:2], "site-packages")
    1.70  
    1.71          # Find the version.
    1.72  
    1.73          self.moin_version = self.get_moin_version()
    1.74  
    1.75 +        # 1.8: moin/share/moin/htdocs
    1.76 +        # 1.9: moin/lib/python2.x/site-packages/MoinMoin/web/static/htdocs
    1.77 +
    1.78 +        if self.moin_version.startswith("1.9"):
    1.79 +            self.htdocs_dir = join(self.prefix_site_packages, "MoinMoin", "web", "static", "htdocs")
    1.80 +        else:
    1.81 +            self.htdocs_dir = join(self.instance_dir, "share", "moin", "htdocs")
    1.82 +
    1.83      def get_moin_version(self):
    1.84  
    1.85          "Inspect the MoinMoin package information, returning the version."
    1.86 @@ -156,6 +169,8 @@
    1.87              if not exists(d):
    1.88                  os.makedirs(d)
    1.89  
    1.90 +    # Main methods.
    1.91 +
    1.92      def setup(self):
    1.93  
    1.94          "Set up the installation."
    1.95 @@ -235,11 +250,9 @@
    1.96          status("Editing configuration from %s..." % wikiconfig_py)
    1.97  
    1.98          s = readfile(wikiconfig_py)
    1.99 -        s = wikiconfig_py_site_name.sub(r"\1\2 %r" % self.site_name, s)
   1.100          s = wikiconfig_py_url_prefix_static.sub(url_prefix_static_sub, s)
   1.101          s = wikiconfig_py_superuser.sub(r"\1\2 %r" % [self.superuser], s)
   1.102          s = wikiconfig_py_acl_rights_before.sub(r"\1\2 %r" % (u"%s:read,write,delete,revert,admin" % self.superuser), s)
   1.103 -        s = wikiconfig_py_page_front_page.sub(r"\1\2 %r" % self.front_page_name, s, count=1)
   1.104  
   1.105          if not self.moin_version.startswith("1.9"):
   1.106              data_dir = join(self.conf_dir, "data")
   1.107 @@ -248,8 +261,25 @@
   1.108              s = wikiconfig_py_data_dir.sub(r"\1\2 %r" % data_dir, s)
   1.109              s = wikiconfig_py_data_underlay_dir.sub(r"\1\2 %r" % data_underlay_dir, s)
   1.110  
   1.111 +        s = self._configure_moin(s)
   1.112 +
   1.113          writefile(join(self.conf_dir, "wikiconfig.py"), s)
   1.114  
   1.115 +    def _configure_moin(self, s):
   1.116 +
   1.117 +        """
   1.118 +        Configure Moin, taking the configuration file's contents as 's' and
   1.119 +        returning the edited contents.
   1.120 +        """
   1.121 +
   1.122 +        s = wikiconfig_py_site_name.sub(r"\1\2 %r" % self.site_name, s)
   1.123 +        s = wikiconfig_py_page_front_page.sub(r"\1\2 %r" % self.front_page_name, s, count=1)
   1.124 +
   1.125 +        if self.theme_default is not None:
   1.126 +            s = wikiconfig_py_theme_default.sub(r"\1\2 %r" % self.theme_default, s)
   1.127 +
   1.128 +        return s
   1.129 +
   1.130      def edit_moin_scripts(self):
   1.131  
   1.132          "Edit the moin script and the CGI script."
   1.133 @@ -342,9 +372,53 @@
   1.134          os.chmod(postinst_script, 0755)
   1.135          note("Run %s as root to set file ownership and permissions." % postinst_script)
   1.136  
   1.137 +    # Accessory methods.
   1.138 +
   1.139 +    def reconfigure_moin(self):
   1.140 +
   1.141 +        "Edit the installed Wiki configuration file."
   1.142 +
   1.143 +        wikiconfig_py = join(self.conf_dir, "wikiconfig.py")
   1.144 +
   1.145 +        status("Editing configuration from %s..." % wikiconfig_py)
   1.146 +
   1.147 +        s = readfile(wikiconfig_py)
   1.148 +        s = self._configure_moin(s)
   1.149 +        writefile(wikiconfig_py, s)
   1.150 +
   1.151 +    def install_theme(self, theme_dir):
   1.152 +
   1.153 +        "Install Wiki theme provided in the given 'theme_dir'."
   1.154 +
   1.155 +        theme_dir = normpath(theme_dir)
   1.156 +        theme_name = split(theme_dir)[-1]
   1.157 +        theme_module = join(theme_dir, theme_name + extsep + "py")
   1.158 +
   1.159 +        data_dir = join(self.conf_dir, "data")
   1.160 +        plugin_theme_dir = join(data_dir, "plugin", "theme")
   1.161 +
   1.162 +        # Copy the theme module.
   1.163 +
   1.164 +        status("Copying theme module to %s..." % plugin_theme_dir)
   1.165 +
   1.166 +        shutil.copy(theme_module, plugin_theme_dir)
   1.167 +
   1.168 +        # Copy the resources.
   1.169 +
   1.170 +        resources_dir = join(self.htdocs_dir, theme_name)
   1.171 +
   1.172 +        status("Copying theme resources to %s..." % resources_dir)
   1.173 +
   1.174 +        for d in ("css", "img"):
   1.175 +            target_dir = join(resources_dir, d)
   1.176 +            if exists(target_dir):
   1.177 +                status("Replacing %s..." % target_dir)
   1.178 +                shutil.rmtree(target_dir)
   1.179 +            shutil.copytree(join(theme_dir, d), target_dir)
   1.180 +
   1.181  # Command line option syntax.
   1.182  
   1.183 -syntax_description = "<argument> ... [ --method=METHOD ]"
   1.184 +syntax_description = "<argument> ... [ --method=METHOD [ <method-argument> ... ] ]"
   1.185  
   1.186  # Main program.
   1.187  
   1.188 @@ -361,6 +435,7 @@
   1.189          # Obtain as many arguments as needed for the configuration.
   1.190  
   1.191          arguments = args["argument"]
   1.192 +        method_arguments = args.get("method-argument", [])
   1.193  
   1.194          # Attempt to initialise the configuration.
   1.195  
   1.196 @@ -381,6 +456,6 @@
   1.197      else:
   1.198          method = installation.setup
   1.199  
   1.200 -    method()
   1.201 +    method(*method_arguments)
   1.202  
   1.203  # vim: tabstop=4 expandtab shiftwidth=4