2
0
mirror of https://github.com/frappe/bench.git synced 2025-01-09 16:36:25 +00:00

Make bench backward compatible with npm style

This commit is contained in:
Faris Ansari 2018-02-22 12:38:26 +05:30
parent c06307e50d
commit 4ce74759e2
4 changed files with 69 additions and 14 deletions

View File

@ -60,9 +60,9 @@ def check_url(url, raise_err = True):
raise TypeError('{url} Not a valid URL'.format(url = url)) raise TypeError('{url} Not a valid URL'.format(url = url))
else: else:
return False return False
return True return True
def get_app(git_url, branch=None, bench_path='.', build_asset_files=True, verbose=False): def get_app(git_url, branch=None, bench_path='.', build_asset_files=True, verbose=False):
# from bench.utils import check_url # from bench.utils import check_url
try: try:
@ -165,7 +165,7 @@ def remove_app(app, bench_path='.'):
def pull_all_apps(bench_path='.', reset=False): def pull_all_apps(bench_path='.', reset=False):
'''Check all apps if there no local changes, pull''' '''Check all apps if there no local changes, pull'''
rebase = '--rebase' if get_config(bench_path).get('rebase_on_pull') else '' rebase = '--rebase' if get_config(bench_path).get('rebase_on_pull') else ''
# chech for local changes # chech for local changes
if not reset: if not reset:
for app in get_apps(bench_path=bench_path): 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) return os.path.join(bench_path, 'apps', app)
def switch_branch(branch, apps=None, bench_path='.', upgrade=False, check_upgrade=True): 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 from . import utils
apps_dir = os.path.join(bench_path, 'apps') apps_dir = os.path.join(bench_path, 'apps')
version_upgrade = (False,) 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: if version_upgrade[0] and upgrade:
update_requirements() update_requirements()
update_npm_packages() update_node_packages()
pre_upgrade(version_upgrade[1], version_upgrade[2]) pre_upgrade(version_upgrade[1], version_upgrade[2])
reload(utils) reload(utils)
backup_all_sites() backup_all_sites()

View File

@ -4,7 +4,7 @@ import click, sys, json
def setup(): def setup():
"Setup bench" "Setup bench"
pass pass
@click.command('sudoers') @click.command('sudoers')
@click.argument('user') @click.argument('user')
def setup_sudoers(user): def setup_sudoers(user):
@ -130,9 +130,18 @@ def setup_socketio():
@click.command('requirements') @click.command('requirements')
def setup_requirements(): def setup_requirements():
"Setup python and node 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_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') @click.command('config')
def setup_config(): def setup_config():
@ -227,6 +236,8 @@ setup.add_command(setup_env)
setup.add_command(setup_procfile) setup.add_command(setup_procfile)
setup.add_command(setup_socketio) setup.add_command(setup_socketio)
setup.add_command(setup_requirements) 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_config)
setup.add_command(setup_fonts) setup.add_command(setup_fonts)
setup.add_command(add_domain) setup.add_command(add_domain)

View File

@ -3,7 +3,7 @@ import sys, os
from bench.config.common_site_config import get_config from bench.config.common_site_config import get_config
from bench.app import pull_all_apps, is_version_upgrade from bench.app import pull_all_apps, is_version_upgrade
from bench.utils import (update_bench, validate_upgrade, pre_upgrade, post_upgrade, before_update, 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 from bench import patches
#TODO: Not DRY #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'): if conf.get('release_bench'):
print('Release bench, cannot update') print('Release bench, cannot update')
sys.exit(1) sys.exit(1)
version_upgrade = is_version_upgrade() version_upgrade = is_version_upgrade()
if version_upgrade[0]: if version_upgrade[0]:
print() print()
print() print()
@ -72,7 +72,7 @@ def _update(pull=False, patch=False, build=False, update_bench=False, auto=False
if requirements: if requirements:
update_requirements(bench_path=bench_path) 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): if version_upgrade[0] or (not version_upgrade[0] and force):
pre_upgrade(version_upgrade[1], version_upgrade[2], bench_path=bench_path) pre_upgrade(version_upgrade[1], version_upgrade[2], bench_path=bench_path)

View File

@ -1,6 +1,7 @@
import os, sys, shutil, subprocess, logging, itertools, requests, json, platform, select, pwd, grp, multiprocessing, hashlib import os, sys, shutil, subprocess, logging, itertools, requests, json, platform, select, pwd, grp, multiprocessing, hashlib
from distutils.spawn import find_executable from distutils.spawn import find_executable
import bench import bench
import semantic_version
from bench import env from bench import env
from six import iteritems from six import iteritems
@ -427,8 +428,19 @@ def update_requirements(bench_path='.'):
req_file = os.path.join(apps_dir, app, 'requirements.txt') req_file = os.path.join(apps_dir, app, 'requirements.txt')
install_requirements(pip, req_file) install_requirements(pip, req_file)
def update_npm_packages(bench_path='.'): def update_node_packages():
print('Updating node libraries...') 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') apps_dir = os.path.join(bench_path, 'apps')
if not find_executable('yarn'): if not find_executable('yarn'):
@ -442,6 +454,38 @@ def update_npm_packages(bench_path='.'):
exec_cmd('yarn install', cwd=app_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): def install_requirements(pip, req_file):
if os.path.exists(req_file): if os.path.exists(req_file):
exec_cmd("{pip} install -q -r {req_file}".format(pip=pip, req_file=req_file)) exec_cmd("{pip} install -q -r {req_file}".format(pip=pip, req_file=req_file))