diff options
author | Markus Lehtonen <markus.lehtonen@linux.intel.com> | 2015-02-04 15:41:01 +0200 |
---|---|---|
committer | Guido Günther <agx@sigxcpu.org> | 2015-08-19 15:33:46 +0200 |
commit | 5cab1bcbc12641a6404ddddea0e6594e3381a4ad (patch) | |
tree | bd097ea392cea95af779c18b129c7e7b7d8e798a /tests/testutils | |
parent | bf60f8a03b30be91b1256dd808db57452fe0c649 (diff) |
tests.testutils: add ls_dir(), ls_tar() and ls_zip()
New helper functions for listing the content of a directory, tarball and
a zip archive.
Signed-off-by: Markus Lehtonen <markus.lehtonen@linux.intel.com>
Diffstat (limited to 'tests/testutils')
-rw-r--r-- | tests/testutils/__init__.py | 38 |
1 files changed, 37 insertions, 1 deletions
diff --git a/tests/testutils/__init__.py b/tests/testutils/__init__.py index 3ead68ee..60765af4 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""" + 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): + """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): + """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) + |