Skip to content

Commit

Permalink
Merge pull request #2197 from pypa/feature/unified-entry-point
Browse files Browse the repository at this point in the history
Unify the entry point template.
  • Loading branch information
jaraco authored Jun 15, 2020
2 parents 1d841d7 + 9db4553 commit 4918afc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 25 deletions.
1 change: 1 addition & 0 deletions changelog.d/2197.change.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Console script wrapper for editable installs now has a unified template and honors importlib_metadata if present for faster script execution on older Pythons.
54 changes: 29 additions & 25 deletions setuptools/command/easy_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -2070,33 +2070,37 @@ class ScriptWriter:
gui apps.
"""

if sys.version_info >= (3, 8):
template = textwrap.dedent(r"""
# EASY-INSTALL-ENTRY-SCRIPT: %(spec)r,%(group)r,%(name)r
import re
import sys
template = textwrap.dedent(r"""
# EASY-INSTALL-ENTRY-SCRIPT: %(spec)r,%(group)r,%(name)r
import re
import sys
try:
from importlib.metadata import distribution
except ImportError:
try:
from importlib_metadata import distribution
except ImportError:
from pkg_resources import load_entry_point
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
for entry_point in distribution(%(spec)r.split('==')[0]).entry_points:
if entry_point.group == %(group)r and entry_point.name == %(name)r:
sys.exit(entry_point.load()())
""").lstrip() # noqa: E501
else:
template = textwrap.dedent(r"""
# EASY-INSTALL-ENTRY-SCRIPT: %(spec)r,%(group)r,%(name)r
__requires__ = %(spec)r
import re
import sys
from pkg_resources import load_entry_point
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
sys.exit(
load_entry_point(%(spec)r, %(group)r, %(name)r)()
)
""").lstrip() # noqa: E501
def importlib_load_entry_point(spec, group, name):
dist_name, _, _ = spec.partition('==')
matches = (
entry_point
for entry_point in distribution(dist_name).entry_points
if entry_point.group == group and entry_point.name == name
)
return next(matches).load()
globals().setdefault('load_entry_point', importlib_load_entry_point)
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
sys.exit(load_entry_point(%(spec)r, %(group)r, %(name)r)())
""").lstrip()

command_spec_class = CommandSpec

Expand Down

0 comments on commit 4918afc

Please sign in to comment.