2
0
mirror of https://github.com/frappe/bench.git synced 2025-01-22 22:58:31 +00:00

refactor: get_env_cmd

* Use globbing first to identify cmd in env paths
* Unbound cache which clears on chdir
* Allow passing patterns like 'python*' as cmd to match
This commit is contained in:
Gavin D'souza 2022-08-02 11:57:38 +05:30
parent f60c2d0def
commit b37135e5b1
2 changed files with 13 additions and 9 deletions

View File

@ -245,6 +245,7 @@ def setup_clear_cache():
def _chdir(*args, **kwargs):
Bench.cache_clear()
get_env_cmd.cache_clear()
return f(*args, **kwargs)
os.chdir = _chdir

View File

@ -6,6 +6,8 @@ import os
import re
import subprocess
import sys
from functools import lru_cache
from glob import glob
from json.decoder import JSONDecodeError
# imports - third party imports
@ -19,19 +21,20 @@ from bench.utils import exec_cmd, get_bench_name, get_cmd_output, log, which
logger = logging.getLogger(bench.PROJECT_NAME)
@lru_cache(maxsize=None)
def get_env_cmd(cmd, bench_path="."):
# this supports envs' generated by patched virtualenv or venv (which may cause an extra 'local' folder to be created)
existing_python_bins = glob(
os.path.abspath(os.path.join(bench_path, "env", "**", "bin", cmd)), recursive=True
)
if existing_python_bins:
return existing_python_bins[0]
return os.path.abspath(os.path.join(bench_path, "env", "bin", cmd))
def get_virtualenv_path(verbose=False):
virtualenv_path = which("virtualenv")
if not virtualenv_path and verbose:
log("virtualenv cannot be found", level=2)
return virtualenv_path
def get_venv_path(verbose=False, python="python3"):
with open(os.devnull, "wb") as devnull:
is_venv_installed = not subprocess.call(