2
0
mirror of https://github.com/frappe/bench.git synced 2024-09-24 04:59:01 +00:00

fix(get-app): handle existing directory and other formatting changes

* if a directory for the application already exists, provide user with 2
  options:

  * ask to remove the directory and reclone (overwrite)
  * ask if the user wants to reinstall the application

* separate get_app_name from get_app

* move building assets and postprocessing to install_app method

* make formatting changes to the code for flake8

* use reload_module from six instead of builtins

Signed-off-by: Chinmay D. Pai <chinmaydpai@gmail.com>
This commit is contained in:
Chinmay D. Pai 2019-12-15 12:06:12 +05:30
parent d2d577201d
commit d1a3017172
No known key found for this signature in database
GPG Key ID: 75507BE256F40CED
2 changed files with 61 additions and 44 deletions

View File

@ -4,6 +4,7 @@ from .utils import (exec_cmd, get_frappe, check_git_for_shallow_clone, build_ass
restart_supervisor_processes, get_cmd_output, run_frappe_cmd, CommandFailedError, restart_supervisor_processes, get_cmd_output, run_frappe_cmd, CommandFailedError,
restart_systemd_processes) restart_systemd_processes)
from .config.common_site_config import get_config from .config.common_site_config import get_config
from six import reload_module
import logging import logging
import requests import requests
@ -14,6 +15,7 @@ import subprocess
import bench import bench
import sys import sys
import shutil import shutil
import click
logging.basicConfig(level="DEBUG") logging.basicConfig(level="DEBUG")
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -93,7 +95,7 @@ def remove_from_excluded_apps_txt(app, bench_path='.'):
return write_excluded_apps_txt(apps, bench_path=bench_path) return write_excluded_apps_txt(apps, bench_path=bench_path)
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,
postprocess = True): postprocess=True, overwrite=False):
# from bench.utils import check_url # from bench.utils import check_url
try: try:
from urlparse import urljoin from urlparse import urljoin
@ -114,37 +116,43 @@ def get_app(git_url, branch=None, bench_path='.', build_asset_files=True, verbos
# Gets repo name from URL # Gets repo name from URL
repo_name = git_url.rsplit('/', 1)[1].rsplit('.', 1)[0] repo_name = git_url.rsplit('/', 1)[1].rsplit('.', 1)[0]
logger.info('getting app {}'.format(repo_name))
shallow_clone = '--depth 1' if check_git_for_shallow_clone() else '' shallow_clone = '--depth 1' if check_git_for_shallow_clone() else ''
branch = '--branch {branch}'.format(branch=branch) if branch else '' branch = '--branch {branch}'.format(branch=branch) if branch else ''
if os.path.isdir(os.path.join(bench_path, 'apps', repo_name)):
# application directory already exists
# prompt user to overwrite it
if overwrite or click.confirm('''A directory for the application "{0}" already exists.
Do you want to continue and overwrite it?'''):
shutil.rmtree(os.path.join(bench_path, 'apps', repo_name))
elif click.confirm('''Do you want to reinstall the existing application?''', abort=True):
app_name = get_app_name(bench_path, repo_name)
print("Reinstalling {0}".format(app_name))
install_app(app=app_name, bench_path=bench_path, verbose=verbose, build_asset_files=build_asset_files)
sys.exit(1)
logger.info('Getting app {0}'.format(repo_name))
exec_cmd("git clone {git_url} {branch} {shallow_clone} --origin upstream".format( exec_cmd("git clone {git_url} {branch} {shallow_clone} --origin upstream".format(
git_url=git_url, git_url=git_url,
shallow_clone=shallow_clone, shallow_clone=shallow_clone,
branch=branch), branch=branch),
cwd=os.path.join(bench_path, 'apps')) cwd=os.path.join(bench_path, 'apps'))
#Retrieves app name from setup.py app_name = get_app_name(bench_path, repo_name)
print("Installing {0}".format(app_name))
install_app(app=app_name, bench_path=bench_path, verbose=verbose, build_asset_files=build_asset_files)
def get_app_name(bench_path, repo_name):
# retrieves app name from setup.py
app_path = os.path.join(bench_path, 'apps', repo_name, 'setup.py') app_path = os.path.join(bench_path, 'apps', repo_name, 'setup.py')
with open(app_path, 'rb') as f: with open(app_path, 'rb') as f:
app_name = re.search(r'name\s*=\s*[\'"](.*)[\'"]', f.read().decode('utf-8')).group(1) app_name = re.search(r'name\s*=\s*[\'"](.*)[\'"]', f.read().decode('utf-8')).group(1)
if repo_name != app_name: if repo_name != app_name:
apps_path = os.path.join(os.path.abspath(bench_path), 'apps') apps_path = os.path.join(os.path.abspath(bench_path), 'apps')
os.rename(os.path.join(apps_path, repo_name), os.path.join(apps_path, app_name)) os.rename(os.path.join(apps_path, repo_name), os.path.join(apps_path, app_name))
return app_name
print('installing', app_name)
install_app(app=app_name, bench_path=bench_path, verbose=verbose)
if postprocess:
if build_asset_files:
build_assets(bench_path=bench_path, app=app_name)
conf = get_config(bench_path=bench_path)
if conf.get('restart_supervisor_on_update'):
restart_supervisor_processes(bench_path=bench_path)
if conf.get('restart_systemd_on_update'):
restart_systemd_processes(bench_path=bench_path)
def new_app(app, bench_path='.'): def new_app(app, bench_path='.'):
# For backwards compatibility # For backwards compatibility
@ -160,7 +168,8 @@ def new_app(app, bench_path='.'):
run_frappe_cmd('make-app', apps, app, bench_path=bench_path) run_frappe_cmd('make-app', apps, app, bench_path=bench_path)
install_app(app, bench_path=bench_path) install_app(app, bench_path=bench_path)
def install_app(app, bench_path=".", verbose=False, no_cache=False):
def install_app(app, bench_path=".", verbose=False, no_cache=False, postprocess=True, build_asset_files=True):
logger.info("installing {}".format(app)) logger.info("installing {}".format(app))
pip_path = os.path.join(bench_path, "env", "bin", "pip") pip_path = os.path.join(bench_path, "env", "bin", "pip")
@ -168,11 +177,23 @@ def install_app(app, bench_path=".", verbose=False, no_cache=False):
app_path = os.path.join(bench_path, "apps", app) app_path = os.path.join(bench_path, "apps", app)
cache_flag = "--no-cache-dir" if no_cache else "" cache_flag = "--no-cache-dir" if no_cache else ""
exec_cmd("{pip} install {quiet} -U -e {app} {no_cache}".format(pip=pip_path, quiet=quiet_flag, app=app_path, no_cache=cache_flag)) exec_cmd("{pip} install {quiet} -U -e {app} {no_cache}".format(pip=pip_path,
quiet=quiet_flag, app=app_path, no_cache=cache_flag))
add_to_appstxt(app, bench_path=bench_path) add_to_appstxt(app, bench_path=bench_path)
if postprocess:
if build_asset_files:
build_assets(bench_path=bench_path, app=app)
conf = get_config(bench_path=bench_path)
if conf.get('restart_supervisor_on_update'):
restart_supervisor_processes(bench_path=bench_path)
if conf.get('restart_systemd_on_update'):
restart_systemd_processes(bench_path=bench_path)
def remove_app(app, bench_path='.'): def remove_app(app, bench_path='.'):
if not app in get_apps(bench_path): if app not in get_apps(bench_path):
print("No app named {0}".format(app)) print("No app named {0}".format(app))
sys.exit(1) sys.exit(1)
@ -281,8 +302,7 @@ def get_current_branch(app, bench_path='.'):
def get_remote(app, bench_path='.'): def get_remote(app, bench_path='.'):
repo_dir = get_repo_dir(app, bench_path=bench_path) repo_dir = get_repo_dir(app, bench_path=bench_path)
contents = subprocess.check_output(['git', 'remote', '-v'], cwd=repo_dir, contents = subprocess.check_output(['git', 'remote', '-v'], cwd=repo_dir, stderr=subprocess.STDOUT)
stderr=subprocess.STDOUT)
contents = contents.decode('utf-8') contents = contents.decode('utf-8')
if re.findall('upstream[\s]+', contents): if re.findall('upstream[\s]+', contents):
return 'upstream' return 'upstream'
@ -371,7 +391,7 @@ def switch_branch(branch, apps=None, bench_path='.', upgrade=False, check_upgrad
except CommandFailedError: except CommandFailedError:
print("Error switching to branch {0} for {1}".format(branch, app)) print("Error switching to branch {0} for {1}".format(branch, app))
except InvalidRemoteException: except InvalidRemoteException:
print("Remote does not exist for app "+app) print("Remote does not exist for app {0}".format(app))
except InvalidBranchException: except InvalidBranchException:
print("Branch {0} does not exist in Upstream for {1}".format(branch, app)) print("Branch {0} does not exist in Upstream for {1}".format(branch, app))
@ -382,11 +402,7 @@ def switch_branch(branch, apps=None, bench_path='.', upgrade=False, check_upgrad
update_requirements() update_requirements()
update_node_packages() update_node_packages()
pre_upgrade(version_upgrade[1], version_upgrade[2]) pre_upgrade(version_upgrade[1], version_upgrade[2])
if sys.version_info >= (3, 4): reload_module(utils)
import importlib
importlib.reload(utils)
else:
reload(utils)
backup_all_sites() backup_all_sites()
patch_sites() patch_sites()
build_assets() build_assets()
@ -402,8 +418,7 @@ def switch_to_develop(apps=None, bench_path='.', upgrade=True):
switch_branch('develop', apps=apps, bench_path=bench_path, upgrade=upgrade) switch_branch('develop', apps=apps, bench_path=bench_path, upgrade=upgrade)
def get_version_from_string(contents, field='__version__'): def get_version_from_string(contents, field='__version__'):
match = re.search(r"^(\s*%s\s*=\s*['\\\"])(.+?)(['\"])(?sm)" % field, match = re.search(r"^(\s*%s\s*=\s*['\\\"])(.+?)(['\"])(?sm)" % field, contents)
contents)
return match.group(2) return match.group(2)
def get_major_version(version): def get_major_version(version):
@ -427,7 +442,8 @@ def validate_branch():
branch = get_current_branch(app) branch = get_current_branch(app)
if branch == "master": if branch == "master":
print(''' master branch is renamed to version-11 and develop to version-12. Please switch to new branches to get future updates. print('''master branch is renamed to version-11 and develop to version-12.
Please switch to new branches to get future updates.
To switch to version 11, run the following commands: bench switch-to-branch version-11''') To switch to version 11, run the following commands: bench switch-to-branch version-11''')
sys.exit(1) sys.exit(1)

View File

@ -35,10 +35,11 @@ def init(path, apps_path, frappe_path, frappe_branch, no_procfile, no_backups,
@click.argument('name', nargs=-1) # Dummy argument for backward compatibility @click.argument('name', nargs=-1) # Dummy argument for backward compatibility
@click.argument('git-url') @click.argument('git-url')
@click.option('--branch', default=None, help="branch to checkout") @click.option('--branch', default=None, help="branch to checkout")
@click.option('--overwrite' is_flag=True)
def get_app(git_url, branch, name=None): def get_app(git_url, branch, name=None):
"clone an app from the internet and set it up in your bench" "clone an app from the internet and set it up in your bench"
from bench.app import get_app from bench.app import get_app
get_app(git_url, branch=branch) get_app(git_url, branch=branch, overwrite=overwrite)
@click.command('new-app') @click.command('new-app')