Skip to content

Commit ffdea2c

Browse files
committed
Rewrote using Path.rglob
1 parent f37f5c9 commit ffdea2c

1 file changed

Lines changed: 20 additions & 41 deletions

File tree

setup.py

Lines changed: 20 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@
2525
# Originally by Thomas Heller, started in 2000 or so.
2626
import glob
2727
import os
28+
from collections.abc import Iterable
29+
from pathlib import Path
2830
import platform
2931
import re
3032
import shutil
3133
import subprocess
3234
import sys
33-
from typing import Union
35+
from typing import List, Tuple, Union
3436
import winreg
3537

3638
# setuptools must be imported before distutils for markh in some python versions.
@@ -2123,66 +2125,42 @@ def finalize_options(self):
21232125
swig_include_files = "mapilib adsilib".split()
21242126

21252127

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]]):
2128+
def expand_modules(module_dir: Union[str, os.PathLike]):
21532129
"""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]
2130+
return [
2131+
str(path.relative_to(module_dir).with_suffix(""))
2132+
for path in Path(module_dir).rglob("*.py")
2133+
]
21562134

21572135

21582136
# NOTE: somewhat counter-intuitively, a result list a-la:
21592137
# [('Lib/site-packages\\pythonwin', ('pythonwin/license.txt',)),]
21602138
# will 'do the right thing' in terms of installing licence.txt into
21612139
# 'Lib/site-packages/pythonwin/licence.txt'. We exploit this to
21622140
# get 'com/win32com/whatever' installed to 'win32com/whatever'
2163-
def convert_data_files(files):
2164-
ret = []
2141+
def convert_data_files(files: Iterable[str]):
2142+
ret: List[Tuple[str, Tuple[str]]] = []
21652143
for file in files:
2166-
file = os.path.normpath(file)
21672144
if file.find("*") >= 0:
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)"),
2145+
files_use = (
2146+
str(path)
2147+
for path in Path(file).parent.rglob(os.path.basename(file))
2148+
if path.suffix not in {".pyc", ".pyo"}
21732149
)
2174-
if not files:
2150+
# We never want CVS. This is done in a separate step to have the normalized slashes
2151+
files_use = [file for file in files_use if not "\\CVS\\" in file]
2152+
if not files_use:
21752153
raise RuntimeError("No files match '%s'" % file)
2176-
files_use = files
21772154
else:
21782155
if not os.path.isfile(file):
21792156
raise RuntimeError("No file '%s'" % file)
21802157
files_use = (file,)
21812158
for fname in files_use:
21822159
path_use = os.path.dirname(fname)
2183-
if path_use.startswith("com/") or path_use.startswith("com\\"):
2160+
if path_use.startswith("com\\"):
21842161
path_use = path_use[4:]
21852162
ret.append((path_use, (fname,)))
2163+
print("DEBUG convert_data_files ret:", ret)
21862164
return ret
21872165

21882166

@@ -2246,6 +2224,7 @@ def convert_optional_data_files(files):
22462224
]
22472225

22482226
py_modules = expand_modules("win32\\lib")
2227+
print("DEBUG py_modules:", py_modules)
22492228
ext_modules = (
22502229
win32_extensions + com_extensions + pythonwin_extensions + other_extensions
22512230
)

0 commit comments

Comments
 (0)