diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..8db78af --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) 2024-present Angus Hollands + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..68b88c5 --- /dev/null +++ b/README.md @@ -0,0 +1,21 @@ +# hatch-deps-selector + +[![PyPI - Version](https://img.shields.io/pypi/v/hatch-deps-selector.svg)](https://pypi.org/project/hatch-deps-selector) +[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/hatch-deps-selector.svg)](https://pypi.org/project/hatch-deps-selector) + +----- + +## Table of Contents + +- [Installation](#installation) +- [License](#license) + +## Installation + +```console +pip install hatch-deps-selector +``` + +## License + +`hatch-deps-selector` is distributed under the terms of the [MIT](https://spdx.org/licenses/MIT.html) license. diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..85fefc8 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,35 @@ +[build-system] +requires = ["hatchling"] +build-backend = "hatchling.build" + +[project] +name = "hatch-deps-selector" +dynamic = ["version"] +description = 'Select variants of your dependencies with environment variables' +readme = "README.md" +requires-python = ">=3.8" +license = "MIT" +keywords = [] +authors = [ + { name = "Angus Hollands", email = "goosey15@gmail.com" }, +] +classifiers = [ + "Development Status :: 4 - Beta", + "Programming Language :: Python", + "Programming Language :: Python :: 3.8", + "Programming Language :: Python :: 3.9", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", + "Programming Language :: Python :: Implementation :: CPython", + "Programming Language :: Python :: Implementation :: PyPy", +] +dependencies = [] + +[project.urls] +Documentation = "https://github.com/jupyter-book/hatch-deps-selector#readme" +Issues = "https://github.com/jupyter-book/hatch-deps-selector/issues" +Source = "https://github.com/jupyter-book/hatch-deps-selector" + +[tool.hatch.version] +path = "src/hatch_deps_selector/__about__.py" diff --git a/src/hatch_deps_selector/__about__.py b/src/hatch_deps_selector/__about__.py new file mode 100644 index 0000000..9cd1bf2 --- /dev/null +++ b/src/hatch_deps_selector/__about__.py @@ -0,0 +1,4 @@ +# SPDX-FileCopyrightText: 2024-present Angus Hollands +# +# SPDX-License-Identifier: MIT +__version__ = "0.0.1" diff --git a/src/hatch_deps_selector/__init__.py b/src/hatch_deps_selector/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/hatch_deps_selector/hooks.py b/src/hatch_deps_selector/hooks.py new file mode 100644 index 0000000..a038600 --- /dev/null +++ b/src/hatch_deps_selector/hooks.py @@ -0,0 +1,12 @@ +# SPDX-FileCopyrightText: 2022-present Angus Hollands +# +# SPDX-License-Identifier: MIT +from hatchling.plugin import hookimpl + +from .plugin import DependenciesSelectorHook + + +@hookimpl +def hatch_register_build_hook(): + return DependenciesSelectorHook + diff --git a/src/hatch_deps_selector/plugin.py b/src/hatch_deps_selector/plugin.py new file mode 100644 index 0000000..f2b0b2d --- /dev/null +++ b/src/hatch_deps_selector/plugin.py @@ -0,0 +1,46 @@ +import os +from hatchling.builders.hooks.plugin.interface import BuildHookInterface + + +class DependenciesSelectorHook(BuildHookInterface): + PLUGIN_NAME = "selector" + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + self.__env_var = None + self.__variants = None + + @property + def env_var(self): + if self.__env_var is None: + env_var = self.config.get("env-var", "HATCH_SELECTOR_VARIANT") + if not isinstance(env_var, str): + raise TypeError(f"Option `env-var` for build hook `{self.PLUGIN_NAME}` must be a string") + + self.__env_var = env_var + return self.__env_var + + @property + def variants(self): + if self.__variants is None: + variants = self.config.get("variants", {}) + if not isinstance(variants, dict): + raise TypeError(f"Option `variants` for build hook `{self.PLUGIN_NAME}` must be a table") + + self.__variants = variants + return self.variants + + + def initialize(self, version, build_data): + # Allow variant to be unset or empty + variant = os.environ.get(self.env_var) + if not variant: + return + + # But if defined, should be valid + dependencies = self.variants.get(variant, []) + if dependencies is None: + raise ValueError(f"Variant `{variant}` not found for build hook `{self.PLUGIN_NAME}`") + build_data["dependencies"].extend(dependencies) + diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..3e6e783 --- /dev/null +++ b/tests/__init__.py @@ -0,0 +1,3 @@ +# SPDX-FileCopyrightText: 2024-present Angus Hollands +# +# SPDX-License-Identifier: MIT