[elbe-devel] [PATCH 10/10] be more robust with encoding and license files
Manuel Traut
manut at linutronix.de
Fri Sep 29 00:11:46 CEST 2017
however we drop adding some licence texts to the licence.xml if we can't
decode it properly.
Signed-off-by: Manuel Traut <manut at linutronix.de>
---
elbepack/efilesystem.py | 32 +++++++++++++++++++++-----------
elbepack/licencexml.py | 18 ++++++++++--------
2 files changed, 31 insertions(+), 19 deletions(-)
diff --git a/elbepack/efilesystem.py b/elbepack/efilesystem.py
index 7e6f9daa..44009954 100644
--- a/elbepack/efilesystem.py
+++ b/elbepack/efilesystem.py
@@ -143,18 +143,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 = str (lic_text, encoding='utf-8')
- except:
- lic_text = str (lic_text, encoding='iso-8859-1')
-
-
if not f is None:
- f.write(str(os.path.basename(dir)))
- f.write(":\n================================================================================")
- f.write("\n")
- f.write(lic_text)
- f.write("\n\n")
+ f.write(unicode(os.path.basename(dir)))
+ 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 f60127d7..2efe327d 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 (str(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.14.1
More information about the elbe-devel
mailing list