From 1ff91b4205f159d9fa2600ef15dfadb74fef7a02 Mon Sep 17 00:00:00 2001
From: Christian Heimes <cheimes@redhat.com>
Date: Sun, 16 Aug 2015 19:00:00 +0200
Subject: [PATCH 35/38] Py3 compatibility: write XML as encoded bytes

Python 3 treats serialized XML as encoded bytes. etree must encode XML
to UTF-8 and write it to a file opened in binary mode.
---
 base/server/python/pki/server/__init__.py                    |  5 +++--
 base/server/python/pki/server/cli/migrate.py                 |  7 ++++---
 base/server/python/pki/server/cli/nuxwdog.py                 | 10 ++++++----
 base/server/python/pki/server/deployment/pkihelper.py        |  5 +++--
 base/server/upgrade/10.0.1/01-ReplaceRandomNumberGenerator   | 12 ++++++------
 base/server/upgrade/10.0.5/01-EnableSessionInAuthenticator   |  4 ++--
 base/server/upgrade/10.1.99/05-RemoveConfigPathFromWebXML    |  4 ++--
 base/server/upgrade/10.2.1/01-AddTLSRangeSupport             |  4 ++--
 .../upgrade/10.2.2/01-MoveWebApplicationDeploymentLocations  |  4 ++--
 base/server/upgrade/10.2.2/02-EnableWebApplicationAutoDeploy |  4 ++--
 10 files changed, 32 insertions(+), 27 deletions(-)

diff --git a/base/server/python/pki/server/__init__.py b/base/server/python/pki/server/__init__.py
index 2f5fb461ff207a3e4b7bee8e3838848df9d97499..97b2e97992ad1daf0313127ce89f826cc3d0fb10 100644
--- a/base/server/python/pki/server/__init__.py
+++ b/base/server/python/pki/server/__init__.py
@@ -215,8 +215,9 @@ class PKIInstance(object):
             context.set('docBase', doc_base)
 
         # write deployment descriptor
-        with open(context_xml, 'w') as f:
-            f.write(etree.tostring(document, pretty_print=True))
+        with open(context_xml, 'wb') as f:
+            # xml as UTF-8 encoded bytes
+            document.write(f, pretty_print=True, encoding='utf-8')
 
         # set deployment descriptor ownership and permission
         os.chown(context_xml, self.uid, self.gid)
diff --git a/base/server/python/pki/server/cli/migrate.py b/base/server/python/pki/server/cli/migrate.py
index 98330eff4c7e4f94879c35b68385314dddbe01f8..ba5e6311616cc902dbcd85eccf7c4a3c4826773e 100644
--- a/base/server/python/pki/server/cli/migrate.py
+++ b/base/server/python/pki/server/cli/migrate.py
@@ -132,8 +132,9 @@ class MigrateCLI(pki.cli.CLI):
             self.print_help()
             sys.exit(1)
 
-        with open(filename, 'w') as f:
-            f.write(etree.tostring(document, pretty_print=True))
+        with open(filename, 'wb') as f:
+            # xml as UTF-8 encoded bytes
+            document.write(f, pretty_print=True, encoding='utf-8')
 
     def migrate_server_xml_to_tomcat7(self, document):
         server = document.getroot()
@@ -395,7 +396,7 @@ class MigrateCLI(pki.cli.CLI):
             sys.exit(1)
 
         with open(filename, 'w') as f:
-            f.write(etree.tostring(document, pretty_print=True))
+            document.write(f, pretty_print=True, encoding='utf-8')
 
     def migrate_context_xml_to_tomcat7(self, document):
         context = document.getroot()
diff --git a/base/server/python/pki/server/cli/nuxwdog.py b/base/server/python/pki/server/cli/nuxwdog.py
index bdcc18a002c873436a95767401988c7cfc382846..dd54f5683f0a9fb28369b8c67bd4a0d10fd4fc9f 100644
--- a/base/server/python/pki/server/cli/nuxwdog.py
+++ b/base/server/python/pki/server/cli/nuxwdog.py
@@ -211,8 +211,9 @@ class NuxwdogEnableCLI(pki.cli.CLI):
                 connector.set('passwordClass', self.nuxwdog_pwstore_class)
                 connector.set('passwordFile', conf_file)
 
