Skip to content

Commit

Permalink
feat: execute bench/frappe commands from anywhere inside a bench
Browse files Browse the repository at this point in the history
directory
  • Loading branch information
gavindsouza committed Feb 1, 2020
1 parent e06d9a2 commit 2257f86
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion bench/cli.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
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

def cli():
global from_command_line
from_command_line = True

change_working_directory()
check_uid()
change_dir()
change_uid()
Expand Down Expand Up @@ -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)

0 comments on commit 2257f86

Please sign in to comment.