[jhbuild/more-py3-fixes: 4/6] tinderbox: Port html generation to Python 3



commit ad8b49e91603c38baf33a92d990fc1df1b676158
Author: Christoph Reiter <reiter christoph gmail com>
Date:   Tue Sep 24 18:52:48 2019 +0200

    tinderbox: Port html generation to Python 3
    
    Instead of writing files using the locale encoding we just use utf-8
    everywhere.
    
    The command itself is still not working fully and gets stuck during building.

 jhbuild/frontends/tinderbox.py | 22 +++++++++-------------
 jhbuild/utils/misc.py          |  6 +++---
 2 files changed, 12 insertions(+), 16 deletions(-)
---
diff --git a/jhbuild/frontends/tinderbox.py b/jhbuild/frontends/tinderbox.py
index 7f80bac5..9cb31054 100644
--- a/jhbuild/frontends/tinderbox.py
+++ b/jhbuild/frontends/tinderbox.py
@@ -21,12 +21,10 @@ import os
 import time
 import subprocess
 import logging
-import codecs
 import sys
 
-from jhbuild.main import _encoding
 from jhbuild.utils import cmds
-from jhbuild.utils import sysid, _
+from jhbuild.utils import sysid, _, udecode, open_text
 from jhbuild.errors import CommandError, FatalError
 from jhbuild.utils.compat import string_types, text_type
 from . import buildscript
@@ -135,7 +133,7 @@ buildlog_footer = '''
 
 def escape(string):
     if not isinstance(string, text_type):
-        string = text_type(string, _encoding, 'replace')
+        string = udecode(string)
     string = string.replace('&', '&amp;').replace('<','&lt;').replace(
             '>','&gt;').replace('\n','<br/>').replace(
             '\t','&nbsp;&nbsp;&nbsp;&nbsp;')
@@ -163,8 +161,6 @@ class TinderboxBuildScript(buildscript.BuildScript):
 
         os.environ['TERM'] = 'dumb'
 
-        self.charset = _encoding
-
     def timestamp(self):
         tm = time.time()
         s = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(tm))
@@ -287,12 +283,12 @@ class TinderboxBuildScript(buildscript.BuildScript):
             buildplatform += '<tr><th align="left">%s</th><td>%s</td></tr>\n' \
                              % (key, val)
         buildplatform += '</table>\n'
-        
-        self.indexfp = codecs.open(os.path.join(self.outputdir, 'index.html'),
-                'w', encoding=self.charset, errors='xmlcharrefreplace')
+
+        self.indexfp = open_text(os.path.join(self.outputdir, 'index.html'),
+                'w', encoding='utf-8', errors='xmlcharrefreplace')
 
         self.indexfp.write(index_header % { 'buildplatform': buildplatform,
-                                            'charset': self.charset })
+                                            'charset': 'UTF-8' })
         self.indexfp.flush()
 
     def end_build(self, failures):
@@ -318,16 +314,16 @@ class TinderboxBuildScript(buildscript.BuildScript):
                            '<td><a href="%s">%s</a></td>'
                            '<td>\n' % (self.timestamp(), self.modulefilename,
                                        module))
-        self.modulefp = codecs.open(
+        self.modulefp = open_text(
                 os.path.join(self.outputdir, self.modulefilename), 'w',
-                encoding=self.charset, errors='xmlcharrefreplace')
+                encoding='utf-8', errors='xmlcharrefreplace')
 
         for handle in logging.getLogger().handlers:
             if isinstance(handle, logging.StreamHandler):
                 handle.stream = self.modulefp
 
         self.modulefp.write(buildlog_header % { 'module': module,
-                                                'charset': self.charset })
+                                                'charset': 'UTF-8' })
     def end_module(self, module, failed):
         if failed:
             self.message('Failed')
diff --git a/jhbuild/utils/misc.py b/jhbuild/utils/misc.py
index b2d23030..ac0e3faf 100644
--- a/jhbuild/utils/misc.py
+++ b/jhbuild/utils/misc.py
@@ -123,7 +123,7 @@ def install_translation(translation):
         _ugettext = translation.gettext
 
 
-def open_text(filename, mode="r", encoding="utf-8"):
+def open_text(filename, mode="r", encoding="utf-8", errors="strict"):
     """An open() which removes some differences between Python 2 and 3 and
     has saner defaults.
     Unlike the builtin open by default utf-8 is used and not the locale
@@ -135,10 +135,10 @@ def open_text(filename, mode="r", encoding="utf-8"):
         raise ValueError("mode %r not supported, must be 'r' or 'w'" % mode)
 
     if PY3:
-        return open(filename, mode, encoding=encoding)
+        return open(filename, mode, encoding=encoding, errors=errors)
     else:
         # We can't use io.open() here as its write method is too strict and
         # only allows unicode instances and not everything in the codebase
         # forces unicode at the moment. codecs.open() on the other hand
         # happily takes ASCII str and decodes it.
-        return codecs.open(filename, mode, encoding=encoding)
\ No newline at end of file
+        return codecs.open(filename, mode, encoding=encoding, errors=errors)
\ No newline at end of file


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]