Skip to content

Commit

Permalink
Allowed setuptools.finalize_distribution_options hook to request the …
Browse files Browse the repository at this point in the history
…stuff it needs.
  • Loading branch information
KOLANICH committed Mar 19, 2020
1 parent 08bc0b9 commit 682b002
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
4 changes: 4 additions & 0 deletions changelog.d/2034.change.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Now ''setuptools.finalize_distribution_options'' hook allows the implementers to ast setuptools to provide them various information by adding a parameter with a certain name into the function signature.
Old behavior is also supported.

For the list of args names see ''DistributionoptionsFinalizationRemap'' in ''dist.py''.
47 changes: 46 additions & 1 deletion setuptools/dist.py
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,8 @@ def fetch_build_eggs(self, requires):
pkg_resources.working_set.add(dist, replace=True)
return resolved_dists

optsFinalizationRemap = None # set later

def finalize_options(self):
"""
Allow plugins to apply arbitrary operations to the
Expand All @@ -789,6 +791,23 @@ def by_order(hook):

fdohac = FinalizeDistributionOptionsHookArgsCache()

def constructArgsForAFunc(f, ep, fdohac):
if sys.version_info.major >= 3:
p = inspect.getfullargspec(f).args
else:
p = inspect.getargspec(f)[0]

if len(p) == 1:
firstParam = p[0]
if firstParam not in self.optsFinalizationRemap:
return [self]

args = []
for parN in p:
a = self.optsFinalizationRemap[parN](self, ep, fdohac, parN)
args.append(a)
return args

eps = pkg_resources.iter_entry_points(hook_key)

for ep in sorted(eps, key=by_order):
Expand All @@ -801,7 +820,16 @@ def by_order(hook):
)
continue

f(self)
try:
args = constructArgsForAFunc(f, ep, fdohac)
except Exception as ex:
warnings.warn(
repr(ep) + " is incompatible to the current"
" version of setuptools: " + str(ex)
)
continue

f(*args)

@staticmethod
def _finalize_setup_keywords(dist):
Expand Down Expand Up @@ -1115,6 +1143,23 @@ def handle_display_options(self, option_order):
sys.stdout.detach(), encoding, errors, newline, line_buffering)


def pyProjectTomlSectionFinRemap(sself, entryPoint, fdohac, par):
return fdohac.pyProjectToml.get(
entryPoint.name, None
)


Distribution.optsFinalizationRemap = {
"dist": lambda sself, entryPoint, fdohac, par: sself,
"setupPySection": lambda sself, entryPoint, fdohac, par: sself.get(
entryPoint.name, None
),
"setupPyDir": lambda sself, entryPoint, fdohac, par: fdohac.setupPyDir,
"pyProjectTomlSection": pyProjectTomlSectionFinRemap,
"entryPoint": lambda sself, entryPoint, fdohac, par: entryPoint,
}


class DistDeprecationWarning(SetuptoolsDeprecationWarning):
"""Class for warning about deprecations in dist in
setuptools. Not ignored by default, unlike DeprecationWarning."""

0 comments on commit 682b002

Please sign in to comment.