-        with open(filename, 'w') as f:
-            f.write(etree.tostring(document, pretty_print=True))
+        with open(filename, 'wb') as f:
+            # xml as UTF-8 encoded bytes
+            document.write(f, pretty_print=True, encoding='utf-8')
 
         os.chown(filename, instance.uid, instance.gid)
 
@@ -366,8 +367,9 @@ class NuxwdogDisableCLI(pki.cli.CLI):
                 connector.set('passwordClass', self.plain_pwstore_class)
                 connector.set('passwordFile', pw_conf)
 
-        with open(filename, 'w') as f:
-            f.write(etree.tostring(document, pretty_print=True))
+        with open(filename, 'wb') as f:
+            # xml as UTF-8 encoded bytes
+            document.write(f, pretty_print=True, encoding='utf-8')
 
         os.chown(filename, instance.uid, instance.gid)
 
diff --git a/base/server/python/pki/server/deployment/pkihelper.py b/base/server/python/pki/server/deployment/pkihelper.py
index e6833d270777a209b9000878b02a75dd3a2ba234..35e218a7b8c2b30946f4d84c3d1086b71bacab1b 100644
--- a/base/server/python/pki/server/deployment/pkihelper.py
+++ b/base/server/python/pki/server/deployment/pkihelper.py
@@ -4599,8 +4599,9 @@ class PKIDeployer:
         context = document.getroot()
         context.set('docBase', doc_base)
 
