From 2257f86feefa3eefef71932449ee28f2e087d688 Mon Sep 17 00:00:00 2001 From: Gavin D'souza Date: Sat, 1 Feb 2020 18:58:37 +0530 Subject: [PATCH 1/2] feat: execute bench/frappe commands from anywhere inside a bench directory --- bench/cli.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/bench/cli.py b/bench/cli.py index 7eef2288..0cab4c9b 100755 --- a/bench/cli.py +++ b/bench/cli.py @@ -1,10 +1,11 @@ import click 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.config.common_site_config import get_config from bench.commands import bench_command + logger = logging.getLogger('bench') from_command_line = False @@ -12,6 +13,7 @@ def cli(): global from_command_line from_command_line = True + change_working_directory() check_uid() change_dir() change_uid() @@ -114,3 +116,24 @@ def get_frappe_help(bench_path='.'): return "Framework commands:\n" + out.split('Commands:')[1] except subprocess.CalledProcessError: 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) From 92cd942fcf32f78e2cd0e8a905700226a04ce332 Mon Sep 17 00:00:00 2001 From: Gavin D'souza Date: Sun, 2 Feb 2020 00:19:58 +0530 Subject: [PATCH 2/2] refactor: `change_working_directory` optimizations --- bench/cli.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/bench/cli.py b/bench/cli.py index 0cab4c9b..733a0dd6 100755 --- a/bench/cli.py +++ b/bench/cli.py @@ -119,21 +119,21 @@ def get_frappe_help(bench_path='.'): def find_parent_bench(path): """Checks if parent directories are benches""" - path = os.path.abspath(path) - is_bench = is_bench_directory(directory=path) + if is_bench_directory(directory=path): + return 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 + # NOTE: the os.path.split assumes that given path is absolute + parent_dir = os.path.split(path)[0] 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) + cur_dir = os.path.abspath(".") + bench_path = find_parent_bench(cur_dir) + + if bench_path: + os.chdir(bench_path)