2
0
mirror of https://github.com/frappe/bench.git synced 2025-01-26 16:08:23 +00:00
bench/bench/cli.py

114 lines
3.5 KiB
Python
Raw Normal View History

2014-07-10 22:51:34 +05:30
import click
import os, sys, logging, json, pwd, subprocess
from bench.utils import is_root, PatchError, drop_privileges, get_env_cmd, get_cmd_output, get_frappe
from bench.app import get_apps
from bench.config.common_site_config import get_config
from bench.commands import bench_command
2014-07-11 15:03:07 +05:30
logger = logging.getLogger('bench')
from_command_line = False
2014-07-11 13:10:33 +05:30
def cli():
global from_command_line
from_command_line = True
check_uid()
change_dir()
change_uid()
2014-07-15 11:47:36 +05:30
if len(sys.argv) > 2 and sys.argv[1] == "frappe":
2015-03-02 17:12:41 +05:30
return old_frappe_cli()
2015-03-02 17:12:41 +05:30
elif len(sys.argv) > 1 and sys.argv[1] in get_frappe_commands():
return frappe_cmd()
2015-03-05 16:34:28 +05:30
elif len(sys.argv) > 1 and sys.argv[1] in ("--site", "--verbose", "--force", "--profile"):
2015-03-02 17:12:41 +05:30
return frappe_cmd()
2015-03-02 17:12:41 +05:30
elif len(sys.argv) > 1 and sys.argv[1]=="--help":
print(click.Context(bench_command).get_help())
print()
print(get_frappe_help())
2015-08-14 15:41:53 +05:30
return
2015-03-02 17:12:41 +05:30
elif len(sys.argv) > 1 and sys.argv[1] in get_apps():
return app_cmd()
2015-03-02 17:12:41 +05:30
else:
2015-05-11 20:51:17 +05:30
try:
# NOTE: this is the main bench command
bench_command()
2015-05-11 20:51:17 +05:30
except PatchError:
sys.exit(1)
2014-07-11 13:10:33 +05:30
def check_uid():
if cmd_requires_root() and not is_root():
print('superuser privileges required for this command')
sys.exit(1)
2014-11-10 14:56:09 +05:30
def cmd_requires_root():
2017-09-18 12:37:55 +05:30
if len(sys.argv) > 2 and sys.argv[2] in ('production', 'sudoers', 'lets-encrypt', 'fonts',
'print', 'firewall', 'ssh-port', 'role', 'fail2ban'):
return True
2017-09-18 18:44:00 +05:30
if len(sys.argv) >= 2 and sys.argv[1] in ('patch', 'renew-lets-encrypt', 'disable-production',
'install'):
return True
2014-11-10 14:56:09 +05:30
def change_dir():
if os.path.exists('config.json') or "init" in sys.argv:
return
dir_path_file = '/etc/frappe_bench_dir'
if os.path.exists(dir_path_file):
with open(dir_path_file) as f:
dir_path = f.read().strip()
if os.path.exists(dir_path):
os.chdir(dir_path)
def change_uid():
2014-11-10 14:56:09 +05:30
if is_root() and not cmd_requires_root():
frappe_user = get_config(".").get('frappe_user')
if frappe_user:
2014-11-10 21:21:45 +05:30
drop_privileges(uid_name=frappe_user, gid_name=frappe_user)
2014-11-10 16:03:23 +05:30
os.environ['HOME'] = pwd.getpwnam(frappe_user).pw_dir
else:
print('You should not run this command as root')
sys.exit(1)
2016-06-16 11:18:51 +05:30
def old_frappe_cli(bench_path='.'):
f = get_frappe(bench_path=bench_path)
os.chdir(os.path.join(bench_path, 'sites'))
2014-07-11 13:10:33 +05:30
os.execv(f, [f] + sys.argv[2:])
2014-07-10 22:51:34 +05:30
2016-06-16 11:18:51 +05:30
def app_cmd(bench_path='.'):
f = get_env_cmd('python', bench_path=bench_path)
os.chdir(os.path.join(bench_path, 'sites'))
os.execv(f, [f] + ['-m', 'frappe.utils.bench_helper'] + sys.argv[1:])
2016-06-16 11:18:51 +05:30
def frappe_cmd(bench_path='.'):
f = get_env_cmd('python', bench_path=bench_path)
os.chdir(os.path.join(bench_path, 'sites'))
2015-03-02 17:12:41 +05:30
os.execv(f, [f] + ['-m', 'frappe.utils.bench_helper', 'frappe'] + sys.argv[1:])
2016-06-16 11:18:51 +05:30
def get_frappe_commands(bench_path='.'):
python = get_env_cmd('python', bench_path=bench_path)
sites_path = os.path.join(bench_path, 'sites')
2015-03-04 11:32:12 +05:30
if not os.path.exists(sites_path):
return []
2015-03-02 17:12:41 +05:30
try:
2018-01-29 17:54:03 +05:30
output = get_cmd_output("{python} -m frappe.utils.bench_helper get-frappe-commands".format(python=python), cwd=sites_path)
2018-03-12 18:25:00 +05:30
# output = output.decode('utf-8')
2018-01-29 17:54:03 +05:30
return json.loads(output)
2015-03-02 17:12:41 +05:30
except subprocess.CalledProcessError:
return []
2016-06-16 11:18:51 +05:30
def get_frappe_help(bench_path='.'):
python = get_env_cmd('python', bench_path=bench_path)
sites_path = os.path.join(bench_path, 'sites')
2015-03-04 11:32:12 +05:30
if not os.path.exists(sites_path):
return []
2015-03-02 17:12:41 +05:30
try:
out = get_cmd_output("{python} -m frappe.utils.bench_helper get-frappe-help".format(python=python), cwd=sites_path)
return "Framework commands:\n" + out.split('Commands:')[1]
except subprocess.CalledProcessError:
return ""