Skip to content

Commit

Permalink
Add option to control which extras are included in the conda package'…
Browse files Browse the repository at this point in the history
…s requirements.
  • Loading branch information
domdfcoding committed Nov 12, 2020
1 parent 4ebe0f1 commit bce178c
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 10 deletions.
8 changes: 7 additions & 1 deletion repo_helper/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,8 +395,14 @@ def write_conda_index(self, build_number: int = 1):
build_string = f"py_{build_number}"
# https://docs.conda.io/projects/conda-build/en/latest/resources/define-metadata.html#build-number-and-string

extras = []

for extra in self.config["conda_extras"]:
extras.extend(self.config["extras_require"][extra])

all_requirements = validate_requirements(
compile_requirements(self.repo_dir, self.config["extras_require"]), self.config["conda_channels"]
compile_requirements(self.repo_dir, extras),
self.config["conda_channels"],
)

index = {
Expand Down
16 changes: 10 additions & 6 deletions repo_helper/conda.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def get_from_cache(channel_name: str) -> List[str]:

def compile_requirements(
repo_dir: PathPlus,
extras: Mapping[str, Iterable[str]],
extras: Iterable[str],
) -> List[ComparableRequirement]:
"""
Compile a list of requirements for the package from the requirements.txt file and any extra dependencies.
Expand All @@ -105,13 +105,12 @@ def compile_requirements(
:param extras: Mapping of "extras" names to lists of requirements.
.. versionadded:: 2020.11.10
.. versionadded:: 2020.11.12 ``extras`` is not an iterable of strings.
"""

extra_requirements = []
all_requirements: List[ComparableRequirement] = []

for extra, requirements in extras.items():
extra_requirements.extend([ComparableRequirement(r) for r in requirements])
extra_requirements = [ComparableRequirement(r) for r in extras]

for requirement in sorted(
combine_requirements(
Expand Down Expand Up @@ -193,8 +192,13 @@ def make_recipe(repo_dir: PathLike, recipe_file: PathLike) -> None:

config = parse_yaml(repo_dir)

extras = []

for extra in config["conda_extras"]:
extras.extend(config["extras_require"][extra])

all_requirements = validate_requirements(
compile_requirements(repo_dir, config["extras_require"]),
compile_requirements(repo_dir, extras),
config["conda_channels"],
)

Expand Down
4 changes: 3 additions & 1 deletion repo_helper/configuration/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

# this package
import repo_helper
from repo_helper.configuration.conda_anaconda import conda_channels, conda_description, enable_conda
from repo_helper.configuration.conda_anaconda import conda_channels, conda_description, conda_extras, enable_conda
from repo_helper.configuration.documentation import (
docs_dir,
enable_docs,
Expand Down Expand Up @@ -135,6 +135,7 @@
"author",
"classifiers",
"conda_channels",
"conda_extras",
"conda_description",
"console_scripts",
"copyright_years",
Expand Down Expand Up @@ -285,6 +286,7 @@ def parse_yaml(repo_path: PathLike) -> Dict:
travis_additional_requirements,
enable_conda,
conda_channels,
conda_extras,
conda_description,
additional_ignore,
yapf_exclude,
Expand Down
40 changes: 38 additions & 2 deletions repo_helper/configuration/conda_anaconda.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@
#

# stdlib
from typing import List
from typing import Any, Dict, List, Optional

# 3rd party
from configconfig.configvar import ConfigVar

# this package
from repo_helper.configuration import metadata

__all__ = ["enable_conda", "conda_channels", "conda_description"]
__all__ = ["enable_conda", "conda_channels", "conda_description", "conda_extras"]


class enable_conda(ConfigVar): # noqa
Expand Down Expand Up @@ -70,6 +70,42 @@ class conda_channels(ConfigVar): # noqa
category: str = "conda & anaconda"


class conda_extras(ConfigVar): # noqa
"""
A list of extras (see :conf:`extras_require`) to include as requirements in the Conda package.
| The special keyword ``all`` indicates all extras should be included.
| The special keyword ``none`` indicates no extras should be included.
Example:
.. code-block:: yaml
conda_extras:
- plotting
- xml
"""

dtype = List[str]
default: List[str] = ["all"]
category: str = "conda & anaconda"

@classmethod
def validate(cls, raw_config_vars: Optional[Dict[str, Any]] = None) -> List[str]:
extras = list(filter(None, super().validate(raw_config_vars)))

if "all" in extras and "none" in extras:
raise ValueError("'all' and 'none' are mutually exclusive.")

if "all" in extras and len(extras) > 1:
raise ValueError("'all' cannot be used alongside other values.")

if "none" in extras and len(extras) > 1:
raise ValueError("'none' cannot be used alongside other values.")

return extras


class conda_description(ConfigVar): # noqa
"""
A short description of the project for Anaconda.
Expand Down
7 changes: 7 additions & 0 deletions repo_helper/repo_helper_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,13 @@
},
"description": "A list of Anaconda channels required to build and use the Conda package."
},
"conda_extras": {
"type": "array",
"items": {
"type": "string"
},
"description": "A list of extras (see :conf:`extras_require`) to include as requirements in the Conda package."
},
"conda_description": {
"type": "string",
"description": "A short description of the project for Anaconda."
Expand Down
2 changes: 2 additions & 0 deletions tests/test_configuration_/test_parse_yaml.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ conda_channels:
- conda-forge
conda_description: Update multiple configuration files, build scripts etc. from a
single location.
conda_extras:
- all
console_scripts:
- repo_helper = repo_helper.__main__:main
- repo-helper = repo_helper.__main__:main
Expand Down

0 comments on commit bce178c

Please sign in to comment.