|
18 | 18 | """ |
19 | 19 | Extract docstrings from pyarrow runtime and insert them into stub files. |
20 | 20 |
|
21 | | -Usage (from python/ directory with pyarrow built): |
22 | | - python scripts/update_stub_docstrings.py pyarrow-stubs |
| 21 | +Called from CMakeLists.txt install(CODE ...) after .pyi stubs and .so |
| 22 | +extensions have been installed to CMAKE_INSTALL_PREFIX. |
| 23 | +
|
| 24 | +Usage: |
| 25 | + python scripts/update_stub_docstrings.py <install_prefix> <source_dir> |
23 | 26 | """ |
24 | 27 |
|
25 | 28 | import argparse |
26 | 29 | import importlib |
27 | 30 | import inspect |
| 31 | +import os |
| 32 | +import shutil |
28 | 33 | import sys |
| 34 | +import sysconfig |
| 35 | +import tempfile |
29 | 36 | from pathlib import Path |
30 | 37 | from textwrap import indent |
31 | 38 |
|
@@ -198,31 +205,56 @@ def add_docstrings_to_stubs(stubs_dir): |
198 | 205 | stub_file.write_text(modified.code) |
199 | 206 |
|
200 | 207 |
|
201 | | -def add_docstrings_from_build(stubs_dir, build_lib): |
| 208 | +def _create_importable_pyarrow(pyarrow_pkg, source_dir, install_prefix): |
202 | 209 | """ |
203 | | - Entry point for setup.py: update docstrings using pyarrow from build directory. |
204 | | -
|
205 | | - During the build process, pyarrow is not installed in the system Python. |
206 | | - We need to temporarily add the build directory to sys.path so we can |
207 | | - import pyarrow and extract docstrings from it. |
| 210 | + Populate pyarrow_pkg with symlinks to source .py files, compiled |
| 211 | + extensions, shared libraries, and subpackages so that pyarrow is |
| 212 | + importable from the parent of pyarrow_pkg. |
208 | 213 | """ |
209 | | - stubs_dir, build_lib = Path(stubs_dir), Path(build_lib) |
210 | | - |
211 | | - sys.path.insert(0, str(build_lib)) |
212 | | - try: |
213 | | - add_docstrings_to_stubs(stubs_dir) |
214 | | - finally: |
215 | | - sys.path.pop(0) |
| 214 | + ext_suffix = sysconfig.get_config_var("EXT_SUFFIX") or ".so" |
| 215 | + source_pyarrow = source_dir / "pyarrow" |
| 216 | + link = shutil.copy2 if sys.platform == "win32" else os.symlink |
| 217 | + |
| 218 | + for f in source_pyarrow.iterdir(): |
| 219 | + if f.suffix == ".py": |
| 220 | + link(f, pyarrow_pkg / f.name) |
| 221 | + elif f.is_dir() and not f.name.startswith((".", "__")): |
| 222 | + if sys.platform == "win32": |
| 223 | + shutil.copytree(f, pyarrow_pkg / f.name, symlinks=True) |
| 224 | + else: |
| 225 | + link(f, pyarrow_pkg / f.name) |
| 226 | + |
| 227 | + # Link compiled extensions and shared libraries from the install prefix |
| 228 | + for f in install_prefix.iterdir(): |
| 229 | + dest = pyarrow_pkg / f.name |
| 230 | + if dest.exists(): |
| 231 | + continue |
| 232 | + is_extension = ext_suffix in f.name or f.suffix == ".pyd" |
| 233 | + is_shared_lib = f.name.startswith("lib") and ( |
| 234 | + ".so" in f.name or f.suffix in (".dylib", ".dll") |
| 235 | + ) |
| 236 | + if is_extension or is_shared_lib: |
| 237 | + link(f, dest) |
216 | 238 |
|
217 | 239 |
|
218 | 240 | if __name__ == "__main__": |
219 | 241 | parser = argparse.ArgumentParser(description=__doc__) |
220 | | - parser.add_argument("stubs_dir", type=Path, help="Path to pyarrow-stubs folder") |
| 242 | + parser.add_argument("install_prefix", type=Path, |
| 243 | + help="CMAKE_INSTALL_PREFIX with built .so and .pyi files") |
| 244 | + parser.add_argument("source_dir", type=Path, |
| 245 | + help="CMake source directory (python/)") |
221 | 246 | args = parser.parse_args() |
222 | 247 |
|
223 | | - # Add the directory containing this script's parent (python/) to sys.path |
224 | | - # so pyarrow can be imported when running from the python/ directory |
225 | | - script_dir = Path(__file__).resolve().parent |
226 | | - python_dir = script_dir.parent |
227 | | - sys.path.insert(0, str(python_dir)) |
228 | | - add_docstrings_to_stubs(args.stubs_dir.resolve()) |
| 248 | + install_prefix = args.install_prefix.resolve() |
| 249 | + source_dir = args.source_dir.resolve() |
| 250 | + |
| 251 | + if not any(install_prefix.glob("*.pyi")): |
| 252 | + print("No .pyi files in install prefix, skipping docstring injection") |
| 253 | + sys.exit(0) |
| 254 | + |
| 255 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 256 | + pyarrow_pkg = Path(tmpdir) / "pyarrow" |
| 257 | + pyarrow_pkg.mkdir() |
| 258 | + _create_importable_pyarrow(pyarrow_pkg, source_dir, install_prefix) |
| 259 | + sys.path.insert(0, tmpdir) |
| 260 | + add_docstrings_to_stubs(install_prefix) |
0 commit comments