[Python-checkins] distutils2: Move iglob into from distutils2.datafiles module into distutils2.util

tarek.ziade python-checkins at python.org
Wed Feb 16 22:23:58 CET 2011


tarek.ziade pushed cbbcc8b44d6d to distutils2:

http://hg.python.org/distutils2/rev/cbbcc8b44d6d
changeset:   1064:cbbcc8b44d6d
user:        Pierre-Yves David <pierre-yves.david at ens-lyon.org>
date:        Sun Jan 30 17:45:18 2011 +0100
summary:
  Move iglob into from distutils2.datafiles module into distutils2.util

files:
  distutils2/datafiles.py
  distutils2/tests/test_datafiles.py
  distutils2/tests/test_glob.py
  distutils2/util.py

diff --git a/distutils2/datafiles.py b/distutils2/datafiles.py
--- a/distutils2/datafiles.py
+++ b/distutils2/datafiles.py
@@ -1,6 +1,6 @@
 import os
 import re
-from glob import iglob as simple_iglob
+from distutils2.util import iglob
 
 __all__ = ['iglob', 'resources_dests']
 
@@ -25,34 +25,6 @@
             dest = os.path.join(destination, path_suffix)
             yield relpath, dest
 
-RICH_GLOB = re.compile(r'\{([^}]*)\}')
-
-# r'\\\{' match "\{"
-
-def iglob(path_glob):
-    rich_path_glob = RICH_GLOB.split(path_glob, 1)
-    if len(rich_path_glob) > 1:
-        assert len(rich_path_glob) == 3, rich_path_glob
-        prefix, set, suffix = rich_path_glob
-        for item in set.split(','):
-            for path in iglob( ''.join((prefix, item, suffix))):
-                yield path
-    else:
-        if '**' not in path_glob:
-            for item in simple_iglob(path_glob):
-                yield item
-        else:
-            prefix, radical = path_glob.split('**', 1)
-            if prefix == '':
-                prefix = '.'
-            if radical == '':
-                radical = '*'
-            else:
-                radical = radical.lstrip('/')
-            for (path, dir, files) in os.walk(prefix):
-                for file in iglob(os.path.join(prefix, path, radical)):
-                   yield os.path.join(prefix, file)
-
 def resources_dests(resources_dir, rules):
     destinations = {}
     for (base, suffix, glob_dest) in rules:
diff --git a/distutils2/tests/test_datafiles.py b/distutils2/tests/test_datafiles.py
--- a/distutils2/tests/test_datafiles.py
+++ b/distutils2/tests/test_datafiles.py
@@ -5,71 +5,55 @@
 from StringIO import StringIO
 
 from distutils2.tests import unittest, support, run_unittest
-from distutils2.datafiles import resources_dests, RICH_GLOB
+from distutils2.tests.test_glob import GlobTestCaseBase
+from distutils2.datafiles import resources_dests
 import re
 
 
 
 
-class DataFilesTestCase(support.TempdirManager,
-                            support.LoggingCatcher,
-                            unittest.TestCase):
+class DataFilesTestCase(GlobTestCaseBase):
 
-    def setUp(self):
-        super(DataFilesTestCase, self).setUp()
-        self.addCleanup(setattr, sys, 'stdout', sys.stdout)
-        self.addCleanup(setattr, sys, 'stderr', sys.stderr)
+    def assertRulesMatch(self, rules, spec):
+        tempdir = self.build_files_tree(spec)
+        expected = self.clean_tree(spec)
+        result = resources_dests(tempdir, rules)
+        self.assertEquals(expected, result)
 
