Blame SOURCES/build-scripts-py3-compatible.patch

89a1c0
commit d526669810e0dc0a454260d5081fc96e16fc9e13
89a1c0
Author: John Dennis <jdennis@redhat.com>
89a1c0
Date:   Mon Jun 25 16:26:24 2018 -0400
89a1c0
89a1c0
    Make Python scripts compatible with both Py2 and Py3
89a1c0
    
89a1c0
    During the build if the Python3 interpreter is used a number of
89a1c0
    scripts will fail because they were never ported from Py2 to Py3. In
89a1c0
    general we want Python code to be compatible with both Py2 and
89a1c0
    Py3. This patch brings the scripts up to date with Py3 but retains
89a1c0
    backwards compatibility with Py2 (specifically Py 2.7, the last Py2
89a1c0
    release).
89a1c0
    
89a1c0
    Examples of the required changes are:
89a1c0
    
89a1c0
    * Replace use of the built-in function file() with open().  file()
89a1c0
      does not exist in Py3, open works in both Py2 and Py3.  The code was
89a1c0
      also modified to use a file context manager (e.g. with open(xxx) as
89a1c0
      f:). This assures open files are properly closed when the code block
89a1c0
      using the file goes out of scope. This is a standard modern Python
89a1c0
      idiom.
89a1c0
    
89a1c0
    * Replace all use of the print keyword with the six.print_()
89a1c0
      function, which itself is an emulation of Py3's print function. Py3
89a1c0
      no longer has a print keyword, only a print() function.
89a1c0
    
89a1c0
    * The dict methods .keys(), .values(), .items() no longer return a
89a1c0
      list in Py3, instead they return a "view" object which is an
89a1c0
      iterator whose result is an unordered set. The most notable
89a1c0
      consequence is you cannot index the result of these functions like
89a1c0
      your could in Py2 (e.g. dict.keys()[0] will raise a run time
89a1c0
      exception).
89a1c0
    
89a1c0
    * Replace use of StringIO.StringIO and cStringIO with
89a1c0
      six.StringIO. Py3 no longer has cStringIO and the six variant
89a1c0
      handles the correct import.
89a1c0
    
89a1c0
    * Py3 no longer allows the "except xxx, variable" syntax, where
89a1c0
      variable appering after the comma is assigned the exception object,
89a1c0
      you must use the "as" keyword to perform the variable assignment
89a1c0
      (e.g. execpt xxx as variable)
89a1c0
    
89a1c0
    Note: the modifications in this patch are the minimum necessary to get
89a1c0
    the build to run with the Py3 interpreter. There are numerous other
89a1c0
    Python scripts in the repo which need Py3 porting as well but because
89a1c0
    they are not invoked during a build they will be updated in a
89a1c0
    subsequent patch.
89a1c0
    
89a1c0
    License: MIT
89a1c0
    Signed-off-by: John Dennis <jdennis@redhat.com>
89a1c0
89a1c0
diff --git a/bindings/python/examples/get_attributes_from_assertion.py b/bindings/python/examples/get_attributes_from_assertion.py
89a1c0
index 44ceb9e5..8f37a337 100644
89a1c0
--- a/bindings/python/examples/get_attributes_from_assertion.py
89a1c0
+++ b/bindings/python/examples/get_attributes_from_assertion.py
89a1c0
@@ -1,8 +1,10 @@
89a1c0
 # Example SP Python code to get attributes from an assertion
89a1c0
 
89a1c0
+from six import print_
89a1c0
+
89a1c0
 for attribute in assertion.attributeStatement[0].attribute:
89a1c0
     if attribute.name == lasso.SAML2_ATTRIBUTE_NAME_EPR:
89a1c0
         continue
89a1c0
-    print 'attribute : ' + attribute.name
89a1c0
+    print_('attribute : ' + attribute.name)
89a1c0
     for value in attribute.attributeValue:
