Skip to content

Commit f37f5c9

Browse files
committed
Direct replacement for distutils.filelist
1 parent 03a8bb1 commit f37f5c9

1 file changed

Lines changed: 39 additions & 15 deletions

File tree

setup.py

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import shutil
3131
import subprocess
3232
import sys
33+
from typing import Union
3334
import winreg
3435

3536
# setuptools must be imported before distutils for markh in some python versions.
@@ -2122,12 +2123,36 @@ def finalize_options(self):
21222123
swig_include_files = "mapilib adsilib".split()
21232124

21242125

2125-
# Helper to allow our script specifications to include wildcards.
2126-
def expand_modules(module_dir):
2127-
flist = FileList()
2128-
flist.findall(module_dir)
2129-
flist.include_pattern("*.py", anchor=0)
2130-
return [os.path.splitext(name)[0] for name in flist.files]
2126+
def findall_files(
2127+
dir: Union[str, os.PathLike],
2128+
include_pattern: Union[re.Pattern, None] = None,
2129+
exclude_pattern: Union[re.Pattern, None] = None,
2130+
):
2131+
"""
2132+
Find all files under 'dir' and return the list of full filenames.
2133+
Filters by `include_pattern` then excludes `exclude_pattern`
2134+
2135+
Re-implemented and simplified from `distutils.filelist.findall`
2136+
"""
2137+
files = filter(
2138+
os.path.isfile,
2139+
(
2140+
os.path.join(base, file)
2141+
for base, dirs, files in os.walk(dir, followlinks=True)
2142+
for file in files
2143+
),
2144+
)
2145+
if include_pattern:
2146+
files = filter(include_pattern.search, files)
2147+
if exclude_pattern:
2148+
files = filter(lambda file: not exclude_pattern.search(file), files)
2149+
return files
2150+
2151+
2152+
def expand_modules(module_dir: Union[str, os.PathLike[str]]):
2153+
"""Helper to allow our script specifications to include wildcards."""
2154+
files = findall_files(module_dir, include_pattern=re.compile(r"(?s:[^\\]*\.py)\Z"))
2155+
return [os.path.splitext(name)[0] for name in files]
21312156

21322157

21332158
# NOTE: somewhat counter-intuitively, a result list a-la:
@@ -2140,16 +2165,15 @@ def convert_data_files(files):
21402165
for file in files:
21412166
file = os.path.normpath(file)
21422167
if file.find("*") >= 0:
2143-
flist = FileList()
2144-
flist.findall(os.path.dirname(file))
2145-
flist.include_pattern(os.path.basename(file), anchor=0)
2146-
# We never want CVS
2147-
flist.exclude_pattern(re.compile(r".*\\CVS\\"), is_regex=1, anchor=0)
2148-
flist.exclude_pattern("*.pyc", anchor=0)
2149-
flist.exclude_pattern("*.pyo", anchor=0)
2150-
if not flist.files:
2168+
files = findall_files(
2169+
os.path.dirname(file),
2170+
include_pattern=re.compile(f"(?s:{os.path.basename(file)})\\Z"),
2171+
# We never want CVS, .pyc and .pyo
2172+
exclude_pattern=re.compile(r".*\\CVS\\|(?s:[^\\]*\.py[co])\Z)"),
2173+
)
2174+
if not files:
21512175
raise RuntimeError("No files match '%s'" % file)
2152-
files_use = flist.files
2176+
files_use = files
21532177
else:
21542178
if not os.path.isfile(file):
21552179
raise RuntimeError("No file '%s'" % file)

0 commit comments

Comments
 (0)