-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
83 lines (58 loc) · 2.3 KB
/
setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"""Setup `overload_numpy` package."""
from __future__ import annotations
import os
import sys
from pathlib import Path
from mypyc.build import mypycify
from setuptools import setup
##############################################################################
# PARAMETERS
USE_MYPYC: bool = False
CURRENT_DIR = Path(__file__).parent
SRC = CURRENT_DIR / "src"
sys.path.insert(0, str(CURRENT_DIR)) # for setuptools.build_meta
##############################################################################
# CODE
##############################################################################
def find_python_files(base: Path, exclude: tuple[str, ...] = ("test_",)) -> list[Path]:
"""Recursively find python files in all subfolders of base.
Parameters
----------
base : Path
Base path from which to search for python files.
exclude : tuple[str, ...], optional
Paths to exclude.
Returns
-------
list[Path]
"""
files: list[Path] = []
for entry in base.iterdir():
if entry.name.startswith(exclude):
continue
if entry.is_file() and entry.suffix == ".py":
files.append(entry)
elif entry.is_dir():
files.extend(find_python_files(entry))
return files
# To compile with mypyc, a mypyc checkout must be present on the PYTHONPATH
if len(sys.argv) > 1 and sys.argv[1] == "--use-mypyc":
sys.argv.pop(1)
USE_MYPYC = True
if os.getenv("NPOVERLOAD_USE_MYPYC", None) == "1":
USE_MYPYC = True
if not USE_MYPYC:
ext_modules = []
else:
print("BUILDING `overload_numpy` WITH MYPYC") # noqa: T201
blocklist = [ # TODO: not block any
"overload_numpy/implementors/dispatch.py", # /~https://github.com/python/mypy/issues/13613
"overload_numpy/implementors/ufunc.py", # TODO: can't call ImplementsUFunc
"overload_numpy/_typeutils.py", # /~https://github.com/mypyc/mypyc/issues/909
]
discovered: list[Path] = []
discovered.extend(find_python_files(SRC / "overload_numpy"))
mypyc_targets = [str(p) for p in discovered if p.relative_to(SRC).as_posix() not in blocklist]
opt_level = os.getenv("MYPYC_OPT_LEVEL", "3")
ext_modules = mypycify(mypyc_targets, opt_level=opt_level, verbose=True)
setup(name="overload_numpy", package_dir={"": "src"}, ext_modules=ext_modules)