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 b078d19 commit 76abe88
Showing 1 changed file with 15 additions and 40 deletions.
55 changes: 15 additions & 40 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,37 @@ 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]]):
"""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:
# [('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))
# We never want CVS
if not (path.suffix in {".pyc", ".pyo"} or "\\CVS\\" in file)
)
if not files:
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 76abe88

Please sign in to comment.