From 4ce74759e21a7dd97cff92cc8f5ba0fef034a919 Mon Sep 17 00:00:00 2001 From: Faris Ansari Date: Thu, 22 Feb 2018 12:38:26 +0530 Subject: [PATCH] Make bench backward compatible with npm style --- bench/app.py | 10 ++++----- bench/commands/setup.py | 17 +++++++++++--- bench/commands/update.py | 8 +++---- bench/utils.py | 48 ++++++++++++++++++++++++++++++++++++++-- 4 files changed, 69 insertions(+), 14 deletions(-) diff --git a/bench/app.py b/bench/app.py index 87a8681f..7a080c81 100755 --- a/bench/app.py +++ b/bench/app.py @@ -60,9 +60,9 @@ def check_url(url, raise_err = True): raise TypeError('{url} Not a valid URL'.format(url = url)) else: return False - + return True - + def get_app(git_url, branch=None, bench_path='.', build_asset_files=True, verbose=False): # from bench.utils import check_url try: @@ -165,7 +165,7 @@ def remove_app(app, bench_path='.'): def pull_all_apps(bench_path='.', reset=False): '''Check all apps if there no local changes, pull''' rebase = '--rebase' if get_config(bench_path).get('rebase_on_pull') else '' - + # chech for local changes if not reset: for app in get_apps(bench_path=bench_path): @@ -283,7 +283,7 @@ def get_repo_dir(app, bench_path='.'): return os.path.join(bench_path, 'apps', app) def switch_branch(branch, apps=None, bench_path='.', upgrade=False, check_upgrade=True): - from .utils import update_requirements, update_npm_packages, backup_all_sites, patch_sites, build_assets, pre_upgrade, post_upgrade + from .utils import update_requirements, update_node_packages, backup_all_sites, patch_sites, build_assets, pre_upgrade, post_upgrade from . import utils apps_dir = os.path.join(bench_path, 'apps') version_upgrade = (False,) @@ -323,7 +323,7 @@ def switch_branch(branch, apps=None, bench_path='.', upgrade=False, check_upgrad if version_upgrade[0] and upgrade: update_requirements() - update_npm_packages() + update_node_packages() pre_upgrade(version_upgrade[1], version_upgrade[2]) reload(utils) backup_all_sites() diff --git a/bench/commands/setup.py b/bench/commands/setup.py index 053e6357..ff6affb7 100755 --- a/bench/commands/setup.py +++ b/bench/commands/setup.py @@ -4,7 +4,7 @@ import click, sys, json def setup(): "Setup bench" pass - + @click.command('sudoers') @click.argument('user') def setup_sudoers(user): @@ -130,9 +130,18 @@ def setup_socketio(): @click.command('requirements') def setup_requirements(): "Setup python and node requirements" - from bench.utils import update_requirements, update_npm_packages + setup_python_requirements() + setup_node_requirements() + +@click.command('python-requirements') +def setup_python_requirements(): + from bench.utils import update_requirements update_requirements() - update_npm_packages() + +@click.command('node-requirements') +def setup_node_requirements(): + from bench.utils import update_node_packages + update_node_packages() @click.command('config') def setup_config(): @@ -227,6 +236,8 @@ setup.add_command(setup_env) setup.add_command(setup_procfile) setup.add_command(setup_socketio) setup.add_command(setup_requirements) +setup.add_command(setup_python_requirements) +setup.add_command(setup_node_requirements) setup.add_command(setup_config) setup.add_command(setup_fonts) setup.add_command(add_domain) diff --git a/bench/commands/update.py b/bench/commands/update.py index 17c9d652..5de03cbe 100755 --- a/bench/commands/update.py +++ b/bench/commands/update.py @@ -3,7 +3,7 @@ import sys, os from bench.config.common_site_config import get_config from bench.app import pull_all_apps, is_version_upgrade from bench.utils import (update_bench, validate_upgrade, pre_upgrade, post_upgrade, before_update, - update_requirements, update_npm_packages, backup_all_sites, patch_sites, build_assets, restart_supervisor_processes) + update_requirements, update_node_packages, backup_all_sites, patch_sites, build_assets, restart_supervisor_processes) from bench import patches #TODO: Not DRY @@ -45,9 +45,9 @@ def update(pull=False, patch=False, build=False, bench=False, auto=False, restar if conf.get('release_bench'): print('Release bench, cannot update') sys.exit(1) - + version_upgrade = is_version_upgrade() - + if version_upgrade[0]: print() print() @@ -72,7 +72,7 @@ def _update(pull=False, patch=False, build=False, update_bench=False, auto=False if requirements: update_requirements(bench_path=bench_path) - update_npm_packages(bench_path=bench_path) + update_node_packages(bench_path=bench_path) if version_upgrade[0] or (not version_upgrade[0] and force): pre_upgrade(version_upgrade[1], version_upgrade[2], bench_path=bench_path) diff --git a/bench/utils.py b/bench/utils.py index db869760..4bca37b0 100755 --- a/bench/utils.py +++ b/bench/utils.py @@ -1,6 +1,7 @@ import os, sys, shutil, subprocess, logging, itertools, requests, json, platform, select, pwd, grp, multiprocessing, hashlib from distutils.spawn import find_executable import bench +import semantic_version from bench import env from six import iteritems @@ -427,8 +428,19 @@ def update_requirements(bench_path='.'): req_file = os.path.join(apps_dir, app, 'requirements.txt') install_requirements(pip, req_file) -def update_npm_packages(bench_path='.'): - print('Updating node libraries...') +def update_node_packages(): + print('Updating node packages...') + from bench.app import get_current_version + v = semantic_version.Version(get_current_version('frappe')) + + # After rollup was merged, frappe_version = 10.1 + # anything before that was npm based + if v.major <= 10 and v.minor < 1: + update_npm_packages() + else: + update_yarn_packages() + +def update_yarn_packages(bench_path='.'): apps_dir = os.path.join(bench_path, 'apps') if not find_executable('yarn'): @@ -442,6 +454,38 @@ def update_npm_packages(bench_path='.'): exec_cmd('yarn install', cwd=app_path) +def update_npm_packages(bench_path='.'): + apps_dir = os.path.join(bench_path, 'apps') + package_json = {} + + for app in os.listdir(apps_dir): + package_json_path = os.path.join(apps_dir, app, 'package.json') + + if os.path.exists(package_json_path): + with open(package_json_path, "r") as f: + app_package_json = json.loads(f.read()) + # package.json is usually a dict in a dict + for key, value in iteritems(app_package_json): + if not key in package_json: + package_json[key] = value + else: + if isinstance(value, dict): + package_json[key].update(value) + elif isinstance(value, list): + package_json[key].extend(value) + else: + package_json[key] = value + + if package_json is {}: + with open(os.path.join(os.path.dirname(__file__), 'package.json'), 'r') as f: + package_json = json.loads(f.read()) + + with open(os.path.join(bench_path, 'package.json'), 'w') as f: + f.write(json.dumps(package_json, indent=1, sort_keys=True)) + + exec_cmd('npm install', cwd=bench_path) + + def install_requirements(pip, req_file): if os.path.exists(req_file): exec_cmd("{pip} install -q -r {req_file}".format(pip=pip, req_file=req_file))