Skip to content

Commit

Permalink
wip: initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
agoose77 committed Aug 27, 2024
0 parents commit dabb6da
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 0 deletions.
9 changes: 9 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
MIT License

Copyright (c) 2024-present Angus Hollands <goosey15@gmail.com>

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.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
35 changes: 35 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -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"
4 changes: 4 additions & 0 deletions src/hatch_deps_selector/__about__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# SPDX-FileCopyrightText: 2024-present Angus Hollands <goosey15@gmail.com>
#
# SPDX-License-Identifier: MIT
__version__ = "0.0.1"
Empty file.
12 changes: 12 additions & 0 deletions src/hatch_deps_selector/hooks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# SPDX-FileCopyrightText: 2022-present Angus Hollands <goosey15@gmail.com>
#
# SPDX-License-Identifier: MIT
from hatchling.plugin import hookimpl

from .plugin import DependenciesSelectorHook


@hookimpl
def hatch_register_build_hook():
return DependenciesSelectorHook

46 changes: 46 additions & 0 deletions src/hatch_deps_selector/plugin.py
Original file line number Diff line number Diff line change
@@ -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)

3 changes: 3 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# SPDX-FileCopyrightText: 2024-present Angus Hollands <goosey15@gmail.com>
#
# SPDX-License-Identifier: MIT

0 comments on commit dabb6da

Please sign in to comment.