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

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