From 4c22a6ca57753d3b5604a90b61a0c6c5efe53a1d Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Fri, 18 Oct 2019 20:37:42 -0400 Subject: Add new hook 'setuptools.finalize_distribution_options' for plugins like 'setuptools_scm' to alter distribution options. --- setuptools/dist.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/setuptools/dist.py b/setuptools/dist.py index 2e5ad4bd..987d684e 100644 --- a/setuptools/dist.py +++ b/setuptools/dist.py @@ -728,6 +728,10 @@ class Distribution(_Distribution): if self.features: self._set_global_opts_from_features() + hook_key = 'setuptools.finalize_distribution_options' + for ep in pkg_resources.iter_entry_points(hook_key): + ep.load()(self) + for ep in pkg_resources.iter_entry_points('distutils.setup_keywords'): value = getattr(self, ep.name, None) if value is not None: -- cgit v1.2.1 From 6b210c65938527a4bbcea34942fe43971be3c014 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sat, 16 Nov 2019 12:06:47 -0500 Subject: Move all finalization of distribution options into hooks. Allow hooks to specify an index for ordering. --- setup.py | 7 +++++++ setuptools/dist.py | 24 ++++++++++++++++++------ 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/setup.py b/setup.py index f5030dd6..ac56a1b0 100755 --- a/setup.py +++ b/setup.py @@ -89,6 +89,13 @@ setup_params = dict( "%(cmd)s = setuptools.command.%(cmd)s:%(cmd)s" % locals() for cmd in read_commands() ], + "setuptools.finalize_distribution_options": [ + "parent_finalize = setuptools.dist:_Distribution.finalize_options", + "features = setuptools.dist:Distribution._finalize_feature_opts", + "keywords = setuptools.dist:Distribution._finalize_setup_keywords", + "2to3_doctests = " + "setuptools.dist:Distribution._finalize_2to3_doctests", + ], "distutils.setup_keywords": [ "eager_resources = setuptools.dist:assert_string_list", "namespace_packages = setuptools.dist:check_nsp", diff --git a/setuptools/dist.py b/setuptools/dist.py index 987d684e..44990431 100644 --- a/setuptools/dist.py +++ b/setuptools/dist.py @@ -724,19 +724,28 @@ class Distribution(_Distribution): return resolved_dists def finalize_options(self): - _Distribution.finalize_options(self) - if self.features: - self._set_global_opts_from_features() - + """ + Allow plugins to apply arbitrary operations to the + distribution. Each hook may optionally define a 'order' + to influence the order of execution. Smaller numbers + go first and the default is 0. + """ hook_key = 'setuptools.finalize_distribution_options' - for ep in pkg_resources.iter_entry_points(hook_key): + + def by_order(hook): + return getattr(hook, 'order', 0) + eps = pkg_resources.iter_entry_points(hook_key) + for ep in sorted(eps, key=by_order): ep.load()(self) + def _finalize_setup_keywords(self): for ep in pkg_resources.iter_entry_points('distutils.setup_keywords'): value = getattr(self, ep.name, None) if value is not None: ep.require(installer=self.fetch_build_egg) ep.load()(self, ep.name, value) + + def _finalize_2to3_doctests(self): if getattr(self, 'convert_2to3_doctests', None): # XXX may convert to set here when we can rely on set being builtin self.convert_2to3_doctests = [ @@ -790,9 +799,12 @@ class Distribution(_Distribution): cmd.ensure_finalized() return cmd.easy_install(req) - def _set_global_opts_from_features(self): + def _finalize_feature_opts(self): """Add --with-X/--without-X options based on optional features""" + if not self.features: + return + go = [] no = self.negative_opt.copy() -- cgit v1.2.1 From 88951e9a5633abd4cdbfa3584647cec4247b8c30 Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Sat, 16 Nov 2019 12:47:35 -0500 Subject: Add changelog entry and documentation about the feature. --- changelog.d/1877.change.rst | 1 + docs/setuptools.txt | 31 +++++++++++++++++++++++++++---- 2 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 changelog.d/1877.change.rst diff --git a/changelog.d/1877.change.rst b/changelog.d/1877.change.rst new file mode 100644 index 00000000..5a744fa3 --- /dev/null +++ b/changelog.d/1877.change.rst @@ -0,0 +1 @@ +Setuptools now exposes a new entry point hook "setuptools.finalize_distribution_options", enabling plugins like `setuptools_scm `_ to configure options on the distribution at finalization time. diff --git a/docs/setuptools.txt b/docs/setuptools.txt index 2e7fe3bd..ccbf069b 100644 --- a/docs/setuptools.txt +++ b/docs/setuptools.txt @@ -1225,7 +1225,7 @@ the quoted part. Distributing a ``setuptools``-based project =========================================== -Detailed instructions to distribute a setuptools project can be found at +Detailed instructions to distribute a setuptools project can be found at `Packaging project tutorials`_. .. _Packaging project tutorials: https://packaging.python.org/tutorials/packaging-projects/#generating-distribution-archives @@ -1241,7 +1241,7 @@ setup.py is located:: This will generate distribution archives in the `dist` directory. -Before you upload the generated archives make sure you're registered on +Before you upload the generated archives make sure you're registered on https://test.pypi.org/account/register/. You will also need to verify your email to be able to upload any packages. You should install twine to be able to upload packages:: @@ -1264,11 +1264,11 @@ Distributing legacy ``setuptools`` projects using ez_setup.py .. warning:: **ez_setup** is deprecated in favor of PIP with **PEP-518** support. -Distributing packages using the legacy ``ez_setup.py`` and ``easy_install`` is +Distributing packages using the legacy ``ez_setup.py`` and ``easy_install`` is deprecated in favor of PIP. Please consider migrating to using pip and twine based distribution. -However, if you still have any ``ez_setup`` based packages, documentation for +However, if you still have any ``ez_setup`` based packages, documentation for ez_setup based distributions can be found at `ez_setup distribution guide`_. .. _ez_setup distribution guide: ez_setup.html @@ -2515,6 +2515,10 @@ script defines entry points for them! Adding ``setup()`` Arguments ---------------------------- +.. warning:: Adding arguments to setup is discouraged as such arguments + are only supported through imperative execution and not supported through + declarative config. + Sometimes, your commands may need additional arguments to the ``setup()`` call. You can enable this by defining entry points in the ``distutils.setup_keywords`` group. For example, if you wanted a ``setup()`` @@ -2566,6 +2570,25 @@ script using your extension lists your project in its ``setup_requires`` argument. +Customizing Distribution Options +-------------------------------- + +Plugins may wish to extend or alter the options on a Distribution object to +suit the purposes of that project. For example, a tool that infers the +``Distribution.version`` from SCM-metadata may need to hook into the +option finalization. To enable this feature, Setuptools offers an entry +point "setuptools.finalize_distribution_options". That entry point must +be a callable taking one argument (the Distribution instance). + +If the callable has an ``.order`` property, that value will be used to +determine the order in which the hook is called. Lower numbers are called +first and the default is zero (0). + +Plugins may read, alter, and set properties on the distribution, but each +plugin is encouraged to load the configuration/settings for their behavior +independently. + + Adding new EGG-INFO Files ------------------------- -- cgit v1.2.1