2
0
mirror of https://github.com/frappe/bench.git synced 2024-11-11 15:51:03 +00:00

feat: execute bench/frappe commands from anywhere inside a bench

directory
This commit is contained in:
Gavin D'souza 2020-02-01 18:58:37 +05:30
parent e06d9a2cf5
commit 2257f86fee

View File

@ -1,10 +1,11 @@
import click import click
import os, sys, logging, json, pwd, subprocess import os, sys, logging, json, pwd, subprocess
from bench.utils import is_root, PatchError, drop_privileges, get_env_cmd, get_cmd_output, get_frappe, log from bench.utils import is_root, PatchError, drop_privileges, get_env_cmd, get_cmd_output, get_frappe, log, is_bench_directory
from bench.app import get_apps from bench.app import get_apps
from bench.config.common_site_config import get_config from bench.config.common_site_config import get_config
from bench.commands import bench_command from bench.commands import bench_command
logger = logging.getLogger('bench') logger = logging.getLogger('bench')
from_command_line = False from_command_line = False
@ -12,6 +13,7 @@ def cli():
global from_command_line global from_command_line
from_command_line = True from_command_line = True
change_working_directory()
check_uid() check_uid()
change_dir() change_dir()
change_uid() change_uid()
@ -114,3 +116,24 @@ def get_frappe_help(bench_path='.'):
return "Framework commands:\n" + out.split('Commands:')[1] return "Framework commands:\n" + out.split('Commands:')[1]
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
return "" return ""
def find_parent_bench(path):
"""Checks if parent directories are benches"""
path = os.path.abspath(path)
is_bench = is_bench_directory(directory=path)
home_path = os.path.expanduser("~")
root_path = os.path.abspath(os.sep)
if path not in {home_path, root_path}:
if is_bench:
return path
dir_list = os.path.split(path)
parent_dir = dir_list[0] if type(dir_list) == tuple else dir_list
return find_parent_bench(parent_dir)
def change_working_directory():
"""Allows bench commands to be run from anywhere inside a bench directory"""
bench_path = find_parent_bench(".") or os.path.abspath(".")
os.chdir(bench_path)