Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/unzip xz #3197

Merged
merged 5 commits into from Jul 12, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions conans/client/remote_manager.py
Expand Up @@ -383,6 +383,11 @@ def unzip_and_get_files(files, destination_dir, tgz_name):
to destination_dir, unzipping the "tgz_name" if found"""

tgz_file = files.pop(tgz_name, None)
bare_name = os.path.splitext(tgz_name)[0]
for f in files:
if bare_name == os.path.splitext(f)[0]:
raise ConanException("This Conan version is not prepared to handle '%s' file format. "
"Please upgrade conan client." % f)
if tgz_file:
uncompress_file(tgz_file, destination_dir)
os.remove(tgz_file)
Expand Down
6 changes: 6 additions & 0 deletions conans/client/tools/files.py
Expand Up @@ -11,6 +11,7 @@
from conans.errors import ConanException
from conans.util.files import (load, save, _generic_algorithm_sum)
from conans.unicode import get_cwd
import six


_global_output = None
Expand Down Expand Up @@ -68,6 +69,11 @@ def unzip(filename, destination=".", keep_permissions=False, pattern=None):
filename.endswith(".tbz2") or filename.endswith(".tar.bz2") or
filename.endswith(".tar")):
return untargz(filename, destination, pattern)
if filename.endswith(".tar.xz") or filename.endswith(".txz"):
if six.PY2:
raise ConanException("XZ format not supported in Python 2. Use Python 3 instead")
return untargz(filename, destination, pattern)

import zipfile
full_path = os.path.normpath(os.path.join(get_cwd(), destination))

Expand Down
33 changes: 33 additions & 0 deletions conans/test/util/xz_test.py
@@ -0,0 +1,33 @@
import os
from unittest import TestCase
import six
import unittest
import tarfile

from conans.test.utils.test_files import temp_folder
from conans.tools import unzip, save
from conans.util.files import load
from conans.errors import ConanException


class XZTest(TestCase):

@unittest.skipUnless(six.PY3, "only Py3")
def test(self):
tmp_dir = temp_folder()
file_path = os.path.join(tmp_dir, "a_file.txt")
save(file_path, "my content!")
txz = os.path.join(tmp_dir, "sample.tar.xz")
with tarfile.open(txz, "w:xz") as tar:
tar.add(file_path, "a_file.txt")

dest_folder = temp_folder()
unzip(txz, dest_folder)
content = load(os.path.join(dest_folder, "a_file.txt"))
self.assertEqual(content, "my content!")

@unittest.skipUnless(six.PY2, "only Py2")
def test_error_python2(self):
with self.assertRaisesRegexp(ConanException, "XZ format not supported in Python 2"):
dest_folder = temp_folder()
unzip("somefile.tar.xz", dest_folder)