Commit 1af011b1 authored by Jason R. Coombs's avatar Jason R. Coombs

Extract duplicate code into a single method.

parent c05fd079
...@@ -94,17 +94,12 @@ class build_py(orig.build_py, Mixin2to3): ...@@ -94,17 +94,12 @@ class build_py(orig.build_py, Mixin2to3):
def find_data_files(self, package, src_dir): def find_data_files(self, package, src_dir):
"""Return filenames for package's data files in 'src_dir'""" """Return filenames for package's data files in 'src_dir'"""
spec = self.package_data patterns = self._get_platform_patterns(
raw_patterns = itertools.chain( self.package_data,
spec.get('', []), package,
spec.get(package, []), src_dir,
)
platform_patterns = (
# Each pattern has to be converted to a platform-specific path
os.path.join(src_dir, convert_path(pattern))
for pattern in raw_patterns
) )
globs_expanded = map(glob, platform_patterns) globs_expanded = map(glob, patterns)
# flatten the expanded globs into an iterable of matches # flatten the expanded globs into an iterable of matches
globs_matches = itertools.chain.from_iterable(globs_expanded) globs_matches = itertools.chain.from_iterable(globs_expanded)
glob_files = filter(os.path.isfile, globs_matches) glob_files = filter(os.path.isfile, globs_matches)
...@@ -197,19 +192,14 @@ class build_py(orig.build_py, Mixin2to3): ...@@ -197,19 +192,14 @@ class build_py(orig.build_py, Mixin2to3):
def exclude_data_files(self, package, src_dir, files): def exclude_data_files(self, package, src_dir, files):
"""Filter filenames for package's data files in 'src_dir'""" """Filter filenames for package's data files in 'src_dir'"""
files = list(files) files = list(files)
spec = self.exclude_package_data patterns = self._get_platform_patterns(
raw_patterns = itertools.chain( self.exclude_package_data,
spec.get('', []), package,
spec.get(package, []), src_dir,
)
platform_patterns = (
# Each pattern has to be converted to a platform-specific path
os.path.join(src_dir, convert_path(pattern))
for pattern in raw_patterns
) )
match_groups = ( match_groups = (
fnmatch.filter(files, pattern) fnmatch.filter(files, pattern)
for pattern in platform_patterns for pattern in patterns
) )
# flatten the groups of matches into an iterable of matches # flatten the groups of matches into an iterable of matches
matches = itertools.chain.from_iterable(match_groups) matches = itertools.chain.from_iterable(match_groups)
...@@ -223,6 +213,24 @@ class build_py(orig.build_py, Mixin2to3): ...@@ -223,6 +213,24 @@ class build_py(orig.build_py, Mixin2to3):
and not next(seen[fn]) and not next(seen[fn])
] ]
@staticmethod
def _get_platform_patterns(spec, package, src_dir):
"""
yield platfrom-specific path patterns (suitable for glob
or fn_match) from a glob-based spec (such as
self.package_data or self.exclude_package_data)
matching package in src_dir.
"""
raw_patterns = itertools.chain(
spec.get('', []),
spec.get(package, []),
)
return (
# Each pattern has to be converted to a platform-specific path
os.path.join(src_dir, convert_path(pattern))
for pattern in raw_patterns
)
def assert_relative(path): def assert_relative(path):
if not os.path.isabs(path): if not os.path.isabs(path):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment