2
0
mirror of https://github.com/frappe/bench.git synced 2024-11-16 10:05:21 +00:00

style: Black

This commit is contained in:
Gavin D'souza 2021-09-09 13:54:19 +05:30
parent 552b935f6b
commit 6160ff48ab

View File

@ -1,7 +1,6 @@
# imports - standard imports # imports - standard imports
import atexit import atexit
import json import json
import logging
import os import os
import pwd import pwd
import sys import sys
@ -14,7 +13,21 @@ import bench
from bench.app import get_apps from bench.app import get_apps
from bench.commands import bench_command from bench.commands import bench_command
from bench.config.common_site_config import get_config from bench.config.common_site_config import get_config
from bench.utils import PatchError, bench_cache_file, check_latest_version, drop_privileges, find_parent_bench, generate_command_cache, get_cmd_output, get_env_cmd, get_frappe, is_bench_directory, is_dist_editable, is_root, log, setup_logging from bench.utils import (
bench_cache_file,
check_latest_version,
drop_privileges,
find_parent_bench,
generate_command_cache,
get_cmd_output,
get_env_cmd,
get_frappe,
is_bench_directory,
is_dist_editable,
is_root,
log,
setup_logging,
)
from_command_line = False from_command_line = False
change_uid_msg = "You should not run this command as root" change_uid_msg = "You should not run this command as root"
@ -30,15 +43,30 @@ def cli():
logger = setup_logging() logger = setup_logging()
logger.info(command) logger.info(command)
if len(sys.argv) > 1 and sys.argv[1] not in ("src", ): if len(sys.argv) > 1 and sys.argv[1] not in ("src",):
check_uid() check_uid()
change_uid() change_uid()
change_dir() change_dir()
if is_dist_editable(bench.PROJECT_NAME) and len(sys.argv) > 1 and sys.argv[1] != "src" and not get_config(".").get("developer_mode"): if (
log("bench is installed in editable mode!\n\nThis is not the recommended mode of installation for production. Instead, install the package from PyPI with: `pip install frappe-bench`\n", level=3) is_dist_editable(bench.PROJECT_NAME)
and len(sys.argv) > 1
and sys.argv[1] != "src"
and not get_config(".").get("developer_mode")
):
log(
"bench is installed in editable mode!\n\nThis is not the recommended mode"
" of installation for production. Instead, install the package from PyPI"
" with: `pip install frappe-bench`\n",
level=3,
)
if not is_bench_directory() and not cmd_requires_root() and len(sys.argv) > 1 and sys.argv[1] not in ("init", "find", "src"): if (
not is_bench_directory()
and not cmd_requires_root()
and len(sys.argv) > 1
and sys.argv[1] not in ("init", "find", "src")
):
log("Command not being executed in bench directory", level=3) log("Command not being executed in bench directory", level=3)
if len(sys.argv) > 2 and sys.argv[1] == "frappe": if len(sys.argv) > 2 and sys.argv[1] == "frappe":
@ -81,24 +109,38 @@ def cli():
def check_uid(): def check_uid():
if cmd_requires_root() and not is_root(): if cmd_requires_root() and not is_root():
log('superuser privileges required for this command', level=3) log("superuser privileges required for this command", level=3)
sys.exit(1) sys.exit(1)
def cmd_requires_root(): def cmd_requires_root():
if len(sys.argv) > 2 and sys.argv[2] in ('production', 'sudoers', 'lets-encrypt', 'fonts', if len(sys.argv) > 2 and sys.argv[2] in (
'print', 'firewall', 'ssh-port', 'role', 'fail2ban', 'wildcard-ssl'): "production",
"sudoers",
"lets-encrypt",
"fonts",
"print",
"firewall",
"ssh-port",
"role",
"fail2ban",
"wildcard-ssl",
):
return True return True
if len(sys.argv) >= 2 and sys.argv[1] in ('patch', 'renew-lets-encrypt', 'disable-production'): if len(sys.argv) >= 2 and sys.argv[1] in (
"patch",
"renew-lets-encrypt",
"disable-production",
):
return True return True
if len(sys.argv) > 2 and sys.argv[1] in ('install'): if len(sys.argv) > 2 and sys.argv[1] in ("install"):
return True return True
def change_dir(): def change_dir():
if os.path.exists('config.json') or "init" in sys.argv: if os.path.exists("config.json") or "init" in sys.argv:
return return
dir_path_file = '/etc/frappe_bench_dir' dir_path_file = "/etc/frappe_bench_dir"
if os.path.exists(dir_path_file): if os.path.exists(dir_path_file):
with open(dir_path_file) as f: with open(dir_path_file) as f:
dir_path = f.read().strip() dir_path = f.read().strip()
@ -108,38 +150,39 @@ def change_dir():
def change_uid(): def change_uid():
if is_root() and not cmd_requires_root(): if is_root() and not cmd_requires_root():
frappe_user = get_config(".").get('frappe_user') frappe_user = get_config(".").get("frappe_user")
if frappe_user: if frappe_user:
drop_privileges(uid_name=frappe_user, gid_name=frappe_user) drop_privileges(uid_name=frappe_user, gid_name=frappe_user)
os.environ['HOME'] = pwd.getpwnam(frappe_user).pw_dir os.environ["HOME"] = pwd.getpwnam(frappe_user).pw_dir
else: else:
log(change_uid_msg, level=3) log(change_uid_msg, level=3)
sys.exit(1) sys.exit(1)
def old_frappe_cli(bench_path='.'): def old_frappe_cli(bench_path="."):
f = get_frappe(bench_path=bench_path) f = get_frappe(bench_path=bench_path)
os.chdir(os.path.join(bench_path, 'sites')) os.chdir(os.path.join(bench_path, "sites"))
os.execv(f, [f] + sys.argv[2:]) os.execv(f, [f] + sys.argv[2:])
def app_cmd(bench_path='.'): def app_cmd(bench_path="."):
f = get_env_cmd('python', bench_path=bench_path) f = get_env_cmd("python", bench_path=bench_path)
os.chdir(os.path.join(bench_path, 'sites')) os.chdir(os.path.join(bench_path, "sites"))
os.execv(f, [f] + ['-m', 'frappe.utils.bench_helper'] + sys.argv[1:]) os.execv(f, [f] + ["-m", "frappe.utils.bench_helper"] + sys.argv[1:])
def frappe_cmd(bench_path='.'): def frappe_cmd(bench_path="."):
f = get_env_cmd('python', bench_path=bench_path) f = get_env_cmd("python", bench_path=bench_path)
os.chdir(os.path.join(bench_path, 'sites')) os.chdir(os.path.join(bench_path, "sites"))
os.execv(f, [f] + ['-m', 'frappe.utils.bench_helper', 'frappe'] + sys.argv[1:]) os.execv(f, [f] + ["-m", "frappe.utils.bench_helper", "frappe"] + sys.argv[1:])
def get_cached_frappe_commands(): def get_cached_frappe_commands():
if os.path.exists(bench_cache_file): if os.path.exists(bench_cache_file):
command_dump = open(bench_cache_file, 'r').read() or '[]' command_dump = open(bench_cache_file, "r").read() or "[]"
return json.loads(command_dump) return json.loads(command_dump)
def get_frappe_commands(): def get_frappe_commands():
if not is_bench_directory(): if not is_bench_directory():
return [] return []
@ -147,12 +190,14 @@ def get_frappe_commands():
return generate_command_cache() return generate_command_cache()
def get_frappe_help(bench_path='.'): def get_frappe_help(bench_path="."):
python = get_env_cmd('python', bench_path=bench_path) python = get_env_cmd("python", bench_path=bench_path)
sites_path = os.path.join(bench_path, 'sites') sites_path = os.path.join(bench_path, "sites")
try: try:
out = get_cmd_output(f"{python} -m frappe.utils.bench_helper get-frappe-help", cwd=sites_path) out = get_cmd_output(
return "\n\nFramework commands:\n" + out.split('Commands:')[1] f"{python} -m frappe.utils.bench_helper get-frappe-help", cwd=sites_path
)
return "\n\nFramework commands:\n" + out.split("Commands:")[1]
except: except:
return "" return ""