Skip to content

Commit

Permalink
add timeout for getting version
Browse files Browse the repository at this point in the history
  • Loading branch information
frostming committed Feb 22, 2022
1 parent c31963b commit 8b5b1ee
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 21 deletions.
15 changes: 10 additions & 5 deletions src/findpython/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
import dataclasses as dc
import logging
import subprocess
from functools import lru_cache
from pathlib import Path

from packaging.version import Version
from packaging.version import parse as parse_version

from findpython.utils import get_binary_hash, subprocess_output
from findpython.utils import get_binary_hash

logger = logging.getLogger("findpython")
GET_VERSION_TIMEOUT = 5


@dc.dataclass
Expand All @@ -26,7 +28,7 @@ def is_valid(self) -> bool:
"""Return True if the python is not broken."""
try:
v = self._get_version()
except (OSError, subprocess.CalledProcessError):
except (OSError, subprocess.CalledProcessError, subprocess.TimeoutExpired):
return False
if not isinstance(v, Version):
return False
Expand Down Expand Up @@ -156,7 +158,7 @@ def __str__(self) -> str:
def _get_version(self) -> Version:
"""Get the version of the python."""
script = "import platform; print(platform.python_version())"
version = self._run_script(script).strip()
version = self._run_script(script, timeout=GET_VERSION_TIMEOUT).strip()
return parse_version(version)

def _get_architecture(self) -> str:
Expand All @@ -167,11 +169,14 @@ def _get_interpreter(self) -> str:
script = "import sys; print(sys.executable)"
return self._run_script(script).strip()

def _run_script(self, script: str) -> str:
@lru_cache(maxsize=1024)
def _run_script(self, script: str, timeout: float | None = None) -> str:
"""Run a script and return the output."""
command = [self.executable.as_posix(), "-c", script]
logger.debug("Running script: %s", command)
return subprocess_output(*command)
return subprocess.check_output(
command, input=None, stderr=subprocess.DEVNULL, timeout=timeout
).decode("utf-8")

def __lt__(self, other: PythonVersion) -> bool:
"""Sort by the version, then by length of the executable path."""
Expand Down
16 changes: 0 additions & 16 deletions src/findpython/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import hashlib
import os
import re
import subprocess
import sys
from functools import lru_cache
from pathlib import Path
Expand Down Expand Up @@ -96,21 +95,6 @@ def path_is_python(path: Path) -> bool:
return path_is_known_executable(path) and looks_like_python(path.name)


@lru_cache(maxsize=1024)
def subprocess_output(*args: str) -> str:
"""
Run a command and return the output.
:param cmd: The command to run.
:type cmd: list[str]
:return: The output of the command.
:rtype: str
"""
return subprocess.check_output(
list(args), input=None, stderr=subprocess.DEVNULL
).decode("utf-8")


@lru_cache(maxsize=1024)
def get_binary_hash(path: Path) -> str:
"""Return the MD5 hash of the given file."""
Expand Down

0 comments on commit 8b5b1ee

Please sign in to comment.