Skip to content

Commit

Permalink
fix: force use PYPI packaged bench
Browse files Browse the repository at this point in the history
  • Loading branch information
gavindsouza committed Mar 16, 2020
1 parent 92d2fbd commit 15b01e9
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 53 deletions.
10 changes: 2 additions & 8 deletions bench/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
from bench.utils import install_checker
from jinja2 import Environment, PackageLoader

__version__ = "4.1.0"

env = Environment(loader=PackageLoader('bench.config'))

FRAPPE_VERSION = None

def set_frappe_version(bench_path='.'):
from .app import get_current_frappe_version
global FRAPPE_VERSION
if not FRAPPE_VERSION:
FRAPPE_VERSION = get_current_frappe_version(bench_path=bench_path)
install_checker()
8 changes: 1 addition & 7 deletions bench/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,7 @@ def new_app(app, bench_path='.'):
app = app.lower().replace(" ", "_").replace("-", "_")
logger.info('creating new app {}'.format(app))
apps = os.path.abspath(os.path.join(bench_path, 'apps'))
bench.set_frappe_version(bench_path=bench_path)

if bench.FRAPPE_VERSION == 4:
exec_cmd("{frappe} --make_app {apps} {app}".format(frappe=get_frappe(bench_path=bench_path),
apps=apps, app=app))
else:
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)

def install_app(app, bench_path=".", verbose=False, no_cache=False):
Expand Down
1 change: 0 additions & 1 deletion bench/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ def bench_command(bench_path='.'):
import bench
from bench.utils import setup_logging

bench.set_frappe_version(bench_path=bench_path)
setup_logging(bench_path=bench_path)


Expand Down
83 changes: 46 additions & 37 deletions bench/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from six.moves.urllib.parse import urlparse

import bench
from bench import env


class PatchError(Exception):
Expand Down Expand Up @@ -110,16 +109,12 @@ def init(path, apps_path=None, no_procfile=False, no_backups=False, no_auto_upda
if apps_path:
install_apps_from_path(apps_path, bench_path=path)


bench.set_frappe_version(bench_path=path)
if bench.FRAPPE_VERSION > 5:
if not skip_assets:
update_node_packages(bench_path=path)

set_all_patches_executed(bench_path=path)
if not skip_assets:
update_node_packages(bench_path=path)
build_assets(bench_path=path)

set_all_patches_executed(bench_path=path)

if not skip_redis_config_generation:
redis.generate_config(path)

Expand Down Expand Up @@ -217,26 +212,16 @@ def setup_socketio(bench_path='.'):
babel-cli babel-preset-es2015 babel-preset-es2016 babel-preset-es2017 babel-preset-babili", cwd=bench_path)

def patch_sites(bench_path='.'):
bench.set_frappe_version(bench_path=bench_path)

try:
if bench.FRAPPE_VERSION == 4:
exec_cmd("{frappe} --latest all".format(frappe=get_frappe(bench_path=bench_path)), cwd=os.path.join(bench_path, 'sites'))
else:
run_frappe_cmd('--site', 'all', 'migrate', bench_path=bench_path)
run_frappe_cmd('--site', 'all', 'migrate', bench_path=bench_path)
except subprocess.CalledProcessError:
raise PatchError

def build_assets(bench_path='.', app=None):
bench.set_frappe_version(bench_path=bench_path)

if bench.FRAPPE_VERSION == 4:
exec_cmd("{frappe} --build".format(frappe=get_frappe(bench_path=bench_path)), cwd=os.path.join(bench_path, 'sites'))
else:
command = 'bench build'
if app:
command += ' --app {}'.format(app)
exec_cmd(command, cwd=bench_path)
command = 'bench build'
if app:
command += ' --app {}'.format(app)
exec_cmd(command, cwd=bench_path)

def get_sites(bench_path='.'):
sites_path = os.path.join(bench_path, 'sites')
Expand All @@ -255,12 +240,7 @@ def setup_auto_update(bench_path='.'):
def setup_backups(bench_path='.'):
logger.info('setting up backups')
bench_dir = get_bench_dir(bench_path=bench_path)
bench.set_frappe_version(bench_path=bench_path)

if bench.FRAPPE_VERSION == 4:
backup_command = "cd {sites_dir} && {frappe} --backup all".format(frappe=get_frappe(bench_path=bench_path),)
else:
backup_command = "cd {bench_dir} && {bench} --site all backup".format(bench_dir=bench_dir, bench=sys.argv[0])
backup_command = "cd {bench_dir} && {bench} --site all backup".format(bench_dir=bench_dir, bench=sys.argv[0])

add_to_crontab('0 */6 * * * {backup_command} >> {logfile} 2>&1'.format(backup_command=backup_command,
logfile=os.path.join(get_bench_dir(bench_path=bench_path), 'logs', 'backup.log')))
Expand Down Expand Up @@ -302,6 +282,8 @@ def update_bench(bench_repo=True, requirements=True):
logger.info("Bench Updated!")

def setup_sudoers(user):
from bench import env

if not os.path.exists('/etc/sudoers.d'):
os.makedirs('/etc/sudoers.d')

Expand Down Expand Up @@ -557,13 +539,7 @@ def install_requirements(req_file, user=False):
exec_cmd("{python} -m pip install {user_flag} -q -U -r {req_file}".format(python=python, user_flag=user_flag, req_file=req_file))

def backup_site(site, bench_path='.'):
bench.set_frappe_version(bench_path=bench_path)

if bench.FRAPPE_VERSION == 4:
exec_cmd("{frappe} --backup {site}".format(frappe=get_frappe(bench_path=bench_path), site=site),
cwd=os.path.join(bench_path, 'sites'))
else:
run_frappe_cmd('--site', site, 'backup', bench_path=bench_path)
run_frappe_cmd('--site', site, 'backup', bench_path=bench_path)

def backup_all_sites(bench_path='.'):
for site in get_sites(bench_path=bench_path):
Expand Down Expand Up @@ -1042,4 +1018,37 @@ def migrate_env(python, backup=False):
log.debug('Migration Successful to {}'.format(python))
except:
log.debug('Migration Error')
raise
raise

def is_dist_editable(dist):
"""Is distribution an editable install?"""
for path_item in sys.path:
egg_link = os.path.join(path_item, dist + '.egg-link')
if os.path.isfile(egg_link):
return True
return False

def install_checker():
development_mode = os.path.exists(os.path.expanduser("~/.bench.dev"))
bench_editable = is_dist_editable(dist="bench")

if development_mode and bench_editable:
log("bench is setup for development mode")

if development_mode and not bench_editable:
log("Clone bench's git repository from https://github.com/frappe/bench to develop using bench")

if not development_mode and not bench_editable:
"""Ideal scenario for bench users"""

if not development_mode and bench_editable:
log("Installing bench in editable mode is not recommended for production", level=3)
import click
# breaking change!!!
if click.confirm("Do you wish to uninstall editable install and install bench from PYPI?"):
log("Uninstalling bench")
os.system("pip uninstall bench -y")
log("Uninstalled bench", level=2)
log("Installing bench via PYPI")
os.system("pip install frappe-bench")
log("Installed bench via PYPI", level=2)

0 comments on commit 15b01e9

Please sign in to comment.