Skip to content

Commit 8095a23

Browse files
authored
Merge pull request #19 from python-project-templates/tkp/sys
Ensure sys wheels built, libs included
2 parents 652f0fb + cabe92f commit 8095a23

File tree

4 files changed

+70
-51
lines changed

4 files changed

+70
-51
lines changed

hatch_rs/plugin.py

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
from logging import getLogger
44
from os import getenv
5+
from pathlib import Path
6+
from platform import machine as platform_machine
7+
from sys import platform as sys_platform, version_info
58
from typing import Any
69

710
from hatchling.builders.hooks.plugin.interface import BuildHookInterface
@@ -63,12 +66,12 @@ def initialize(self, version: str, build_data: dict[str, Any]) -> None:
6366
# Perform any cleanup actions
6467
build_plan.cleanup()
6568

66-
# if build_plan.libraries:
67-
# # force include libraries
68-
# # for library in build_plan.libraries:
69-
# # name = library.get_qualified_name(build_plan.platform.platform)
70-
# # build_data["force_include"][name] = name
69+
if not build_plan._libraries:
70+
raise ValueError("No libraries were created by the build.")
7171

72+
# force include libraries
73+
# for library in build_plan._libraries:
74+
# build_data["force_include"][library] = library
7275
# build_data["pure_python"] = False
7376
# machine = platform_machine()
7477
# version_major = version_info.major
@@ -83,28 +86,28 @@ def initialize(self, version: str, build_data: dict[str, Any]) -> None:
8386
# build_data["tag"] = f"cp{version_major}{version_minor}-abi3-{os_name}_{machine}"
8487
# else:
8588
# build_data["tag"] = f"cp{version_major}{version_minor}-cp{version_major}{version_minor}-{os_name}_{machine}"
86-
# else:
87-
# build_data["pure_python"] = False
88-
# machine = platform_machine()
89-
# version_major = version_info.major
90-
# version_minor = version_info.minor
91-
# # TODO abi3
92-
# if "darwin" in sys_platform:
93-
# os_name = "macosx_11_0"
94-
# elif "linux" in sys_platform:
95-
# os_name = "linux"
96-
# else:
97-
# os_name = "win"
98-
# build_data["tag"] = f"cp{version_major}{version_minor}-cp{version_major}{version_minor}-{os_name}_{machine}"
99-
100-
# # force include libraries
101-
# for path in Path(".").rglob("*"):
102-
# if path.is_dir():
103-
# continue
104-
# if str(path).startswith(str(build_plan.cmake.build)) or str(path).startswith("dist"):
105-
# continue
106-
# if path.suffix in (".pyd", ".dll", ".so", ".dylib"):
107-
# build_data["force_include"][str(path)] = str(path)
108-
109-
# for path in build_data["force_include"]:
110-
# self._logger.warning(f"Force include: {path}")
89+
build_data["pure_python"] = False
90+
machine = platform_machine()
91+
version_major = version_info.major
92+
version_minor = version_info.minor
93+
94+
# TODO abi3
95+
if "darwin" in sys_platform:
96+
os_name = "macosx_11_0"
97+
elif "linux" in sys_platform:
98+
os_name = "linux"
99+
else:
100+
os_name = "win"
101+
build_data["tag"] = f"cp{version_major}{version_minor}-cp{version_major}{version_minor}-{os_name}_{machine}"
102+
103+
# force include libraries
104+
for path in Path(".").rglob("*"):
105+
if path.is_dir():
106+
continue
107+
if str(path).startswith("target") or str(path).startswith("dist"):
108+
continue
109+
if path.suffix in (".pyd", ".dll", ".so", ".dylib"):
110+
build_data["force_include"][str(path)] = str(path)
111+
112+
for path in build_data["force_include"]:
113+
self._logger.warning(f"Force include: {path}")

hatch_rs/structs.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from sys import platform as sys_platform
88
from typing import List, Literal, Optional
99

10-
from pydantic import BaseModel, Field, field_validator
10+
from pydantic import BaseModel, Field, PrivateAttr, field_validator
1111

