[elbe-devel] [PATCH 07/25] py3: care on encoding of license files

Manuel Traut manut at linutronix.de
Fri Dec 1 16:51:04 CET 2017


This tries to find the correct encoding of a copyright file and write the
decoded content of the copyright file to licence.xml.

Licence texts that can't be decoded are dropped and an error message is
logged.

Signed-off-by: Manuel Traut <manut at linutronix.de>
---
 elbepack/efilesystem.py | 30 ++++++++++++++++++++----------
 elbepack/licencexml.py  | 18 ++++++++++--------
 2 files changed, 30 insertions(+), 18 deletions(-)

diff --git a/elbepack/efilesystem.py b/elbepack/efilesystem.py
index be7d7e25..bd9f7453 100644
--- a/elbepack/efilesystem.py
+++ b/elbepack/efilesystem.py
@@ -142,18 +142,28 @@ class ElbeFilesystem(Filesystem):
                         (os.path.join(dir, "copyright"), e.strerror))
                 lic_text = "Error while processing license file %s: '%s'" % (os.path.join(dir, "copyright"), e.strerror)
 
-            try:
-                lic_text = unicode (lic_text, encoding='utf-8')
-            except:
-                lic_text = unicode (lic_text, encoding='iso-8859-1')
-
-
             if not f is None:
                 f.write(unicode(os.path.basename(dir)))
-                f.write(u":\n================================================================================")
-                f.write(u"\n")
-                f.write(lic_text)
-                f.write(u"\n\n")
+                f.write(unicode(":\n================================================================================"))
+                f.write(unicode("\n"))
+                try:
+                    f.write(lic_text.encode('utf-8'))
+                except TypeError as e:
+                    log.printo(e)
+                    log.printo("error by writing licence of %s" % (os.path.join(dir, "copyright")))
+                    log.printo(str(lic_text))
+                    f.write(unicode(str(lic_text)))
+                except UnicodeDecodeError as e:
+                    log.printo(e)
+                    log.printo("error by writing licence of %s" % (os.path.join(dir, "copyright")))
+                    log.printo(str(lic_text))
+                    n = f.name
+                    f.close()
+                    with open (n, 'a') as f:
+                        f.write(lic_text)
+                    f = open(n, 'ab')
+
+                f.write(unicode("\n\n"))
 
             if not xml_fname is None:
                 licence_xml.add_copyright_file (os.path.basename(dir), lic_text)
diff --git a/elbepack/licencexml.py b/elbepack/licencexml.py
index a40dffcd..d2c4979a 100644
--- a/elbepack/licencexml.py
+++ b/elbepack/licencexml.py
@@ -66,9 +66,17 @@ class copyright_xml (object):
         xmlpkg = self.pkglist.append('pkglicense')
         xmlpkg.et.attrib['name'] = pkg_name
         txtnode = xmlpkg.append ('text')
-        txtnode.et.text = copyright
 
-        bytesio = io.StringIO (unicode(txtnode.et.text))
+        # just return if we cant decode the copyright file; we also return
+        # if we can't interpret it, so this should be ok
+        try:
+            txtnode.et.text = unicode(copyright)
+            bytesio = io.StringIO (txtnode.et.text)
+        except TypeError as e:
+            return
+        except UnicodeDecodeError as e:
+            return
+
         try:
             c = Copyright (bytesio)
             files = []
@@ -125,9 +133,3 @@ class copyright_xml (object):
 
     def write(self, fname):
         self.outxml.write (fname, encoding="iso-8859-1")
-
-        
-
-
-
-
-- 
2.15.1




More information about the elbe-devel mailing list