89a1c0
-        print '  value : ' + value.any[0].content
89a1c0
+        print_('  value : ' + value.any[0].content)
89a1c0
diff --git a/bindings/python/tests/binding_tests.py b/bindings/python/tests/binding_tests.py
89a1c0
index 6d8e0dfa..54c3635f 100755
89a1c0
--- a/bindings/python/tests/binding_tests.py
89a1c0
+++ b/bindings/python/tests/binding_tests.py
89a1c0
@@ -311,8 +311,8 @@ class BindingTestCase(unittest.TestCase):
89a1c0
                    </samlp:Extensions>'''
89a1c0
         node = lasso.Node.newFromXmlNode(content)
89a1c0
         assert 'next_url' in node.any[1]
89a1c0
-        assert 'huhu' in node.attributes.keys()[0]
89a1c0
-        assert node.attributes.values()[0] == 'xxx'
89a1c0
+        assert '{https://www.entrouvert.com/}huhu' in node.attributes.keys()
89a1c0
+        assert 'xxx' in node.attributes.values()
89a1c0
         node.any = ('<zob>coin</zob>',)
89a1c0
         node.attributes = {'michou': 'zozo'}
89a1c0
         assert '<zob>coin</zob>' in node.dump()
89a1c0
diff --git a/bindings/python/tests/idwsf2_tests.py b/bindings/python/tests/idwsf2_tests.py
89a1c0
index 6f80c53d..4e47a4a1 100755
89a1c0
--- a/bindings/python/tests/idwsf2_tests.py
89a1c0
+++ b/bindings/python/tests/idwsf2_tests.py
89a1c0
@@ -27,7 +27,7 @@
89a1c0
 import os
89a1c0
 import unittest
89a1c0
 import sys
89a1c0
-from StringIO import StringIO
89a1c0
+from six import StringIO
89a1c0
 import logging
89a1c0
 
89a1c0
 logging.basicConfig()
89a1c0
@@ -310,11 +310,11 @@ class MetadataTestCase(IdWsf2TestCase):
89a1c0
         self.failUnless(idp_disco.request.svcMD[0].svcMDID is None)
89a1c0
         try:
89a1c0
             idp_disco.checkSecurityMechanism()
89a1c0
-        except lasso.Error, e:
89a1c0
+        except lasso.Error as e:
89a1c0
             self.fail(e)
89a1c0
         try:
89a1c0
             idp_disco.validateRequest()
89a1c0
-        except lasso.Error, e:
89a1c0
+        except lasso.Error as e:
89a1c0
             self.fail(e)
89a1c0
         self.failUnless(idp_disco.response is not None)
89a1c0
         self.failUnlessEqual(len(idp_disco.metadatas), 1)
89a1c0
@@ -391,16 +391,16 @@ class MetadataTestCase(IdWsf2TestCase):
89a1c0
         self.failUnless(idp_disco is not None)
89a1c0
         try:
89a1c0
             idp_disco.processRequestMsg(wsp_disco.msgBody)
89a1c0
-        except lasso.Error, e:
89a1c0
+        except lasso.Error as e:
89a1c0
             self.fail(e)
89a1c0
         self.failUnless(idp_disco.request is not None)
89a1c0
         try:
89a1c0
             idp_disco.checkSecurityMechanism()
89a1c0
-        except lasso.Error, e:
89a1c0
+        except lasso.Error as e:
89a1c0
             self.fail(e)
89a1c0
         try:
89a1c0
             idp_disco.failRequest(lasso.IDWSF2_DISCOVERY_STATUS_CODE_FAILED, lasso.IDWSF2_DISCOVERY_STATUS_CODE_FORBIDDEN)
89a1c0
-        except lasso.Error, e:
89a1c0
+        except lasso.Error as e:
89a1c0
             self.fail(e)
89a1c0
         self.failUnless(idp_disco.response is not None)
89a1c0
         self.failUnless(idp_disco.response.status is not None)
89a1c0
@@ -415,7 +415,7 @@ class MetadataTestCase(IdWsf2TestCase):
89a1c0
             wsp_disco.processResponseMsg(idp_disco.msgBody)
89a1c0
         except lasso.Idwsf2DiscoveryForbiddenError:
89a1c0
             pass
89a1c0
-        except lasso.Error, e:
89a1c0
+        except lasso.Error as e:
89a1c0
             self.fail(e)
89a1c0
 
89a1c0
     def test03(self):
89a1c0
@@ -475,7 +475,7 @@ class MetadataTestCase(IdWsf2TestCase):
89a1c0
         self.failUnless(soap_envelope.getMessageId() is not None)
89a1c0
         try:
89a1c0
             idp_disco.checkSecurityMechanism()
89a1c0
-        except lasso.Error, e:
89a1c0
+        except lasso.Error as e:
89a1c0
             self.fail(e)
89a1c0
         # redirect
89a1c0
         interactionUrl = spInteractionUrl
89a1c0
@@ -488,7 +488,7 @@ class MetadataTestCase(IdWsf2TestCase):
89a1c0
         self.failUnless(response.detail.any[0].redirectURL.startswith(interactionUrl + '?transactionID='))
89a1c0
         try:
89a1c0
             idp_disco.buildResponseMsg()
89a1c0
-        except lasso.Error, e:
89a1c0
+        except lasso.Error as e:
89a1c0
             self.fail(e)
89a1c0
         self.failUnless(idp_disco.msgBody is not None)
89a1c0
 
89a1c0
@@ -500,7 +500,7 @@ class MetadataTestCase(IdWsf2TestCase):
89a1c0
             wsp_disco.processResponseMsg(idp_disco.msgBody)
89a1c0
         except lasso.WsfprofileRedirectRequestError:
89a1c0
             pass
89a1c0
-        except lasso.Error, e:
89a1c0
+        except lasso.Error as e:
89a1c0
             self.fail(e)
89a1c0
         response_envelope = wsp_disco.getSoapEnvelopeResponse()
89a1c0
         self.failUnless(response_envelope.sb2GetRedirectRequestUrl().startswith(interactionUrl + '?transactionID='))
89a1c0
@@ -527,11 +527,11 @@ class MetadataTestCase(IdWsf2TestCase):
89a1c0
         self.failUnless(idp_disco.request.svcMD[0].svcMDID is None)
89a1c0
         try:
89a1c0
             idp_disco.checkSecurityMechanism()
89a1c0
-        except lasso.Error, e:
89a1c0
+        except lasso.Error as e:
89a1c0
             self.fail(e)
89a1c0
         try:
89a1c0
             idp_disco.validateRequest()
89a1c0
-        except lasso.Error, e:
89a1c0
+        except lasso.Error as e:
89a1c0
             self.fail(e)
89a1c0
         self.failUnless(idp_disco.response is not None)
89a1c0
         self.failUnlessEqual(len(idp_disco.metadatas), 1)
89a1c0
diff --git a/lasso/build_strerror.py b/lasso/build_strerror.py
89a1c0
index fca59628..908638d5 100644
89a1c0
--- a/lasso/build_strerror.py
89a1c0
+++ b/lasso/build_strerror.py
89a1c0
@@ -1,42 +1,42 @@
89a1c0
 #! /usr/bin/env python
89a1c0
 
89a1c0
-from cStringIO import StringIO
89a1c0
 import glob
89a1c0
 import re
89a1c0
 import sys
89a1c0
 import os
89a1c0
+from six import print_, StringIO
89a1c0
 
89a1c0
 srcdir = sys.argv[1]
89a1c0
 
89a1c0
-hlines = file('%s/errors.h' % srcdir,'r').readlines()
89a1c0
 messages = dict()
89a1c0
 description = ''
89a1c0
 
89a1c0
-for line in hlines:
89a1c0
-    m = re.match(r'^ \* LASSO.*ERROR', line)
89a1c0
-    if m:
89a1c0
-        description = ''
89a1c0
-        continue
89a1c0
-    m = re.match(r'^ \* (.*[^:])$', line)
89a1c0
-    if m:
89a1c0
-        description += m.group(1)
89a1c0
-    m = re.match(r'#define (LASSO_\w*ERROR\w+)', line)
89a1c0
-    if m and description:
89a1c0
-        description = re.sub(r'[ \n]+', ' ', description).strip()
89a1c0
-        messages[m.group(1)] = description
89a1c0
-        description = ''
89a1c0
-    else:
89a1c0
-        m = re.match(r'#define (LASSO_\w*ERROR\w+)',line)
89a1c0
+with open('%s/errors.h' % srcdir,'r') as f:
89a1c0
+    for line in f:
89a1c0
+        m = re.match(r'^ \* LASSO.*ERROR', line)
89a1c0
         if m:
89a1c0
-            messages[m.group(1)] = m.group(1)
89a1c0
+            description = ''
89a1c0
+            continue
89a1c0
+        m = re.match(r'^ \* (.*[^:])$', line)
89a1c0
+        if m:
89a1c0
+            description += m.group(1)
89a1c0
+        m = re.match(r'#define (LASSO_\w*ERROR\w+)', line)
89a1c0
+        if m and description:
89a1c0
+            description = re.sub(r'[ \n]+', ' ', description).strip()
89a1c0
+            messages[m.group(1)] = description
89a1c0
+            description = ''
89a1c0
+        else:
89a1c0
+            m = re.match(r'#define (LASSO_\w*ERROR\w+)',line)
89a1c0
+            if m:
89a1c0
+                messages[m.group(1)] = m.group(1)
89a1c0
 
89a1c0
-clines = file('%s/errors.c.in' % srcdir,'r').readlines()
89a1c0
-for line in clines:
89a1c0
-    if '@ERROR_CASES@' in line:
89a1c0
-        keys = messages.keys()
89a1c0
-        keys.sort()
89a1c0
-        for k in keys:
89a1c0
-            print """		case %s:
89a1c0
-			return "%s";""" % (k,messages[k].rstrip('\n'))
89a1c0
-    else:
89a1c0
-        print line,
89a1c0
+with open('%s/errors.c.in' % srcdir,'r') as f:
89a1c0
+    for line in f:
89a1c0
+        if '@ERROR_CASES@' in line:
89a1c0
+            keys = sorted(messages.keys())
89a1c0
+            for k in keys:
89a1c0
+                print_('		case %s:\n'
89a1c0
+                       '			return "%s";' %
89a1c0
+                       (k,messages[k].rstrip('\n')))
89a1c0
+        else:
89a1c0
+            print_(line, end="")