diff options
author | Adrien Di Mascio <Adrien.DiMascio@logilab.fr> | 2008-11-13 11:09:19 +0100 |
---|---|---|
committer | Adrien Di Mascio <Adrien.DiMascio@logilab.fr> | 2008-11-13 11:09:19 +0100 |
commit | 75f1b62191d9f294b6c518b482ec050feae05140 (patch) | |
tree | a1501b1b5b519dd2832f3db8412150fdf2707097 | |
parent | f3fa43de6a1f1e169abaec895c054c4602d90afb (diff) | |
download | logilab-common-75f1b62191d9f294b6c518b482ec050feae05140.tar.gz |
introduce shellutils.globfind as an alternative to find that accepts glob patterns (easier to have separate function for backward compatibility reasons)
-rw-r--r-- | shellutils.py | 33 | ||||
-rw-r--r-- | test/unittest_shellutils.py | 21 |
2 files changed, 47 insertions, 7 deletions
diff --git a/shellutils.py b/shellutils.py index f6072f1..6a7509d 100644 --- a/shellutils.py +++ b/shellutils.py @@ -8,12 +8,13 @@ scripts. """ __docformat__ = "restructuredtext en" -import os +import os import glob import shutil import sys import tempfile import time +import fnmatch from os.path import exists, isdir, islink, basename, join, walk from logilab.common import STD_BLACKLIST @@ -141,6 +142,35 @@ def find(directory, exts, exclude=False, blacklist=STD_BLACKLIST): return files +def globfind(directory, pattern, blacklist=STD_BLACKLIST): + """Recursively finds files matching glob `pattern` under `directory`. + + This is an alternative to `logilab.common.shellutils.find`. + + :type directory: str + :param directory: + directory where the search should start + + :type pattern: basestring + :param pattern: + the glob pattern (e.g *.py, foo*.py, etc.) + + :type blacklist: list or tuple + :param blacklist: + optional list of files or directory to ignore, default to the value of + `logilab.common.STD_BLACKLIST` + + :rtype: iterator + :return: + iterator over the list of all matching files + """ + for curdir, dirnames, filenames in os.walk(directory): + for fname in fnmatch.filter(filenames, pattern): + yield join(curdir, fname) + for skipped in blacklist: + if skipped in dirnames: + dirnames.remove(skipped) + def unzip(archive, destdir): import zipfile if not exists(destdir): @@ -154,7 +184,6 @@ def unzip(archive, destdir): outfile.write(zfobj.read(name)) outfile.close() - class Execute: """This is a deadlock safe version of popen2 (no stdin), that returns an object with errorlevel, out and err. diff --git a/test/unittest_shellutils.py b/test/unittest_shellutils.py index 5e63170..396d7c6 100644 --- a/test/unittest_shellutils.py +++ b/test/unittest_shellutils.py @@ -5,7 +5,7 @@ from os.path import join from logilab.common.testlib import TestCase, unittest_main -from logilab.common.shellutils import find, ProgressBar +from logilab.common.shellutils import globfind, find, ProgressBar from StringIO import StringIO DATA_DIR = join('data','find_test') @@ -36,10 +36,21 @@ class FindTC(TestCase): join('sub', 'doc.txt'), 'write_protected_file.txt', ]])) - -# def test_exclude_base_dir(self): -# self.assertEquals(files_by_ext(DATA_DIR, include_exts=('.py',), exclude_dirs=(DATA_DIR,)), -# []) + + def test_globfind(self): + files = set(globfind(DATA_DIR, '*.py')) + self.assertSetEqual(files, + set([join(DATA_DIR, f) for f in ['__init__.py', 'module.py', + 'module2.py', 'noendingnewline.py', + 'nonregr.py', join('sub', 'momo.py')]])) + files = set(globfind(DATA_DIR, 'mo*.py')) + self.assertSetEqual(files, + set([join(DATA_DIR, f) for f in ['module.py', 'module2.py', + join('sub', 'momo.py')]])) + files = set(globfind(DATA_DIR, 'mo*.py', blacklist=('sub',))) + self.assertSetEqual(files, + set([join(DATA_DIR, f) for f in ['module.py', 'module2.py']])) + class ProgressBarTC(TestCase): def test_refresh(self): |