-
Notifications
You must be signed in to change notification settings - Fork 610
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
126 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
#!python3 | ||
from __future__ import annotations | ||
|
||
import argparse | ||
import sys | ||
from pathlib import Path | ||
|
||
if sys.version_info >= (3, 11): | ||
import tomllib | ||
else: | ||
import tomli as tomllib | ||
from packaging.requirements import Requirement | ||
from packaging.version import Version | ||
|
||
|
||
def min_dep(req: Requirement) -> str: | ||
""" | ||
Given a requirement, return the minimum version specifier. | ||
Example | ||
------- | ||
>>> min_dep(Requirement("numpy>=1.0")) | ||
"numpy==1.0" | ||
""" | ||
req_name = req.name | ||
if req.extras: | ||
req_name = f"{req_name}[{','.join(req.extras)}]" | ||
|
||
# TODO: Should this be allowed? | ||
if not req.specifier: | ||
return req_name | ||
|
||
min_version = Version("0.0.0.a1") | ||
for spec in req.specifier: | ||
if spec.operator in [">", ">=", "~-"]: | ||
min_version = max(min_version, Version(spec.version)) | ||
elif spec.operator == "==": | ||
min_version = Version(spec.version) | ||
|
||
# TODO: should this return `~=` or `==`? | ||
return f"{req_name}=={min_version}.*" | ||
|
||
|
||
def extract_min_deps( | ||
dependencies: list[str], | ||
*, | ||
pyproject | ||
) -> list[str]: | ||
dependencies = dependencies.copy() # We'll be mutating this | ||
requirements: list[Requirement] = [] | ||
project_name = pyproject["project"]["name"] | ||
|
||
while len(dependencies) > 0: | ||
req = Requirement(dependencies.pop()) | ||
|
||
# If we are reffering to other optional dependency lists, resolve them | ||
if req.name == project_name: | ||
assert req.extras, f"Project included itself as dependency, without specifying extras: {req}" | ||
for extra in req.extras: | ||
dependencies.extend(pyproject["project"]["optional-dependencies"][extra]) | ||
else: | ||
requirements.append(min_dep(req)) | ||
|
||
return requirements | ||
|
||
|
||
def main(): | ||
# TODO: Allow optional dependencies | ||
parser = argparse.ArgumentParser( | ||
prog="min-deps", | ||
description="""Parse a pyproject.toml file and output a list of minimum dependencies. | ||
Output is directly passable to `pip install`.""", | ||
usage="pip install `python min-deps.py pyproject.toml`", | ||
) | ||
parser.add_argument( | ||
"path", type=Path, help="pyproject.toml to parse minimum dependencies from" | ||
) | ||
parser.add_argument("--extras", type=str, nargs="*", help="extras to install") | ||
|
||
args = parser.parse_args() | ||
|
||
pyproject = tomllib.loads(args.path.read_text()) | ||
|
||
project_name = pyproject["project"]["name"] | ||
deps = pyproject["project"]["dependencies"] | ||
|
||
for extra in args.extras: | ||
deps.append(f"{project_name}[{extra}]") | ||
|
||
min_deps = extract_min_deps(deps, pyproject=pyproject) | ||
|
||
print(" ".join(min_deps)) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
mamba env remove -yn scanpy-min-deps-test | ||
mamba create -yn scanpy-min-deps-test "python=3.9" | ||
|
||
PACKAGES=`python3 ci/scripts/min-deps.py pyproject.toml --extra dev test` | ||
|
||
# conda activate anndata-min-deps-test | ||
# conda run -n anndata-min-deps-test pip install cupy-cuda12x | ||
|
||
|
||
echo Installing $PACKAGES | ||
conda run -n scanpy-min-deps-test pip install $PACKAGES | ||
conda run -n scanpy-min-deps-test pip install pytest-xdist # cupy-cuda12x | ||
conda run -n scanpy-min-deps-test pip install -e . --no-deps | ||
echo "Starting tests" | ||
conda run -n scanpy-min-deps-test pytest -n auto | ||
|
||
conda list -n scanpy-min-deps-tests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters