Skip to content

Commit dd343f3

Browse files
committed
reinit with code
1 parent da3c633 commit dd343f3

File tree

17 files changed

+768
-25
lines changed

17 files changed

+768
-25
lines changed

Makefile

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@ lint-py: ## lint python with ruff
2727
python -m ruff format --check hatch_go
2828

2929
lint-docs: ## lint docs with mdformat and codespell
30-
python -m mdformat --check README.md
31-
python -m codespell_lib README.md
30+
python -m mdformat --check README.md
31+
python -m codespell_lib README.md
3232

3333
fix-py: ## autoformat python code with ruff
3434
python -m ruff check --fix hatch_go
3535
python -m ruff format hatch_go
3636

3737
fix-docs: ## autoformat docs with mdformat and codespell
38-
python -m mdformat README.md
39-
python -m codespell_lib --write README.md
38+
python -m mdformat README.md
39+
python -m codespell_lib --write README.md
4040

4141
lint: lint-py lint-docs ## run all linters
4242
lints: lint
@@ -46,15 +46,12 @@ format: fix
4646
################
4747
# Other Checks #
4848
################
49-
.PHONY: check-dist check-types checks check
49+
.PHONY: check-manifest checks check
5050

51-
check-dist: ## check python sdist and wheel with check-dist
52-
check-dist -v
51+
check-manifest: ## check python sdist manifest with check-manifest
52+
check-manifest -v
5353

54-
check-types: ## check python types with ty
55-
ty check --python $$(which python)
56-
57-
checks: check-dist
54+
checks: check-manifest
5855

5956
# Alias
6057
check: checks

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,14 @@ Hatch plugin for Go builds
99

1010
## Overview
1111

12+
A simple, extensible Go build plugin for [hatch](https://hatch.pypa.io/latest/). Build Python extension modules written in Go using cgo.
13+
14+
```toml
15+
[tool.hatch.build.hooks.hatch-go]
16+
verbose = true
17+
path = "."
18+
module = "project"
19+
```
20+
1221
> [!NOTE]
1322
> This library was generated using [copier](https://copier.readthedocs.io/en/stable/) from the [Base Python Project Template repository](https://github.com/python-project-templates/base).

hatch_go/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
__version__ = "0.1.0"
2+
3+
from .hooks import hatch_register_build_hook
4+
from .plugin import HatchGoBuildHook
5+
from .structs import *

hatch_go/hooks.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from typing import Type
2+
3+
from hatchling.plugin import hookimpl
4+
5+
from .plugin import HatchGoBuildHook
6+
7+
8+
@hookimpl
9+
def hatch_register_build_hook() -> Type[HatchGoBuildHook]:
10+
return HatchGoBuildHook

hatch_go/plugin.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
from __future__ import annotations
2+
3+
from logging import getLogger
4+
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
8+
from typing import Any
9+
10+
from hatchling.builders.hooks.plugin.interface import BuildHookInterface
11+
12+
from .structs import HatchGoBuildConfig, HatchGoBuildPlan
13+
from .utils import import_string
14+
15+
__all__ = ("HatchGoBuildHook",)
16+
17+
log = getLogger(__name__)
18+
19+
20+
class HatchGoBuildHook(BuildHookInterface[HatchGoBuildConfig]):
21+
"""The hatch-go build hook."""
22+
23+
PLUGIN_NAME = "hatch-go"
24+
_logger = log
25+
26+
def initialize(self, version: str, build_data: dict[str, Any]) -> None:
27+
"""Initialize the plugin."""
28+
# Log some basic information
29+
project_name = self.metadata.config["project"]["name"]
30+
self._logger.info("Initializing hatch-go plugin version %s", version)
31+
self._logger.info(f"Running hatch-go: {project_name}")
32+
33+
# Only run if creating wheel
34+
if self.target_name != "wheel":
35+
self._logger.info("ignoring target name %s", self.target_name)
36+
return
37+
38+
# Skip if SKIP_HATCH_GO is set
39+
if getenv("SKIP_HATCH_GO"):
40+
self._logger.info("Skipping the build hook since SKIP_HATCH_GO was set")
41+
return
42+
43+
# Get build config class or use default
44+
build_config_class = import_string(self.config["build-config-class"]) if "build-config-class" in self.config else HatchGoBuildConfig
45+
46+
# Instantiate build config
47+
config = build_config_class(name=project_name, **self.config)
48+
49+
# Get build plan class or use default
50+
build_plan_class = import_string(self.config["build-plan-class"]) if "build-plan-class" in self.config else HatchGoBuildPlan
51+
52+
# Instantiate builder
53+
build_plan = build_plan_class(**config.model_dump())
54+
55+
# Generate commands
56+
build_plan.generate()
57+
58+
# Log commands if in verbose mode
59+
if build_plan.verbose:
60+
for command in build_plan.commands:
61+
self._logger.warning(command)
62+
63+
if build_plan.skip:
64+
self._logger.warning("Skipping build")
65+
return
66+
67+
# Execute build plan
68+
build_plan.execute()
69+
70+
# Perform any cleanup actions
71+
build_plan.cleanup()
72+
73+
if not build_plan._libraries:
74+
raise ValueError("No libraries were created by the build.")
75+
76+
build_data["pure_python"] = False
77+
machine = platform_machine()
78+
version_major = version_info.major
79+
version_minor = version_info.minor
80+
81+
if "darwin" in sys_platform:
82+
os_name = "macosx_11_0"
83+
elif "linux" in sys_platform:
84+
os_name = "linux"
85+
else:
86+
os_name = "win"
87+
88+
build_data["tag"] = f"cp{version_major}{version_minor}-cp{version_major}{version_minor}-{os_name}_{machine}"
89+
90+
# Force include libraries
91+
for path in Path(".").rglob("*"):
92+
if path.is_dir():
93+
continue
94+
if str(path).startswith("dist") or not str(path).startswith(config.module):
95+
continue
96+
if path.suffix in (".pyd", ".dll", ".so", ".dylib"):
97+
build_data["force_include"][str(path)] = str(path)
98+
99+
for path in build_data["force_include"]:
100+
self._logger.warning(f"Force include: {path}")

0 commit comments

Comments
 (0)