CoolFace
Apppublic

Aluode/PerceptionLabPortable

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
test_archive_util.py354 linesDownload Raw Back to tests
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        # now for a dry_run181        base_name = os.path.join(tmpdir2, 'archive')182        old_dir = os.getcwd()183        os.chdir(tmpdir)184        try:185            make_tarball(base_name, 'dist', compress=None, dry_run=True)186        finally:187            os.chdir(old_dir)188        tarball = base_name + '.tar'189        assert os.path.exists(tarball)190 191    @pytest.mark.usefixtures('needs_zlib')192    def test_make_zipfile(self):193        zipfile = pytest.importorskip('zipfile')194        # creating something to tar195        tmpdir = self._create_files()196        base_name = os.path.join(self.mkdtemp(), 'archive')197        with path.Path(tmpdir):198            make_zipfile(base_name, 'dist')199 200        # check if the compressed tarball was created201        tarball = base_name + '.zip'202        assert os.path.exists(tarball)203        with zipfile.ZipFile(tarball) as zf:204            assert sorted(zf.namelist()) == self._zip_created_files205 206    def test_make_zipfile_no_zlib(self):207        zipfile = pytest.importorskip('zipfile')208        patch(self, archive_util.zipfile, 'zlib', None)  # force zlib ImportError209 210        called = []211        zipfile_class = zipfile.ZipFile212 213        def fake_zipfile(*a, **kw):214            if kw.get('compression', None) == zipfile.ZIP_STORED:215                called.append((a, kw))216            return zipfile_class(*a, **kw)217 218        patch(self, archive_util.zipfile, 'ZipFile', fake_zipfile)219 220        # create something to tar and compress221        tmpdir = self._create_files()222        base_name = os.path.join(self.mkdtemp(), 'archive')223        with path.Path(tmpdir):224            make_zipfile(base_name, 'dist')225 226        tarball = base_name + '.zip'227        assert called == [((tarball, "w"), {'compression': zipfile.ZIP_STORED})]228        assert os.path.exists(tarball)229        with zipfile.ZipFile(tarball) as zf:230            assert sorted(zf.namelist()) == self._zip_created_files231 232    def test_check_archive_formats(self):233        assert check_archive_formats(['gztar', 'xxx', 'zip']) == 'xxx'234        assert (235            check_archive_formats(['gztar', 'bztar', 'xztar', 'ztar', 'tar', 'zip'])236            is None237        )238 239    def test_make_archive(self):240        tmpdir = self.mkdtemp()241        base_name = os.path.join(tmpdir, 'archive')242        with pytest.raises(ValueError):243            make_archive(base_name, 'xxx')244 245    def test_make_archive_cwd(self):246        current_dir = os.getcwd()247 248        def _breaks(*args, **kw):249            raise RuntimeError()250 251        ARCHIVE_FORMATS['xxx'] = (_breaks, [], 'xxx file')252        try:253            try:254                make_archive('xxx', 'xxx', root_dir=self.mkdtemp())255            except Exception:256                pass257            assert os.getcwd() == current_dir258        finally:259            ARCHIVE_FORMATS.pop('xxx')260 261    def test_make_archive_tar(self):262        base_dir = self._create_files()263        base_name = os.path.join(self.mkdtemp(), 'archive')264        res = make_archive(base_name, 'tar', base_dir, 'dist')265        assert os.path.exists(res)266        assert os.path.basename(res) == 'archive.tar'267        assert self._tarinfo(res) == self._created_files268 269    @pytest.mark.usefixtures('needs_zlib')270    def test_make_archive_gztar(self):271        base_dir = self._create_files()272        base_name = os.path.join(self.mkdtemp(), 'archive')273        res = make_archive(base_name, 'gztar', base_dir, 'dist')274        assert os.path.exists(res)275        assert os.path.basename(res) == 'archive.tar.gz'276        assert self._tarinfo(res) == self._created_files277 278    def test_make_archive_bztar(self):279        pytest.importorskip('bz2')280        base_dir = self._create_files()281        base_name = os.path.join(self.mkdtemp(), 'archive')282        res = make_archive(base_name, 'bztar', base_dir, 'dist')283        assert os.path.exists(res)284        assert os.path.basename(res) == 'archive.tar.bz2'285        assert self._tarinfo(res) == self._created_files286 287    def test_make_archive_xztar(self):288        pytest.importorskip('lzma')289        base_dir = self._create_files()290        base_name = os.path.join(self.mkdtemp(), 'archive')291        res = make_archive(base_name, 'xztar', base_dir, 'dist')292        assert os.path.exists(res)293        assert os.path.basename(res) == 'archive.tar.xz'294        assert self._tarinfo(res) == self._created_files295 296    def test_make_archive_owner_group(self):297        # testing make_archive with owner and group, with various combinations298        # this works even if there's not gid/uid support299        if UID_0_SUPPORT:300            group = grp.getgrgid(0)[0]301            owner = pwd.getpwuid(0)[0]302        else:303            group = owner = 'root'304 305        base_dir = self._create_files()306        root_dir = self.mkdtemp()307        base_name = os.path.join(self.mkdtemp(), 'archive')308        res = make_archive(309            base_name, 'zip', root_dir, base_dir, owner=owner, group=group310        )311        assert os.path.exists(res)312 313        res = make_archive(base_name, 'zip', root_dir, base_dir)314        assert os.path.exists(res)315 316        res = make_archive(317            base_name, 'tar', root_dir, base_dir, owner=owner, group=group318        )319        assert os.path.exists(res)320 321        res = make_archive(322            base_name, 'tar', root_dir, base_dir, owner='kjhkjhkjg', group='oihohoh'323        )324        assert os.path.exists(res)325 326    @pytest.mark.usefixtures('needs_zlib')327    @require_unix_id328    @require_uid_0329    def test_tarfile_root_owner(self):330        tmpdir = self._create_files()331        base_name = os.path.join(self.mkdtemp(), 'archive')332        old_dir = os.getcwd()333        os.chdir(tmpdir)334        group = grp.getgrgid(0)[0]335        owner = pwd.getpwuid(0)[0]336        try:337            archive_name = make_tarball(338                base_name, 'dist', compress=None, owner=owner, group=group339            )340        finally:341            os.chdir(old_dir)342 343        # check if the compressed tarball was created344        assert os.path.exists(archive_name)345 346        # now checks the rights347        archive = tarfile.open(archive_name)348        try:349            for member in archive.getmembers():350                assert member.uid == 0351                assert member.gid == 0352        finally:353            archive.close()354 
Aluode/PerceptionLabPortable · CoolFace