From 217f27f51188d6fc4fea9e3f86d88d70410ba1a7 Mon Sep 17 00:00:00 2001 From: Gavin D'souza Date: Tue, 28 Dec 2021 12:35:06 +0530 Subject: [PATCH 1/4] fix: Set proper branch to clone Fixes https://github.com/frappe/bench/issues/1242 --- bench/app.py | 1 + 1 file changed, 1 insertion(+) diff --git a/bench/app.py b/bench/app.py index 12690302..069d4f0d 100755 --- a/bench/app.py +++ b/bench/app.py @@ -94,6 +94,7 @@ class AppMeta: def _setup_details_from_name_tag(self): self.org, self.repo, self.tag = fetch_details_from_tag(self.name) + self.tag = self.tag or self.branch def _setup_details_from_installed_apps(self): self.org, self.repo, self.tag = os.path.split( From 9802feaa9cc658b7c96911f3fcdc6e6f72b9ec6e Mon Sep 17 00:00:00 2001 From: Gavin D'souza Date: Tue, 28 Dec 2021 12:35:59 +0530 Subject: [PATCH 2/4] fix: Set correct remote_server for cloning Fixes https://github.com/frappe/bench/issues/1228 --- bench/app.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/bench/app.py b/bench/app.py index 069d4f0d..665d5e5c 100755 --- a/bench/app.py +++ b/bench/app.py @@ -106,9 +106,11 @@ class AppMeta: def __setup_details_from_git(self): if self.use_ssh: - self.org, _repo = self.name.split(":")[1].split("/") + _first_part, _second_part = self.name.split(":") + self.remote_server = _first_part.split("@")[-1] + self.org, _repo = _second_part.split("/") else: - self.org, _repo = self.name.split("/")[-2:] + self.remote_server, self.org, _repo = self.name.split("/")[-3:] self.tag = self.branch self.repo = _repo.split(".")[0] From 2caf7e7d154cddddcfa98cda81cdfa11ae9e24a9 Mon Sep 17 00:00:00 2001 From: Gavin D'souza Date: Tue, 28 Dec 2021 12:42:53 +0530 Subject: [PATCH 3/4] fix: Set verbosity if --verbose is passed anywhere period Fixes https://github.com/frappe/bench/issues/1224 --- bench/cli.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/bench/cli.py b/bench/cli.py index c34e90a7..96ac2365 100755 --- a/bench/cli.py +++ b/bench/cli.py @@ -40,7 +40,7 @@ src = os.path.dirname(__file__) def cli(): - global from_command_line, bench_config, is_envvar_warn_set + global from_command_line, bench_config, is_envvar_warn_set, verbose from_command_line = True command = " ".join(sys.argv) @@ -48,6 +48,9 @@ def cli(): is_envvar_warn_set = not (os.environ.get("BENCH_DEVELOPER") or os.environ.get("CI")) is_cli_command = len(sys.argv) > 1 and not argv.intersection({"src", "--version"}) + if "--verbose" in sys.argv: + verbose = True + change_working_directory() logger = setup_logging() logger.info(command) From 2fa25d8791e23385eec60614abfd190f3d39a617 Mon Sep 17 00:00:00 2001 From: Gavin D'souza Date: Tue, 28 Dec 2021 13:36:01 +0530 Subject: [PATCH 4/4] fix: Command picking & execution Parse sys.argv appropriately and use them to match commands "better". Using hacks to match with flags and options appear to be faulty, so dropped that. Only matching with command names remains. Fixes https://github.com/frappe/bench/issues/1223 --- bench/cli.py | 26 +++++++++++++------------- bench/utils/__init__.py | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 13 deletions(-) diff --git a/bench/cli.py b/bench/cli.py index 96ac2365..9cf946cc 100755 --- a/bench/cli.py +++ b/bench/cli.py @@ -25,6 +25,7 @@ from bench.utils import ( is_root, log, setup_logging, + parse_sys_argv, ) from bench.utils.bench import get_env_cmd @@ -34,21 +35,23 @@ verbose = False is_envvar_warn_set = None from_command_line = False # set when commands are executed via the CLI bench.LOG_BUFFER = [] +sys_argv = None change_uid_msg = "You should not run this command as root" src = os.path.dirname(__file__) def cli(): - global from_command_line, bench_config, is_envvar_warn_set, verbose + global from_command_line, bench_config, is_envvar_warn_set, verbose, sys_argv from_command_line = True command = " ".join(sys.argv) argv = set(sys.argv) is_envvar_warn_set = not (os.environ.get("BENCH_DEVELOPER") or os.environ.get("CI")) is_cli_command = len(sys.argv) > 1 and not argv.intersection({"src", "--version"}) + sys_argv = parse_sys_argv() - if "--verbose" in sys.argv: + if "--verbose" in sys_argv.options: verbose = True change_working_directory() @@ -92,13 +95,10 @@ def cli(): print(get_frappe_help()) return - if sys.argv[1] in ["--site", "--force", "--profile"]: - frappe_cmd() - - if sys.argv[1] in get_cached_frappe_commands(): - frappe_cmd() - - if sys.argv[1] in get_frappe_commands(): + if ( + sys_argv.commands.intersection(get_cached_frappe_commands()) + or sys_argv.commands.intersection(get_frappe_commands()) + ): frappe_cmd() if sys.argv[1] in Bench(".").apps: @@ -188,15 +188,15 @@ def frappe_cmd(bench_path="."): def get_cached_frappe_commands(): if os.path.exists(bench_cache_file): command_dump = open(bench_cache_file, "r").read() or "[]" - return json.loads(command_dump) - return [] + return set(json.loads(command_dump)) + return set() def get_frappe_commands(): if not is_bench_directory(): - return [] + return set() - return generate_command_cache() + return set(generate_command_cache()) def get_frappe_help(bench_path="."): diff --git a/bench/utils/__init__.py b/bench/utils/__init__.py index adfbf6c0..f75fe263 100644 --- a/bench/utils/__init__.py +++ b/bench/utils/__init__.py @@ -471,3 +471,38 @@ def get_traceback() -> str: trace_list = format_exception(exc_type, exc_value, exc_tb) return "".join(trace_list) + + +class _dict(dict): + """dict like object that exposes keys as attributes""" + # bench port of frappe._dict + def __getattr__(self, key): + ret = self.get(key) + # "__deepcopy__" exception added to fix frappe#14833 via DFP + if not ret and key.startswith("__") and key != "__deepcopy__": + raise AttributeError() + return ret + def __setattr__(self, key, value): + self[key] = value + def __getstate__(self): + return self + def __setstate__(self, d): + self.update(d) + def update(self, d): + """update and return self -- the missing dict feature in python""" + super(_dict, self).update(d) + return self + def copy(self): + return _dict(dict(self).copy()) + + +def parse_sys_argv(): + sys_argv = _dict(options=set(), commands=set()) + + for c in sys.argv[1:]: + if c.startswith("-"): + sys_argv.options.add(c) + else: + sys_argv.commands.add(c) + + return sys_argv