2011-12-26 11:15:15 -04:00
|
|
|
# -*- coding: utf-8 -*-
|
2009-05-17 09:04:57 -03:00
|
|
|
"""Tests for distutils.archive_util."""
|
2009-05-17 11:59:05 -03:00
|
|
|
__revision__ = "$Id$"
|
2009-05-17 09:04:57 -03:00
|
|
|
|
|
|
|
import unittest
|
|
|
|
import os
|
2009-05-28 09:53:54 -03:00
|
|
|
import tarfile
|
2009-05-18 09:21:26 -03:00
|
|
|
from os.path import splitdrive
|
2009-05-28 09:53:54 -03:00
|
|
|
import warnings
|
2009-05-17 09:04:57 -03:00
|
|
|
|
|
|
|
from distutils.archive_util import (check_archive_formats, make_tarball,
|
2009-10-24 10:29:44 -03:00
|
|
|
make_zipfile, make_archive,
|
|
|
|
ARCHIVE_FORMATS)
|
2009-05-28 09:53:54 -03:00
|
|
|
from distutils.spawn import find_executable, spawn
|
2009-05-17 09:04:57 -03:00
|
|
|
from distutils.tests import support
|
Merged revisions 86236,86240,86332,86340,87271,87273,87447 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k
To comply with the 2.x doc style, the methods in trace.rst use brackets around
optional arguments. The rest is a mostly straight merge, modulo support changed
to test_support and use of the old super call style in test_tuple.
........
r86236 | eric.araujo | 2010-11-06 03:44:43 +0100 (sam., 06 nov. 2010) | 2 lines
Make sure each test can be run standalone (./python Lib/distutils/tests/x.py)
........
r86240 | eric.araujo | 2010-11-06 05:11:59 +0100 (sam., 06 nov. 2010) | 2 lines
Prevent ResourceWarnings in test_gettext
........
r86332 | eric.araujo | 2010-11-08 19:15:17 +0100 (lun., 08 nov. 2010) | 4 lines
Add missing NEWS entry for a fix committed by Senthil.
All recent modifications to distutils should now be covered in NEWS.
........
r86340 | eric.araujo | 2010-11-08 22:48:23 +0100 (lun., 08 nov. 2010) | 2 lines
This was actually fixed for the previous alpha.
........
r87271 | eric.araujo | 2010-12-15 20:09:58 +0100 (mer., 15 déc. 2010) | 2 lines
Improve trace documentation (#9264). Patch by Eli Bendersky.
........
r87273 | eric.araujo | 2010-12-15 20:30:15 +0100 (mer., 15 déc. 2010) | 2 lines
Use nested method directives, rewrap long lines, fix whitespace.
........
r87447 | eric.araujo | 2010-12-23 20:13:05 +0100 (jeu., 23 déc. 2010) | 2 lines
Fix typo in superclass method name
........
2011-02-02 20:12:18 -04:00
|
|
|
from test.test_support import check_warnings, run_unittest
|
2009-05-17 09:04:57 -03:00
|
|
|
|
2009-10-02 20:49:48 -03:00
|
|
|
try:
|
|
|
|
import grp
|
|
|
|
import pwd
|
|
|
|
UID_GID_SUPPORT = True
|
|
|
|
except ImportError:
|
|
|
|
UID_GID_SUPPORT = False
|
|
|
|
|
2009-05-17 09:04:57 -03:00
|
|
|
try:
|
|
|
|
import zipfile
|
|
|
|
ZIP_SUPPORT = True
|
|
|
|
except ImportError:
|
|
|
|
ZIP_SUPPORT = find_executable('zip')
|
|
|
|
|
2009-09-12 11:43:43 -03:00
|
|
|
# some tests will fail if zlib is not available
|
|
|
|
try:
|
|
|
|
import zlib
|
|
|
|
except ImportError:
|
|
|
|
zlib = None
|
|
|
|
|
|
|
|
|
2009-05-17 09:04:57 -03:00
|
|
|
class ArchiveUtilTestCase(support.TempdirManager,
|
2009-05-28 10:55:51 -03:00
|
|
|
support.LoggingSilencer,
|
2009-05-17 09:04:57 -03:00
|
|
|
unittest.TestCase):
|
|
|
|
|
2009-10-02 20:49:48 -03:00
|
|
|
@unittest.skipUnless(zlib, "requires zlib")
|
2009-05-17 09:04:57 -03:00
|
|
|
def test_make_tarball(self):
|
2011-12-26 11:15:15 -04:00
|
|
|
self._make_tarball('archive')
|
|
|
|
|
|
|
|
def _make_tarball(self, target_name):
|
2009-05-17 09:04:57 -03:00
|
|
|
# creating something to tar
|
|
|
|
tmpdir = self.mkdtemp()
|
|
|
|
self.write_file([tmpdir, 'file1'], 'xxx')
|
|
|
|
self.write_file([tmpdir, 'file2'], 'xxx')
|
2009-05-28 09:53:54 -03:00
|
|
|
os.mkdir(os.path.join(tmpdir, 'sub'))
|
|
|
|
self.write_file([tmpdir, 'sub', 'file3'], 'xxx')
|
2009-05-17 09:04:57 -03:00
|
|
|
|
|
|
|
tmpdir2 = self.mkdtemp()
|
2009-05-18 09:21:26 -03:00
|
|
|
unittest.skipUnless(splitdrive(tmpdir)[0] == splitdrive(tmpdir2)[0],
|
2009-10-02 20:49:48 -03:00
|
|
|
"source and target should be on same drive")
|
2009-05-18 09:21:26 -03:00
|
|
|
|
2011-12-26 11:15:15 -04:00
|
|
|
base_name = os.path.join(tmpdir2, target_name)
|
2009-05-18 05:20:55 -03:00
|
|
|
|
|
|
|
# working with relative paths to avoid tar warnings
|
|
|
|
old_dir = os.getcwd()
|
|
|
|
os.chdir(tmpdir)
|
|
|
|
try:
|
2009-05-18 09:21:26 -03:00
|
|
|
make_tarball(splitdrive(base_name)[1], '.')
|
2009-05-18 05:20:55 -03:00
|
|
|
finally:
|
|
|
|
os.chdir(old_dir)
|
2009-05-17 09:04:57 -03:00
|
|
|
|
|
|
|
# check if the compressed tarball was created
|
|
|
|
tarball = base_name + '.tar.gz'
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(os.path.exists(tarball))
|
2009-05-17 09:04:57 -03:00
|
|
|
|
|
|
|
# trying an uncompressed one
|
2011-12-26 11:15:15 -04:00
|
|
|
base_name = os.path.join(tmpdir2, target_name)
|
2009-05-18 05:20:55 -03:00
|
|
|
old_dir = os.getcwd()
|
|
|
|
os.chdir(tmpdir)
|
|
|
|
try:
|
2009-05-18 09:21:26 -03:00
|
|
|
make_tarball(splitdrive(base_name)[1], '.', compress=None)
|
2009-05-18 05:20:55 -03:00
|
|
|
finally:
|
|
|
|
os.chdir(old_dir)
|
2009-05-17 09:04:57 -03:00
|
|
|
tarball = base_name + '.tar'
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(os.path.exists(tarball))
|
2009-05-17 09:04:57 -03:00
|
|
|
|
2009-05-28 09:53:54 -03:00
|
|
|
def _tarinfo(self, path):
|
|
|
|
tar = tarfile.open(path)
|
|
|
|
try:
|
|
|
|
names = tar.getnames()
|
|
|
|
names.sort()
|
|
|
|
return tuple(names)
|
|
|
|
finally:
|
|
|
|
tar.close()
|
|
|
|
|
|
|
|
def _create_files(self):
|
|
|
|
# creating something to tar
|
|
|
|
tmpdir = self.mkdtemp()
|
|
|
|
dist = os.path.join(tmpdir, 'dist')
|
|
|
|
os.mkdir(dist)
|
|
|
|
self.write_file([dist, 'file1'], 'xxx')
|
|
|
|
self.write_file([dist, 'file2'], 'xxx')
|
|
|
|
os.mkdir(os.path.join(dist, 'sub'))
|
|
|
|
self.write_file([dist, 'sub', 'file3'], 'xxx')
|
|
|
|
os.mkdir(os.path.join(dist, 'sub2'))
|
|
|
|
tmpdir2 = self.mkdtemp()
|
|
|
|
base_name = os.path.join(tmpdir2, 'archive')
|
|
|
|
return tmpdir, tmpdir2, base_name
|
|
|
|
|
2009-09-12 11:43:43 -03:00
|
|
|
@unittest.skipUnless(zlib, "Requires zlib")
|
2009-05-28 10:55:51 -03:00
|
|
|
@unittest.skipUnless(find_executable('tar') and find_executable('gzip'),
|
|
|
|
'Need the tar command to run')
|
2009-05-28 09:53:54 -03:00
|
|
|
def test_tarfile_vs_tar(self):
|
|
|
|
tmpdir, tmpdir2, base_name = self._create_files()
|
|
|
|
old_dir = os.getcwd()
|
|
|
|
os.chdir(tmpdir)
|
|
|
|
try:
|
|
|
|
make_tarball(base_name, 'dist')
|
|
|
|
finally:
|
|
|
|
os.chdir(old_dir)
|
|
|
|
|
|
|
|
# check if the compressed tarball was created
|
|
|
|
tarball = base_name + '.tar.gz'
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(os.path.exists(tarball))
|
2009-05-28 09:53:54 -03:00
|
|
|
|
|
|
|
# now create another tarball using `tar`
|
|
|
|
tarball2 = os.path.join(tmpdir, 'archive2.tar.gz')
|
2009-05-28 10:55:51 -03:00
|
|
|
tar_cmd = ['tar', '-cf', 'archive2.tar', 'dist']
|
|
|
|
gzip_cmd = ['gzip', '-f9', 'archive2.tar']
|
2009-05-28 09:53:54 -03:00
|
|
|
old_dir = os.getcwd()
|
|
|
|
os.chdir(tmpdir)
|
|
|
|
try:
|
2009-05-28 10:55:51 -03:00
|
|
|
spawn(tar_cmd)
|
|
|
|
spawn(gzip_cmd)
|
2009-05-28 09:53:54 -03:00
|
|
|
finally:
|
|
|
|
os.chdir(old_dir)
|
|
|
|
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(os.path.exists(tarball2))
|
2009-05-28 09:53:54 -03:00
|
|
|
# let's compare both tarballs
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(self._tarinfo(tarball), self._tarinfo(tarball2))
|
2009-05-28 09:53:54 -03:00
|
|
|
|
|
|
|
# trying an uncompressed one
|
|
|
|
base_name = os.path.join(tmpdir2, 'archive')
|
|
|
|
old_dir = os.getcwd()
|
|
|
|
os.chdir(tmpdir)
|
|
|
|
try:
|
|
|
|
make_tarball(base_name, 'dist', compress=None)
|
|
|
|
finally:
|
|
|
|
os.chdir(old_dir)
|
|
|
|
tarball = base_name + '.tar'
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(os.path.exists(tarball))
|
2009-05-28 09:53:54 -03:00
|
|
|
|
|
|
|
# now for a dry_run
|
|
|
|
base_name = os.path.join(tmpdir2, 'archive')
|
|
|
|
old_dir = os.getcwd()
|
|
|
|
os.chdir(tmpdir)
|
|
|
|
try:
|
|
|
|
make_tarball(base_name, 'dist', compress=None, dry_run=True)
|
|
|
|
finally:
|
|
|
|
os.chdir(old_dir)
|
|
|
|
tarball = base_name + '.tar'
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(os.path.exists(tarball))
|
2009-05-28 09:53:54 -03:00
|
|
|
|
|
|
|
@unittest.skipUnless(find_executable('compress'),
|
|
|
|
'The compress program is required')
|
|
|
|
def test_compress_deprecated(self):
|
|
|
|
tmpdir, tmpdir2, base_name = self._create_files()
|
|
|
|
|
|
|
|
# using compress and testing the PendingDeprecationWarning
|
|
|
|
old_dir = os.getcwd()
|
|
|
|
os.chdir(tmpdir)
|
|
|
|
try:
|
|
|
|
with check_warnings() as w:
|
|
|
|
warnings.simplefilter("always")
|
|
|
|
make_tarball(base_name, 'dist', compress='compress')
|
|
|
|
finally:
|
|
|
|
os.chdir(old_dir)
|
|
|
|
tarball = base_name + '.tar.Z'
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(os.path.exists(tarball))
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(len(w.warnings), 1)
|
2009-05-28 09:53:54 -03:00
|
|
|
|
|
|
|
# same test with dry_run
|
|
|
|
os.remove(tarball)
|
|
|
|
old_dir = os.getcwd()
|
|
|
|
os.chdir(tmpdir)
|
|
|
|
try:
|
|
|
|
with check_warnings() as w:
|
|
|
|
warnings.simplefilter("always")
|
|
|
|
make_tarball(base_name, 'dist', compress='compress',
|
|
|
|
dry_run=True)
|
|
|
|
finally:
|
|
|
|
os.chdir(old_dir)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(not os.path.exists(tarball))
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(len(w.warnings), 1)
|
2009-05-28 09:53:54 -03:00
|
|
|
|
2009-09-12 11:43:43 -03:00
|
|
|
@unittest.skipUnless(zlib, "Requires zlib")
|
2009-05-17 09:04:57 -03:00
|
|
|
@unittest.skipUnless(ZIP_SUPPORT, 'Need zip support to run')
|
2009-05-17 11:59:05 -03:00
|
|
|
def test_make_zipfile(self):
|
2009-05-17 09:04:57 -03:00
|
|
|
# creating something to tar
|
|
|
|
tmpdir = self.mkdtemp()
|
|
|
|
self.write_file([tmpdir, 'file1'], 'xxx')
|
|
|
|
self.write_file([tmpdir, 'file2'], 'xxx')
|
|
|
|
|
|
|
|
tmpdir2 = self.mkdtemp()
|
|
|
|
base_name = os.path.join(tmpdir2, 'archive')
|
|
|
|
make_zipfile(base_name, tmpdir)
|
|
|
|
|
|
|
|
# check if the compressed tarball was created
|
|
|
|
tarball = base_name + '.zip'
|
|
|
|
|
|
|
|
def test_check_archive_formats(self):
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(check_archive_formats(['gztar', 'xxx', 'zip']),
|
|
|
|
'xxx')
|
|
|
|
self.assertEqual(check_archive_formats(['gztar', 'zip']), None)
|
2009-05-17 09:04:57 -03:00
|
|
|
|
|
|
|
def test_make_archive(self):
|
|
|
|
tmpdir = self.mkdtemp()
|
|
|
|
base_name = os.path.join(tmpdir, 'archive')
|
|
|
|
self.assertRaises(ValueError, make_archive, base_name, 'xxx')
|
|
|
|
|
2009-10-16 20:04:16 -03:00
|
|
|
@unittest.skipUnless(zlib, "Requires zlib")
|
2009-10-02 20:49:48 -03:00
|
|
|
def test_make_archive_owner_group(self):
|
|
|
|
# testing make_archive with owner and group, with various combinations
|
|
|
|
# this works even if there's not gid/uid support
|
|
|
|
if UID_GID_SUPPORT:
|
|
|
|
group = grp.getgrgid(0)[0]
|
|
|
|
owner = pwd.getpwuid(0)[0]
|
|
|
|
else:
|
|
|
|
group = owner = 'root'
|
|
|
|
|
|
|
|
base_dir, root_dir, base_name = self._create_files()
|
|
|
|
base_name = os.path.join(self.mkdtemp() , 'archive')
|
|
|
|
res = make_archive(base_name, 'zip', root_dir, base_dir, owner=owner,
|
|
|
|
group=group)
|
|
|
|
self.assertTrue(os.path.exists(res))
|
|
|
|
|
|
|
|
res = make_archive(base_name, 'zip', root_dir, base_dir)
|
|
|
|
self.assertTrue(os.path.exists(res))
|
|
|
|
|
|
|
|
res = make_archive(base_name, 'tar', root_dir, base_dir,
|
|
|
|
owner=owner, group=group)
|
|
|
|
self.assertTrue(os.path.exists(res))
|
|
|
|
|
|
|
|
res = make_archive(base_name, 'tar', root_dir, base_dir,
|
|
|
|
owner='kjhkjhkjg', group='oihohoh')
|
|
|
|
self.assertTrue(os.path.exists(res))
|
|
|
|
|
|
|
|
@unittest.skipUnless(zlib, "Requires zlib")
|
|
|
|
@unittest.skipUnless(UID_GID_SUPPORT, "Requires grp and pwd support")
|
|
|
|
def test_tarfile_root_owner(self):
|
|
|
|
tmpdir, tmpdir2, base_name = self._create_files()
|
|
|
|
old_dir = os.getcwd()
|
|
|
|
os.chdir(tmpdir)
|
|
|
|
group = grp.getgrgid(0)[0]
|
|
|
|
owner = pwd.getpwuid(0)[0]
|
|
|
|
try:
|
|
|
|
archive_name = make_tarball(base_name, 'dist', compress=None,
|
|
|
|
owner=owner, group=group)
|
|
|
|
finally:
|
|
|
|
os.chdir(old_dir)
|
|
|
|
|
|
|
|
# check if the compressed tarball was created
|
|
|
|
self.assertTrue(os.path.exists(archive_name))
|
|
|
|
|
|
|
|
# now checks the rights
|
|
|
|
archive = tarfile.open(archive_name)
|
|
|
|
try:
|
|
|
|
for member in archive.getmembers():
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(member.uid, 0)
|
|
|
|
self.assertEqual(member.gid, 0)
|
2009-10-02 20:49:48 -03:00
|
|
|
finally:
|
|
|
|
archive.close()
|
|
|
|
|
2009-10-24 10:29:44 -03:00
|
|
|
def test_make_archive_cwd(self):
|
|
|
|
current_dir = os.getcwd()
|
|
|
|
def _breaks(*args, **kw):
|
|
|
|
raise RuntimeError()
|
|
|
|
ARCHIVE_FORMATS['xxx'] = (_breaks, [], 'xxx file')
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
make_archive('xxx', 'xxx', root_dir=self.mkdtemp())
|
|
|
|
except:
|
|
|
|
pass
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(os.getcwd(), current_dir)
|
2009-10-24 10:29:44 -03:00
|
|
|
finally:
|
|
|
|
del ARCHIVE_FORMATS['xxx']
|
|
|
|
|
2011-12-26 11:15:15 -04:00
|
|
|
@unittest.skipUnless(zlib, "requires zlib")
|
|
|
|
def test_make_tarball_unicode(self):
|
|
|
|
"""
|
|
|
|
Mirror test_make_tarball, except filename is unicode.
|
|
|
|
"""
|
|
|
|
self._make_tarball(u'archive')
|
|
|
|
|
|
|
|
@unittest.skipUnless(zlib, "requires zlib")
|
|
|
|
def test_make_tarball_unicode_latin1(self):
|
|
|
|
"""
|
|
|
|
Mirror test_make_tarball, except filename is unicode and contains
|
|
|
|
latin characters.
|
|
|
|
"""
|
|
|
|
self._make_tarball(u'årchiv') # note this isn't a real word
|
|
|
|
|
|
|
|
@unittest.skipUnless(zlib, "requires zlib")
|
|
|
|
def test_make_tarball_unicode_extended(self):
|
|
|
|
"""
|
|
|
|
Mirror test_make_tarball, except filename is unicode and contains
|
|
|
|
characters outside the latin charset.
|
|
|
|
"""
|
|
|
|
self._make_tarball(u'のアーカイブ') # japanese for archive
|
|
|
|
|
2009-05-17 09:04:57 -03:00
|
|
|
def test_suite():
|
|
|
|
return unittest.makeSuite(ArchiveUtilTestCase)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
Merged revisions 86236,86240,86332,86340,87271,87273,87447 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k
To comply with the 2.x doc style, the methods in trace.rst use brackets around
optional arguments. The rest is a mostly straight merge, modulo support changed
to test_support and use of the old super call style in test_tuple.
........
r86236 | eric.araujo | 2010-11-06 03:44:43 +0100 (sam., 06 nov. 2010) | 2 lines
Make sure each test can be run standalone (./python Lib/distutils/tests/x.py)
........
r86240 | eric.araujo | 2010-11-06 05:11:59 +0100 (sam., 06 nov. 2010) | 2 lines
Prevent ResourceWarnings in test_gettext
........
r86332 | eric.araujo | 2010-11-08 19:15:17 +0100 (lun., 08 nov. 2010) | 4 lines
Add missing NEWS entry for a fix committed by Senthil.
All recent modifications to distutils should now be covered in NEWS.
........
r86340 | eric.araujo | 2010-11-08 22:48:23 +0100 (lun., 08 nov. 2010) | 2 lines
This was actually fixed for the previous alpha.
........
r87271 | eric.araujo | 2010-12-15 20:09:58 +0100 (mer., 15 déc. 2010) | 2 lines
Improve trace documentation (#9264). Patch by Eli Bendersky.
........
r87273 | eric.araujo | 2010-12-15 20:30:15 +0100 (mer., 15 déc. 2010) | 2 lines
Use nested method directives, rewrap long lines, fix whitespace.
........
r87447 | eric.araujo | 2010-12-23 20:13:05 +0100 (jeu., 23 déc. 2010) | 2 lines
Fix typo in superclass method name
........
2011-02-02 20:12:18 -04:00
|
|
|
run_unittest(test_suite())
|