Skip to content

Commit

Permalink
Skip PoetryExecutor if poetry is not found
Browse files Browse the repository at this point in the history
  • Loading branch information
kzrnm committed Oct 17, 2023
1 parent 69a6e13 commit 9bba525
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
17 changes: 10 additions & 7 deletions poethepoet/executor/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ def __init__(
self.capture_stdout = capture_stdout
self._is_windows = sys.platform == "win32"

@classmethod
def fits_in(cls, context: "RunContext") -> bool:
return True

@classmethod
def get(
cls,
Expand Down Expand Up @@ -96,7 +100,6 @@ def _resolve_implementation(
by making some reasonable assumptions based on visible features of the
environment
"""
from ..virtualenv import Virtualenv

config_executor_type = context.executor_type
if executor_config:
Expand All @@ -106,13 +109,13 @@ def _resolve_implementation(
)
return cls.__executor_types[executor_config["type"]]
elif config_executor_type == "auto":
if "poetry" in context.config.project["tool"]:
# Looks like this is a poetry project!
return cls.__executor_types["poetry"]
for impl in [
cls.__executor_types["poetry"],
cls.__executor_types["virtualenv"],
]:
if impl.fits_in(context):
return impl

if Virtualenv.detect(context.project_dir):
# Looks like there's a local virtualenv
return cls.__executor_types["virtualenv"]
# Fallback to not using any particular environment
return cls.__executor_types["simple"]
else:
Expand Down
24 changes: 19 additions & 5 deletions poethepoet/executor/poetry.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from os import environ
from pathlib import Path
from typing import Dict, Optional, Sequence, Type
from typing import TYPE_CHECKING, Dict, Optional, Sequence, Type

from ..exceptions import PoeException
from .base import PoeExecutor

if TYPE_CHECKING:
from ..context import RunContext


class PoetryExecutor(PoeExecutor):
"""
Expand All @@ -15,6 +18,12 @@ class PoetryExecutor(PoeExecutor):
__key__ = "poetry"
__options__: Dict[str, Type] = {}

@classmethod
def fits_in(cls, context: "RunContext") -> bool:
if "poetry" not in context.config.project["tool"]:
return False
return cls._poetry_cmd_from_path()

def execute(
self, cmd: Sequence[str], input: Optional[bytes] = None, use_exec: bool = False
) -> int:
Expand Down Expand Up @@ -90,15 +99,20 @@ def _get_poetry_virtualenv(self, force: bool = True):

return exec_cache.get("poetry_virtualenv")

def _poetry_cmd(self):
import shutil

from_path = shutil.which("poetry")
@classmethod
def _poetry_cmd(cls):
from_path = cls._poetry_cmd_from_path()
if from_path:
return str(Path(from_path).resolve())

return "poetry"

@classmethod
def _poetry_cmd_from_path(cls):
import shutil

return shutil.which("poetry")

def _virtualenv_creation_disabled(self):
exec_cache = self.context.exec_cache

Expand Down
7 changes: 7 additions & 0 deletions poethepoet/executor/virtualenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from .base import PoeExecutor

if TYPE_CHECKING:
from ..context import RunContext
from ..virtualenv import Virtualenv


Expand All @@ -15,6 +16,12 @@ class VirtualenvExecutor(PoeExecutor):
__key__ = "virtualenv"
__options__: Dict[str, Type] = {"location": str}

@classmethod
def fits_in(cls, context: "RunContext") -> bool:
from ..virtualenv import Virtualenv

return Virtualenv.detect(context.project_dir)

def execute(
self, cmd: Sequence[str], input: Optional[bytes] = None, use_exec: bool = False
) -> int:
Expand Down

0 comments on commit 9bba525

Please sign in to comment.