2
0
mirror of https://github.com/frappe/bench.git synced 2025-01-09 08:30:39 +00:00

feat: bench apps commands caching

Port of https://github.com/frappe/bench/pull/888
This commit is contained in:
Gavin D'souza 2020-04-11 21:17:13 +05:30
parent 8a5e7e9ec9
commit c0afa041c8
3 changed files with 54 additions and 3 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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!")