diff --git a/bench/commands/update.py b/bench/commands/update.py index f87cda95..82f857ba 100755 --- a/bench/commands/update.py +++ b/bench/commands/update.py @@ -5,7 +5,7 @@ from bench.config.common_site_config import get_config, update_config from bench.app import pull_all_apps, is_version_upgrade, validate_branch from bench.utils import (update_bench, validate_upgrade, pre_upgrade, post_upgrade, before_update, update_requirements, update_node_packages, backup_all_sites, patch_sites, build_assets, - restart_supervisor_processes, restart_systemd_processes) + restart_supervisor_processes, restart_systemd_processes, is_bench_directory) from bench import patches from six.moves import reload_module @@ -25,6 +25,11 @@ from six.moves import reload_module def update(pull=False, patch=False, build=False, bench=False, auto=False, restart_supervisor=False, restart_systemd=False, requirements=False, no_backup=False, force=False, reset=False): "Update bench" + if not is_bench_directory(): + """Update only bench if bench update called from outside a bench""" + update_bench(bench_repo=True, requirements=True) + sys.exit() + if not (pull or patch or build or bench or requirements): pull, patch, build, bench, requirements = True, True, True, True, True @@ -35,7 +40,7 @@ def update(pull=False, patch=False, build=False, bench=False, auto=False, restar conf = get_config(".") if bench and conf.get('update_bench_on_update'): - update_bench() + update_bench(bench_repo=True, requirements=False) restart_update({ 'pull': pull, 'patch': patch, diff --git a/bench/utils.py b/bench/utils.py index 8e3dfd0b..ab75dad0 100755 --- a/bench/utils.py +++ b/bench/utils.py @@ -16,6 +16,18 @@ logger = logging.getLogger(__name__) folders_in_bench = ('apps', 'sites', 'config', 'logs', 'config/pids') + +def is_bench_directory(): + cur_dir = os.path.curdir + is_bench = True + + for folder in folders_in_bench: + path = os.path.join(cur_dir, folder) + is_bench = is_bench and os.path.exists(path) + + return is_bench + + def safe_decode(string, encoding = 'utf-8'): try: string = string.decode(encoding) @@ -256,13 +268,23 @@ def read_crontab(): s.stdout.close() return out -def update_bench(): - logger.info('updating bench') +def update_bench(bench_repo=True, requirements=True): + logger.info("Updating bench") # bench-repo folder cwd = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) - exec_cmd("git pull", cwd=cwd) + if bench_repo: + try: + exec_cmd("git pull", cwd=cwd) + except bench.utils.CommandFailedError: + exec_cmd("git stash", cwd=cwd) + logger.info("Stashing changes made at {}\nUse git stash apply to recover changes after the successful update!".format(cwd)) + + if requirements: + update_bench_requirements() + + logger.info("Bench Updated!") def setup_sudoers(user): if not os.path.exists('/etc/sudoers.d'): @@ -424,19 +446,24 @@ def set_default_site(site, bench_path='.'): exec_cmd("{frappe} --use {site}".format(frappe=get_frappe(bench_path=bench_path), site=site), cwd=os.path.join(bench_path, 'sites')) -def update_requirements(bench_path='.'): - print('Updating Python libraries...') - - # update env pip - env_pip = os.path.join(bench_path, 'env', 'bin', 'pip') - exec_cmd("{pip} install -q -U pip".format(pip=env_pip)) - - # Update bench requirements (at user level) +def update_bench_requirements(): bench_req_file = os.path.join(os.path.dirname(bench.__path__[0]), 'requirements.txt') user_pip = which("pip" if PY2 else "pip3") install_requirements(user_pip, bench_req_file, user=True) +def update_env_pip(bench_path): + env_pip = os.path.join(bench_path, 'env', 'bin', 'pip') + exec_cmd("{pip} install -q -U pip".format(pip=env_pip)) + +def update_requirements(bench_path='.'): from bench.app import get_apps, install_app + print('Updating Python libraries...') + + # update env pip + update_env_pip(bench_path) + + # Update bench requirements (at user level) + update_bench_requirements() for app in get_apps(): install_app(app, bench_path=bench_path)