-
-    def build_spec(self, spec, clean=True):
-        tempdir = self.mkdtemp()
-        for filepath in spec:
-            filepath = os.path.join(tempdir, *filepath.split('/'))
-            dirname = os.path.dirname(filepath)
-            if dirname and not os.path.exists(dirname):
-                os.makedirs(dirname)
-            self.write_file(filepath, 'babar')
-        if clean:
-            for key, value in list(spec.items()):
-                if value is None:
-                    del spec[key]
-        return tempdir
-
-    def assertFindGlob(self, rules, spec):
-        tempdir = self.build_spec(spec)
-        result = resources_dests(tempdir, rules)
-        self.assertEquals(spec, result)
-
-    def test_regex_rich_glob(self):
-        matches = RICH_GLOB.findall(r"babar aime les {fraises} est les {huitres}")
-        self.assertEquals(["fraises","huitres"], matches)
+    def clean_tree(self, spec):
+        files = {}
+        for path, value in spec.items():
+            if value is not None:
+                path = self.os_dependant_path(path)
+                files[path] = value
+        return files
 
     def test_simple_glob(self):
         rules = [('', '*.tpl', '{data}')]
         spec  = {'coucou.tpl': '{data}/coucou.tpl',
                  'Donotwant': None}
-        self.assertFindGlob(rules, spec)
+        self.assertRulesMatch(rules, spec)
 
     def test_multiple_match(self):
         rules = [('scripts', '*.bin', '{appdata}'),
                  ('scripts', '*', '{appscript}')]
         spec  = {'scripts/script.bin': '{appscript}/script.bin',
                  'Babarlikestrawberry': None}
-        self.assertFindGlob(rules, spec)
+        self.assertRulesMatch(rules, spec)
 
     def test_set_match(self):
         rules = [('scripts', '*.{bin,sh}', '{appscript}')]
         spec  = {'scripts/script.bin': '{appscript}/script.bin',
                  'scripts/babar.sh':  '{appscript}/babar.sh',
                  'Babarlikestrawberry': None}
-        self.assertFindGlob(rules, spec)
+        self.assertRulesMatch(rules, spec)
 
     def test_set_match_multiple(self):
         rules = [('scripts', 'script{s,}.{bin,sh}', '{appscript}')]
         spec  = {'scripts/scripts.bin': '{appscript}/scripts.bin',
                  'scripts/script.sh':  '{appscript}/script.sh',
                  'Babarlikestrawberry': None}
-        self.assertFindGlob(rules, spec)
+        self.assertRulesMatch(rules, spec)
 
     def test_set_match_exclude(self):
         rules = [('scripts', '*', '{appscript}'),
@@ -77,13 +61,13 @@
         spec  = {'scripts/scripts.bin': '{appscript}/scripts.bin',
                  'scripts/script.sh':  None,
                  'Babarlikestrawberry': None}
-        self.assertFindGlob(rules, spec)
+        self.assertRulesMatch(rules, spec)
 
     def test_glob_in_base(self):
         rules = [('scrip*', '*.bin', '{appscript}')]
         spec  = {'scripts/scripts.bin': '{appscript}/scripts.bin',
                  'Babarlikestrawberry': None}
-        tempdir = self.build_spec(spec)
+        tempdir = self.build_files_tree(spec)
         self.assertRaises(NotImplementedError, resources_dests, tempdir, rules)
 
     def test_recursive_glob(self):
@@ -92,7 +76,7 @@
                  'scripts/binary1.bin': '{binary}/scripts/binary1.bin',
                  'scripts/bin/binary2.bin': '{binary}/scripts/bin/binary2.bin',
                  'you/kill/pandabear.guy': None}
-        self.assertFindGlob(rules, spec)
+        self.assertRulesMatch(rules, spec)
 
     def test_final_exemple_glob(self):
         rules = [
@@ -118,7 +102,7 @@
             'developer-docs/api/toc.txt': '{doc}/developer-docs/api/toc.txt',
         }
         self.maxDiff = None
-        self.assertFindGlob(rules, spec)
+        self.assertRulesMatch(rules, spec)
 
 def test_suite():
     return unittest.makeSuite(DataFilesTestCase)
