diff --git a/frappe/utils/bench_helper.py b/frappe/utils/bench_helper.py index a0b011acc1..01e611f021 100644 --- a/frappe/utils/bench_helper.py +++ b/frappe/utils/bench_helper.py @@ -3,6 +3,7 @@ import json import os import traceback import warnings +from pathlib import Path import click @@ -18,22 +19,18 @@ def main(): click.Group(commands=commands)(prog_name="bench") -def get_app_groups(): +def get_app_groups() -> dict[str, click.Group]: """Get all app groups, put them in main group "frappe" since bench is designed to only handle that""" - commands = dict() + commands = {} for app in get_apps(): - app_commands = get_app_commands(app) - if app_commands: - commands.update(app_commands) - - ret = dict(frappe=click.group(name="frappe", commands=commands)(app_group)) - return ret + if app_commands := get_app_commands(app): + commands |= app_commands + return dict(frappe=click.group(name="frappe", commands=commands)(app_group)) -def get_app_group(app): - app_commands = get_app_commands(app) - if app_commands: +def get_app_group(app: str) -> click.Group: + if app_commands := get_app_commands(app): return click.group(name=app, commands=app_commands)(app_group) @@ -48,7 +45,7 @@ def app_group(ctx, site=False, force=False, verbose=False, profile=False): ctx.info_name = "" -def get_sites(site_arg): +def get_sites(site_arg: str) -> list[str]: if site_arg == "all": return frappe.utils.get_sites() elif site_arg: @@ -57,25 +54,23 @@ def get_sites(site_arg): return [os.environ.get("FRAPPE_SITE")] elif os.path.exists("currentsite.txt"): with open("currentsite.txt") as f: - site = f.read().strip() - if site: + if site := f.read().strip(): return [site] return [] -def get_app_commands(app): - if os.path.exists(os.path.join("..", "apps", app, app, "commands.py")) or os.path.exists( - os.path.join("..", "apps", app, app, "commands", "__init__.py") - ): - try: - app_command_module = importlib.import_module(app + ".commands") - except Exception: - traceback.print_exc() - return [] - else: - return [] - +def get_app_commands(app: str) -> dict: ret = {} + app_path = Path("..", "apps", app, app) + + if not ((app_path / "commands.py").exists() or (app_path / "commands" / "__init__.py").exists()): + return ret + + try: + app_command_module = importlib.import_module(f"{app}.commands") + except Exception: + traceback.print_exc() + return ret for command in getattr(app_command_module, "commands", []): ret[command.name] = command return ret