changeset 169:261b75d7b972

Extend listdir_abs with an optional glob pattern
author Bernhard Herzog <bh@intevation.de>
date Fri, 20 Jun 2008 15:51:24 +0000
parents be07b88b44ea
children bfcb2bbf9a52
files test/test_util.py treepkg/util.py
diffstat 2 files changed, 35 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/test/test_util.py	Fri Jun 20 15:19:03 2008 +0000
+++ b/test/test_util.py	Fri Jun 20 15:51:24 2008 +0000
@@ -7,11 +7,12 @@
 
 """Tests for the treepkg.util module"""
 
+import os
 import unittest
 
 from filesupport import FileTestMixin
 
-from treepkg.util import replace_in_file
+from treepkg.util import replace_in_file, listdir_abs
 
 
 class TestReplaceInFile(unittest.TestCase, FileTestMixin):
@@ -48,3 +49,26 @@
                     "and more filler")
         self.runtest(template % dict(rev=0), template % dict(rev=0),
                      r"0\.9-svn0", "1.0-svn321")
+
+
+class TestListDirAbs(unittest.TestCase, FileTestMixin):
+
+    def setUp(self):
+        self.directory = self.create_temp_dir(self.id())
+
+    def test_listdir_abs(self):
+        self.create_files(self.directory, [("foo.orig.tgz", ""),
+                                           ("foo.dsc", ""),
+                                           ("foo.diff.gz", ""),])
+        self.assertEquals(sorted(listdir_abs(self.directory)),
+                          sorted([os.path.join(self.directory, d)
+                                  for d in ["foo.orig.tgz", "foo.dsc",
+                                            "foo.diff.gz"]]))
+
+    def test_listdir_abs_pattern(self):
+        self.create_files(self.directory, [("foo.orig.tgz", ""),
+                                           ("foo.dsc", ""),
+                                           ("foo.diff.gz", ""),])
+        self.assertEquals(sorted(listdir_abs(self.directory, '*.dsc')),
+                          [os.path.join(self.directory, "foo.dsc")])
+
--- a/treepkg/util.py	Fri Jun 20 15:19:03 2008 +0000
+++ b/treepkg/util.py	Fri Jun 20 15:51:24 2008 +0000
@@ -11,6 +11,7 @@
 import re
 import tempfile
 import shutil
+import fnmatch
 
 import run
 
@@ -51,9 +52,15 @@
     if not os.path.isdir(directory):
         os.makedirs(directory)
 
-def listdir_abs(directory):
-    """Like os.listdir, but returns a list of absolute pathnames"""
-    return [os.path.join(directory, d) for d in os.listdir(directory)]
+def listdir_abs(directory, pattern=None):
+    """Like os.listdir, but returns a list of absolute pathnames.
+    Optionally, a glob pattern can be given to restrict the names
+    returned by the function.
+    """
+    filenames = os.listdir(directory)
+    if pattern is not None:
+        filenames = fnmatch.filter(filenames, pattern)
+    return [os.path.join(directory, filename) for filename in filenames]
 
 def copytree(src, dst, symlinks=False):
     """Recursively copy a directory tree using copy2().
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)