open-contracting / software-development-handbook

A guide for developers of OCP's tools
https://ocp-software-handbook.readthedocs.io/en/latest/
Other
4 stars 1 forks source link

Transition from setup.py to setup.cfg #62

Closed jpmckinney closed 1 year ago

jpmckinney commented 2 years ago

https://setuptools.pypa.io/en/latest/userguide/quickstart.html#transitioning-from-setup-py-to-setup-cfg

setuptools' pyproject.toml support is still experimental.

jpmckinney commented 1 year ago

Update: pyproject.toml is no longer experimental, but some features are still beta.

jpmckinney commented 1 year ago

One-time setup:

pip install setuptools-py2cfg

Run:

setuptools-py2cfg -a | sed /long_description_content_type/d >> setup.cfg
sed -i '' '/^dist/i\
/build' .gitignore
sed -i '' 's/cache-dependency-path: setup.py/cache-dependency-path: setup.cfg/' .github/workflows/{ci,lint}.yml
sed -i '' 's/ setuptools//' .github/workflows/lint.yml
if [ -f pyproject.toml ]
sed -i '' '1i \
[build-system]\
requires = ["setuptools>=61.2"]\
build-backend = "setuptools.build_meta"\
\
' pyproject.toml
else
echo '[build-system]
requires = ["setuptools>=61.2"]
build-backend = "setuptools.build_meta"' > pyproject.toml
end
git apply ../setup-patch
git rm setup.py
git add pyproject.toml setup.cfg

Then:

git diff HEAD

where setup-patch is:

diff --git a/.github/workflows/pypi.yml b/.github/workflows/pypi.yml
index 9301126..780ff22 100644
--- a/.github/workflows/pypi.yml
+++ b/.github/workflows/pypi.yml
@@ -8,8 +8,8 @@ jobs:
       - uses: actions/setup-python@v4
         with:
           python-version: 3.8
-      - run: pip install --upgrade setuptools wheel
-      - run: python setup.py sdist bdist_wheel
+      - run: pip install --upgrade build
+      - run: python -m build --sdist --wheel
       - name: Publish to TestPyPI
         uses: pypa/gh-action-pypi-publish@release/v1
         with:

This yields a setup.cfg file that can be used right away.

Then check occurrences across all files:

setup.py\b|(?<!(?:\n# pip| file:)\n# )setuptools(?!>=61\.2|\.build_meta)|(?<!--)wheel\b(?!==)

For TOML

pip install ini2toml
sed -i "" 's/packages=.*//' setup.cfg | ini2toml -p setup.cfg -

This yields a decent first conversion to a pyproject.toml file. Can tidy:

Still need to:

jpmckinney commented 1 year ago

Closing as transitioned to setup.cfg, which protects us from future deprecations.

See https://setuptools.pypa.io/en/latest/userguide/quickstart.html

It is important to remember, however, that running this file as a script (e.g. python setup.py sdist) is strongly discouraged, and that the majority of the command line interfaces are (or will be) deprecated (e.g. python setup.py install, python setup.py bdist_wininst, …).

We also recommend users to expose as much as possible configuration in a more declarative way via the pyproject.toml or setup.cfg, and keep the setup.py minimal with only the dynamic parts (or even omit it completely if applicable).


We use build to replace python setup.py sdist wheel.

We still use setuptools for metadata. We could change to flit, which would require changing the build-system and some options like install_requires to dependencies, etc. However, we've already solved the problems that flit addresses (ensuring data files are included, versions are correctly set, etc.) through documentation, templates, CI, etc. Flit's metadata format seems to be unstable. Setuptools remains the de-facto standard.


For setuptools, TOML features in beta are: