From fb2c69d21f2a187fb9dd03cac79e5490b7729af2 Mon Sep 17 00:00:00 2001 From: Mike Taves Date: Wed, 2 Aug 2023 22:40:47 +1200 Subject: [PATCH] Generate requirements files from pyproject.toml --- .pre-commit-config.yaml | 4 +- pyproject.toml | 12 +----- requirements/developer.txt | 2 +- requirements/doc.txt | 1 + requirements/test.txt | 1 + tools/generate_pyproject.toml.py | 52 ----------------------- tools/generate_requirements.py | 22 ++++++++++ tools/pyproject.toml.in | 71 -------------------------------- 8 files changed, 29 insertions(+), 136 deletions(-) delete mode 100755 tools/generate_pyproject.toml.py create mode 100755 tools/generate_requirements.py delete mode 100644 tools/pyproject.toml.in diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index bd9d7e4d..5f9d513f 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -44,5 +44,5 @@ repos: - id: pyproject.toml name: pyproject.toml language: system - entry: python tools/generate_pyproject.toml.py - files: "pyproject.toml|requirements/.*\\.txt|tools/.*pyproject.*" + entry: python tools/generate_requirements.py + files: "pyproject.toml|requirements/.*\\.txt|tools/.*generate.*" diff --git a/pyproject.toml b/pyproject.toml index 7e17b148..2dedbcfe 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,13 +1,3 @@ -################################################################################ -# DO NOT EDIT -# AUTOGENERATED BY -# -# $ python tools/generate_pyproject.toml.py -# -# EDIT tools/pyproject.toml.in AND RUN THAT SCRIPT. -# -################################################################################ - [build-system] build-backend = 'setuptools.build_meta' requires = ['setuptools>=61.2'] @@ -67,6 +57,7 @@ test = [ [project.scripts] validate-docstrings = 'numpydoc.hooks.validate_docstrings:main' + [tool.setuptools] include-package-data = false packages = [ @@ -84,6 +75,7 @@ numpydoc = [ 'tests/tinybuild/*.py', 'templates/*.rst', ] + [tool.pytest.ini_options] addopts = ''' --showlocals --doctest-modules -ra --cov-report= --cov=numpydoc diff --git a/requirements/developer.txt b/requirements/developer.txt index 832c8cbe..fb283a26 100644 --- a/requirements/developer.txt +++ b/requirements/developer.txt @@ -1,2 +1,2 @@ pre-commit>=3.3 -rtoml +tomli; python_version < '3.11' diff --git a/requirements/doc.txt b/requirements/doc.txt index 0cdb577e..df1d26ef 100644 --- a/requirements/doc.txt +++ b/requirements/doc.txt @@ -1,3 +1,4 @@ +# Generated from pyproject.toml numpy>=1.22 matplotlib>=3.5 pydata-sphinx-theme>=0.13 diff --git a/requirements/test.txt b/requirements/test.txt index 5f58f032..27e43374 100644 --- a/requirements/test.txt +++ b/requirements/test.txt @@ -1,3 +1,4 @@ +# Generated from pyproject.toml pytest pytest-cov matplotlib diff --git a/tools/generate_pyproject.toml.py b/tools/generate_pyproject.toml.py deleted file mode 100755 index bc7d9671..00000000 --- a/tools/generate_pyproject.toml.py +++ /dev/null @@ -1,52 +0,0 @@ -#!/usr/bin/env python - -import os -import sys - -try: - import rtoml as toml -except ImportError: - print("Please install `rtoml` first: `pip install rtoml`") - sys.exit(1) - - -if not os.path.isfile("pyproject.toml"): - print("Please run this script from the skimage repository root:") - print(" python tools/generate_pyproject.toml.py") - sys.exit(1) - - -def parse_requirements_file(filename): - with open(filename) as fid: - requires = [l.strip() for l in fid.readlines() if not l.startswith("#")] - requires = [l for l in requires if l] - - return requires - - -with open("tools/pyproject.toml.in") as f: - pyproject = toml.load(f) - -# pyproject['project']['dependencies'] = \ -# parse_requirements_file("requirements/default.txt") - -pyproject["project"]["optional-dependencies"] = { - dep: parse_requirements_file(f"requirements/{dep}.txt") for dep in ["doc", "test"] -} - -banner = f"""\ -{"#" * 80} -# DO NOT EDIT -# AUTOGENERATED BY -# -# $ python tools/generate_pyproject.toml.py -# -# EDIT tools/pyproject.toml.in AND RUN THAT SCRIPT. -# -{"#" * 80} - -""" - -with open("pyproject.toml", "w") as f: - f.write(banner) - toml.dump(pyproject, f, pretty=True) diff --git a/tools/generate_requirements.py b/tools/generate_requirements.py new file mode 100755 index 00000000..4e24ed45 --- /dev/null +++ b/tools/generate_requirements.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +"""Generate requirements/*.txt files from pyproject.toml.""" + +import sys +from pathlib import Path + +try: # standard module since Python 3.11 + import tomllib as toml +except ImportError: + try: # available for older Python via pip + import tomli as toml + except ImportError: + sys.exit("Please install `tomli` first: `pip install tomli`") + +repo_dir = (Path(__file__).parent / "..").resolve() +req_dir = repo_dir / "requirements" +pyproject = toml.loads((repo_dir / "pyproject.toml").read_text()) + +for key, opt_list in pyproject["project"]["optional-dependencies"].items(): + lines = ["# Generated from pyproject.toml"] + opt_list + req_fname = req_dir / f"{key}.txt" + req_fname.write_text("\n".join(lines) + "\n") diff --git a/tools/pyproject.toml.in b/tools/pyproject.toml.in deleted file mode 100644 index 51d33b64..00000000 --- a/tools/pyproject.toml.in +++ /dev/null @@ -1,71 +0,0 @@ -[build-system] -requires = [ - "setuptools>=61.2", -] -build-backend = "setuptools.build_meta" - -[project] -name = "numpydoc" -description = "Sphinx extension to support docstrings in Numpy format" -license = {file = "LICENSE.txt"} -dynamic = ['version'] -keywords = [ - "sphinx", - "numpy", -] -readme = "README.rst" -classifiers = [ - "Development Status :: 4 - Beta", - "Environment :: Plugins", - "License :: OSI Approved :: BSD License", - "Topic :: Documentation", - "Programming Language :: Python", - "Programming Language :: Python :: 3", - "Programming Language :: Python :: 3.8", - "Programming Language :: Python :: 3.9", - "Programming Language :: Python :: 3.10", - "Programming Language :: Python :: 3.11", -] -requires-python = ">=3.8" -dependencies = [ - "sphinx>=5", - "Jinja2>=2.10", - "tabulate>=0.8.10", - "tomli>=1.1.0;python_version<'3.11'", -] - -[[project.authors]] -name = "Pauli Virtanen and others" -email = "pav@iki.fi" - -[project.urls] -Homepage = "https://numpydoc.readthedocs.io" -Source = "https://github.com/numpy/numpydoc/" - -[project.optional-dependencies] - -[project.scripts] -validate-docstrings = "numpydoc.hooks.validate_docstrings:main" - -[tool.setuptools] -packages = [ - "numpydoc", - "numpydoc.hooks", -] -include-package-data = false - -[tool.setuptools.dynamic] -version = {attr = "numpydoc.__version__"} - -[tool.setuptools.package-data] -numpydoc = [ - "tests/test_*.py", - "tests/tinybuild/Makefile", - "tests/tinybuild/index.rst", - "tests/tinybuild/*.py", - "templates/*.rst", -] - -[tool.pytest.ini_options] -addopts = "--showlocals --doctest-modules -ra --cov-report= --cov=numpydoc\n--junit-xml=junit-results.xml --ignore=doc/ --ignore=tools/" -junit_family = "xunit2"