Skip to content

Commit

Permalink
Rewrote using Path.rglob
Browse files Browse the repository at this point in the history
  • Loading branch information
Avasam committed Oct 22, 2023
1 parent f37f5c9 commit d49d1c0
Showing 1 changed file with 20 additions and 42 deletions.
62 changes: 20 additions & 42 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

# setuptools must be imported before distutils for markh in some python versions.
Expand All @@ -44,7 +46,6 @@
from distutils.command.install_data import install_data
from distutils.command.install_lib import install_lib
from distutils.core import Extension
from tempfile import gettempdir

# some modules need a static CRT to avoid problems caused by them having a
# manifest.
Expand Down Expand Up @@ -2123,66 +2124,42 @@ def finalize_options(self):
swig_include_files = "mapilib adsilib".split()


def findall_files(
dir: Union[str, os.PathLike],
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 [
str(path.relative_to(module_dir).with_suffix(""))
for path in Path(module_dir).rglob("*.py")
]


# NOTE: somewhat counter-intuitively, a result list a-la:
# [('Lib/site-packages\\pythonwin', ('pythonwin/license.txt',)),]
# 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,)))
print("DEBUG convert_data_files ret:", ret)
return ret


Expand Down Expand Up @@ -2246,6 +2223,7 @@ def convert_optional_data_files(files):
]

py_modules = expand_modules("win32\\lib")
print("DEBUG py_modules:", py_modules)
ext_modules = (
win32_extensions + com_extensions + pythonwin_extensions + other_extensions
)
Expand Down

0 comments on commit d49d1c0

Please sign in to comment.