[Python-checkins] distutils2: merge
tarek.ziade
python-checkins at python.org
Wed Feb 16 22:23:57 CET 2011
tarek.ziade pushed 7f6a4b4b6e7b to distutils2:
http://hg.python.org/distutils2/rev/7f6a4b4b6e7b
changeset: 1039:7f6a4b4b6e7b
parent: 1038:f1c840b37d7a
parent: 1036:9d8967f87a31
user: FELD Boris <lothiraldan at gmail.com>
date: Sat Jan 29 14:11:13 2011 +0100
summary:
merge
files:
diff --git a/distutils2/config.py b/distutils2/config.py
--- a/distutils2/config.py
+++ b/distutils2/config.py
@@ -167,19 +167,17 @@
# manifest template
self.dist.extra_files = files.get('extra_files', [])
-
+
if 'resources' in content:
resources = []
- regex = re.compile(r'[^\\](?:\\{2})* ')
for glob, destination in content['resources'].iteritems():
- splitted_glob = regex.split(glob, 1)
+ splitted_glob = glob.split(' ', 1)
if len(splitted_glob) == 1:
prefix = ''
suffix = splitted_glob[0]
else:
prefix = splitted_glob[0]
suffix = splitted_glob[1]
-
resources.append((prefix, suffix, destination))
dir = os.path.dirname(os.path.join(os.getcwd(), filename))
diff --git a/distutils2/datafiles.py b/distutils2/datafiles.py
--- a/distutils2/datafiles.py
+++ b/distutils2/datafiles.py
@@ -1,6 +1,7 @@
-from glob import glob as simple_glob
import os
+import re
from os import path as osp
+from glob import iglob as simple_iglob
class SmartGlob(object):
@@ -15,32 +16,41 @@
base = osp.join(basepath, self.base)
else:
base = basepath
+ if '*' in base or '{' in base or '}' in base:
+ raise NotImplementedError('glob are not supported into base part of datafiles definition. %r is an invalide basepath' % base)
absglob = osp.join(base, self.suffix)
- for file in glob(absglob):
+ for file in iglob(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:
- files = rglob(path_glob)
+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:
- files = simple_glob(path_glob)
- return files
-
-def rglob(path_glob):
- prefix, radical = path_glob.split('**', 1)
- if prefix == '':
- prefix = '.'
- if radical == '':
- radical = '*'
- else:
- radical = radical.lstrip('/')
- glob_files = []
- 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
+ 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(osp.join(prefix, path, radical)):
+ yield os.path.join(prefix, file)
def resources_dests(resources_dir, rules):
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
@@ -5,15 +5,13 @@
from StringIO import StringIO
from distutils2.tests import unittest, support, run_unittest
-from distutils2.datafiles import resources_dests
+from distutils2.datafiles import resources_dests, RICH_GLOB
import re
from os import path as osp
-SLASH = re.compile(r'(?<=[^\\])(?:\\{2})*/')
-
class DataFilesTestCase(support.TempdirManager,
support.LoggingCatcher,
unittest.TestCase):
@@ -23,20 +21,30 @@
self.addCleanup(setattr, sys, 'stdout', sys.stdout)
self.addCleanup(setattr, sys, 'stderr', sys.stderr)
- def assertFindGlob(self, rules, spec):
+
+ def build_spec(self, spec, clean=True):
tempdir = self.mkdtemp()
for filepath in spec:
- filepath = osp.join(tempdir, *SLASH.split(filepath))
+ filepath = osp.join(tempdir, *filepath.split('/'))
dirname = osp.dirname(filepath)
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]
+ 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 test_simple_glob(self):
rules = [('', '*.tpl', '{data}')]
spec = {'coucou.tpl': '{data}/coucou.tpl',
@@ -50,6 +58,27 @@
'Babarlikestrawberry': None}
self.assertFindGlob(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)
+
+ 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)
+
+ def test_glob_in_base(self):
+ rules = [('scrip*', '*.bin', '{appscript}')]
+ spec = {'scripts/scripts.bin': '{appscript}/scripts.bin',
+ 'Babarlikestrawberry': None}
+ tempdir = self.build_spec(spec)
+ self.assertRaises(NotImplementedError, resources_dests, tempdir, rules)
+
def test_recursive_glob(self):
rules = [('', '**/*.bin', '{binary}')]
spec = {'binary0.bin': '{binary}/binary0.bin',
@@ -62,7 +91,7 @@
rules = [
('mailman/database/schemas/','*', '{appdata}/schemas'),
('', '**/*.tpl', '{appdata}/templates'),
- ('developer-docs/', '**/*.txt', '{doc}'),
+ ('', 'developer-docs/**/*.txt', '{doc}'),
('', 'README', '{doc}'),
('mailman/etc/', '*', '{config}'),
('mailman/foo/', '**/bar/*.cfg', '{config}/baz'),
@@ -78,8 +107,8 @@
'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/other.cfg',
- 'developer-docs/index.txt': '{doc}/index.txt',
- 'developer-docs/api/toc.txt': '{doc}/api/toc.txt',
+ 'developer-docs/index.txt': '{doc}/developer-docs/index.txt',
+ 'developer-docs/api/toc.txt': '{doc}/developer-docs/api/toc.txt',
}
self.maxDiff = None
self.assertFindGlob(rules, spec)
--
Repository URL: http://hg.python.org/distutils2
More information about the Python-checkins
mailing list