2011-12-23 06:38:02 +08:00
|
|
|
import os
|
|
|
|
import shutil
|
2016-12-02 00:05:08 +08:00
|
|
|
import stat
|
|
|
|
import sys
|
2011-12-23 06:38:02 +08:00
|
|
|
import tempfile
|
2013-07-01 20:22:27 +08:00
|
|
|
import unittest
|
2011-12-23 06:38:02 +08:00
|
|
|
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.utils.archive import Archive, extract
|
2011-12-23 06:38:02 +08:00
|
|
|
|
2017-01-20 21:01:02 +08:00
|
|
|
TEST_DIR = os.path.join(os.path.dirname(__file__), 'archives')
|
2011-12-23 06:38:02 +08:00
|
|
|
|
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class ArchiveTester:
|
2011-12-23 06:38:02 +08:00
|
|
|
archive = None
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
"""
|
|
|
|
Create temporary directory for testing extraction.
|
|
|
|
"""
|
|
|
|
self.old_cwd = os.getcwd()
|
|
|
|
self.tmpdir = tempfile.mkdtemp()
|
|
|
|
self.addCleanup(shutil.rmtree, self.tmpdir)
|
|
|
|
self.archive_path = os.path.join(TEST_DIR, self.archive)
|
2014-05-22 20:12:22 +08:00
|
|
|
self.archive_lead_path = os.path.join(TEST_DIR, "leadpath_%s" % self.archive)
|
2011-12-23 06:38:02 +08:00
|
|
|
# Always start off in TEST_DIR.
|
|
|
|
os.chdir(TEST_DIR)
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
os.chdir(self.old_cwd)
|
|
|
|
|
|
|
|
def test_extract_method(self):
|
2012-08-15 17:53:40 +08:00
|
|
|
with Archive(self.archive) as archive:
|
|
|
|
archive.extract(self.tmpdir)
|
2011-12-23 06:38:02 +08:00
|
|
|
self.check_files(self.tmpdir)
|
|
|
|
|
|
|
|
def test_extract_method_no_to_path(self):
|
|
|
|
os.chdir(self.tmpdir)
|
2012-08-15 17:53:40 +08:00
|
|
|
with Archive(self.archive_path) as archive:
|
|
|
|
archive.extract()
|
2011-12-23 06:38:02 +08:00
|
|
|
self.check_files(self.tmpdir)
|
|
|
|
|
|
|
|
def test_extract_function(self):
|
|
|
|
extract(self.archive_path, self.tmpdir)
|
|
|
|
self.check_files(self.tmpdir)
|
2014-05-22 20:12:22 +08:00
|
|
|
|
2016-12-02 00:05:08 +08:00
|
|
|
@unittest.skipIf(sys.platform == 'win32', 'Python on Windows has a limited os.chmod().')
|
|
|
|
def test_extract_file_permissions(self):
|
|
|
|
"""Archive.extract() preserves file permissions."""
|
|
|
|
extract(self.archive_path, self.tmpdir)
|
|
|
|
filepath = os.path.join(self.tmpdir, 'executable')
|
|
|
|
# The file has executable permission.
|
|
|
|
self.assertTrue(os.stat(filepath).st_mode & stat.S_IXOTH)
|
2016-12-29 04:20:24 +08:00
|
|
|
filepath = os.path.join(self.tmpdir, 'no_permissions')
|
|
|
|
# The file is readable even though it doesn't have permission data in
|
|
|
|
# the archive.
|
|
|
|
self.assertTrue(os.stat(filepath).st_mode & stat.S_IROTH)
|
2016-12-02 00:05:08 +08:00
|
|
|
|
2014-05-22 20:12:22 +08:00
|
|
|
def test_extract_function_with_leadpath(self):
|
|
|
|
extract(self.archive_lead_path, self.tmpdir)
|
|
|
|
self.check_files(self.tmpdir)
|
2011-12-23 06:38:02 +08:00
|
|
|
|
|
|
|
def test_extract_function_no_to_path(self):
|
|
|
|
os.chdir(self.tmpdir)
|
|
|
|
extract(self.archive_path)
|
|
|
|
self.check_files(self.tmpdir)
|
|
|
|
|
|
|
|
def check_files(self, tmpdir):
|
|
|
|
self.assertTrue(os.path.isfile(os.path.join(self.tmpdir, '1')))
|
|
|
|
self.assertTrue(os.path.isfile(os.path.join(self.tmpdir, '2')))
|
|
|
|
self.assertTrue(os.path.isfile(os.path.join(self.tmpdir, 'foo', '1')))
|
|
|
|
self.assertTrue(os.path.isfile(os.path.join(self.tmpdir, 'foo', '2')))
|
|
|
|
self.assertTrue(os.path.isfile(os.path.join(self.tmpdir, 'foo', 'bar', '1')))
|
|
|
|
self.assertTrue(os.path.isfile(os.path.join(self.tmpdir, 'foo', 'bar', '2')))
|
|
|
|
|
|
|
|
|
|
|
|
class TestZip(ArchiveTester, unittest.TestCase):
|
|
|
|
archive = 'foobar.zip'
|
|
|
|
|
|
|
|
|
|
|
|
class TestTar(ArchiveTester, unittest.TestCase):
|
|
|
|
archive = 'foobar.tar'
|
|
|
|
|
|
|
|
|
|
|
|
class TestGzipTar(ArchiveTester, unittest.TestCase):
|
|
|
|
archive = 'foobar.tar.gz'
|
|
|
|
|
|
|
|
|
|
|
|
class TestBzip2Tar(ArchiveTester, unittest.TestCase):
|
|
|
|
archive = 'foobar.tar.bz2'
|