Skip to content

Commit

Permalink
Fix script name detection
Browse files Browse the repository at this point in the history
  • Loading branch information
Secrus committed Oct 25, 2023
1 parent 3465582 commit c1c9607
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions src/cleo/commands/completions_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@

from typing import TYPE_CHECKING
from typing import ClassVar
from typing import cast

from cleo import helpers
from cleo._compat import shell_quote
from cleo.commands.command import Command
from cleo.commands.completions.templates import TEMPLATES
from cleo.exceptions import CleoRuntimeError


if TYPE_CHECKING:
Expand Down Expand Up @@ -137,10 +139,32 @@ def render(self, shell: str) -> str:

raise RuntimeError(f"Unrecognized shell: {shell}")

@staticmethod
def _get_prog_name_from_stack() -> str:
package_name = ""
frame = inspect.currentframe()
f_back = frame.f_back if frame is not None else None
f_globals = f_back.f_globals if f_back is not None else None
# break reference cycle
# https://docs.python.org/3/library/inspect.html#the-interpreter-stack
del frame

if f_globals is not None:
package_name = cast(str, f_globals.get("__name__"))

if package_name == "__main__":
package_name = cast(str, f_globals.get("__package__"))

if package_name:
package_name = package_name.partition(".")[0]

if not package_name:
raise CleoRuntimeError("Can not determine package name")

return package_name

def _get_script_name_and_path(self) -> tuple[str, str]:
# FIXME: when generating completions via `python -m script completions`,
# we incorrectly infer `script_name` as `__main__.py`
script_name = self._io.input.script_name or inspect.stack()[-1][1]
script_name = self._io.input.script_name or self._get_prog_name_from_stack()
script_path = posixpath.realpath(script_name)
script_name = os.path.basename(script_path)

Expand Down

0 comments on commit c1c9607

Please sign in to comment.