From 375ea621828c235f8c67f43176750ed68b8b1ff9 Mon Sep 17 00:00:00 2001 From: "E. G. Patrick Bos" Date: Thu, 19 Oct 2023 19:47:54 +0200 Subject: [PATCH 1/3] small fixup of #352 This commit changed the test_subpackage build command, but left the now unnecessary '--sdist' and '--wheel' parameters. They are on by default for `python -m build` --- tests/test_project.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/tests/test_project.py b/tests/test_project.py index 5fe61098..42bc02d3 100644 --- a/tests/test_project.py +++ b/tests/test_project.py @@ -80,7 +80,7 @@ def test_tox(baked_with_development_dependencies, project_env_bin_dir): def test_subpackage(baked_with_development_dependencies, project_env_bin_dir): - """Test if subpackages end up in sdist and bdist_wheel distributions""" + """Test if subpackages end up in (wheel) distributions""" project_dir = baked_with_development_dependencies bin_dir = project_env_bin_dir subpackage = (project_dir / 'my_python_package' / 'mysub') @@ -91,9 +91,14 @@ def test_subpackage(baked_with_development_dependencies, project_env_bin_dir): subsubpackage.mkdir() (subsubpackage / '__init__.py').write_text('FOO = "bar"\n', encoding="utf-8") - # sdist and bdist_wheel both call build command to create build/ dir - # So instead of looking in distribution archives we can look in build/ dir - result = run([f'{bin_dir}python', '-m', 'build', '--sdist', '--wheel'], project_dir) + # Note: we pass --wheel explicitly, because wheel has a useful side-effect + # of leaving a build directory after building that we can check for its + # contents in the asserts below. However, be aware that this behavior is + # not guaranteed to stay and is in fact a known bug / PEP-violation! + # See https://github.com/pypa/wheel/issues/447. Also, by passing --wheel + # explicitly (although by default build already builds a wheel as well), + # we omit the sdist being built, saving some seconds. + result = run([f'{bin_dir}python', '-m', 'build', '--wheel'], project_dir) assert result.returncode == 0 assert (project_dir / 'build' / 'lib' / 'my_python_package' / 'mysub' / '__init__.py').exists() assert (project_dir / 'build' / 'lib' / 'my_python_package' / 'mysub' / 'mysub2' / '__init__.py').exists() From e24c0aa1340bd401f309ad9eb3cc97db429032b1 Mon Sep 17 00:00:00 2001 From: "E. G. Patrick Bos" Date: Fri, 20 Oct 2023 13:23:19 +0200 Subject: [PATCH 2/3] switch to python -m build --- README.md | 2 -- .../.github/workflows/build.yml | 2 +- {{cookiecutter.directory_name}}/README.dev.md | 32 ++++++------------- .../pyproject.toml | 3 +- 4 files changed, 13 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 2e3095bc..d70b7d73 100644 --- a/README.md +++ b/README.md @@ -135,8 +135,6 @@ my-python-project/ ├── pyproject.toml ├── README.dev.md ├── README.md -├── setup.cfg -├── setup.py ├── sonar-project.properties └── tests ├── __init__.py diff --git a/{{cookiecutter.directory_name}}/.github/workflows/build.yml b/{{cookiecutter.directory_name}}/.github/workflows/build.yml index 80ec495f..b2e166db 100644 --- a/{{cookiecutter.directory_name}}/.github/workflows/build.yml +++ b/{{cookiecutter.directory_name}}/.github/workflows/build.yml @@ -36,7 +36,7 @@ jobs: - name: Run unit tests run: python -m pytest -v - name: Verify that we can build the package - run: python setup.py sdist bdist_wheel + run: python -m build lint: name: Linting build diff --git a/{{cookiecutter.directory_name}}/README.dev.md b/{{cookiecutter.directory_name}}/README.dev.md index 7217de9b..921634c0 100644 --- a/{{cookiecutter.directory_name}}/README.dev.md +++ b/{{cookiecutter.directory_name}}/README.dev.md @@ -138,35 +138,23 @@ This section describes how to make a release in 3 parts: ### (2/3) PyPI -In a new terminal, without an activated virtual environment or an env directory: +In a new terminal: ```shell -# prepare a new directory +# OPTIONAL: prepare a new directory with fresh git clone to ensure the release +# has the state of origin/main branch cd $(mktemp -d {{ cookiecutter.package_name }}.XXXXXX) - -# fresh git clone ensures the release has the state of origin/main branch git clone {{ cookiecutter.repository }} . -# prepare a clean virtual environment and activate it -python -m venv env -source env/bin/activate - -# make sure to have a recent version of pip and setuptools -python -m pip install --upgrade pip setuptools - -# install runtime dependencies and publishing dependencies -python -m pip install --no-cache-dir . -python -m pip install --no-cache-dir .[publishing] - -# clean up any previously generated artefacts -rm -rf {{ cookiecutter.package_name }}.egg-info -rm -rf dist +# make sure to have a recent version of pip and the publishing dependencies +python -m pip install --upgrade pip +python -m pip install .[publishing] # create the source distribution and the wheel -python setup.py sdist bdist_wheel +python -m build # upload to test pypi instance (requires credentials) -twine upload --repository-url https://test.pypi.org/legacy/ dist/* +python -m twine upload --repository testpypi dist/* ``` Visit @@ -183,7 +171,7 @@ python -m venv env source env/bin/activate # make sure to have a recent version of pip and setuptools -python -m pip install --upgrade pip setuptools +python -m pip install --upgrade pip # install from test pypi instance: python -m pip -v install --no-cache-dir \ @@ -198,7 +186,7 @@ Then upload to pypi.org with: ```shell # Back to the first terminal, # FINAL STEP: upload to PyPI (requires credentials) -twine upload dist/* +python -m twine upload dist/* ``` ### (3/3) GitHub diff --git a/{{cookiecutter.directory_name}}/pyproject.toml b/{{cookiecutter.directory_name}}/pyproject.toml index f29a49b1..7c216c7f 100644 --- a/{{cookiecutter.directory_name}}/pyproject.toml +++ b/{{cookiecutter.directory_name}}/pyproject.toml @@ -43,7 +43,7 @@ version = "{{ cookiecutter.version }}" [project.optional-dependencies] dev = [ - "build", + "build", # build is not only used in publishing (below), but also in the template's test suite "bump2version", "coverage [toml]", "pytest", @@ -56,6 +56,7 @@ dev = [ "myst_parser", ] publishing = [ + "build", "twine", "wheel", ] From 0635bcdd521a61a6459e5bd592bde423c5d70b4a Mon Sep 17 00:00:00 2001 From: "E. G. Patrick Bos" Date: Fri, 20 Oct 2023 13:24:14 +0200 Subject: [PATCH 3/3] use setup-python@v3 action in documentation workflow For some reason, we use setup-python@v3 in all workflows, except the documentation one, which still had v2. --- .../.github/workflows/documentation.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/{{cookiecutter.directory_name}}/.github/workflows/documentation.yml b/{{cookiecutter.directory_name}}/.github/workflows/documentation.yml index 4338910e..2bccfd13 100644 --- a/{{cookiecutter.directory_name}}/.github/workflows/documentation.yml +++ b/{{cookiecutter.directory_name}}/.github/workflows/documentation.yml @@ -17,7 +17,7 @@ jobs: steps: - uses: actions/checkout@v2 - name: Set up Python 3.9 - uses: actions/setup-python@v2 + uses: actions/setup-python@v3 with: python-version: 3.9 - name: Python info