codekingpro/portable-devtools
114k
1"""Tests for distutils.archive_util."""2 3import functools4import operator5import os6import pathlib7import sys8import tarfile9from distutils import archive_util10from distutils.archive_util import (11 ARCHIVE_FORMATS,12 check_archive_formats,13 make_archive,14 make_tarball,15 make_zipfile,16)17from distutils.spawn import spawn18from distutils.tests import support19from os.path import splitdrive20 21import path22import pytest23from test.support import patch24 25from .unix_compat import UID_0_SUPPORT, grp, pwd, require_uid_0, require_unix_id26 27 28def can_fs_encode(filename):29 """30 Return True if the filename can be saved in the file system.31 """32 if os.path.supports_unicode_filenames:33 return True34 try:35 filename.encode(sys.getfilesystemencoding())36 except UnicodeEncodeError:37 return False38 return True39 40 41def all_equal(values):42 return functools.reduce(operator.eq, values)43 44 45def same_drive(*paths):46 return all_equal(pathlib.Path(path).drive for path in paths)47 48 49class ArchiveUtilTestCase(support.TempdirManager):50 @pytest.mark.usefixtures('needs_zlib')51 def test_make_tarball(self, name='archive'):52 # creating something to tar53 tmpdir = self._create_files()54 self._make_tarball(tmpdir, name, '.tar.gz')55 # trying an uncompressed one56 self._make_tarball(tmpdir, name, '.tar', compress=None)57 58 @pytest.mark.usefixtures('needs_zlib')59 def test_make_tarball_gzip(self):60 tmpdir = self._create_files()61 self._make_tarball(tmpdir, 'archive', '.tar.gz', compress='gzip')62 63 def test_make_tarball_bzip2(self):64 pytest.importorskip('bz2')65 tmpdir = self._create_files()66 self._make_tarball(tmpdir, 'archive', '.tar.bz2', compress='bzip2')67 68 def test_make_tarball_xz(self):69 pytest.importorskip('lzma')70 tmpdir = self._create_files()71 self._make_tarball(tmpdir, 'archive', '.tar.xz', compress='xz')72 73 @pytest.mark.skipif("not can_fs_encode('årchiv')")74 def test_make_tarball_latin1(self):75 """76 Mirror test_make_tarball, except filename contains latin characters.77 """78 self.test_make_tarball('årchiv') # note this isn't a real word79 80 @pytest.mark.skipif("not can_fs_encode('のアーカイブ')")81 def test_make_tarball_extended(self):82 """83 Mirror test_make_tarball, except filename contains extended84 characters outside the latin charset.85 """86 self.test_make_tarball('のアーカイブ') # japanese for archive87 88 def _make_tarball(self, tmpdir, target_name, suffix, **kwargs):89 tmpdir2 = self.mkdtemp()90 if same_drive(tmpdir, tmpdir2):91 pytest.skip("source and target should be on same drive")92 93 base_name = os.path.join(tmpdir2, target_name)94 95 # working with relative paths to avoid tar warnings96 with path.Path(tmpdir):97 make_tarball(splitdrive(base_name)[1], 'dist', **kwargs)98 99 # check if the compressed tarball was created100 tarball = base_name + suffix101 assert os.path.exists(tarball)102 assert self._tarinfo(tarball) == self._created_files103 104 def _tarinfo(self, path):105 tar = tarfile.open(path)106 try:107 names = tar.getnames()108 names.sort()109 return names110 finally:111 tar.close()112 113 _zip_created_files = [114 'dist/',115 'dist/file1',116 'dist/file2',117 'dist/sub/',118 'dist/sub/file3',119 'dist/sub2/',120 ]121 _created_files = [p.rstrip('/') for p in _zip_created_files]122 123 def _create_files(self):124 # creating something to tar125 tmpdir = self.mkdtemp()126 dist = os.path.join(tmpdir, 'dist')127 os.mkdir(dist)128 self.write_file([dist, 'file1'], 'xxx')129 self.write_file([dist, 'file2'], 'xxx')130 os.mkdir(os.path.join(dist, 'sub'))131 self.write_file([dist, 'sub', 'file3'], 'xxx')132 os.mkdir(os.path.join(dist, 'sub2'))133 return tmpdir134 135 @pytest.mark.usefixtures('needs_zlib')136 @pytest.mark.skipif("not (shutil.which('tar') and shutil.which('gzip'))")137 def test_tarfile_vs_tar(self):138 tmpdir = self._create_files()139 tmpdir2 = self.mkdtemp()140 base_name = os.path.join(tmpdir2, 'archive')141 old_dir = os.getcwd()142 os.chdir(tmpdir)143 try:144 make_tarball(base_name, 'dist')145 finally:146 os.chdir(old_dir)147 148 # check if the compressed tarball was created149 tarball = base_name + '.tar.gz'150 assert os.path.exists(tarball)151 152 # now create another tarball using `tar`153 tarball2 = os.path.join(tmpdir, 'archive2.tar.gz')154 tar_cmd = ['tar', '-cf', 'archive2.tar', 'dist']155 gzip_cmd = ['gzip', '-f', '-9', 'archive2.tar']156 old_dir = os.getcwd()157 os.chdir(tmpdir)158 try:159 spawn(tar_cmd)160 spawn(gzip_cmd)161 finally:162 os.chdir(old_dir)163 164 assert os.path.exists(tarball2)165 # let's compare both tarballs166 assert self._tarinfo(tarball) == self._created_files167 assert self._tarinfo(tarball2) == self._created_files168 169 # trying an uncompressed one170 base_name = os.path.join(tmpdir2, 'archive')171 old_dir = os.getcwd()172 os.chdir(tmpdir)173 try:174 make_tarball(base_name, 'dist', compress=None)175 finally:176 os.chdir(old_dir)177 tarball = base_name + '.tar'178 assert os.path.exists(tarball)179 180 @pytest.mark.usefixtures('needs_zlib')181 def test_make_zipfile(self):182 zipfile = pytest.importorskip('zipfile')183 # creating something to tar184 tmpdir = self._create_files()185 base_name = os.path.join(self.mkdtemp(), 'archive')186 with path.Path(tmpdir):187 make_zipfile(base_name, 'dist')188 189 # check if the compressed tarball was created190 tarball = base_name + '.zip'191 assert os.path.exists(tarball)192 with zipfile.ZipFile(tarball) as zf:193 assert sorted(zf.namelist()) == self._zip_created_files194 195 def test_make_zipfile_no_zlib(self):196 zipfile = pytest.importorskip('zipfile')197 patch(self, archive_util.zipfile, 'zlib', None) # force zlib ImportError198 199 called = []200 zipfile_class = zipfile.ZipFile201 202 def fake_zipfile(*a, **kw):203 if kw.get('compression', None) == zipfile.ZIP_STORED:204 called.append((a, kw))205 return zipfile_class(*a, **kw)206 207 patch(self, archive_util.zipfile, 'ZipFile', fake_zipfile)208 209 # create something to tar and compress210 tmpdir = self._create_files()211 base_name = os.path.join(self.mkdtemp(), 'archive')212 with path.Path(tmpdir):213 make_zipfile(base_name, 'dist')214 215 tarball = base_name + '.zip'216 assert called == [((tarball, "w"), {'compression': zipfile.ZIP_STORED})]217 assert os.path.exists(tarball)218 with zipfile.ZipFile(tarball) as zf:219 assert sorted(zf.namelist()) == self._zip_created_files220 221 def test_check_archive_formats(self):222 assert check_archive_formats(['gztar', 'xxx', 'zip']) == 'xxx'223 assert (224 check_archive_formats(['gztar', 'bztar', 'xztar', 'ztar', 'tar', 'zip'])225 is None226 )227 228 def test_make_archive(self):229 tmpdir = self.mkdtemp()230 base_name = os.path.join(tmpdir, 'archive')231 with pytest.raises(ValueError):232 make_archive(base_name, 'xxx')233 234 def test_make_archive_cwd(self):235 current_dir = os.getcwd()236 237 def _breaks(*args, **kw):238 raise RuntimeError()239 240 ARCHIVE_FORMATS['xxx'] = (_breaks, [], 'xxx file')241 try:242 try:243 make_archive('xxx', 'xxx', root_dir=self.mkdtemp())244 except Exception:245 pass246 assert os.getcwd() == current_dir247 finally:248 ARCHIVE_FORMATS.pop('xxx')249 250 def test_make_archive_tar(self):251 base_dir = self._create_files()252 base_name = os.path.join(self.mkdtemp(), 'archive')253 res = make_archive(base_name, 'tar', base_dir, 'dist')254 assert os.path.exists(res)255 assert os.path.basename(res) == 'archive.tar'256 assert self._tarinfo(res) == self._created_files257 258 @pytest.mark.usefixtures('needs_zlib')259 def test_make_archive_gztar(self):260 base_dir = self._create_files()261 base_name = os.path.join(self.mkdtemp(), 'archive')262 res = make_archive(base_name, 'gztar', base_dir, 'dist')263 assert os.path.exists(res)264 assert os.path.basename(res) == 'archive.tar.gz'265 assert self._tarinfo(res) == self._created_files266 267 def test_make_archive_bztar(self):268 pytest.importorskip('bz2')269 base_dir = self._create_files()270 base_name = os.path.join(self.mkdtemp(), 'archive')271 res = make_archive(base_name, 'bztar', base_dir, 'dist')272 assert os.path.exists(res)273 assert os.path.basename(res) == 'archive.tar.bz2'274 assert self._tarinfo(res) == self._created_files275 276 def test_make_archive_xztar(self):277 pytest.importorskip('lzma')278 base_dir = self._create_files()279 base_name = os.path.join(self.mkdtemp(), 'archive')280 res = make_archive(base_name, 'xztar', base_dir, 'dist')281 assert os.path.exists(res)282 assert os.path.basename(res) == 'archive.tar.xz'283 assert self._tarinfo(res) == self._created_files284 285 def test_make_archive_owner_group(self):286 # testing make_archive with owner and group, with various combinations287 # this works even if there's not gid/uid support288 if UID_0_SUPPORT:289 group = grp.getgrgid(0)[0]290 owner = pwd.getpwuid(0)[0]291 else:292 group = owner = 'root'293 294 base_dir = self._create_files()295 root_dir = self.mkdtemp()296 base_name = os.path.join(self.mkdtemp(), 'archive')297 res = make_archive(298 base_name, 'zip', root_dir, base_dir, owner=owner, group=group299 )300 assert os.path.exists(res)301 302 res = make_archive(base_name, 'zip', root_dir, base_dir)303 assert os.path.exists(res)304 305 res = make_archive(306 base_name, 'tar', root_dir, base_dir, owner=owner, group=group307 )308 assert os.path.exists(res)309 310 res = make_archive(311 base_name, 'tar', root_dir, base_dir, owner='kjhkjhkjg', group='oihohoh'312 )313 assert os.path.exists(res)314 315 @pytest.mark.usefixtures('needs_zlib')316 @require_unix_id317 @require_uid_0318 def test_tarfile_root_owner(self):319 tmpdir = self._create_files()320 base_name = os.path.join(self.mkdtemp(), 'archive')321 old_dir = os.getcwd()322 os.chdir(tmpdir)323 group = grp.getgrgid(0)[0]324 owner = pwd.getpwuid(0)[0]325 try:326 archive_name = make_tarball(327 base_name, 'dist', compress=None, owner=owner, group=group328 )329 finally:330 os.chdir(old_dir)331 332 # check if the compressed tarball was created333 assert os.path.exists(archive_name)334 335 # now checks the rights336 archive = tarfile.open(archive_name)337 try:338 for member in archive.getmembers():339 assert member.uid == 0340 assert member.gid == 0341 finally:342 archive.close()343 