moinsetup

Changeset

12:e33cb1930c02
2010-05-24 Paul Boddie raw files shortlog changelog graph Introduced a class for editing the wikiconfig.py file. Added support for installing a Wiki using an existing MoinMoin software installation (without installing MoinMoin's libraries again). Added support for setting and adding specific configuration settings.
moinsetup.py (file)
     1.1 --- a/moinsetup.py	Sun May 23 22:26:29 2010 +0200
     1.2 +++ b/moinsetup.py	Mon May 24 22:32:41 2010 +0200
     1.3 @@ -15,15 +15,6 @@
     1.4  moin_cgi_prefix                 = re.compile("^#sys.path.insert\(0, 'PREFIX.*$", re.MULTILINE)
     1.5  moin_cgi_wikiconfig             = re.compile("^#sys.path.insert\(0, '/path/to/wikiconfigdir.*$", re.MULTILINE)
     1.6  
     1.7 -wikiconfig_py_site_name         = compile_definition("site_?name")
     1.8 -wikiconfig_py_url_prefix_static = compile_definition("url_prefix_static")
     1.9 -wikiconfig_py_superuser         = compile_definition("superuser")
    1.10 -wikiconfig_py_acl_rights_before = compile_definition("acl_rights_before")
    1.11 -wikiconfig_py_page_front_page   = compile_definition("page_front_page")
    1.12 -wikiconfig_py_data_dir          = compile_definition("data_dir")
    1.13 -wikiconfig_py_data_underlay_dir = compile_definition("data_underlay_dir")
    1.14 -wikiconfig_py_theme_default     = compile_definition("theme_default")
    1.15 -
    1.16  # Templates for Apache site definitions.
    1.17  
    1.18  apache_site = """
    1.19 @@ -56,6 +47,91 @@
    1.20  def note(message):
    1.21      print message
    1.22  
    1.23 +class Configuration:
    1.24 +
    1.25 +    "A class representing the configuration."
    1.26 +
    1.27 +    special_names = ["site_name"]
    1.28 +
    1.29 +    def __init__(self, filename):
    1.30 +        self.content = readfile(filename)
    1.31 +        self.filename = filename
    1.32 +
    1.33 +    def get_pattern(self, name):
    1.34 +
    1.35 +        # Make underscores optional for certain names.
    1.36 +
    1.37 +        if name in self.special_names:
    1.38 +            name = name.replace("_", "_?")
    1.39 +
    1.40 +        return compile_definition(name)
    1.41 +
    1.42 +    def set(self, name, value, count=None, raw=0):
    1.43 +
    1.44 +        """
    1.45 +        Set the configuration parameter having the given 'name' with the given
    1.46 +        'value', limiting the number of appropriately named parameters changed
    1.47 +        to 'count', if specified.
    1.48 +
    1.49 +        If the configuration parameter of the given 'name' does not exist,
    1.50 +        insert such a parameter at the end of the file.
    1.51 +
    1.52 +        If the optional 'raw' parameter is specified and set to a true value,
    1.53 +        the provided 'value' is inserted directly into the configuration file.
    1.54 +        """
    1.55 +
    1.56 +        if not self.replace(name, value, count, raw):
    1.57 +            self.insert(name, value, raw)
    1.58 +
    1.59 +    def replace(self, name, value, count=None, raw=0):
    1.60 +
    1.61 +        """
    1.62 +        Replace configuration parameters having the given 'name' with the given
    1.63 +        'value', limiting the number of appropriately named parameters changed
    1.64 +        to 'count', if specified.
    1.65 +
    1.66 +        If the optional 'raw' parameter is specified and set to a true value,
    1.67 +        the provided 'value' is inserted directly into the configuration file.
    1.68 +
    1.69 +        Return the number of substitutions made.
    1.70 +        """
    1.71 +
    1.72 +        if raw:
    1.73 +            substitution = r"\1\2 %s" % value
    1.74 +        else:
    1.75 +            substitution = r"\1\2 %r" % value
    1.76 +
    1.77 +        pattern = self.get_pattern(name)
    1.78 +
    1.79 +        if count is None:
    1.80 +            self.content, n = pattern.subn(substitution, self.content)
    1.81 +        else:
    1.82 +            self.content, n = pattern.subn(substitution, self.content, count=count)
    1.83 +
    1.84 +        return n
    1.85 +
    1.86 +    def insert(self, name, value, raw=0):
    1.87 +
    1.88 +        """
    1.89 +        Insert the configuration parameter having the given 'name' and 'value'.
    1.90 +
    1.91 +        If the optional 'raw' parameter is specified and set to a true value,
    1.92 +        the provided 'value' is inserted directly into the configuration file.
    1.93 +        """
    1.94 +
    1.95 +        if raw:
    1.96 +            insertion = "\n    %s = %s\n"
    1.97 +        else:
    1.98 +            insertion = "\n    %s = %r\n"
    1.99 +
   1.100 +        self.content += insertion % (name, value)
   1.101 +
   1.102 +    def close(self):
   1.103 +
   1.104 +        "Close the file, writing the content."
   1.105 +
   1.106 +        writefile(self.filename, self.content)
   1.107 +
   1.108  class Installation:
   1.109  
   1.110      "A class for installing and initialising MoinMoin."
   1.111 @@ -182,6 +258,20 @@
   1.112  
   1.113          self.ensure_directories()
   1.114          self.install_moin()
   1.115 +        self._setup_wiki()
   1.116 +
   1.117 +    def setup_wiki(self):
   1.118 +
   1.119 +        "Set up a Wiki without installing MoinMoin."
   1.120 +
   1.121 +        self.ensure_directories()
   1.122 +        self.install_moin(data_only=1)
   1.123 +        self._setup_wiki()
   1.124 +
   1.125 +    def _setup_wiki(self):
   1.126 +
   1.127 +        "Set up a Wiki without installing MoinMoin."
   1.128 +
   1.129          self.install_data()
   1.130          self.configure_moin()
   1.131          self.edit_moin_scripts()
   1.132 @@ -189,7 +279,7 @@
   1.133          self.make_site_files()
   1.134          self.make_post_install_script()
   1.135  
   1.136 -    def install_moin(self):
   1.137 +    def install_moin(self, data_only=0):
   1.138  
   1.139          "Enter the distribution directory and run the setup script."
   1.140  
   1.141 @@ -203,8 +293,14 @@
   1.142  
   1.143          status("Installing MoinMoin in %s..." % self.prefix)
   1.144  
   1.145 -        os.system("python setup.py --quiet install --force --prefix='%s' --install-data='%s' --record='%s'" % (
   1.146 -            self.prefix, self.instance_dir, log_filename))
   1.147 +        if data_only:
   1.148 +            install_cmd = "install_data"
   1.149 +            options = "--install-dir='%s'" % self.instance_dir
   1.150 +        else:
   1.151 +            install_cmd = "install"
   1.152 +            options = "--prefix='%s' --install-data='%s' --record='%s'" % (self.prefix, self.instance_dir, log_filename)
   1.153 +
   1.154 +        os.system("python setup.py --quiet %s %s --force" % (install_cmd, options))
   1.155  
   1.156          os.chdir(this_dir)
   1.157  
   1.158 @@ -243,47 +339,50 @@
   1.159  
   1.160          if self.moin_version.startswith("1.9"):
   1.161              self.static_url_path = self.url_path
   1.162 -            url_prefix_static_sub = r"\1\2 %r + url_prefix_static" % self.static_url_path
   1.163 +            url_prefix_static = "%r + url_prefix_static" % self.static_url_path
   1.164          else:
   1.165              self.static_url_path = self.url_path + "-static"
   1.166 -            url_prefix_static_sub = r"\1\2 %r" % self.static_url_path
   1.167 +            url_prefix_static = "%r" % self.static_url_path
   1.168  
   1.169 -        # Edit the Wiki configuration file.
   1.170 +        # Copy the Wiki configuration file from the distribution.
   1.171  
   1.172 -        wikiconfig_py = join(self.moin_distribution, "wiki", "config", "wikiconfig.py")
   1.173 +        wikiconfig_py = join(self.conf_dir, "wikiconfig.py")
   1.174 +        shutil.copyfile(join(self.moin_distribution, "wiki", "config", "wikiconfig.py"), wikiconfig_py)
   1.175  
   1.176          status("Editing configuration from %s..." % wikiconfig_py)
   1.177  
   1.178 -        s = readfile(wikiconfig_py)
   1.179 -        s = wikiconfig_py_url_prefix_static.sub(url_prefix_static_sub, s)
   1.180 -        s = wikiconfig_py_superuser.sub(r"\1\2 %r" % [self.superuser], s)
   1.181 -        s = wikiconfig_py_acl_rights_before.sub(r"\1\2 %r" % (u"%s:read,write,delete,revert,admin" % self.superuser), s)
   1.182 +        # Edit the Wiki configuration file.
   1.183 +
   1.184 +        wikiconfig = Configuration(wikiconfig_py)
   1.185  
   1.186 -        if not self.moin_version.startswith("1.9"):
   1.187 -            data_dir = join(self.conf_dir, "data")
   1.188 -            data_underlay_dir = join(self.conf_dir, "underlay")
   1.189 +        try:
   1.190 +            wikiconfig.set("url_prefix_static", url_prefix_static, raw=1)
   1.191 +            wikiconfig.set("superuser", [self.superuser])
   1.192 +            wikiconfig.set("acl_rights_before", u"%s:read,write,delete,revert,admin" % self.superuser)
   1.193  
   1.194 -            s = wikiconfig_py_data_dir.sub(r"\1\2 %r" % data_dir, s)
   1.195 -            s = wikiconfig_py_data_underlay_dir.sub(r"\1\2 %r" % data_underlay_dir, s)
   1.196 +            if not self.moin_version.startswith("1.9"):
   1.197 +                data_dir = join(self.conf_dir, "data")
   1.198 +                data_underlay_dir = join(self.conf_dir, "underlay")
   1.199 +
   1.200 +                wikiconfig.set("data_dir", data_dir)
   1.201 +                wikiconfig.set("data_underlay_dir", data_underlay_dir)
   1.202  
   1.203 -        s = self._configure_moin(s)
   1.204 +            self._configure_moin(wikiconfig)
   1.205  
   1.206 -        writefile(join(self.conf_dir, "wikiconfig.py"), s)
   1.207 +        finally:
   1.208 +            wikiconfig.close()
   1.209  
   1.210 -    def _configure_moin(self, s):
   1.211 +    def _configure_moin(self, wikiconfig):
   1.212  
   1.213          """
   1.214 -        Configure Moin, taking the configuration file's contents as 's' and
   1.215 -        returning the edited contents.
   1.216 +        Configure Moin, accessing the configuration file using 'wikiconfig'.
   1.217          """
   1.218  
   1.219 -        s = wikiconfig_py_site_name.sub(r"\1\2 %r" % self.site_name, s)
   1.220 -        s = wikiconfig_py_page_front_page.sub(r"\1\2 %r" % self.front_page_name, s, count=1)
   1.221 +        wikiconfig.set("site_name", self.site_name)
   1.222 +        wikiconfig.set("page_front_page", self.front_page_name, count=1)
   1.223  
   1.224          if self.theme_default is not None:
   1.225 -            s = wikiconfig_py_theme_default.sub(r"\1\2 %r" % self.theme_default, s)
   1.226 -
   1.227 -        return s
   1.228 +            wikiconfig.set("theme_default", self.theme_default)
   1.229  
   1.230      def edit_moin_scripts(self):
   1.231  
   1.232 @@ -379,7 +478,7 @@
   1.233  
   1.234      # Accessory methods.
   1.235  
   1.236 -    def reconfigure_moin(self):
   1.237 +    def reconfigure_moin(self, name=None, value=None, raw=0):
   1.238  
   1.239          "Edit the installed Wiki configuration file."
   1.240  
   1.241 @@ -387,9 +486,18 @@
   1.242  
   1.243          status("Editing configuration from %s..." % wikiconfig_py)
   1.244  
   1.245 -        s = readfile(wikiconfig_py)
   1.246 -        s = self._configure_moin(s)
   1.247 -        writefile(wikiconfig_py, s)
   1.248 +        wikiconfig = Configuration(wikiconfig_py)
   1.249 +
   1.250 +        try:
   1.251 +            # Perform default configuration.
   1.252 +
   1.253 +            if name is None and value is None:
   1.254 +                self._configure_moin(wikiconfig)
   1.255 +            else:
   1.256 +                wikiconfig.set(name, value, raw=raw)
   1.257 +
   1.258 +        finally:
   1.259 +            wikiconfig.close()
   1.260  
   1.261      def install_theme(self, theme_dir):
   1.262