1212
__all__ = (
1313
"HatchRustBuildConfig",
@@ -52,6 +52,8 @@ class HatchRustBuildPlan(HatchRustBuildConfig):
5252
build_type: BuildType = "release"
5353
commands: List[str] = Field(default_factory=list)
5454

55+
_libraries: List[str] = PrivateAttr(default_factory=list)
56+
5557
def generate(self):
5658
self.commands = []
5759

@@ -145,14 +147,16 @@ def execute(self):
145147

146148
# Copy each file to the current directory
147149
if sys_platform == "win32":
148-
copy_command = f"copy {file} {cwd}\\{self.module}\\{file_name}.pyd"
150+
library_name = f"{self.module}\\{file_name}.pyd"
151+
self._libraries.append(library_name)
152+
copy_command = f"copy {file} {cwd}\\{library_name}"
149153
else:
150154
if which("cp") is None:
151155
raise EnvironmentError("cp command not found. Ensure it is installed and available in PATH.")
152-
copy_command = f"cp -f {file} {cwd}/{self.module}/{file_name}.so"
153-
print(copy_command)
156+
library_name = f"{self.module}/{file_name}.so"
157+
self._libraries.append(library_name)
158+
copy_command = f"cp -f {file} {cwd}/{library_name}"
154159
system_call(copy_command)
155-
156160
return self.commands
157161

158162
def cleanup(self):

hatch_rs/tests/test_projects.py

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,24 @@
22
from pathlib import Path
33
from shutil import rmtree
44
from subprocess import check_call
5-
from sys import modules, path, platform
5+
from sys import executable, modules, path, platform, version_info
66

77
import pytest
88

99

1010
class TestProject:
1111
@pytest.mark.parametrize(
12-
"project",
12+
"project_folder",
1313
[
1414
"test_project_basic",
1515
],
1616
)
17-
def test_basic(self, project):
17+
def test_basic(self, project_folder):
1818
# cleanup
19-
rmtree(f"hatch_rs/tests/{project}/project/extension.so", ignore_errors=True)
20-
rmtree(f"hatch_rs/tests/{project}/project/extension.pyd", ignore_errors=True)
19+
rmtree(f"hatch_rs/tests/{project_folder}/dist", ignore_errors=True)
20+
rmtree(f"hatch_rs/tests/{project_folder}/target", ignore_errors=True)
21+
rmtree(f"hatch_rs/tests/{project_folder}/project/extension.so", ignore_errors=True)
22+
rmtree(f"hatch_rs/tests/{project_folder}/project/extension.pyd", ignore_errors=True)
2123
modules.pop("project", None)
2224
modules.pop("project.extension", None)
2325

@@ -28,21 +30,31 @@ def test_basic(self, project):
2830
"build",
2931
"--hooks-only",
3032
],
31-
cwd=f"hatch_rs/tests/{project}",
33+
cwd=f"hatch_rs/tests/{project_folder}",
3234
)
3335

3436
# assert built
35-
36-
if project == "test_project_limited_api" and platform != "win32":
37-
assert "project.abi3.so" in listdir(f"hatch_rs/tests/{project}/project")
37+
if platform == "win32":
38+
assert "project.pyd" in listdir(f"hatch_rs/tests/{project_folder}/project")
3839
else:
39-
if platform == "win32":
40-
assert "project.pyd" in listdir(f"hatch_rs/tests/{project}/project")
41-
else:
42-
assert "project.so" in listdir(f"hatch_rs/tests/{project}/project")
40+
assert "project.so" in listdir(f"hatch_rs/tests/{project_folder}/project")
41+
42+
# dist
43+
check_call(
44+
[
45+
executable,
46+
"-m",
47+
"build",
48+
"-w",
49+
"-n",
50+
],
51+
cwd=f"hatch_rs/tests/{project_folder}",
52+
)
53+
54+
assert f"cp3{version_info.minor}-cp3{version_info.minor}" in listdir(f"hatch_rs/tests/{project_folder}/dist")[0]
4355

4456
# import
45-
here = Path(__file__).parent / project
57+
here = Path(__file__).parent / project_folder
4658
path.insert(0, str(here))
4759
import project.project
4860

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ exclude_also = [
9797
"@(abc\\.)?abstractmethod",
9898
]
9999
ignore_errors = true
100-
fail_under = 1
100+
fail_under = 75
101101

102102
[tool.hatch.build]
103103
artifacts = []
@@ -130,4 +130,4 @@ known-first-party = ["hatch_rs"]
130130
section-order = ["future", "standard-library", "third-party", "first-party", "local-folder"]
131131

132132
[tool.ruff.lint.per-file-ignores]
133-
"__init__.py" = ["F401", "F403"]
133+
"__init__.py" = ["F401", "F403"]

0 commit comments

Comments
 (0)