From a86ae948f7b35a4afff6594dbd1499eb6a516b03 Mon Sep 17 00:00:00 2001 From: Gavin D'souza Date: Mon, 18 May 2020 13:01:33 +0530 Subject: [PATCH 1/4] fix(commands): raise SiteNotSpecifiedError if site not found in context --- frappe/__init__.py | 3 +-- frappe/commands/__init__.py | 1 - frappe/commands/scheduler.py | 8 +++++++- frappe/commands/site.py | 35 +++++++++++++++++++++++++++++++++-- frappe/commands/translate.py | 2 ++ frappe/commands/utils.py | 26 ++++++++++++++++++++++++++ frappe/exceptions.py | 5 +++++ frappe/utils/bench_helper.py | 16 +++++++++------- 8 files changed, 83 insertions(+), 13 deletions(-) diff --git a/frappe/__init__.py b/frappe/__init__.py index f0b6bfe41b..dbc16fa056 100644 --- a/frappe/__init__.py +++ b/frappe/__init__.py @@ -231,9 +231,8 @@ def get_site_config(sites_path=None, site_path=None): if os.path.exists(site_config): config.update(get_file_json(site_config)) elif local.site and not local.flags.new_site: - print("{0} does not exist".format(local.site)) + print("Site {0} does not exist".format(local.site)) sys.exit(1) - #raise IncorrectSitePath, "{0} does not exist".format(site_config) return _dict(config) diff --git a/frappe/commands/__init__.py b/frappe/commands/__init__.py index 8110f2ec19..7d36cbe28c 100644 --- a/frappe/commands/__init__.py +++ b/frappe/commands/__init__.py @@ -44,7 +44,6 @@ def get_site(context): site = context.sites[0] return site except (IndexError, TypeError): - print('Please specify --site sitename') sys.exit(1) def popen(command, *args, **kwargs): diff --git a/frappe/commands/scheduler.py b/frappe/commands/scheduler.py index 6f51c81211..d073c3d90e 100755 --- a/frappe/commands/scheduler.py +++ b/frappe/commands/scheduler.py @@ -4,6 +4,7 @@ import sys import frappe from frappe.utils import cint from frappe.commands import pass_context, get_site +from frappe.exceptions import SiteNotSpecifiedError def _is_scheduler_enabled(): enable_scheduler = False @@ -30,6 +31,8 @@ def trigger_scheduler_event(context, event): frappe.utils.scheduler.trigger(site, event, now=True) finally: frappe.destroy() + else: + raise SiteNotSpecifiedError @click.command('enable-scheduler') @pass_context @@ -45,6 +48,8 @@ def enable_scheduler(context): print("Enabled for", site) finally: frappe.destroy() + else: + raise SiteNotSpecifiedError @click.command('disable-scheduler') @pass_context @@ -60,7 +65,8 @@ def disable_scheduler(context): print("Disabled for", site) finally: frappe.destroy() - + else: + raise SiteNotSpecifiedError @click.command('scheduler') diff --git a/frappe/commands/site.py b/frappe/commands/site.py index 82ed72dd5c..f50ec05af4 100755 --- a/frappe/commands/site.py +++ b/frappe/commands/site.py @@ -15,6 +15,7 @@ import frappe from frappe import _ from frappe.commands import get_site, pass_context from frappe.commands.scheduler import _is_scheduler_enabled +from frappe.exceptions import SiteNotSpecifiedError from frappe.installer import update_site_config from frappe.utils import get_site_path, touch_file @@ -192,6 +193,8 @@ def install_app(context, apps): _install_app(app, verbose=context.verbose) finally: frappe.destroy() + else: + raise SiteNotSpecifiedError @click.command('list-apps') @pass_context @@ -221,7 +224,8 @@ def add_system_manager(context, email, first_name, last_name, send_welcome_email frappe.db.commit() finally: frappe.destroy() - + else: + raise SiteNotSpecifiedError @click.command('disable-user') @click.argument('email') @@ -252,6 +256,8 @@ def migrate(context, rebuild_website=False, skip_failing=False): migrate(context.verbose, rebuild_website=rebuild_website, skip_failing=skip_failing) finally: frappe.destroy() + else: + raise SiteNotSpecifiedError print("Compiling Python Files...") compileall.compile_dir('../apps', quiet=1, rx=re.compile('.*node_modules.*')) @@ -264,6 +270,8 @@ def migrate_to(context, frappe_provider): from frappe.integrations.frappe_providers import migrate_to for site in context.sites: migrate_to(site, frappe_provider) + else: + raise SiteNotSpecifiedError @click.command('run-patch') @click.argument('module') @@ -278,6 +286,8 @@ def run_patch(context, module): frappe.modules.patch_handler.run_single(module, force=context.force) finally: frappe.destroy() + else: + raise SiteNotSpecifiedError @click.command('reload-doc') @click.argument('module') @@ -294,6 +304,8 @@ def reload_doc(context, module, doctype, docname): frappe.db.commit() finally: frappe.destroy() + else: + raise SiteNotSpecifiedError @click.command('reload-doctype') @click.argument('doctype') @@ -308,6 +320,8 @@ def reload_doctype(context, doctype): frappe.db.commit() finally: frappe.destroy() + else: + raise SiteNotSpecifiedError @click.command('add-to-hosts') @pass_context @@ -315,6 +329,8 @@ def add_to_hosts(context): "Add site to hosts" for site in context.sites: frappe.commands.popen('echo 127.0.0.1\t{0} | sudo tee -a /etc/hosts'.format(site)) + else: + raise SiteNotSpecifiedError @click.command('use') @click.argument('site') @@ -328,7 +344,7 @@ def use(site, sites_path='.'): sitefile.write(site) print("Current Site set to {}".format(site)) else: - print("{} does not exist".format(site)) + print("Site {} does not exist".format(site)) @click.command('backup') @click.option('--with-files', default=False, is_flag=True, help="Take backup with files") @@ -361,6 +377,9 @@ def backup(context, with_files=False, backup_path_db=None, backup_path_files=Non print("Private files: ", odb.backup_path_private_files) frappe.destroy() + else: + raise SiteNotSpecifiedError + sys.exit(exit_code) @click.command('remove-from-installed-apps') @@ -376,6 +395,8 @@ def remove_from_installed_apps(context, app): remove_from_installed_apps(app) finally: frappe.destroy() + else: + raise SiteNotSpecifiedError @click.command('uninstall-app') @click.argument('app') @@ -392,6 +413,8 @@ def uninstall(context, app, dry_run=False, yes=False): remove_app(app, dry_run, yes) finally: frappe.destroy() + else: + raise SiteNotSpecifiedError @click.command('drop-site') @@ -483,6 +506,8 @@ def set_admin_password(context, admin_password, logout_all_sessions=False): admin_password = None finally: frappe.destroy() + else: + raise SiteNotSpecifiedError @click.command('set-last-active-for-user') @click.option('--user', help="Setup last active date for user") @@ -528,6 +553,8 @@ def publish_realtime(context, event, message, room, user, doctype, docname, afte frappe.db.commit() finally: frappe.destroy() + else: + raise SiteNotSpecifiedError @click.command('browse') @click.argument('site', required=False) @@ -555,6 +582,8 @@ def start_recording(context): for site in context.sites: frappe.init(site=site) frappe.recorder.start() + else: + raise SiteNotSpecifiedError @click.command('stop-recording') @@ -563,6 +592,8 @@ def stop_recording(context): for site in context.sites: frappe.init(site=site) frappe.recorder.stop() + else: + raise SiteNotSpecifiedError commands = [ diff --git a/frappe/commands/translate.py b/frappe/commands/translate.py index 5a48e2b409..de4d8aab0b 100644 --- a/frappe/commands/translate.py +++ b/frappe/commands/translate.py @@ -15,6 +15,8 @@ def build_message_files(context): frappe.translate.rebuild_all_translation_files() finally: frappe.destroy() + else: + raise SiteNotSpecifiedError @click.command('new-language') #, help="Create lang-code.csv for given app") @pass_context diff --git a/frappe/commands/utils.py b/frappe/commands/utils.py index 3610393d9a..835fa2d40e 100644 --- a/frappe/commands/utils.py +++ b/frappe/commands/utils.py @@ -51,6 +51,8 @@ def clear_cache(context): frappe.website.render.clear_cache() finally: frappe.destroy() + else: + raise SiteNotSpecifiedError @click.command('clear-website-cache') @@ -65,6 +67,8 @@ def clear_website_cache(context): frappe.website.render.clear_cache() finally: frappe.destroy() + else: + raise SiteNotSpecifiedError @click.command('destroy-all-sessions') @@ -81,6 +85,8 @@ def destroy_all_sessions(context, reason=None): frappe.db.commit() finally: frappe.destroy() + else: + raise SiteNotSpecifiedError @click.command('show-config') @@ -117,6 +123,8 @@ def reset_perms(context): reset_perms(d) finally: frappe.destroy() + else: + raise SiteNotSpecifiedError @click.command('execute') @@ -163,6 +171,8 @@ def execute(context, method, args=None, kwargs=None, profile=False): frappe.destroy() if ret: print(json.dumps(ret, default=json_handler)) + else: + raise SiteNotSpecifiedError @click.command('add-to-email-queue') @@ -197,6 +207,8 @@ def export_doc(context, doctype, docname): frappe.modules.export_doc(doctype, docname) finally: frappe.destroy() + else: + raise SiteNotSpecifiedError @click.command('export-json') @@ -214,6 +226,8 @@ def export_json(context, doctype, path, name=None): data_import.export_json(doctype, path, name=name) finally: frappe.destroy() + else: + raise SiteNotSpecifiedError @click.command('export-csv') @@ -230,6 +244,8 @@ def export_csv(context, doctype, path): data_import.export_csv(doctype, path) finally: frappe.destroy() + else: + raise SiteNotSpecifiedError @click.command('export-fixtures') @@ -245,6 +261,8 @@ def export_fixtures(context, app=None): export_fixtures(app=app) finally: frappe.destroy() + else: + raise SiteNotSpecifiedError @click.command('import-doc') @@ -267,6 +285,8 @@ def import_doc(context, path, force=False): data_import.import_doc(path, overwrite=context.force) finally: frappe.destroy() + else: + raise SiteNotSpecifiedError @click.command('import-csv') @@ -577,6 +597,8 @@ def request(context, args=None, path=None): print(frappe.response) finally: frappe.destroy() + else: + raise SiteNotSpecifiedError @click.command('make-app') @@ -610,6 +632,8 @@ def set_config(context, key, value, global_ = False, as_dict=False): frappe.init(site=site) update_site_config(key, value, validate=False) frappe.destroy() + else: + raise SiteNotSpecifiedError @click.command('version') @@ -658,6 +682,8 @@ def rebuild_global_search(context, static_pages=False): finally: frappe.destroy() + else: + raise SiteNotSpecifiedError @click.command('auto-deploy') diff --git a/frappe/exceptions.py b/frappe/exceptions.py index 5a1181f31e..1aac339228 100644 --- a/frappe/exceptions.py +++ b/frappe/exceptions.py @@ -13,6 +13,11 @@ if sys.version_info.major == 2: else: from builtins import FileNotFoundError +class SiteNotSpecifiedError(Exception): + def __init__(self, *args, **kwargs): + self.message = "Please specify --site sitename" + super(Exception, self).__init__(self.message) + class ValidationError(Exception): http_status_code = 417 diff --git a/frappe/utils/bench_helper.py b/frappe/utils/bench_helper.py index 7c5d209179..c46b42b132 100644 --- a/frappe/utils/bench_helper.py +++ b/frappe/utils/bench_helper.py @@ -50,14 +50,16 @@ def app_group(ctx, site=False, force=False, verbose=False, profile=False): ctx.info_name = '' def get_sites(site_arg): - if site_arg and site_arg == 'all': + if site_arg == 'all': return frappe.utils.get_sites() - else: - if site_arg: - return [site_arg] - if os.path.exists('currentsite.txt'): - with open('currentsite.txt') as f: - return [f.read().strip()] + elif site_arg: + return [site_arg] + elif os.path.exists('currentsite.txt'): + with open('currentsite.txt') as f: + site = f.read().strip() + if site: + return [site] + return [] def get_app_commands(app): if os.path.exists(os.path.join('..', 'apps', app, app, 'commands.py'))\ From 56eba9cb5d0d803468031f8a69945db8862651bb Mon Sep 17 00:00:00 2001 From: Gavin D'souza Date: Fri, 29 May 2020 17:37:35 +0530 Subject: [PATCH 2/4] fix: handle SiteNotSpecifiedError in passcontext --- frappe/commands/__init__.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/frappe/commands/__init__.py b/frappe/commands/__init__.py index 7d36cbe28c..832a7eb415 100644 --- a/frappe/commands/__init__.py +++ b/frappe/commands/__init__.py @@ -22,7 +22,11 @@ def pass_context(f): pr = cProfile.Profile() pr.enable() - ret = f(frappe._dict(ctx.obj), *args, **kwargs) + try: + ret = f(frappe._dict(ctx.obj), *args, **kwargs) + except frappe.exceptions.SiteNotSpecifiedError as e: + click.secho(str(e), fg='yellow') + sys.exit(1) if profile: pr.disable() From e3ae498e4d69c9bceab5c2b0841ee03e77c1b9fd Mon Sep 17 00:00:00 2001 From: Gavin D'souza Date: Fri, 29 May 2020 17:45:43 +0530 Subject: [PATCH 3/4] fix: fixed imports --- frappe/commands/translate.py | 1 + frappe/commands/utils.py | 1 + 2 files changed, 2 insertions(+) diff --git a/frappe/commands/translate.py b/frappe/commands/translate.py index de4d8aab0b..f11954cb28 100644 --- a/frappe/commands/translate.py +++ b/frappe/commands/translate.py @@ -1,6 +1,7 @@ from __future__ import unicode_literals, absolute_import, print_function import click from frappe.commands import pass_context, get_site +from frappe.exceptions import SiteNotSpecifiedError # translation @click.command('build-message-files') diff --git a/frappe/commands/utils.py b/frappe/commands/utils.py index 835fa2d40e..869178ff3c 100644 --- a/frappe/commands/utils.py +++ b/frappe/commands/utils.py @@ -5,6 +5,7 @@ import click import json, os, sys, subprocess from distutils.spawn import find_executable import frappe +from frappe.exceptions import SiteNotSpecifiedError from frappe.commands import pass_context, get_site from frappe.utils import update_progress_bar, get_bench_path from frappe.utils.response import json_handler From 2149283fd1ec45e26cbb0f7b0c97fca987487e36 Mon Sep 17 00:00:00 2001 From: Gavin D'souza Date: Mon, 1 Jun 2020 12:43:41 +0530 Subject: [PATCH 4/4] fix: raise SitNotSpecifiedError via get_site --- frappe/commands/__init__.py | 2 +- frappe/commands/utils.py | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/frappe/commands/__init__.py b/frappe/commands/__init__.py index 832a7eb415..42f4440547 100644 --- a/frappe/commands/__init__.py +++ b/frappe/commands/__init__.py @@ -48,7 +48,7 @@ def get_site(context): site = context.sites[0] return site except (IndexError, TypeError): - sys.exit(1) + raise frappe.SiteNotSpecifiedError def popen(command, *args, **kwargs): output = kwargs.get('output', True) diff --git a/frappe/commands/utils.py b/frappe/commands/utils.py index 869178ff3c..9bed7eb472 100644 --- a/frappe/commands/utils.py +++ b/frappe/commands/utils.py @@ -385,6 +385,8 @@ def mariadb(context): import os site = get_site(context) + if not site: + raise SiteNotSpecifiedError frappe.init(site=site) # This is assuming you're within the bench instance.