[Python-checkins] distutils2: [datafiles] implement suffix in resources_dests

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


tarek.ziade pushed 045d9c2d723a to distutils2:

http://hg.python.org/distutils2/rev/045d9c2d723a
changeset:   1032:045d9c2d723a
user:        Pierre-Yves David <pierre-yves.david at ens-lyon.org>
date:        Fri Jan 28 12:59:54 2011 +0100
summary:
  [datafiles] implement suffix in resources_dests

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

diff --git a/distutils2/datafiles.py b/distutils2/datafiles.py
--- a/distutils2/datafiles.py
+++ b/distutils2/datafiles.py
@@ -5,25 +5,28 @@
 
 class SmartGlob(object):
 
-    def __init__(self, path_glob):
-        self.path_glob = path_glob
-        if '**' in path_glob:
-            self.base = path_glob.split('**', 1)[0] # XXX not exactly what we want
-        else:
-            self.base = osp.dirname(path_glob)
+    def __init__(self, base, suffix):
+        self.base = base
+        self.suffix = suffix
 
 
     def expand(self, basepath, category):
-        for file in glob(osp.join(basepath, self.path_glob)):
-            file = file[len(basepath):].lstrip('/')
-            suffix = file[len(self.base):].lstrip('/')
-            yield file, osp.join(category, suffix)
+        if self.base:
+            base = osp.join(basepath, self.base)
+        else:
+            base = basepath
+        absglob = osp.join(base, self.suffix)
+        for file in glob(absglob):
+            path_suffix = file[len(base):].lstrip('/')
+            relpath = file[len(basepath):].lstrip('/')
+            yield relpath, osp.join(category, path_suffix)
 
 def glob(path_glob):
     if '**' in path_glob:
-        return rglob(path_glob)
+        files = rglob(path_glob)
     else:
-        return simple_glob(path_glob)
+        files = simple_glob(path_glob)
+    return files
 
 def rglob(path_glob):
     prefix, radical = path_glob.split('**', 1)
@@ -37,13 +40,12 @@
     for (path, dir, files) in os.walk(prefix):
         for file in glob(osp.join(prefix, path, radical)):
            glob_files.append(os.path.join(prefix, file))
-
     return glob_files
 
 def resources_dests(resources_dir, rules):
     destinations = {}
-    for (path_glob, glob_dest) in rules:
-        sglob = SmartGlob(path_glob)
+    for (base, suffix, glob_dest) in rules:
+        sglob = SmartGlob(base, suffix)
         for file, file_dest in sglob.expand(resources_dir, glob_dest):
             destinations[file] = file_dest
     return destinations
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
@@ -12,7 +12,7 @@
 
 
 
-SLASH = re.compile(r'[^\\](?:\\{2})* ')
+SLASH = re.compile(r'(?<=[^\\])(?:\\{2})*/')
 
 class DataFilesTestCase(support.TempdirManager,
                             support.LoggingCatcher,
@@ -31,37 +31,43 @@
             if dirname and not osp.exists(dirname):
                 os.makedirs(dirname)
             self.write_file(filepath, 'babar')
+        for key, value in list(spec.items()):
+            if value is None:
+                del spec[key]
         result = resources_dests(tempdir, rules)
         self.assertEquals(spec, result)
 
     def test_simple_glob(self):
-        rules = [('*.tpl', '{data}')]
-        spec  = {'coucou.tpl': '{data}/coucou.tpl'}
+        rules = [('', '*.tpl', '{data}')]
+        spec  = {'coucou.tpl': '{data}/coucou.tpl',
+                 'Donotwant': None}
         self.assertFindGlob(rules, spec)
 
     def test_multiple_match(self):
-        rules = [('scripts/*.bin', '{appdata}'),
-                 ('scripts/*', '{appscript}')]
-        spec  = {'scripts/script.bin': '{appscript}/script.bin'}
+        rules = [('scripts', '*.bin', '{appdata}'),
+                 ('scripts', '*', '{appscript}')]
+        spec  = {'scripts/script.bin': '{appscript}/script.bin',
+                 'Babarlikestrawberry': None}
         self.assertFindGlob(rules, spec)
 
     def test_recursive_glob(self):
-        rules = [('**/*.bin', '{binary}')]
+        rules = [('', '**/*.bin', '{binary}')]
         spec  = {'binary0.bin': '{binary}/binary0.bin',
                  'scripts/binary1.bin': '{binary}/scripts/binary1.bin',
-                 'scripts/bin/binary2.bin': '{binary}/scripts/bin/binary2.bin'}
+                 'scripts/bin/binary2.bin': '{binary}/scripts/bin/binary2.bin',
+                 'you/kill/pandabear.guy': None}
         self.assertFindGlob(rules, spec)
 
     def test_final_exemple_glob(self):
         rules = [
-            ('mailman/database/schemas/*', '{appdata}/schemas'),
-            ('**/*.tpl', '{appdata}/templates'),
-            ('developer-docs/**/*.txt', '{doc}'),
-            ('README', '{doc}'),
-            ('mailman/etc/*', '{config}'),
-            ('mailman/foo/**/bar/*.cfg', '{config}/baz'),
-            ('mailman/foo/**/*.cfg', '{config}/hmm'),
-            ('some-new-semantic.sns', '{funky-crazy-category}')
+            ('mailman/database/schemas/','*', '{appdata}/schemas'),
+            ('', '**/*.tpl', '{appdata}/templates'),
+            ('developer-docs/', '**/*.txt', '{doc}'),
+            ('', 'README', '{doc}'),
+            ('mailman/etc/', '*', '{config}'),
+            ('mailman/foo/', '**/bar/*.cfg', '{config}/baz'),
+            ('mailman/foo/', '**/*.cfg', '{config}/hmm'),
+            ('', 'some-new-semantic.sns', '{funky-crazy-category}')
         ]
         spec = {
             'README': '{doc}/README',
@@ -71,23 +77,12 @@
             'mailman/database/schemas/blah.schema': '{appdata}/schemas/blah.schema',
             'mailman/etc/my.cnf': '{config}/my.cnf',
             'mailman/foo/some/path/bar/my.cfg': '{config}/hmm/some/path/bar/my.cfg',
-            'mailman/foo/some/path/other.cfg': '{config}/hmm/some/path/bar/other.cfg',
+            'mailman/foo/some/path/other.cfg': '{config}/hmm/some/path/other.cfg',
             'developer-docs/index.txt': '{doc}/index.txt',
             'developer-docs/api/toc.txt': '{doc}/api/toc.txt',
         }
+        self.maxDiff = None
         self.assertFindGlob(rules, spec)
-        result = {
-            os.path.join('mailman', 'database', 'schemas', 'blah.schema') : set([resources[0][1]]),
-            'some.tpl' : set([resources[1][1]]),
-            os.path.join('developer-docs', 'index.txt') : set([resources[2][1]]),
-            os.path.join('developer-docs', 'api', 'toc.txt') : set([resources[2][1]]),
-            'README' : set([resources[3][1]]),
-            os.path.join('mailman', 'etc', 'my.cnf') : set([resources[4][1]]),
-            os.path.join('mailman', 'foo', 'some', 'path', 'bar', 'my.cfg') : set([resources[5][1], resources[6][1]]),
-            os.path.join('mailman', 'foo', 'some', 'path', 'other.cfg') : set([resources[6][1]]),
-            'some-new-semantic.sns' : set([resources[7][1]])
-        }
-        self.assertEquals(find_glob(resources), result)
 
 def test_suite():
     return unittest.makeSuite(DataFilesTestCase)

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


More information about the Python-checkins mailing list