[Zodb-checkins] CVS: ZODB3/ZEO - zeopasswd.py:1.2.6.2

Christian Reis kiko at async.com.br
Thu Dec 18 21:09:13 EST 2003


Update of /cvs-repository/ZODB3/ZEO
In directory cvs.zope.org:/tmp/cvs-serv17673

Modified Files:
      Tag: Zope-2_7-branch
	zeopasswd.py 
Log Message:
Backporting zeopasswd fix from HEAD; make the file work as a script, and
allow specifying the authentication database via commandline parameters.
Extra fixes thrown in for free.


=== ZODB3/ZEO/zeopasswd.py 1.2.6.1 => 1.2.6.2 ===
--- ZODB3/ZEO/zeopasswd.py:1.2.6.1	Mon Sep 15 17:26:53 2003
+++ ZODB3/ZEO/zeopasswd.py	Thu Dec 18 21:08:42 2003
@@ -16,73 +16,105 @@
 
 usage: python zeopasswd.py [options] username [password]
 
--C/--configuration URL -- configuration file or URL
--d/--delete -- delete user instead of updating password
+Specify either a configuration file:
+
+    -C/--configuration -- ZConfig configuration file
+
+or the individual options:
+
+    -f/--filename -- authentication database filename
+    -p/--protocol -- authentication protocol name
+    -r/--realm -- authentication database realm
+
+Additional options:
+
+    -d/--delete -- delete user instead of updating password
 """
 
 import getopt
 import getpass
 import sys
+import os
 
 import ZConfig
 import ZEO
 
 def usage(msg):
-    print msg
     print __doc__
+    print msg
     sys.exit(2)
 
 def options(args):
     """Password-specific options loaded from regular ZEO config file."""
 
-    schema = ZConfig.loadSchema(os.path.join(os.path.dirname(ZEO.__file__),
-                                             "schema.xml"))
-
     try:
-        options, args = getopt.getopt(args, "C:", ["configure="])
+        options, args = getopt.getopt(args, "dr:p:f:C:", ["configure=", 
+                                                          "protocol=", 
+                                                          "filename=",
+                                                          "realm"])
     except getopt.error, msg:
         usage(msg)
     config = None
-    delete = False
+    delete = 0
+    auth_protocol = None
+    auth_db = "" 
+    auth_realm = None
     for k, v in options:
         if k == '-C' or k == '--configure':
+            schemafile = os.path.join(os.path.dirname(ZEO.__file__),
+                                                     "schema.xml")
+            schema = ZConfig.loadSchema(schemafile)
             config, nil = ZConfig.loadConfig(schema, v)
         if k == '-d' or k == '--delete':
-            delete = True
-    if config is None:
-        usage("Must specifiy configuration file")
+            delete = 1
+        if k == '-p' or k == '--protocol':
+            auth_protocol = v
+        if k == '-f' or k == '--filename':
+            auth_db = v
+        if k == '-r' or k == '--realm':
+            auth_realm = v
+
+    if config is not None:
+        if auth_protocol or auth_db:
+            usage("Error: Conflicting options; use either -C *or* -p and -f")
+        auth_protocol = config.zeo.authentication_protocol
+        auth_db = config.zeo.authentication_database
+        auth_realm = config.zeo.authentication_realm
+    elif not (auth_protocol and auth_db):
+        usage("Error: Must specifiy configuration file or protocol and database")
 
     password = None
     if delete:
         if not args:
-            usage("Must specify username to delete")
+            usage("Error: Must specify a username to delete")
         elif len(args) > 1:
-            usage("Too many arguments")
+            usage("Error: Too many arguments")
         username = args[0]
     else:
         if not args:
-            usage("Must specify username")
+            usage("Error: Must specify a username")
         elif len(args) > 2:
-            usage("Too many arguments")
+            usage("Error: Too many arguments")
         elif len(args) == 1:
             username = args[0]
         else:
             username, password = args
 
-    return config.zeo, delete, username, password
+    return auth_protocol, auth_db, auth_realm, delete, username, password
 
 def main(args=None):
-    options, delete, username, password = options(args)
-    p = options.authentication_protocol
+    p, auth_db, auth_realm, delete, username, password = options(args)
     if p is None:
-        usage("ZEO configuration does not specify authentication-protocol")
+        usage("Error: configuration does not specify auth protocol")
     if p == "digest":
         from ZEO.auth.auth_digest import DigestDatabase as Database
     elif p == "srp":
         from ZEO.auth.auth_srp import SRPDatabase as Database
-    if options.authentication_database is None:
-        usage("ZEO configuration does not specify authentication-database")
-    db = Database(options.authentication_database)
+    else:
+        raise ValueError, "Unknown database type %r" % p
+    if auth_db is None:
+        usage("Error: configuration does not specify auth database")
+    db = Database(auth_db, auth_realm)
     if delete:
         db.del_user(username)
     else:
@@ -92,4 +124,5 @@
     db.save()
 
 if __name__ == "__main__":
-    main(sys.argv)
+    main(sys.argv[1:])
+




More information about the Zodb-checkins mailing list