2
0
mirror of https://github.com/frappe/bench.git synced 2024-11-12 00:06:36 +00:00

Merge pull request #876 from Thunderbottom/get-app-overwrite

fix(get-app): handle existing directory and other formatting changes
This commit is contained in:
gavin 2020-02-27 11:10:52 +05:30 committed by GitHub
commit aef35c6957
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 58 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.moves 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='.', skip_assets=False, verbose=False, def get_app(git_url, branch=None, bench_path='.', skip_assets=False, 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,41 @@ def get_app(git_url, branch=None, bench_path='.', skip_assets=False, verbose=Fal
# 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 ''
exec_cmd("git clone -q {git_url} {branch} {shallow_clone} --origin upstream".format( 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?'''.format(repo_name)):
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)
install_app(app=app_name, bench_path=bench_path, verbose=verbose, skip_assets=skip_assets)
sys.exit()
logger.info('Getting app {0}'.format(repo_name))
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)
install_app(app=app_name, bench_path=bench_path, verbose=verbose, skip_assets=skip_assets)
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 not skip_assets:
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 +166,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, skip_assets=False):
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 +175,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 not skip_assets:
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 +300,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 +389,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 +400,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 +416,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):

View File

@ -56,11 +56,12 @@ def init(path, apps_path, frappe_path, frappe_branch, no_procfile, no_backups, n
@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, default=False)
@click.option('--skip-assets', is_flag=True, default=False, help="Do not build assets") @click.option('--skip-assets', is_flag=True, default=False, help="Do not build assets")
def get_app(git_url, branch, name=None, skip_assets=False): def get_app(git_url, branch, name=None, overwrite=False, skip_assets=False):
"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, skip_assets=skip_assets) get_app(git_url, branch=branch, skip_assets=skip_assets, overwrite=overwrite)
@click.command('new-app') @click.command('new-app')