Skip to content

Commit

Permalink
fix plugin for transitive deps
Browse files Browse the repository at this point in the history
  • Loading branch information
carderne committed Aug 15, 2024
1 parent b5cc774 commit 5c0ce5b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 24 deletions.
21 changes: 10 additions & 11 deletions plugins/hatch/hatch_una/hatch_build.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
import tomllib
from pathlib import Path
from typing import Any

from hatchling.builders.config import BuilderConfig
from hatchling.builders.hooks.plugin.interface import BuildHookInterface
from hatchling.plugin import hookimpl

from hatch_una import util

class BuildConfig(BuilderConfig):
pass


class UnaHook(BuildHookInterface[BuildConfig]):
class UnaBuildHook(BuildHookInterface[BuilderConfig]):
PLUGIN_NAME = "una-build"

def initialize(self, version: str, build_data: dict[str, Any]) -> None:
print("una: Injecting internal dependencies")
root = Path(self.root)
with (root / "pyproject.toml").open("rb") as fp:
conf = tomllib.load(fp)
conf = util.load_conf(root)

int_deps: dict[str, str] = conf["tool"]["una"]["libs"]
found = {k: v for k, v in int_deps.items() if (root / k).exists()}
found = [Path(k) for k in int_deps if (root / k).exists()]
if not int_deps or not found:
# should I raise here?
return
build_data["force_include"] = {**build_data["force_include"], **int_deps}

add_pyproj = {str(f.parents[1] / util.PYPROJ): str(util.EXTRA_PYPROJ / f.name / util.PYPROJ) for f in found}
build_data["force_include"] = {**build_data["force_include"], **int_deps, **add_pyproj}


@hookimpl
def hatch_register_build_hook():
return UnaHook
def hatch_register_build_hook() -> type[UnaBuildHook]:
return UnaBuildHook
26 changes: 13 additions & 13 deletions plugins/hatch/hatch_una/hatch_meta.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
import tomllib
from pathlib import Path
from typing import Any

from hatchling.metadata.plugin.interface import MetadataHookInterface
from hatchling.plugin import hookimpl


def load_conf(path: Path) -> dict[str, Any]:
with (path / "pyproject.toml").open("rb") as fp:
return tomllib.load(fp)
from hatch_una import util


class UnaMetaHook(MetadataHookInterface):
PLUGIN_NAME = "una-meta"

def update(self, metadata: dict[str, Any]) -> None:
print("una: Injecting transitive external dependencies")
root = Path(self.root)
conf = load_conf(root)
conf = util.load_conf(root)
int_deps: dict[str, str] = conf["tool"]["una"]["libs"]

project_deps: list[str] = metadata.get("dependencies", {})
Expand All @@ -25,19 +22,22 @@ def update(self, metadata: dict[str, Any]) -> None:
add_deps: list[str] = []
for dep_path in int_deps:
dep_project_path = Path(dep_path).parents[1]
try:
dep_conf = load_conf(dep_project_path)
except FileNotFoundError:
extra_path = util.EXTRA_PYPROJ / Path(dep_path).name
if dep_project_path.exists():
use_path = dep_project_path
elif extra_path.exists():
use_path = extra_path
else:
# should I raise here?
continue
dep_conf = util.load_conf(use_path)
dep_deps: list[str] = dep_conf["project"]["dependencies"]
dep_deps = [d.strip().replace(" ", "") for d in dep_deps]
add_deps.extend(dep_deps)

all_deps = list(set(project_deps + add_deps))
print(all_deps)
metadata["dependencies"] = all_deps
metadata["dependencies"] = list(set(project_deps + add_deps))


@hookimpl
def hatch_register_metadata_hook():
def hatch_register_metadata_hook() -> type[UnaMetaHook]:
return UnaMetaHook
11 changes: 11 additions & 0 deletions plugins/hatch/hatch_una/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import tomllib
from pathlib import Path
from typing import Any

PYPROJ = "pyproject.toml"
EXTRA_PYPROJ = Path("extra_pyproj")


def load_conf(path: Path) -> dict[str, Any]:
with (path / PYPROJ).open("rb") as fp:
return tomllib.load(fp)

0 comments on commit 5c0ce5b

Please sign in to comment.