Checked in dcruncmd.C
[doldaconnect.git] / lib / python / dolcon / __init__.py
index 11cb6d4..c1611ac 100644 (file)
@@ -25,7 +25,7 @@ def login(useauthless = True, **kw):
         select()
     return result[0]
 
         select()
     return result[0]
 
-def mustconnect(host, port = -1):
+def mustconnect(host, revision = latest):
     """A convenience function for connect.
 
     This function will connect to the given host, perform a select
     """A convenience function for connect.
 
     This function will connect to the given host, perform a select
@@ -33,29 +33,31 @@ def mustconnect(host, port = -1):
     any of these steps fail, an exception is raised. If successful,
     the file descriptor for the server connection is returned.
     """
     any of these steps fail, an exception is raised. If successful,
     the file descriptor for the server connection is returned.
     """
-    fd = connect(host, port)
+    fd = connect(host)
     while True:
         resp = getresp()
         if resp is not None and resp.getcmd() == u".connect":
             break
         select()
     while True:
         resp = getresp()
         if resp is not None and resp.getcmd() == u".connect":
             break
         select()
-    if resp.getcode() != 200:
+    if resp.getcode() != 201:
         raise RuntimeError, resp.intresp()[0][0]
         raise RuntimeError, resp.intresp()[0][0]
+    if not checkproto(resp, revision):
+        raise RuntimeError, resp
     return fd
 
     return fd
 
-def cnl(host = None, port = -1, useauthless = True, **kw):
+def cnl(host = None, useauthless = True, revision = latest, **kw):
     """A convenience function for connect and loginasync.
 
     This function will connect to the given server, or the server in
     """A convenience function for connect and loginasync.
 
     This function will connect to the given server, or the server in
-    the environment variable $DCSERVER if none is given, and
-    authenticate to the server. If any of the steps fail, an exception
-    is raised.
+    the environment variable $DCSERVER if none is given, or, if that
+    fails, localhost, and authenticate to the server. If any of the
+    steps fail, an exception is raised.
     """
     if host is None:
         host = os.getenv("DCSERVER")
     if host is None:
     """
     if host is None:
         host = os.getenv("DCSERVER")
     if host is None:
-        raise ValueError, "No DC host to connect to"
-    fd = mustconnect(host, port)
+        host = "localhost"
+    fd = mustconnect(host, revision)
     err, reason = login(useauthless, **kw)
     if err != "success":
         raise RuntimeError, (err, reason)
     err, reason = login(useauthless, **kw)
     if err != "success":
         raise RuntimeError, (err, reason)
@@ -88,3 +90,25 @@ def ecmda(code, *args):
     if resp.getcode() != code:
         raise ValueError, resp.getcode()
     return resp
     if resp.getcode() != code:
         raise ValueError, resp.getcode()
     return resp
+
+def ecmds(*args):
+    """Another convenience function for ecmd.
+
+    Like ecmda, but will fail on all 5xx codes, and succeed on all
+    others.
+    """
+    resp = ecmd(*args)
+    if resp.getcode() >= 500 and resp.getcode() < 600:
+        raise ValueError, tuple(resp.extract()[0])
+    return resp
+
+def getresps():
+    """A generator function which will iterate over all responses from
+    getresp.
+    """
+    while True:
+        resp = getresp()
+        if resp is None:
+            break
+        else:
+            yield resp