[Zope3-checkins] CVS: Zope3/src/zope/fssync - fssync.py:1.44 main.py:1.36

Fred L. Drake, Jr. fred at zope.com
Wed Aug 27 16:36:21 EDT 2003


Update of /cvs-repository/Zope3/src/zope/fssync
In directory cvs.zope.org:/tmp/cvs-serv15278

Modified Files:
	fssync.py main.py 
Log Message:
Integrate the new password manager.  Two new zsync commands, "login" and
"logout", can be used to control the authentication cache.
Documentation is still needed.


=== Zope3/src/zope/fssync/fssync.py 1.43 => 1.44 ===
--- Zope3/src/zope/fssync/fssync.py:1.43	Tue Aug 26 14:49:27 2003
+++ Zope3/src/zope/fssync/fssync.py	Wed Aug 27 15:36:21 2003
@@ -21,13 +21,13 @@
 
 import os
 import sys
-import base64
 import shutil
 import urllib
 import filecmp
 import htmllib
 import httplib
 import tempfile
+import urlparse
 import formatter
 
 from StringIO import StringIO
@@ -40,6 +40,7 @@
 from zope.fssync.fsmerger import FSMerger
 from zope.fssync.fsutil import Error
 from zope.fssync import fsutil
+from zope.fssync.passwd import PasswordManager
 from zope.fssync.snarf import Snarfer, Unsnarfer
 
 
@@ -49,7 +50,7 @@
     DEV_NULL = "/dev/null"
 
 
-class Network(object):
+class Network(PasswordManager):
 
     """Handle network communication.
 
@@ -69,6 +70,7 @@
 
     def __init__(self, rooturl=None):
         """Constructor.  Optionally pass the root url."""
+        super(Network, self).__init__()
         self.setrooturl(rooturl)
 
     def loadrooturl(self, target):
@@ -91,7 +93,7 @@
         """
         if self.rooturl:
             dir = join(target, "@@Zope")
-            if not os.path.exists(dir):
+            if not exists(dir):
                 os.mkdir(dir)
             fn = join(dir, "Root")
             self.writefile(self.rooturl + "\n",
@@ -219,11 +221,11 @@
             conn.putheader("Transfer-encoding", "chunked")
         if self.user_passwd:
             if ":" not in self.user_passwd:
-                import getpass
-                pw = getpass.getpass("Password for %s @ %s: "
-                                     % (self.user_passwd, self.host_port))
-                self.user_passwd = "%s:%s" % (self.user_passwd, pw)
-            auth = base64.encodestring(self.user_passwd).strip()
+                auth = self.getToken(self.roottype,
+                                     self.host_port,
+                                     self.user_passwd)
+            else:
+                auth = self.createToken(self.user_passwd)
             conn.putheader('Authorization', 'Basic %s' % auth)
         conn.putheader("Host", self.host_port)
         conn.putheader("Connection", "close")
@@ -298,6 +300,56 @@
         self.network = network
         self.network.setrooturl(rooturl)
         self.fsmerger = FSMerger(self.metadata, self.reporter)
+
+    def login(self, url=None, user=None):
+        scheme, host_port, user = self.get_login_info(url, user)
+        token = self.network.getToken(scheme, host_port, user)
+        self.network.addToken(scheme, host_port, user, token)
+
+    def logout(self, url=None, user=None):
+        upw = self.network.user_passwd
+        scheme, host_port, user = self.get_login_info(url, user)
+        if scheme:
+            ok = self.network.removeToken(scheme, host_port, user)
+        else:
+            # remove both, if present
+            ok1 = self.network.removeToken("http", host_port, user)
+            ok2 = self.network.removeToken("https", host_port, user)
+            ok = ok1 or ok2
+        if not ok:
+            raise Error("matching login info not found")
+
+    def get_login_info(self, url, user):
+        if url:
+            parts = urlparse.urlsplit(url)
+            scheme = parts[0]
+            host_port = parts[1]
+            if not (scheme and host_port):
+                raise Error(
+                    "URLs must include both protocol (http or https)"
+                    " and host information")
+            if "@" in host_port:
+                user_passwd, host_port = host_port.split("@", 1)
+                if not user:
+                    if ":" in user_passwd:
+                        user = user_passwd.split(":", 1)[0]
+                    else:
+                        user = user_passwd
+        else:
+            self.network.loadrooturl(os.curdir)
+            scheme = self.network.roottype
+            host_port = self.network.host_port
+            if not user:
+                upw = self.network.user_passwd
+                if ":" in upw:
+                    user = upw.split(":", 1)[0]
+                else:
+                    user = upw
+        if not user:
+            user = raw_input("Username: ").strip()
+            if not user:
+                raise Error("username cannot be empty")
+        return scheme, host_port, user
 
     def checkout(self, target):
         rootpath = self.network.rootpath


=== Zope3/src/zope/fssync/main.py 1.35 => 1.36 ===
--- Zope3/src/zope/fssync/main.py:1.35	Tue Aug 12 14:23:34 2003
+++ Zope3/src/zope/fssync/main.py	Wed Aug 27 15:36:21 2003
@@ -235,16 +235,31 @@
     fs = FSSync(rooturl=rooturl)
     fs.checkin(target, message)
 
-def revert(opts, args):
-    """%(program)s revert [TARGET ...]
+def login(opts, args):
+    """%(program)s login [-u user] [URL]
 
-    Revert changes to targets.  Modified files are overwritten by the
-    unmodified copy cached in @@Zope/Original/ and scheduled additions
-    and deletions are de-scheduled.  Additions that are de-scheduled
-    do not cause the working copy of the file to be removed.
+    Save a basic authentication token for a URL that doesn't include a
+    password component.
     """
-    fs = FSSync()
-    fs.multiple(args, fs.revert)
+    _loginout(opts, args, "login", FSSync().login)
+
+def logout(opts, args):
+    """%(program)s logout [-u user] [URL]
+
+    Remove a saved basic authentication token for a URL.
+    """
+    _loginout(opts, args, "logout", FSSync().logout)
+
+def _loginout(opts, args, cmdname, cmdfunc):
+    url = user = None
+    if args:
+        if len(args) > 1:
+            raise Usage("%s allows at most one argument" % cmdname)
+        url = args[0]
+    for o, a in opts:
+        if o in ("-u", "--user"):
+            user = a
+    cmdfunc(url, user)
 
 def mkdir(opts, args):
     """%(program)s mkdir PATH ...
@@ -264,6 +279,17 @@
     fs = FSSync()
     fs.multiple(args, fs.resolve)
 
+def revert(opts, args):
+    """%(program)s revert [TARGET ...]
+
+    Revert changes to targets.  Modified files are overwritten by the
+    unmodified copy cached in @@Zope/Original/ and scheduled additions
+    and deletions are de-scheduled.  Additions that are de-scheduled
+    do not cause the working copy of the file to be removed.
+    """
+    fs = FSSync()
+    fs.multiple(args, fs.revert)
+
 def extract_message(opts, cmd):
     L = []
     message = None
@@ -296,6 +322,8 @@
     (checkout, "co",      "",           ""),
     (commit,   "ci",      "F:m:r",      "file= message= raise-on-conflicts"),
     (diff,     "di",      "bBcC:iNuU:", "brief context= unified="),
+    (login,    "",        "u:",         "user="),
+    (logout,   "",        "u:",         "user="),
     (mkdir,    "",        "",           ""),
     (remove,   "del delete rm", "",     ""),
     (resolve,  "",        "",           ""),




More information about the Zope3-Checkins mailing list