diff --git a/bench/commands/__init__.py b/bench/commands/__init__.py index 090013c2..da4eb8c8 100755 --- a/bench/commands/__init__.py +++ b/bench/commands/__init__.py @@ -41,7 +41,8 @@ bench_command.add_command(switch_to_develop) from bench.commands.utils import (start, restart, set_nginx_port, set_ssl_certificate, set_ssl_certificate_key, set_url_root, set_mariadb_host, set_default_site, download_translations, backup_site, backup_all_sites, release, renew_lets_encrypt, - disable_production, bench_src, prepare_beta_release, set_redis_cache_host, set_redis_queue_host, set_redis_socketio_host, find_benches, migrate_env) + disable_production, bench_src, prepare_beta_release, set_redis_cache_host, set_redis_queue_host, set_redis_socketio_host, find_benches, migrate_env, + generate_command_cache, clear_command_cache) bench_command.add_command(start) bench_command.add_command(restart) bench_command.add_command(set_nginx_port) @@ -63,7 +64,8 @@ bench_command.add_command(bench_src) bench_command.add_command(prepare_beta_release) bench_command.add_command(find_benches) bench_command.add_command(migrate_env) - +bench_command.add_command(generate_command_cache) +bench_command.add_command(clear_command_cache) from bench.commands.setup import setup bench_command.add_command(setup) diff --git a/bench/commands/utils.py b/bench/commands/utils.py index 822086a7..fcb11512 100644 --- a/bench/commands/utils.py +++ b/bench/commands/utils.py @@ -180,3 +180,15 @@ def find_benches(location): def migrate_env(python, backup=True): from bench.utils import migrate_env migrate_env(python=python, backup=backup) + + +@click.command('generate-command-cache', help="Caches Frappe Framework commands") +def generate_command_cache(bench_path='.'): + from bench.utils import generate_command_cache + return generate_command_cache(bench_path=bench_path) + + +@click.command('clear-command-cache', help="Clears Frappe Framework cached commands") +def clear_command_cache(bench_path='.'): + from bench.utils import clear_command_cache + return clear_command_cache(bench_path=bench_path) diff --git a/bench/utils.py b/bench/utils.py index 897acb64..33a9a6cb 100755 --- a/bench/utils.py +++ b/bench/utils.py @@ -39,7 +39,7 @@ class CommandFailedError(Exception): pass logger = logging.getLogger(__name__) - +bench_cache_file = '.bench.cmd' folders_in_bench = ('apps', 'sites', 'config', 'logs', 'config/pids') sudoers_file = '/etc/sudoers.d/frappe' @@ -187,6 +187,8 @@ def update(pull=False, patch=False, build=False, requirements=False, backup=True patches.run(bench_path=bench_path) conf = get_config(bench_path) + clear_command_cache(bench_path='.') + if conf.get('release_bench'): print('Release bench detected, cannot update!') sys.exit(1) @@ -1137,3 +1139,38 @@ def find_parent_bench(path): # 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 generate_command_cache(bench_path='.'): + """ + Caches all available commands (even custom apps) via Frappe + Default caching behaviour: generated the first time any command (for a specific bench directory) + """ + + python = get_env_cmd('python', bench_path=bench_path) + sites_path = os.path.join(bench_path, 'sites') + + if os.path.exists(bench_cache_file): + os.remove(bench_cache_file) + + try: + output = get_cmd_output("{0} -m frappe.utils.bench_helper get-frappe-commands".format(python), cwd=sites_path) + with open(bench_cache_file, 'w') as f: + json.dump(eval(output), f) + return json.loads(output) + + except subprocess.CalledProcessError as e: + if hasattr(e, "stderr"): + print(e.stderr.decode('utf-8')) + + +def clear_command_cache(bench_path='.'): + """ + Clears commands cached + Default invalidation behaviour: destroyed on each run of `bench update` + """ + + if os.path.exists(bench_cache_file): + os.remove(bench_cache_file) + else: + print("Bench command cache doesn't exist in this folder!") \ No newline at end of file