-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
92 lines (73 loc) · 2.83 KB
/
setup.py
File metadata and controls
92 lines (73 loc) · 2.83 KB
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
84
85
86
87
88
89
90
91
92
from setuptools import setup
from pathlib import Path
import subprocess
import shutil
import sys
import os
def find_python_files(directory: str) -> list[str]:
python_files: list[str] = []
for file in Path(directory).rglob("*.py"):
python_files.append(str(file))
return python_files
def generate_stubs_for_package():
print("\nGenerating stub files with stubgen...\n")
try:
skip_stubgen = {
Path("src/xulbux/base/types.py"), # COMPLEX TYPE DEFINITIONS
Path("src/xulbux/__init__.py"), # PRESERVE PACKAGE METADATA CONSTANTS
}
src_dir = Path("src/xulbux")
generated_count = 0
skipped_count = 0
for py_file in src_dir.rglob("*.py"):
pyi_file = py_file.with_suffix(".pyi")
rel_path = py_file.relative_to(src_dir.parent)
if py_file in skip_stubgen:
pyi_file.write_text(py_file.read_text(encoding="utf-8"), encoding="utf-8")
print(f" copied {rel_path.with_suffix('.pyi')} (preserving type definitions)")
skipped_count += 1
continue
stubgen_exe = (
shutil.which("stubgen")
or str(Path(sys.executable).parent / ("stubgen.exe" if sys.platform == "win32" else "stubgen"))
)
result = subprocess.run(
[stubgen_exe,
str(py_file),
"-o", "src",
"--include-private",
"--export-less"],
capture_output=True,
text=True
)
if result.returncode == 0:
print(f" generated {rel_path.with_suffix('.pyi')}")
generated_count += 1
else:
print(f" failed {rel_path}")
if result.stderr:
print(f" {result.stderr.strip()}")
print(f"\nStub generation complete. ({generated_count} generated, {skipped_count} copied)\n")
except Exception as e:
fmt_error = "\n ".join(str(e).splitlines())
print(f"[WARNING] Could not generate stubs:\n {fmt_error}\n")
ext_modules = []
# OPTIONALLY USE MYPYC COMPILATION
if os.environ.get("XULBUX_USE_MYPYC", "1") == "1":
try:
from mypyc.build import mypycify
print("\nCompiling with mypyc...\n")
source_files = find_python_files("src/xulbux")
ext_modules = mypycify(source_files, opt_level="3")
print("\nMypyc compilation complete.\n")
generate_stubs_for_package()
except (ImportError, Exception) as e:
fmt_error = "\n ".join(str(e).splitlines())
print(
f"\n[WARNING] mypyc compilation disabled (not available or failed):\n {fmt_error}\n"
"\nInstalling as pure Python package...\n"
)
setup(
name="xulbux",
ext_modules=ext_modules,
)