2
0
mirror of https://github.com/frappe/bench.git synced 2024-09-29 07:19:05 +00:00

refactor: CLI entry point

* Optimized condiitonal checks
* Removed warnings for src, --version
* Handled positional shifts due to usages of --use-feature (by
  set.intersection)
* Raise Exception always instead of hiding the traceback
This commit is contained in:
Gavin D'souza 2021-11-20 04:16:19 +05:30
parent 225fce5be6
commit 7c653db703

View File

@ -4,7 +4,6 @@ import json
import os import os
import pwd import pwd
import sys import sys
from traceback import format_exception
# imports - third party imports # imports - third party imports
import click import click
@ -42,9 +41,12 @@ src = os.path.dirname(__file__)
def cli(): def cli():
global from_command_line, bench_config global from_command_line, bench_config
from_command_line = True from_command_line = True
command = " ".join(sys.argv) command = " ".join(sys.argv)
argv = set(sys.argv)
is_envvar_warn_set = not (os.environ.get("BENCH_DEVELOPER") or os.environ.get("CI")) 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"})
change_working_directory() change_working_directory()
logger = setup_logging() logger = setup_logging()
@ -52,15 +54,15 @@ def cli():
bench_config = get_config(".") bench_config = get_config(".")
if len(sys.argv) > 1 and sys.argv[1] not in ("src",): if is_cli_command:
check_uid() check_uid()
change_uid() change_uid()
change_dir() change_dir()
if is_envvar_warn_set and ( if (
is_dist_editable(bench.PROJECT_NAME) is_envvar_warn_set
and len(sys.argv) > 1 and is_cli_command
and sys.argv[1] != "src" and is_dist_editable(bench.PROJECT_NAME)
and not bench_config.get("developer_mode") and not bench_config.get("developer_mode")
): ):
log( log(
@ -74,9 +76,9 @@ def cli():
if ( if (
not in_bench not in_bench
and not cmd_requires_root()
and len(sys.argv) > 1 and len(sys.argv) > 1
and sys.argv[1] not in ("init", "find", "src", "drop", "get", "get-app") and not argv.intersection({"init", "find", "src", "drop", "get", "get-app", "--version"})
and not cmd_requires_root()
): ):
log("Command not being executed in bench directory", level=3) log("Command not being executed in bench directory", level=3)
@ -98,35 +100,21 @@ def cli():
if sys.argv[1] in Bench(".").apps: if sys.argv[1] in Bench(".").apps:
app_cmd() app_cmd()
if not (len(sys.argv) > 1 and sys.argv[1] == "src"): if not is_cli_command:
atexit.register(check_latest_version) atexit.register(check_latest_version)
try: try:
bench_command() bench_command()
except BaseException as e: except BaseException as e:
return_code = getattr(e, "code", 0) return_code = getattr(e, "code", 1)
if isinstance(e, Exception):
click.secho(f"ERROR: {e}", fg="red")
if return_code: if return_code:
logger.warning(f"{command} executed with exit code {return_code}") logger.warning(f"{command} executed with exit code {return_code}")
if isinstance(e, Exception): raise e
if (
os.environ.get("BENCH_DEVELOPER")
or bench_config.get("developer_mode")
or (
sys.argv[1] in ("init", "get", "get-app") and not in_bench
)
):
from bench.utils import get_traceback
click.echo(get_traceback())
click.secho(f"ERROR: {e}", fg="red")
return_code = 1
raise e
finally:
try:
return_code
except NameError:
return_code = 0
sys.exit(return_code)
def check_uid(): def check_uid():