From 87a872ee5c2018c224175b57391489793167322a Mon Sep 17 00:00:00 2001 From: Avasam Date: Sat, 21 Oct 2023 22:27:22 -0400 Subject: [PATCH] Rewrote using Path.rglob --- setup.py | 58 +++++++++++++++++--------------------------------------- 1 file changed, 17 insertions(+), 41 deletions(-) diff --git a/setup.py b/setup.py index 3f773fe83..8c467baae 100644 --- a/setup.py +++ b/setup.py @@ -25,12 +25,14 @@ # Originally by Thomas Heller, started in 2000 or so. import glob import os +from collections.abc import Iterable +from pathlib import Path import platform import re import shutil import subprocess import sys -from typing import Union +from typing import List, Tuple, Union import winreg import logging @@ -2123,36 +2125,11 @@ def finalize_options(self): swig_include_files = "mapilib adsilib".split() -def findall_files( - dir: Union[str, os.PathLike[str]], - include_pattern: Union[re.Pattern, None] = None, - exclude_pattern: Union[re.Pattern, None] = None, -): - """ - Find all files under 'dir' and return the list of full filenames. - Filters by `include_pattern` then excludes `exclude_pattern` - - Re-implemented and simplified from `distutils.filelist.findall` - """ - files = filter( - os.path.isfile, - ( - os.path.join(base, file) - for base, dirs, files in os.walk(dir, followlinks=True) - for file in files - ), - ) - if include_pattern: - files = filter(include_pattern.search, files) - if exclude_pattern: - files = filter(lambda file: not exclude_pattern.search(file), files) - return files - - -def expand_modules(module_dir: Union[str, os.PathLike[str]]): +def expand_modules(module_dir: Union[str, os.PathLike]): """Helper to allow our script specifications to include wildcards.""" - files = findall_files(module_dir, include_pattern=re.compile(r"(?s:[^\\]*\.py)\Z")) - return [os.path.splitext(name)[0] for name in files] + return [ + path.parent.relative_to(module_dir) for path in Path(module_dir).rglob("*.py") + ] # NOTE: somewhat counter-intuitively, a result list a-la: @@ -2160,27 +2137,26 @@ def expand_modules(module_dir: Union[str, os.PathLike[str]]): # will 'do the right thing' in terms of installing licence.txt into # 'Lib/site-packages/pythonwin/licence.txt'. We exploit this to # get 'com/win32com/whatever' installed to 'win32com/whatever' -def convert_data_files(files): - ret = [] +def convert_data_files(files: Iterable[str]): + ret: List[Tuple[str, Tuple[str]]] = [] for file in files: - file = os.path.normpath(file) if file.find("*") >= 0: - files = findall_files( - os.path.dirname(file), - include_pattern=re.compile(f"(?s:{os.path.basename(file)})\\Z"), - # We never want CVS, .pyc and .pyo - exclude_pattern=re.compile(r".*\\CVS\\|(?s:[^\\]*\.py[co])\Z)"), + files_use = ( + str(path) + for path in Path(file).parent.rglob(os.path.basename(file)) + if path.suffix not in {".pyc", ".pyo"} ) - if not files: + # We never want CVS. This is done in a separate step to have the normalized slashes + files_use = [file for file in files_use if not "\\CVS\\" in file] + if not files_use: raise RuntimeError("No files match '%s'" % file) - files_use = files else: if not os.path.isfile(file): raise RuntimeError("No file '%s'" % file) files_use = (file,) for fname in files_use: path_use = os.path.dirname(fname) - if path_use.startswith("com/") or path_use.startswith("com\\"): + if path_use.startswith("com\\"): path_use = path_use[4:] ret.append((path_use, (fname,))) return ret