moinsetup

Changeset

34:959f5d1bd619
2011-04-15 Paul Boddie raw files shortlog changelog graph Improved configuration editing involving import statements. Improved OpenID configuration, supporting MoinMoin 1.9, and added support for other authentication methods.
TO_DO.txt (file) moinsetup.py (file)
     1.1 --- a/TO_DO.txt	Wed Feb 23 01:36:12 2011 +0100
     1.2 +++ b/TO_DO.txt	Fri Apr 15 23:08:37 2011 +0200
     1.3 @@ -10,4 +10,8 @@
     1.4    ./configure --with-python --prefix={$moin-prefix} XAPIAN_CONFIG={$moin-prefix}/bin/xapian-config PYTHON_LIB={$moin-prefix}/lib/python2.5/site-packages && make && make install
     1.5  Support OpenID and other authentication methods.
     1.6    Remember anonymous_session_lifetime for 1.8.x and cookie_lifetime for 1.9.x.
     1.7 +Support OpenID provision (http://moinmo.in/HelpOnOpenIDProvider):
     1.8 +  openid_server_enabled = True
     1.9 +  openid_server_enable_user = True # optional, for nominating pages as user OpenID URLs
    1.10 +  openid_server_restricted_users_group = None # or a group name for OpenID users
    1.11  Support integration of additional theme material with themes.
     2.1 --- a/moinsetup.py	Wed Feb 23 01:36:12 2011 +0100
     2.2 +++ b/moinsetup.py	Fri Apr 15 23:08:37 2011 +0200
     2.3 @@ -36,13 +36,18 @@
     2.4  def compile_definition(name):
     2.5      return re.compile(r"^(\s*)#*\s*(%s =)\s*(.*)$" % name, re.MULTILINE)
     2.6  
     2.7 -moin_cgi_prefix          = re.compile("^#sys\.path\.insert\(0, 'PREFIX.*$", re.MULTILINE)
     2.8 -moin_cgi_wikiconfig      = re.compile("^#sys\.path\.insert\(0, '/path/to/wikiconfigdir.*$", re.MULTILINE)
     2.9 +wikiconfig_import        = re.compile(r"^(\s*)#*\s*"
    2.10 +                            r"(from\s+)(?P<module>\S+)"
    2.11 +                            r"(\s+import\s+)(?P<names>(?:\S|[^,\s])+(?:\s*,\s*(?:\S|[^,\s])+)*)"
    2.12 +                            r"(\s*)$", re.MULTILINE)
    2.13 +
    2.14 +moin_cgi_prefix          = re.compile(r"^#sys\.path\.insert\(0, 'PREFIX.*$", re.MULTILINE)
    2.15 +moin_cgi_wikiconfig      = re.compile(r"^#sys\.path\.insert\(0, '/path/to/wikiconfigdir.*$", re.MULTILINE)
    2.16  moin_cgi_properties      = compile_definition("properties")
    2.17  moin_cgi_fix_script_name = compile_definition("fix_script_name")
    2.18 -moin_cgi_force_cgi       = re.compile("^#(os.environ\['FCGI_FORCE_CGI'\].*)$", re.MULTILINE)
    2.19 +moin_cgi_force_cgi       = re.compile(r"^#(os.environ\['FCGI_FORCE_CGI'\].*)$", re.MULTILINE)
    2.20  
    2.21 -css_import_stylesheet    = re.compile("(\s*@import\s+[\"'])(.*?)([\"']\s*;)")
    2.22 +css_import_stylesheet    = re.compile(r"(\s*@import\s+[\"'])(.*?)([\"']\s*;)")
    2.23  
    2.24  # Templates for Apache site definitions.
    2.25  
    2.26 @@ -224,6 +229,47 @@
    2.27              self.content += "\n"
    2.28          self.content += "    %s\n" % text
    2.29  
    2.30 +    def set_import(self, imported_module, imported_names):
    2.31 +
    2.32 +        """
    2.33 +        Set up an import of the given 'imported_module' exposing the given
    2.34 +        'imported_names'.
    2.35 +        """
    2.36 +
    2.37 +        s = self.content
    2.38 +        after_point = 0
    2.39 +        first_point = None
    2.40 +
    2.41 +        for module_import in wikiconfig_import.finditer(s):
    2.42 +            before, from_keyword, module, import_keyword, names, after = module_import.groups()
    2.43 +            before_point, after_point = module_import.span()
    2.44 +
    2.45 +            if first_point is None:
    2.46 +                first_point = after_point
    2.47 +
    2.48 +            names = [name.strip() for name in names.split(",")]
    2.49 +
    2.50 +            # Test the import for a reference to the requested imported module.
    2.51 +
    2.52 +            if imported_module == module:
    2.53 +                for name in imported_names:
    2.54 +                    if name not in names:
    2.55 +                        names.append(name)
    2.56 +
    2.57 +                self.content = s[:before_point] + (
    2.58 +                    "%s%s%s%s%s%s" % (before, from_keyword, module, import_keyword, ", ".join(names), after)
    2.59 +                    ) + s[after_point:]
    2.60 +                break
    2.61 +
    2.62 +        # Where no import references the imported module, insert a reference
    2.63 +        # into the configuration.
    2.64 +
    2.65 +        else:
    2.66 +            # Add the import after the first one.
    2.67 +
    2.68 +            if first_point is not None:
    2.69 +                self.content = s[:first_point] + ("\nfrom %s import %s" % (imported_module, ", ".join(imported_names))) + s[first_point:]
    2.70 +
    2.71      def close(self):
    2.72  
    2.73          "Close the file, writing the content."
    2.74 @@ -802,19 +848,48 @@
    2.75          wikiconfig = Configuration(wikiconfig_py)
    2.76  
    2.77          try:
    2.78 +            # OpenID authentication.
    2.79 +
    2.80              if method_name.lower() == "openid":
    2.81 -                wikiconfig.insert_text("from MoinMoin.auth.openidrp import OpenIDAuth")
    2.82 +                wikiconfig.set_import("MoinMoin.auth.openidrp", ["OpenIDAuth"])
    2.83  
    2.84 -                if wikiconfig.get("anonymous_session_lifetime"):
    2.85 -                    wikiconfig.replace("anonymous_session_lifetime", "1000", raw=1)
    2.86 +                if self.moin_version.startswith("1.9"):
    2.87 +                    if wikiconfig.get("cookie_lifetime"):
    2.88 +                        wikiconfig.replace("cookie_lifetime", "(12, 12)", raw=1)
    2.89 +                    else:
    2.90 +                        wikiconfig.set("cookie_lifetime", "(12, 12)", raw=1)
    2.91                  else:
    2.92 -                    wikiconfig.set("anonymous_session_lifetime", "1000", raw=1)
    2.93 +                    if wikiconfig.get("anonymous_session_lifetime"):
    2.94 +                        wikiconfig.replace("anonymous_session_lifetime", "1000", raw=1)
    2.95 +                    else:
    2.96 +                        wikiconfig.set("anonymous_session_lifetime", "1000", raw=1)
    2.97 +
    2.98 +                auth_object = "OpenIDAuth()"
    2.99 +
   2.100 +            # Default Moin authentication.
   2.101 +
   2.102 +            elif method_name.lower() in ("moin", "default"):
   2.103 +                wikiconfig.set_import("MoinMoin.auth", ["MoinAuth"])
   2.104 +                auth_object = "MoinAuth()"
   2.105 +
   2.106 +            # REMOTE_USER authentication.
   2.107  
   2.108 -                auth = wikiconfig.get("auth")
   2.109 -                if auth:
   2.110 -                    wikiconfig.replace("auth", "%s + [OpenIDAuth()]" % auth, raw=1)
   2.111 -                else:
   2.112 -                    wikiconfig.set("auth", "[OpenIDAuth()]", raw=1)
   2.113 +            elif method_name.lower() in ("given", "remote-user"):
   2.114 +                wikiconfig.set_import("MoinMoin.auth.http", ["HTTPAuth"])
   2.115 +                auth_object = "HTTPAuth(autocreate=True)"
   2.116 +
   2.117 +            # Other methods are not currently supported.
   2.118 +
   2.119 +            else:
   2.120 +                return
   2.121 +
   2.122 +            # Edit the authentication setting.
   2.123 +
   2.124 +            auth = wikiconfig.get("auth")
   2.125 +            if auth:
   2.126 +                wikiconfig.replace("auth", "%s + [%s]" % (auth, auth_object), raw=1)
   2.127 +            else:
   2.128 +                wikiconfig.set("auth", "[%s]" % auth_object, raw=1)
   2.129  
   2.130          finally:
   2.131              wikiconfig.close()