diff --git a/distutils2/tests/test_glob.py b/distutils2/tests/test_glob.py
new file mode 100644
--- /dev/null
+++ b/distutils2/tests/test_glob.py
@@ -0,0 +1,160 @@
+import os
+import sys
+import re
+from StringIO import StringIO
+
+from distutils2.tests import unittest, support, run_unittest
+from distutils2.util import iglob, RICH_GLOB
+
+class GlobTestCaseBase(support.TempdirManager,
+                       support.LoggingCatcher,
+                       unittest.TestCase):
+
+    def build_files_tree(self, files):
+        tempdir = self.mkdtemp()
+        for filepath in files:
+            is_dir = filepath.endswith('/')
+            filepath = os.path.join(tempdir, *filepath.split('/'))
+            if is_dir:
+                dirname = filepath
+            else:
+                dirname = os.path.dirname(filepath)
+            if dirname and not os.path.exists(dirname):
+                os.makedirs(dirname)
+            if not is_dir:
+                self.write_file(filepath, 'babar')
+        return tempdir
+
+    @staticmethod
+    def os_dependant_path(path):
+        path = path.rstrip('/').split('/')
+        return os.path.join(*path)
+
+    def clean_tree(self, spec):
+        files = []
+        for path, includes in list(spec.items()):
+            if includes:
+                files.append(self.os_dependant_path(path))
+        return files
+
+class GlobTestCase(GlobTestCaseBase):
+
+
+    def assertGlobMatch(self, glob, spec):
+        """"""
+        tempdir  = self.build_files_tree(spec)
+        expected = self.clean_tree(spec)
+        self.addCleanup(os.chdir, os.getcwd())
+        os.chdir(tempdir)
+        result = list(iglob(glob))
+        self.assertItemsEqual(expected, result)
+
+    def test_regex_rich_glob(self):
+        matches = RICH_GLOB.findall(r"babar aime les {fraises} est les {huitres}")
+        self.assertEquals(["fraises","huitres"], matches)
+
+    def test_simple_glob(self):
+        glob = '*.tp?'
+        spec  = {'coucou.tpl': True,
+                 'coucou.tpj': True,
+                 'Donotwant': False}
+        self.assertGlobMatch(glob, spec)
+
+    def test_simple_glob_in_dir(self):
+        glob = 'babar/*.tp?'
+        spec  = {'babar/coucou.tpl': True,
+                 'babar/coucou.tpj': True,
+                 'babar/toto.bin': False,
+                 'Donotwant': False}
+        self.assertGlobMatch(glob, spec)
+
+    def test_recursive_glob_head(self):
+        glob = '**/tip/*.t?l'
+        spec  = {'babar/zaza/zuzu/tip/coucou.tpl': True,
+                 'babar/z/tip/coucou.tpl': True,
+                 'babar/tip/coucou.tpl': True,
+                 'babar/zeop/tip/babar/babar.tpl': False,
+                 'babar/z/tip/coucou.bin': False,
+                 'babar/toto.bin': False,
+                 'zozo/zuzu/tip/babar.tpl': True,
+                 'zozo/tip/babar.tpl': True,
+                 'Donotwant': False}
+        self.assertGlobMatch(glob, spec)
+
+    def test_recursive_glob_tail(self):
+        glob = 'babar/**'
+        spec = {'babar/zaza/': True,
+                'babar/zaza/zuzu/': True,
+                'babar/zaza/zuzu/babar.xml': True,
+                'babar/zaza/zuzu/toto.xml': True,
+                'babar/zaza/zuzu/toto.csv': True,
+                'babar/zaza/coucou.tpl': True,
+                'babar/bubu.tpl': True,
+                'zozo/zuzu/tip/babar.tpl': False,
+                'zozo/tip/babar.tpl': False,
+                'Donotwant': False}
+        self.assertGlobMatch(glob, spec)
+
+    def test_recursive_glob_middle(self):
+        glob = 'babar/**/tip/*.t?l'
+        spec  = {'babar/zaza/zuzu/tip/coucou.tpl': True,
+                 'babar/z/tip/coucou.tpl': True,
+                 'babar/tip/coucou.tpl': True,
+                 'babar/zeop/tip/babar/babar.tpl': False,
+                 'babar/z/tip/coucou.bin': False,
+                 'babar/toto.bin': False,
+                 'zozo/zuzu/tip/babar.tpl': False,
+                 'zozo/tip/babar.tpl': False,
+                 'Donotwant': False}
+        self.assertGlobMatch(glob, spec)
+
+    def test_glob_set_tail(self):
+        glob = 'bin/*.{bin,sh,exe}'
+        spec  = {'bin/babar.bin': True,
+                 'bin/zephir.sh': True,
+                 'bin/celestine.exe': True,
+                 'bin/cornelius.bat': False,
+                 'bin/cornelius.xml': False,
+                 'toto/yurg': False,
+                 'Donotwant': False}
+        self.assertGlobMatch(glob, spec)
+
+    def test_glob_set_middle(self):
+        glob = 'xml/{babar,toto}.xml'
+        spec  = {'xml/babar.xml': True,
+                 'xml/toto.xml': True,
+                 'xml/babar.xslt': False,
+                 'xml/cornelius.sgml': False,
+                 'xml/zephir.xml': False,
+                 'toto/yurg.xml': False,
+                 'Donotwant': False}
+        self.assertGlobMatch(glob, spec)
+
+    def test_glob_set_head(self):
+        glob = '{xml,xslt}/babar.*'
+        spec  = {'xml/babar.xml': True,
+                 'xml/toto.xml': False,
+                 'xslt/babar.xslt': True,
+                 'xslt/toto.xslt': False,
+                 'toto/yurg.xml': False,
+                 'Donotwant': False}
+        self.assertGlobMatch(glob, spec)
+
+    def test_glob_all(self):
+        glob = '{xml/*,xslt/**}/babar.xml'
+        spec  = {'xml/a/babar.xml': True,
+                 'xml/b/babar.xml': True,
+                 'xml/a/c/babar.xml': False,
+                 'xslt/a/babar.xml': True,
+                 'xslt/b/babar.xml': True,
+                 'xslt/a/c/babar.xml': True,
+                 'toto/yurg.xml': False,
+                 'Donotwant': False}
+        self.assertGlobMatch(glob, spec)
+
+
+def test_suite():
+    return unittest.makeSuite(GlobTestCase)
+
+if __name__ == '__main__':
+    run_unittest(test_suite())
diff --git a/distutils2/util.py b/distutils2/util.py
--- a/distutils2/util.py
+++ b/distutils2/util.py
@@ -14,6 +14,7 @@
 import zipfile
 from copy import copy
 from fnmatch import fnmatchcase
