-
Notifications
You must be signed in to change notification settings - Fork 34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add setuptools entry point #156
Open
Freed-Wu
wants to merge
1
commit into
iterative:main
Choose a base branch
from
Freed-Wu:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
"""A entry point for setuptools.""" | ||
import io | ||
import os | ||
import sys | ||
from contextlib import redirect_stdout, suppress | ||
from importlib import import_module | ||
from importlib.metadata import EntryPoint, EntryPoints | ||
from typing import Callable | ||
|
||
from setuptools import Distribution | ||
|
||
try: | ||
import tomllib as tomli | ||
except ImportError: | ||
import tomli | ||
|
||
|
||
def get_stdout(function: Callable) -> str: | ||
"""Get stdout.""" | ||
string = io.StringIO() | ||
with redirect_stdout(string), suppress(SystemExit): | ||
function() | ||
string.seek(0) | ||
content = string.read() | ||
return content | ||
|
||
|
||
def generate_completions(distribution: Distribution) -> None: | ||
"""Generate completions.""" | ||
# Get entry_points from setup.py | ||
entry_points = getattr(distribution, "entry_points") | ||
if entry_points is None: | ||
entry_points = EntryPoints() | ||
entry_points = entry_points.select(group="console_scripts") | ||
# Get entry_points from setup.cfg | ||
if len(entry_points) == 0 and os.path.isfile("setup.cfg"): | ||
import configparser | ||
|
||
parser = configparser.ConfigParser() | ||
parser.read(["setup.cfg"], encoding="utf-8") | ||
_entry_points = parser.get("metadata", "entry_points", fallback=None) | ||
if isinstance(_entry_points, dict): | ||
console_scripts = _entry_points.get("console_scripts", []) | ||
for console_script in console_scripts: | ||
k, _, v = console_script.partition("=") | ||
entry_points += [ | ||
EntryPoint(name=k.strip(), group="console_scripts", value=v.strip())] | ||
# Get entry_points from pyproject.toml | ||
if len(entry_points) == 0 and os.path.isfile("pyproject.toml"): | ||
with open("pyproject.toml", "rb") as f: | ||
pyproject = tomli.load(f) | ||
scripts = pyproject.get("project", {}).get("scripts", {}) | ||
if isinstance(scripts, dict): | ||
for k, v in scripts.items(): | ||
entry_points += [EntryPoint(name=k, group="scripts", value=v)] | ||
|
||
cwd = os.getcwd() | ||
# https://setuptools.pypa.io/en/latest/userguide/package_discovery.html#flat-layout | ||
sys.path.insert(0, cwd) | ||
# https://setuptools.pypa.io/en/latest/userguide/package_discovery.html#src-layout | ||
sys.path.insert(0, os.path.join(cwd, "src")) | ||
for entry_point in entry_points: | ||
# entry_point.value can be 'module_path:function_name[extra1, extra2]' | ||
path = entry_point.value.split("[")[0] | ||
module_path, _, function_name = path.rpartition(":") | ||
module = import_module(module_path) | ||
function = vars(module).get(function_name) | ||
prog = entry_point.name | ||
shells = {"bash": prog, "zsh": "_" + prog, "tcsh": prog + ".csh"} | ||
os.makedirs("sdist", exist_ok=True) | ||
argv = sys.argv | ||
for shell, filename in shells.items(): | ||
sys.argv = [prog, "--print-completion", shell] | ||
# bash, zsh, tcsh only use `\n` | ||
content = get_stdout(function).replace("\r\n", "\n") # type: ignore | ||
if content == "": | ||
continue | ||
with open(os.path.join("sdist", filename), "w", newline="") as f: | ||
f.write(content) | ||
sys.argv = argv |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not all programs use this. It may be e.g.:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, It's a fast dirty trial 😄 How can we get the command option about print completion or get the completion content directly from python?