-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
run.py
64 lines (44 loc) · 1.76 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from __future__ import annotations
from typing import TYPE_CHECKING
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"
description = "Runs a command in the appropriate environment."
arguments = [
argument("args", "The command and arguments/options to run.", multiple=True)
]
def handle(self) -> int:
args = self.argument("args")
script = args[0]
scripts = self.poetry.local_config.get("scripts")
if scripts and script in scripts:
return self.run_script(scripts[script], args)
try:
return self.env.execute(*args)
except FileNotFoundError:
self.line_error(f"<error>Command not found: <c1>{script}</c1></error>")
return 1
@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):
script = script["callable"]
module, callable_ = script.split(":")
src_in_sys_path = "sys.path.append('src'); " if self._module.is_in_src() else ""
cmd = ["python", "-c"]
cmd += [
"import sys; "
"from importlib import import_module; "
f"sys.argv = {args!r}; {src_in_sys_path}"
f"sys.exit(import_module('{module}').{callable_}())"
]
return self.env.execute(*cmd)