run.py (poetry-1.1.15) | : | run.py (poetry-1.2.0) | ||
---|---|---|---|---|
from cleo import argument | from __future__ import annotations | |||
from .env_command import EnvCommand | from typing import TYPE_CHECKING | |||
class RunCommand(EnvCommand): | from cleo.helpers import argument | |||
from poetry.console.commands.env_command import EnvCommand | ||||
if TYPE_CHECKING: | ||||
from poetry.core.masonry.utils.module import Module | ||||
class RunCommand(EnvCommand): | ||||
name = "run" | name = "run" | |||
description = "Runs a command in the appropriate environment." | description = "Runs a command in the appropriate environment." | |||
arguments = [ | arguments = [ | |||
argument("args", "The command and arguments/options to run.", multiple=T rue) | argument("args", "The command and arguments/options to run.", multiple=T rue) | |||
] | ] | |||
def __init__(self): # type: () -> None | def handle(self) -> int: | |||
from poetry.console.args.run_args_parser import RunArgsParser | ||||
super(RunCommand, self).__init__() | ||||
self.config.set_args_parser(RunArgsParser()) | ||||
def handle(self): | ||||
args = self.argument("args") | args = self.argument("args") | |||
script = args[0] | script = args[0] | |||
scripts = self.poetry.local_config.get("scripts") | scripts = self.poetry.local_config.get("scripts") | |||
if scripts and script in scripts: | if scripts and script in scripts: | |||
return self.run_script(scripts[script], args) | return self.run_script(scripts[script], args) | |||
return self.env.execute(*args) | try: | |||
return self.env.execute(*args) | ||||
except FileNotFoundError: | ||||
self.line_error(f"<error>Command not found: <c1>{script}</c1></error | ||||
>") | ||||
return 1 | ||||
def run_script(self, script, args): | @property | |||
def _module(self) -> Module: | ||||
from poetry.core.masonry.utils.module import Module | ||||
poetry = self.poetry | ||||
package = poetry.package | ||||
path = poetry.file.parent | ||||
module = Module(package.name, path.as_posix(), package.packages) | ||||
return module | ||||
def run_script(self, script: str | dict[str, str], args: str) -> int: | ||||
if isinstance(script, dict): | if isinstance(script, dict): | |||
script = script["callable"] | script = script["callable"] | |||
module, callable_ = script.split(":") | module, callable_ = script.split(":") | |||
src_in_sys_path = "sys.path.append('src'); " if self._module.is_in_src() else "" | src_in_sys_path = "sys.path.append('src'); " if self._module.is_in_src() else "" | |||
cmd = ["python", "-c"] | cmd = ["python", "-c"] | |||
cmd += [ | cmd += [ | |||
"import sys; " | "import sys; " | |||
"from importlib import import_module; " | "from importlib import import_module; " | |||
"sys.argv = {!r}; {}" | f"sys.argv = {args!r}; {src_in_sys_path}" | |||
"import_module('{}').{}()".format(args, src_in_sys_path, module, cal | f"import_module('{module}').{callable_}()" | |||
lable_) | ||||
] | ] | |||
return self.env.execute(*cmd) | return self.env.execute(*cmd) | |||
@property | ||||
def _module(self): | ||||
from poetry.core.masonry.utils.module import Module | ||||
poetry = self.poetry | ||||
package = poetry.package | ||||
path = poetry.file.parent | ||||
module = Module(package.name, path.as_posix(), package.packages) | ||||
return module | ||||
End of changes. 9 change blocks. | ||||
16 lines changed or deleted | 30 lines changed or added |