Skip to content
Snippets Groups Projects
Commit bc5f6ff6 authored by Sébastien Luttringer's avatar Sébastien Luttringer
Browse files

tarball interface encode check encoding is in UTF8

parent 410ee846
No related branches found
No related tags found
No related merge requests found
......@@ -651,7 +651,7 @@ class PackageImage(Image):
'''
desc = {}
# check format
img_format = self._tarball.get_str("format")
img_format = self._tarball.get_utf8("format")
try:
if float(img_format) >= math.floor(float(self.format)) + 1.0:
raise Exception()
......@@ -660,7 +660,7 @@ class PackageImage(Image):
desc["format"] = img_format
# check description
try:
img_desc = self._tarball.get_str("description.json")
img_desc = self._tarball.get_utf8("description.json")
desc.update(json.loads(img_desc))
self.check_image_name(desc["name"])
self.check_image_version(desc["version"])
......@@ -674,7 +674,7 @@ class PackageImage(Image):
raise Exception(u"Invalid description: %s" % e)
# try to load changelog
try:
img_changelog = self._tarball.get_str("changelog")
img_changelog = self._tarball.get_utf8("changelog")
desc["changelog"] = Changelog(img_changelog)
except KeyError:
desc["changelog"] = Changelog("")
......@@ -747,7 +747,7 @@ class PackageImage(Image):
warn(u"No file matching %s" % filename)
for filename in filelist:
arrow(filename)
out(self._tarball.get_str(filename))
out(self._tarball.get_utf8(filename))
def download(self, directory, force=False, image=True, payload=False):
'''
......
......@@ -25,6 +25,9 @@ class Tarball(tarfile.TarFile):
ti.mtime = int(time.time())
ti.uid = ti.gid = 0
ti.uname = ti.gname = "root"
# unicode char is encoded in UTF-8, has changelog must be in UTF-8
if isinstance(content, unicode):
content = content.encode("UTF-8")
ti.size = len(content) if content is not None else 0
self.addfile(ti, StringIO.StringIO(content))
......@@ -36,6 +39,15 @@ class Tarball(tarfile.TarFile):
fd = self.extractfile(ti)
return fd.read() if fd is not None else ""
def get_utf8(self, name):
'''
Return an unicode string from a file encoded in UTF-8 inside tarball
'''
try:
return unicode(self.get_str(name), "UTF-8")
except UnicodeDecodeError:
raise Exception(u"Invalid UTF-8 character in %s" % name)
def getnames(self, re_pattern=None, glob_pattern=None, dir=True):
names = super(Tarball, self).getnames()
# regexp matching
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment