[git-buildpackage] [PATCH 05/11] tests.testutils: add ls_dir(), ls_tar() and ls_zip()
Guido Günther
agx at sigxcpu.org
Thu Apr 2 08:04:51 CEST 2015
On Wed, Feb 04, 2015 at 03:41:01PM +0200, Markus Lehtonen wrote:
> New helper functions for listing the content of a directory, tarball and
> a zip archive.
>
> Signed-off-by: Markus Lehtonen <markus.lehtonen at linux.intel.com>
> ---
> tests/testutils/__init__.py | 38 +++++++++++++++++++++++++++++++++++++-
> 1 file changed, 37 insertions(+), 1 deletion(-)
>
> diff --git a/tests/testutils/__init__.py b/tests/testutils/__init__.py
> index 092d377..30de531 100644
> --- a/tests/testutils/__init__.py
> +++ b/tests/testutils/__init__.py
> @@ -5,7 +5,9 @@ from .. import context
> import os
> import shutil
> import subprocess
> +import tarfile
> import tempfile
> +import zipfile
>
> import gbp.log
> import gbp.errors
> @@ -16,7 +18,8 @@ from . debiangittestrepo import DebianGitTestRepo
> from . capture import capture_stderr
>
> __all__ = ['GbpLogTester', 'DebianGitTestRepo', 'OsReleaseFile',
> - 'MockedChangeLog', 'get_dch_default_urgency', 'capture_stderr' ]
> + 'MockedChangeLog', 'get_dch_default_urgency', 'capture_stderr',
> + 'ls_dir', 'ls_tar', 'ls_zip']
>
> class OsReleaseFile(object):
> """Repesents a simple file with key-value pairs"""
> @@ -85,3 +88,36 @@ def get_dch_default_urgency():
> shutil.rmtree(tempdir)
> return urgency
>
> +
> +def ls_dir(directory, directories=True):
> + """List the contents of directory, recurse to subdirectories"""
direcories paramter should be documented.
> + contents = set()
> + for root, dirs, files in os.walk(directory):
> + prefix = ''
> + if root != directory:
> + prefix = os.path.relpath(root, directory) + '/'
> + contents.update(['%s%s' % (prefix, fname) for fname in files])
> + if directories:
> + contents.update(['%s%s' % (prefix, dname) for dname in dirs])
> + return contents
> +
> +def ls_tar(tarball, directories=True):
directories parameter is unused. I assume directories are included by default?
> + """List the contents of tar archive"""
> + tmpdir = tempfile.mkdtemp()
> + try:
> + tarobj = tarfile.open(tarball, 'r')
> + tarobj.extractall(tmpdir)
> + return ls_dir(tmpdir, directories)
> + finally:
> + shutil.rmtree(tmpdir)
> +
> +def ls_zip(archive, directories=True):
Ditto.
> + """List the contents of zip file"""
> + tmpdir = tempfile.mkdtemp()
> + try:
> + zipobj = zipfile.ZipFile(archive, 'r')
> + zipobj.extractall(tmpdir)
> + return ls_dir(tmpdir, directories)
> + finally:
> + shutil.rmtree(tmpdir)
> +
Shouldn't we better make all three functions work the same way (either
all handling directories=... or none?
Cheers,
-- Guido
More information about the git-buildpackage
mailing list