From 0910278efdab255609d93b197c4d35eadfe3f752 Mon Sep 17 00:00:00 2001 From: "Chinmay D. Pai" Date: Thu, 20 Feb 2020 12:08:51 +0530 Subject: [PATCH] feat: add support to start bench console Signed-off-by: Chinmay D. Pai --- build/frappe-worker/commands/console.py | 25 ++++++++++++++++++++++++ build/frappe-worker/docker-entrypoint.sh | 17 ++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 build/frappe-worker/commands/console.py diff --git a/build/frappe-worker/commands/console.py b/build/frappe-worker/commands/console.py new file mode 100644 index 00000000..20b3fd34 --- /dev/null +++ b/build/frappe-worker/commands/console.py @@ -0,0 +1,25 @@ +import sys +import frappe +import IPython + +from frappe.utils import get_sites + + +def console(site): + "Start ipython console for a site" + if site not in get_sites(): + print("Site {0} does not exist on the current bench".format(site)) + return + + frappe.init(site=site) + frappe.connect() + frappe.local.lang = frappe.db.get_default("lang") + all_apps = frappe.get_installed_apps() + for app in all_apps: + locals()[app] = __import__(app) + print("Apps in this namespace:\n{}".format(", ".join(all_apps))) + IPython.embed(display_banner="", header="") + + +site = sys.argv[-1] +console(site) diff --git a/build/frappe-worker/docker-entrypoint.sh b/build/frappe-worker/docker-entrypoint.sh index fef4eda9..d5b65abf 100755 --- a/build/frappe-worker/docker-entrypoint.sh +++ b/build/frappe-worker/docker-entrypoint.sh @@ -151,6 +151,23 @@ elif [ "$1" = 'backup' ]; then python /home/frappe/frappe-bench/commands/backup.py fi +elif [ "$1" = 'console' ]; then + + if [[ -z "$2" ]]; then + echo "Need to specify a sitename with the command:" + echo "console " + exit 1 + fi + + if [[ -z "$RUN_AS_ROOT" ]]; then + su frappe -c ". /home/frappe/frappe-bench/env/bin/activate \ + && python /home/frappe/frappe-bench/commands/console.py $2" + exit + else + . /home/frappe/frappe-bench/env/bin/activate + python /home/frappe/frappe-bench/commands/console.py "$2" + fi + else exec su frappe -c "$@"