Fixing py35 unicode bug

In py35 syntribos init failed with unicode error,
this patch fixes that. This issue was in the file
type checking code. Now syntribos will try to
extract the remote `tar.gz` file downloaded and
if it fails, then the absolute path is returned.

Change-Id: I6cd5f6951d42a6230635af4c0afec9e8d88c117a
Closes-Bug: #1680022
This commit is contained in:
rahulunair 2017-04-10 05:18:20 +00:00
parent 956aa0672e
commit c1d3407c3f
2 changed files with 25 additions and 21 deletions

View File

@ -99,3 +99,16 @@ def delete_file(path):
def delete_dir(dir_path):
return shutil.rmtree(dir_path)
def file_type(path):
"""Identifies what the type of file is."""
signature = {
"\x1f\x8b\x08": "gz",
"\x42\x5a\x68": "bz2",
"\x50\x4b\x03\x04": "zip"
}
with open(path, "r") as f:
for sig, f_type in signature.items():
if f.read(4).startswith(sig):
return f_type

View File

@ -20,7 +20,7 @@ import tempfile
from oslo_config import cfg
from syntribos.clients.http.client import SynHTTPClient
from syntribos._i18n import _LI, _LE, _LW # noqa
from syntribos._i18n import _, _LI, _LE, _LW # noqa
CONF = cfg.CONF
LOG = logging.getLogger(__name__)
@ -67,7 +67,7 @@ def download(uri, cache_dir=None):
saved_umask = os.umask(0o77)
fname = uri.split("/")[-1]
try:
with open(fname, 'w') as fh:
with open(fname, 'wb') as fh:
fh.write(resp.content)
return os.path.abspath(fname)
except IOError:
@ -76,19 +76,6 @@ def download(uri, cache_dir=None):
os.umask(saved_umask)
def file_type(path):
"""Identifies what the type of file is."""
signature = {
"\x1f\x8b\x08": "gz",
"\x42\x5a\x68": "bz2",
"\x50\x4b\x03\x04": "zip"
}
with open(path) as f:
for sig, f_type in signature.items():
if f.read(4).startswith(sig):
return f_type
def extract_tar(abs_path):
"""Extract a gzipped tar file from the given absolute_path
@ -117,9 +104,12 @@ def extract_tar(abs_path):
each_f = os.path.abspath(os.path.join(work_dir, fh.name))
if os.path.realpath(each_f).startswith(work_dir):
yield fh
with tarfile.open(tar_file, mode="r:gz") as tarf:
tarf.extractall(path=remote_path, members=safe_paths(tarf))
try:
with tarfile.open(tar_file, mode="r:gz") as tarf:
tarf.extractall(path=remote_path, members=safe_paths(tarf))
except tarfile.ExtractError as e:
LOG.error(_LE("Unable to extract the file: %s") % e)
raise
os.remove(abs_path)
return remote_path
@ -138,14 +128,15 @@ def get(uri, cache_dir=None):
temp = tempfile.TemporaryFile(dir=os.path.abspath(user_base_dir))
temp.close()
except OSError:
LOG.error(_("Failed to write remote files to: %s") %
LOG.error(_LE("Failed to write remote files to: %s") %
os.path.abspath(user_base_dir))
exit(1)
abs_path = download(uri, os.path.abspath(user_base_dir))
else:
abs_path = download(uri)
if not file_type(abs_path) == "gz":
try:
return extract_tar(abs_path)
except (tarfile.TarError, Exception):
msg = _("Not a gz file, returning abs_path")
LOG.debug(msg)
return abs_path
return extract_tar(abs_path)