-        with open(new_descriptor, 'w') as f:
-            f.write(etree.tostring(document, pretty_print=True))
+        with open(new_descriptor, 'wb') as f:
+            # xml as UTF-8 encoded bytes
+            document.write(f, pretty_print=True, encoding='utf-8')
 
         os.chown(new_descriptor, self.mdict['pki_uid'], self.mdict['pki_gid'])
         os.chmod(
diff --git a/base/server/upgrade/10.0.1/01-ReplaceRandomNumberGenerator b/base/server/upgrade/10.0.1/01-ReplaceRandomNumberGenerator
index 11b3794654f6e67310b8e637c1934c9c8a606a8c..a17a27d703da347115b88e2c8571a777f39d60e0 100755
--- a/base/server/upgrade/10.0.1/01-ReplaceRandomNumberGenerator
+++ b/base/server/upgrade/10.0.1/01-ReplaceRandomNumberGenerator
@@ -66,8 +66,8 @@ class ReplaceRandomNumberGenerator(
             self.remove_authenticator(document)
             self.remove_realm(document)
 
-        with open(context_xml, 'w') as f:
-            f.write(etree.tostring(document, pretty_print=True))
+        with open(context_xml, 'wb') as f:
+            document.write(f, pretty_print=True, encoding='utf-8')
 
     def upgrade_instance(self, instance):
 
@@ -97,8 +97,8 @@ class ReplaceRandomNumberGenerator(
 
         self.add_manager(document)
 
-        with open(context_xml, 'w') as f:
-            f.write(etree.tostring(document, pretty_print=True))
+        with open(context_xml, 'wb') as f:
+            document.write(f, pretty_print=True, encoding='utf-8')
 
     def update_pki_context_xml(self, instance):
 
@@ -123,8 +123,8 @@ class ReplaceRandomNumberGenerator(
 
         self.add_manager(document)
 
-        with open(context_xml, 'w') as f:
-            f.write(etree.tostring(document, pretty_print=True))
+        with open(context_xml, 'wb') as f:
+            document.write(f, pretty_print=True, encoding='utf-8')
 
     def create_meta_inf(self, instance, path):
 
diff --git a/base/server/upgrade/10.0.5/01-EnableSessionInAuthenticator b/base/server/upgrade/10.0.5/01-EnableSessionInAuthenticator
index 36b22697b2fda16d852380d5d915959ea962690a..a4e836d24a46f38ad607dfbe6bd40e72fff8a327 100755
--- a/base/server/upgrade/10.0.5/01-EnableSessionInAuthenticator
+++ b/base/server/upgrade/10.0.5/01-EnableSessionInAuthenticator
@@ -44,8 +44,8 @@ class EnableSessionInAuthenticator(
 
         self.enable_session(document)
 
-        with open(context_xml, 'w') as f:
-            f.write(etree.tostring(document, pretty_print=True))
+        with open(context_xml, 'wb') as f:
+            document.write(f, pretty_print=True, encoding='utf-8')
 
     def enable_session(self, document):
 
diff --git a/base/server/upgrade/10.1.99/05-RemoveConfigPathFromWebXML b/base/server/upgrade/10.1.99/05-RemoveConfigPathFromWebXML
index 3e1d47e2e1fb34ec1a4e2c1577c94722a85a35dd..42a4127c5cfad9fea322d02c023b9f1dfe110cf8 100755
--- a/base/server/upgrade/10.1.99/05-RemoveConfigPathFromWebXML
+++ b/base/server/upgrade/10.1.99/05-RemoveConfigPathFromWebXML
@@ -54,8 +54,8 @@ class RemoveConfigPathFromWebXML(pki.server.upgrade.PKIServerUpgradeScriptlet):
         document = etree.parse(web_xml)
         self.remove_config_path(document, default_cs_cfg)
 
-        with open(web_xml, 'w') as f:
-            f.write(etree.tostring(document, pretty_print=True))
+        with open(web_xml, 'wb') as f:
+            document.write(f, pretty_print=True, encoding='utf-8')
 
     def remove_config_path(self, document, default_cs_cfg):
 
diff --git a/base/server/upgrade/10.2.1/01-AddTLSRangeSupport b/base/server/upgrade/10.2.1/01-AddTLSRangeSupport
index 4a3997fce3051435c35720d835d06f3e5c0e1d0c..db27d015c00a16942c03ad971acd833ca9f28dc5 100755
--- a/base/server/upgrade/10.2.1/01-AddTLSRangeSupport
+++ b/base/server/upgrade/10.2.1/01-AddTLSRangeSupport
@@ -47,8 +47,8 @@ class AddTLSRangeSupport(pki.server.upgrade.PKIServerUpgradeScriptlet):
         # Once all changes are made, write the XML back into the same server.xml
         # This way we're preserving any other customization that has been done
         # to the server.xml
-        with open(server_xml, 'w') as f:
-            f.write(etree.tostring(document, pretty_print=True))
+        with open(server_xml, 'wb') as f:
+            document.write(f, pretty_print=True, encoding='utf-8')
 
     def add_tls_range(self, document):
 
diff --git a/base/server/upgrade/10.2.2/01-MoveWebApplicationDeploymentLocations b/base/server/upgrade/10.2.2/01-MoveWebApplicationDeploymentLocations
index 313ee0d7174503ae01c1515c93c0dad803d9d30e..d83372de16bb1c2b95ef2a826dedff95a952dc28 100755
--- a/base/server/upgrade/10.2.2/01-MoveWebApplicationDeploymentLocations
+++ b/base/server/upgrade/10.2.2/01-MoveWebApplicationDeploymentLocations
@@ -128,5 +128,5 @@ class MoveWebApplicationDeploymentLocations(
 
         context.set('docBase', new_webapp)
 
-        with open(context_xml, 'w') as f:
-            f.write(etree.tostring(document, pretty_print=True))
+        with open(context_xml, 'wb') as f:
+            document.write(f, pretty_print=True, encoding='utf-8')
diff --git a/base/server/upgrade/10.2.2/02-EnableWebApplicationAutoDeploy b/base/server/upgrade/10.2.2/02-EnableWebApplicationAutoDeploy
index ffa94d81c2973b363bfb1034057a4faa483562e5..04b6a6fda37d4a2290d5b1c2337662838e120eab 100755
--- a/base/server/upgrade/10.2.2/02-EnableWebApplicationAutoDeploy
+++ b/base/server/upgrade/10.2.2/02-EnableWebApplicationAutoDeploy
@@ -49,5 +49,5 @@ class EnableWebApplicationAutoDeploy(
         for host in hosts:
             host.set('autoDeploy', 'true')
 
-        with open(server_xml, 'w') as f:
-            f.write(etree.tostring(document, pretty_print=True))
+        with open(server_xml, 'wb') as f:
+            document.write(f, pretty_print=True, encoding='utf-8')
-- 
2.4.3

