Skip to contentSkip to conversation

Python Build Modernization

PATCH In review Jeremy Sowden 2 participants 5 messages Jul 31, 2023 – Aug 3, 2023 netfilter

lore
Share & tools
lore
#0 lore raw

cover Python Build Modernization

Directly invoking setup.py to build and install a Python module is deprecated:

https://blog.ganssle.io/articles/2021/10/setup-py-deprecated.html

Pablo recently removed the autotooling which did this. This patch-set updates the Python build configuration to add support for modern alternatives while leaving the setup.py script around for users who may need it.

The approach I have used is described here:

https://setuptools.pypa.io/en/latest/build_meta.html

We move the setuptools configuration out of setup.py into setup.cfg, and then add a pyproject.toml configuration file to tell PEP-517 build tools to use setuptools.

Setuptools also supports putting all its configuration into pyproject.toml files:

https://setuptools.pypa.io/en/latest/userguide/pyproject_config.html

However, this is much more recent than its setup.cfg support (2022 vs 2016), which is why I decided to stick with the approach described above.

Jeremy Sowden (3):
py: move package source into src directory
py: use setup.cfg to configure setuptools
py: add pyproject.toml to support PEP-517-compatible build-systems

 INSTALL                  |  3 ++-
 py/Makefile.am           |  2 +-
 py/pyproject.toml        |  3 +++
 py/setup.cfg             | 24 ++++++++++++++++++++++++
 py/setup.py              | 23 ++---------------------
 py/{ => src}/__init__.py |  0
 py/{ => src}/nftables.py |  0
 py/{ => src}/schema.json |  0
 8 files changed, 32 insertions(+), 23 deletions(-)
 create mode 100644 py/pyproject.toml
 create mode 100644 py/setup.cfg
 rename py/{ => src}/__init__.py (100%)
 rename py/{ => src}/nftables.py (100%)
 rename py/{ => src}/schema.json (100%)
Signature · 1 line
2.40.1
#1 lore raw

PATCH 1/3 py: move package source into src directory

Separate the actual package source from the build files. In addition to being a bit tidier, this will prevent setup.py being erroneously installed when we introduce PEP-517 support in a later commit.

---
 py/Makefile.am           | 2 +-
 py/setup.py              | 2 +-
 py/{ => src}/__init__.py | 0
 py/{ => src}/nftables.py | 0
 py/{ => src}/schema.json | 0
 5 files changed, 2 insertions(+), 2 deletions(-)
 rename py/{ => src}/__init__.py (100%)
 rename py/{ => src}/nftables.py (100%)
 rename py/{ => src}/schema.json (100%)
