|
18 | 18 | import os |
19 | 19 | import shutil |
20 | 20 | from pathlib import Path |
| 21 | +from typing import Any |
21 | 22 |
|
22 | | -allowed_to_fail = os.environ.get("CIBUILDWHEEL", "0") != "1" |
| 23 | +from hatchling.builders.hooks.plugin.interface import BuildHookInterface |
23 | 24 |
|
| 25 | +allowed_to_fail = os.environ.get("CIBUILDWHEEL", "0") != "1" |
24 | 26 |
|
25 | | -def build_cython_extensions() -> None: |
26 | | - import Cython.Compiler.Options |
27 | | - from Cython.Build import build_ext, cythonize |
28 | | - from setuptools import Extension |
29 | | - from setuptools.dist import Distribution |
30 | 27 |
|
31 | | - Cython.Compiler.Options.annotate = True |
| 28 | +class CythonBuildHook(BuildHookInterface): |
| 29 | + def build_cython_extensions(self) -> None: |
| 30 | + import Cython.Compiler.Options |
| 31 | + from Cython.Build import build_ext, cythonize |
| 32 | + from setuptools import Extension |
| 33 | + from setuptools.dist import Distribution |
32 | 34 |
|
33 | | - if os.name == "nt": # Windows |
34 | | - extra_compile_args = [ |
35 | | - "/O2", |
36 | | - ] |
37 | | - else: # UNIX-based systems |
38 | | - extra_compile_args = [ |
39 | | - "-O3", |
40 | | - ] |
| 35 | + Cython.Compiler.Options.annotate = True |
41 | 36 |
|
42 | | - package_path = "pyiceberg" |
| 37 | + if os.name == "nt": # Windows |
| 38 | + extra_compile_args = [ |
| 39 | + "/O2", |
| 40 | + ] |
| 41 | + else: # UNIX-based systems |
| 42 | + extra_compile_args = [ |
| 43 | + "-O3", |
| 44 | + ] |
43 | 45 |
|
44 | | - extension = Extension( |
45 | | - # Your .pyx file will be available to cpython at this location. |
46 | | - name="pyiceberg.avro.decoder_fast", |
47 | | - sources=[ |
48 | | - os.path.join(package_path, "avro", "decoder_fast.pyx"), |
49 | | - ], |
50 | | - extra_compile_args=extra_compile_args, |
51 | | - language="c", |
52 | | - ) |
| 46 | + package_path = "pyiceberg" |
53 | 47 |
|
54 | | - ext_modules = cythonize([extension], include_path=list(package_path), language_level=3, annotate=True) |
55 | | - dist = Distribution({"ext_modules": ext_modules}) |
56 | | - cmd = build_ext(dist) |
57 | | - cmd.ensure_finalized() |
| 48 | + extension = Extension( |
| 49 | + # Your .pyx file will be available to cpython at this location. |
| 50 | + name="pyiceberg.avro.decoder_fast", |
| 51 | + sources=[ |
| 52 | + os.path.join(package_path, "avro", "decoder_fast.pyx"), |
| 53 | + ], |
| 54 | + extra_compile_args=extra_compile_args, |
| 55 | + language="c", |
| 56 | + ) |
58 | 57 |
|
59 | | - cmd.run() |
| 58 | + ext_modules = cythonize([extension], include_path=list(package_path), language_level=3, annotate=True) |
| 59 | + dist = Distribution({"ext_modules": ext_modules}) |
| 60 | + cmd = build_ext(dist) |
| 61 | + cmd.ensure_finalized() |
60 | 62 |
|
61 | | - for output in cmd.get_outputs(): |
62 | | - output = Path(output) |
63 | | - relative_extension = output.relative_to(cmd.build_lib) |
64 | | - shutil.copyfile(output, relative_extension) |
| 63 | + cmd.run() |
65 | 64 |
|
| 65 | + for output in cmd.get_outputs(): |
| 66 | + output = Path(output) |
| 67 | + relative_extension = output.relative_to(cmd.build_lib) |
| 68 | + shutil.copyfile(output, relative_extension) |
66 | 69 |
|
67 | | -try: |
68 | | - build_cython_extensions() |
69 | | -except Exception: |
70 | | - if not allowed_to_fail: |
71 | | - raise |
| 70 | + def initialize(self, version: str, build_data: dict[str, Any]) -> None: |
| 71 | + try: |
| 72 | + self.build_cython_extensions() |
| 73 | + except Exception: |
| 74 | + if not allowed_to_fail: |
| 75 | + raise |
0 commit comments