+from glob import iglob as std_iglob
 from ConfigParser import RawConfigParser
 from inspect import getsource
 
@@ -1052,6 +1053,33 @@
         return run_2to3(files, doctests_only, self.fixer_names,
                         self.options, self.explicit)
 
+RICH_GLOB = re.compile(r'\{([^}]*)\}')
+def iglob(path_glob):
+    """Richer glob than the std glob module support ** and {opt1,opt2,opt3}"""
+    rich_path_glob = RICH_GLOB.split(path_glob, 1)
+    if len(rich_path_glob) > 1:
+        assert len(rich_path_glob) == 3, rich_path_glob
+        prefix, set, suffix = rich_path_glob
+        for item in set.split(','):
+            for path in iglob( ''.join((prefix, item, suffix))):
+                yield path
+    else:
+        if '**' not in path_glob:
+            for item in std_iglob(path_glob):
+                yield item
+        else:
+            prefix, radical = path_glob.split('**', 1)
+            if prefix == '':
+                prefix = '.'
+            if radical == '':
+                radical = '*'
+            else:
+                radical = radical.lstrip('/')
+            for (path, dir, files) in os.walk(prefix):
+                path = os.path.normpath(path)
+                for file in iglob(os.path.join(path, radical)):
+                   yield file
+
 
 def generate_distutils_kwargs_from_setup_cfg(file='setup.cfg'):
     """ Distutils2 to distutils1 compatibility util.

--
Repository URL: http://hg.python.org/distutils2


More information about the Python-checkins mailing list