pep8-fied distutils.archive_util + added minimum test coverage

This commit is contained in:
Tarek Ziadé 2009-05-17 12:04:57 +00:00
parent 513a8b7d99
commit 6f826ed4c2
2 changed files with 109 additions and 39 deletions

View File

@ -11,15 +11,16 @@ from distutils.spawn import spawn
from distutils.dir_util import mkpath
from distutils import log
def make_tarball (base_name, base_dir, compress="gzip",
verbose=0, dry_run=0):
def make_tarball(base_name, base_dir, compress="gzip", verbose=0, dry_run=0):
"""Create a (possibly compressed) tar file from all the files under
'base_dir'. 'compress' must be "gzip" (the default), "compress",
"bzip2", or None. Both "tar" and the compression utility named by
'compress' must be on the default program search path, so this is
probably Unix-specific. The output tar file will be named 'base_dir' +
".tar", possibly plus the appropriate compression extension (".gz",
".bz2" or ".Z"). Return the output filename.
'base_dir'.
'compress' must be "gzip" (the default), "compress", "bzip2", or None.
Both "tar" and the compression utility named by 'compress' must be on
the default program search path, so this is probably Unix-specific.
The output tar file will be named 'base_dir' + ".tar", possibly plus
the appropriate compression extension (".gz", ".bz2" or ".Z").
Returns the output filename.
"""
# XXX GNU tar 1.13 has a nifty option to add a prefix directory.
# It's pretty new, though, so we certainly can't require it --
@ -27,7 +28,7 @@ def make_tarball (base_name, base_dir, compress="gzip",
# "create a tree of hardlinks" step! (Would also be nice to
# detect GNU tar to use its 'z' option and save a step.)
compress_ext = { 'gzip': ".gz",
compress_ext = {'gzip': ".gz",
'bzip2': '.bz2',
'compress': ".Z" }
@ -52,15 +53,14 @@ def make_tarball (base_name, base_dir, compress="gzip",
else:
return archive_name
# make_tarball ()
def make_zipfile(base_name, base_dir, verbose=0, dry_run=0):
"""Create a zip file from all the files under 'base_dir'.
def make_zipfile (base_name, base_dir, verbose=0, dry_run=0):
"""Create a zip file from all the files under 'base_dir'. The output
zip file will be named 'base_dir' + ".zip". Uses either the "zipfile"
Python module (if available) or the InfoZIP "zip" utility (if installed
and found on the default search path). If neither tool is available,
raises DistutilsExecError. Returns the name of the output zip file.
The output zip file will be named 'base_dir' + ".zip". Uses either the
"zipfile" Python module (if available) or the InfoZIP "zip" utility
(if installed and found on the default search path). If neither tool is
available, raises DistutilsExecError. Returns the name of the output zip
file.
"""
try:
import zipfile
@ -94,22 +94,19 @@ def make_zipfile (base_name, base_dir, verbose=0, dry_run=0):
zip_filename, base_dir)
if not dry_run:
z = zipfile.ZipFile(zip_filename, "w",
zip = zipfile.ZipFile(zip_filename, "w",
compression=zipfile.ZIP_DEFLATED)
for dirpath, dirnames, filenames in os.walk(base_dir):
for name in filenames:
path = os.path.normpath(os.path.join(dirpath, name))
if os.path.isfile(path):
z.write(path, path)
zip.write(path, path)
log.info("adding '%s'" % path)
z.close()
zip.close()
return zip_filename
# make_zipfile ()
ARCHIVE_FORMATS = {
'gztar': (make_tarball, [('compress', 'gzip')], "gzip'ed tar-file"),
'bztar': (make_tarball, [('compress', 'bzip2')], "bzip2'ed tar-file"),
@ -118,19 +115,24 @@ ARCHIVE_FORMATS = {
'zip': (make_zipfile, [],"ZIP file")
}
def check_archive_formats (formats):
def check_archive_formats(formats):
"""Returns the first format from the 'format' list that is unknown.
If all formats are known, returns None
"""
for format in formats:
if format not in ARCHIVE_FORMATS:
return format
else:
return None
def make_archive (base_name, format,
root_dir=None, base_dir=None,
verbose=0, dry_run=0):
"""Create an archive file (eg. zip or tar). 'base_name' is the name
of the file to create, minus any format-specific extension; 'format'
is the archive format: one of "zip", "tar", "ztar", or "gztar".
def make_archive(base_name, format, root_dir=None, base_dir=None, verbose=0,
dry_run=0):
"""Create an archive file (eg. zip or tar).
'base_name' is the name of the file to create, minus any format-specific
extension; 'format' is the archive format: one of "zip", "tar", "ztar",
or "gztar".
'root_dir' is a directory that will be the root directory of the
archive; ie. we typically chdir into 'root_dir' before creating the
archive. 'base_dir' is the directory where we start archiving from;
@ -148,7 +150,7 @@ def make_archive (base_name, format,
if base_dir is None:
base_dir = os.curdir
kwargs = { 'dry_run': dry_run }
kwargs = {'dry_run': dry_run}
try:
format_info = ARCHIVE_FORMATS[format]
@ -156,7 +158,7 @@ def make_archive (base_name, format,
raise ValueError, "unknown archive format '%s'" % format
func = format_info[0]
for (arg,val) in format_info[1]:
for arg, val in format_info[1]:
kwargs[arg] = val
filename = apply(func, (base_name, base_dir), kwargs)
@ -165,5 +167,3 @@ def make_archive (base_name, format,
os.chdir(save_cwd)
return filename
# make_archive ()

View File

@ -0,0 +1,70 @@
"""Tests for distutils.archive_util."""
__revision__ = "$Id:$"
import unittest
import os
from distutils.archive_util import (check_archive_formats, make_tarball,
make_zipfile, make_archive)
from distutils.spawn import find_executable
from distutils.tests import support
try:
import zipfile
ZIP_SUPPORT = True
except ImportError:
ZIP_SUPPORT = find_executable('zip')
class ArchiveUtilTestCase(support.TempdirManager,
unittest.TestCase):
@unittest.skipUnless(find_executable('tar'), 'Need the tar command to run')
def test_make_tarball(self):
# 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_tarball(base_name, tmpdir)
# check if the compressed tarball was created
tarball = base_name + '.tar.gz'
self.assert_(os.path.exists(tarball))
# trying an uncompressed one
base_name = os.path.join(tmpdir2, 'archive')
make_tarball(base_name, tmpdir, compress=None)
tarball = base_name + '.tar'
self.assert_(os.path.exists(tarball))
@unittest.skipUnless(ZIP_SUPPORT, 'Need zip support to run')
def test_make_tarball(self):
# 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):
self.assertEquals(check_archive_formats(['gztar', 'xxx', 'zip']),
'xxx')
self.assertEquals(check_archive_formats(['gztar', 'zip']), None)
def test_make_archive(self):
tmpdir = self.mkdtemp()
base_name = os.path.join(tmpdir, 'archive')
self.assertRaises(ValueError, make_archive, base_name, 'xxx')
def test_suite():
return unittest.makeSuite(ArchiveUtilTestCase)
if __name__ == "__main__":
unittest.main(defaultTest="test_suite")