py/Makefile.am +1 -1
index f10ae360599f..4056aa61f820 100644
@@ -1 +1 @@
-EXTRA_DIST = setup.py __init__.py nftables.py schema.json
+EXTRA_DIST = setup.py src
py/setup.py +1 -1
index 8ad73e7b58e5..d08b8b129a81 100755
@@ -10,7 +10,7 @@ setup(name='nftables',
       url='https://netfilter.org/projects/nftables/index.html',
       packages=['nftables'],
       provides=['nftables'],
-      package_dir={'nftables':'.'},
+      package_dir={'nftables':'src'},
       package_data={'nftables':['schema.json']},
       classifiers=[
           'Development Status :: 4 - Beta',
py/src/__init__.py renamed from py/__init__.py +0 -0
similarity index 100%
rename from py/__init__.py
rename to py/src/__init__.py
py/src/nftables.py renamed from py/nftables.py +0 -0
similarity index 100%
rename from py/nftables.py
rename to py/src/nftables.py
py/src/schema.json renamed from py/schema.json +0 -0
similarity index 100%
rename from py/schema.json
rename to py/src/schema.json
Signature · 1 line
2.40.1
#2 lore raw

PATCH 2/3 py: use setup.cfg to configure setuptools

In reply to Jeremy Sowden

Setuptools has had support for declarative configuration for several years. To quote their documentation:

Setuptools allows using configuration files (usually setup.cfg) to define a package’s metadata and other options that are normally supplied to the setup() function (declarative config).

This approach not only allows automation scenarios but also reduces boilerplate code in some cases.

Additionally, this allows us to introduce support for PEP-517-compatible build-systems.

---
 py/Makefile.am |  2 +-
 py/setup.cfg   | 24 ++++++++++++++++++++++++
 py/setup.py    | 23 ++---------------------
 3 files changed, 27 insertions(+), 22 deletions(-)
 create mode 100644 py/setup.cfg
py/Makefile.am +1 -1
index 4056aa61f820..974539fd44ad 100644
@@ -1 +1 @@
-EXTRA_DIST = setup.py src
+EXTRA_DIST = setup.cfg setup.py src
py/setup.cfg new file +24 -0
new file mode 100644
index 000000000000..953b7f4164b4
@@ -0,0 +1,24 @@
+[metadata]
+name = nftables
+version = attr: nftables.NFTABLES_VERSION
+description = Libnftables binding
+author = Netfilter project
+author_email = [email protected]
+url = https://netfilter.org/projects/nftables/index.html
+provides = nftables
+classifiers =
+  Development Status :: 4 - Beta
+  Environment :: Console
+  Intended Audience :: Developers
+  License :: OSI Approved :: GNU General Public License v2 (GPLv2)
+  Operating System :: POSIX :: Linux
+  Programming Language :: Python
+  Topic :: System :: Networking :: Firewalls
+
+[options]
+packages = nftables
+package_dir =
+  nftables = src
+
+[options.package_data]
+nftables = schema.json
py/setup.py +2 -21
index d08b8b129a81..beda28e82166 100755
@@ -1,24 +1,5 @@
 #!/usr/bin/env python
+
 from setuptools import setup
-from nftables import NFTABLES_VERSION
 
-setup(name='nftables',
-      version=NFTABLES_VERSION,
-      description='Libnftables binding',
-      author='Netfilter project',
-      author_email='[email protected]',
-      url='https://netfilter.org/projects/nftables/index.html',
-      packages=['nftables'],
-      provides=['nftables'],
-      package_dir={'nftables':'src'},
-      package_data={'nftables':['schema.json']},
-      classifiers=[
-          'Development Status :: 4 - Beta',
-          'Environment :: Console',
-          'Intended Audience :: Developers',
-          'License :: OSI Approved :: GNU General Public License v2 (GPLv2)',
-          'Operating System :: POSIX :: Linux',
-          'Programming Language :: Python',
-          'Topic :: System :: Networking :: Firewalls',
-          ],
-      )
+setup()
Signature · 1 line
2.40.1
#3 lore raw

PATCH 3/3 py: add pyproject.toml to support PEP-517-compatible build-systems

In reply to Jeremy Sowden

This makes it possible to build and install the module without directly invoking setup.py which has been deprecated.

Retain the setup.py script for backwards-compatibility.

Update INSTALL to mention the new config-file.

---
 INSTALL           | 3 ++-
 py/Makefile.am    | 2 +-
 py/pyproject.toml | 3 +++
 3 files changed, 6 insertions(+), 2 deletions(-)
 create mode 100644 py/pyproject.toml
INSTALL +2 -1
index 9b626745d7a4..53021e5aafc3 100644
@@ -86,7 +86,8 @@ Installation instructions for nftables
 
  CPython bindings are available for nftables under the py/ folder.
 
- setup.py is provided to install it.
+ A pyproject.toml config file and legacy setup.py script are provided to install
+ it.
 
  Source code
  ===========
py/Makefile.am +1 -1
index 974539fd44ad..76aa082f8709 100644
@@ -1 +1 @@
-EXTRA_DIST = setup.cfg setup.py src
+EXTRA_DIST = pyproject.toml setup.cfg setup.py src
py/pyproject.toml new file +3 -0
new file mode 100644
index 000000000000..fed528d4a7a1
@@ -0,0 +1,3 @@
+[build-system]
+requires = ["setuptools"]
+build-backend = "setuptools.build_meta"
Signature · 1 line
2.40.1