Skip to content

Commit

Permalink
feat: bench apps commands caching
Browse files Browse the repository at this point in the history
Port of frappe#888
  • Loading branch information
gavindsouza committed Apr 11, 2020
1 parent 8a5e7e9 commit c0afa04
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
6 changes: 4 additions & 2 deletions bench/commands/__init__.py
Expand Up @@ -41,7 +41,8 @@ def bench_command(bench_path='.'):

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)
Expand All @@ -63,7 +64,8 @@ def bench_command(bench_path='.'):
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)
Expand Down
12 changes: 12 additions & 0 deletions bench/commands/utils.py
Expand Up @@ -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)
39 changes: 38 additions & 1 deletion bench/utils.py
Expand Up @@ -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'

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

0 comments on commit c0afa04

Please sign in to comment.