Skip to content

Commit

Permalink
Rewrote using Path.rglob
Browse files Browse the repository at this point in the history
expand_modules should return list[str]
  • Loading branch information
Avasam committed Oct 22, 2023
1 parent d9e783d commit 4c18d3d
Showing 1 changed file with 18 additions and 41 deletions.
59 changes: 18 additions & 41 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
import logging

Expand Down Expand Up @@ -2123,64 +2125,39 @@ 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.parent.relative_to(module_dir))
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,)))
return ret
Expand Down

0 comments on commit 4c18d3d

Please sign in to comment.