[Treepkg-commits] r94 - in trunk: test treepkg

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Fri Jun 20 17:51:24 CEST 2008


Author: bh
Date: 2008-06-20 17:51:24 +0200 (Fri, 20 Jun 2008)
New Revision: 94

Modified:
   trunk/test/test_util.py
   trunk/treepkg/util.py
Log:
Extend listdir_abs with an optional glob pattern


Modified: trunk/test/test_util.py
===================================================================
--- trunk/test/test_util.py	2008-06-20 15:19:03 UTC (rev 93)
+++ trunk/test/test_util.py	2008-06-20 15:51:24 UTC (rev 94)
@@ -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")])
+

Modified: trunk/treepkg/util.py
===================================================================
--- trunk/treepkg/util.py	2008-06-20 15:19:03 UTC (rev 93)
+++ trunk/treepkg/util.py	2008-06-20 15:51:24 UTC (rev 94)
@@ -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().



More information about the Treepkg-commits mailing list