[dpdk-ci] [PATCH v5 5/9] tools: fix pwclient for proxy and python 3

Thomas Monjalon thomas.monjalon at 6wind.com
Thu Dec 15 00:05:19 CET 2016


Python 3 can be used.

The environment variables http_proxy and https_proxy can be used.

These fixes have been sent to the patchwork project.

Signed-off-by: Thomas Monjalon <thomas.monjalon at 6wind.com>
---
 README         |  1 +
 tools/pwclient | 77 +++++++++++++++++++++++++++++++++-------------------------
 2 files changed, 45 insertions(+), 33 deletions(-)

diff --git a/README b/README
index 00e1415..f662f6d 100644
--- a/README
+++ b/README
@@ -66,3 +66,4 @@ the scripts.
 
 The file pwclientrc must be copied in ~/.pwclientrc in order to access to
 the XML-RPC interface of patchwork with the script pwclient.
+A proxy can be specified in the environment variables http_proxy and https_proxy.
diff --git a/tools/pwclient b/tools/pwclient
index f437786..3d5be69 100755
--- a/tools/pwclient
+++ b/tools/pwclient
@@ -102,31 +102,47 @@ class Filter(object):
         return str(self.d)
 
 
-class BasicHTTPAuthTransport(xmlrpclib.SafeTransport):
+class Transport(xmlrpclib.SafeTransport):
 
-    def __init__(self, username=None, password=None, use_https=False):
-        self.username = username
-        self.password = password
-        self.use_https = use_https
+    def __init__(self, url):
         xmlrpclib.SafeTransport.__init__(self)
+        self.credentials = None
+        self.host = None
+        self.proxy = None
+        self.scheme = url.split('://', 1)[0]
+        self.https = url.startswith('https')
+        if self.https:
+            self.proxy = os.environ.get('https_proxy')
+        else:
+            self.proxy = os.environ.get('http_proxy')
+        if self.proxy:
+            self.https = self.proxy.startswith('https')
 
-    def authenticated(self):
-        return self.username is not None and self.password is not None
-
-    def send_host(self, connection, host):
-        xmlrpclib.Transport.send_host(self, connection, host)
-        if not self.authenticated():
-            return
-        credentials = '%s:%s' % (self.username, self.password)
-        auth = 'Basic ' + base64.encodestring(credentials).strip()
-        connection.putheader('Authorization', auth)
+    def set_credentials(self, username=None, password=None):
+        self.credentials = '%s:%s' % (username, password)
 
     def make_connection(self, host):
-        if self.use_https:
-            fn = xmlrpclib.SafeTransport.make_connection
+        self.host = host
+        if self.proxy:
+            host = self.proxy.split('://', 1)[-1]
+        if self.credentials:
+            host = '@'.join([self.credentials, host])
+        if self.https:
+            return xmlrpclib.SafeTransport.make_connection(self, host)
         else:
-            fn = xmlrpclib.Transport.make_connection
-        return fn(self, host)
+            return xmlrpclib.Transport.make_connection(self, host)
+
+    if sys.version_info[0] == 2:
+
+        def send_request(self, connection, handler, request_body):
+            handler = '%s://%s%s' % (self.scheme, self.host, handler)
+            xmlrpclib.Transport.send_request(self, connection, handler, request_body)
+
+    else: # Python 3
+
+        def send_request(self, host, handler, request_body, debug):
+            handler = '%s://%s%s' % (self.scheme, host, handler)
+            return xmlrpclib.Transport.send_request(self, host, handler, request_body, debug)
 
 
 def project_id_by_name(rpc, linkname):
@@ -253,7 +269,7 @@ def action_check_info(rpc, check_id):
     print(s)
     print('-' * len(s))
     for key, value in sorted(check.items()):
-        print("- %- 14s: %s" % (key, unicode(value).encode("utf-8")))
+        print("- %- 14s: %s" % (key, unicode(value)))
 
 
 def action_check_create(rpc, patch_id, context, state, url, description):
@@ -277,7 +293,7 @@ def action_info(rpc, patch_id):
     print(s)
     print('-' * len(s))
     for key, value in sorted(patch.items()):
-        print("- %- 14s: %s" % (key, unicode(value).encode("utf-8")))
+        print("- %- 14s: %s" % (key, unicode(value)))
 
 
 def action_get(rpc, patch_id):
@@ -301,7 +317,7 @@ def action_get(rpc, patch_id):
         sys.exit(1)
 
     try:
-        f.write(unicode(s).encode("utf-8"))
+        f.write(unicode(s))
         f.close()
         print('Saved patch to %s' % fname)
     except:
@@ -670,18 +686,13 @@ def main():
 
     url = config.get(project_str, 'url')
 
-    transport = None
+    transport = Transport(url)
     if action in auth_actions:
         if config.has_option(project_str, 'username') and \
                 config.has_option(project_str, 'password'):
-
-            use_https = url.startswith('https')
-
-            transport = BasicHTTPAuthTransport(
+            transport.set_credentials(
                 config.get(project_str, 'username'),
-                config.get(project_str, 'password'),
-                use_https)
-
+                config.get(project_str, 'password'))
         else:
             sys.stderr.write("The %s action requires authentication, but no "
                              "username or password\nis configured\n" % action)
@@ -746,15 +757,15 @@ def main():
             for patch_id in non_empty(h, patch_ids):
                 s = rpc.patch_get_mbox(patch_id)
                 if len(s) > 0:
-                    i.append(unicode(s).encode("utf-8"))
+                    i.append(unicode(s))
             if len(i) > 0:
-                pager.communicate(input="\n".join(i))
+                pager.communicate(input="\n".join(i).encode("utf-8"))
             pager.stdin.close()
         else:
             for patch_id in non_empty(h, patch_ids):
                 s = rpc.patch_get_mbox(patch_id)
                 if len(s) > 0:
-                    print(unicode(s).encode("utf-8"))
+                    print(unicode(s))
 
     elif action == 'info':
         for patch_id in non_empty(h, patch_ids):
-- 
2.7.0



More information about the ci mailing list