-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
96 lines (71 loc) · 3.35 KB
/
Copy pathsetup.py
File metadata and controls
96 lines (71 loc) · 3.35 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
93
94
95
96
"""setup.py — places ``dbt_polyglot.pth`` at site-packages so the patch
auto-activates on Python startup.
The package activates by shipping a one-line ``.pth`` file at the site-packages
root; Python's ``site.py`` evaluates ``import`` lines in ``.pth`` files during
interpreter start, and that import triggers ``__init__.py``, which registers
the compile-hook and target-shim patches.
For that to work, the ``.pth`` file has to land at site-packages regardless of
how the package was installed. There are three install paths and each one hooks
into a different setuptools command:
1. **Wheel install** (``pip install .`` / ``pip install dbt-polyglot``) —
setuptools calls ``build_py``, we copy the ``.pth`` into ``build_lib`` and
it flows through into the wheel body and out to site-packages.
2. **Modern editable install** (``pip install -e .`` / ``uv sync`` with
``editable=true`` — PEP 660) — setuptools ≥ 64 uses ``editable_wheel``
and BYPASSES ``build_py`` entirely. Without a second hook the ``.pth``
never lands and the package is silently inert. We override
``editable_wheel`` to also copy the ``.pth`` into the wheel dir.
3. **Legacy editable install** (``python setup.py develop``, still used by
some CI systems) — uses the ``develop`` command; we hook it too.
All three hooks are wrapped in ``try/except`` — a hook failure is swallowed
silently rather than aborting the install. If a future setuptools version
moves any of these seams, the other hooks keep the package installable.
"""
import os
import shutil
from setuptools import setup
from setuptools.command.build_py import build_py
from setuptools.command.develop import develop
PTH = "dbt_polyglot.pth"
def _copy_pth(target_dir):
"""Best-effort copy of the .pth file into ``target_dir``. Never raises."""
if not target_dir:
return
try:
os.makedirs(target_dir, exist_ok=True)
shutil.copyfile(PTH, os.path.join(target_dir, PTH))
except Exception:
pass
class build_py_with_pth(build_py):
"""Wheel-build path (pip install .)."""
def run(self):
super().run()
_copy_pth(self.build_lib)
class develop_with_pth(develop):
"""Legacy editable path (python setup.py develop)."""
def run(self):
super().run()
_copy_pth(getattr(self, "install_dir", None))
cmdclass = {
"build_py": build_py_with_pth,
"develop": develop_with_pth,
}
# PEP 660 editable install path — setuptools ≥ 64 uses `editable_wheel`.
# The command class is import-guarded so this file still imports on older
# setuptools; the wheel and legacy-develop hooks above are enough there.
try:
from setuptools.command.editable_wheel import editable_wheel
class editable_wheel_with_pth(editable_wheel):
"""Modern editable path (pip install -e .) — PEP 660."""
def run(self):
super().run()
# `editable_wheel` writes its output wheel(s) into `self.dist_dir`;
# setuptools then unpacks them into site-packages. Copying the .pth
# into every wheel dir the command produced is the closest hook to
# "wheel body" available for editable installs.
wheel_dir = getattr(self, "dist_dir", None)
_copy_pth(wheel_dir)
cmdclass["editable_wheel"] = editable_wheel_with_pth
except Exception:
pass
setup(cmdclass=cmdclass)