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

fix: Cleanup import hell

There was this bug that said `bench.utils.bench` doesn't have attribute
VERSION or whatever bench/__init__ was supposed to have. Now
bench/utils/__init__ had an `import bench` statement that was supposed
to ask for the top level module...but conflicted with the utils.bench
namespace. Changed the line in utils to `from bench import PROJECT_NAME,
VERSION` and it just works now...oh well

Other changes made were an attempt to cleanup and simplify the multi
level dotted path calls
This commit is contained in:
Gavin D'souza 2021-11-13 01:48:30 +05:30
parent b2e0fd15c6
commit e2fd9de921
3 changed files with 39 additions and 33 deletions

View File

@ -20,13 +20,13 @@ from bench.utils import (
find_parent_bench, find_parent_bench,
generate_command_cache, generate_command_cache,
get_cmd_output, get_cmd_output,
get_env_cmd,
is_bench_directory, is_bench_directory,
is_dist_editable, is_dist_editable,
is_root, is_root,
log, log,
setup_logging, setup_logging,
) )
from bench.utils.bench import get_env_cmd
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"

View File

@ -6,11 +6,6 @@ import sys
import click import click
# imports - module imports # imports - module imports
import bench.config.procfile
import bench.config.redis
import bench.config.site_config
import bench.config.supervisor
import bench.utils
from bench.utils import exec_cmd, run_playbook from bench.utils import exec_cmd, run_playbook
@ -22,22 +17,23 @@ def setup():
@click.command("sudoers", help="Add commands to sudoers list for execution without password") @click.command("sudoers", help="Add commands to sudoers list for execution without password")
@click.argument("user") @click.argument("user")
def setup_sudoers(user): def setup_sudoers(user):
bench.utils.setup_sudoers(user) from bench.utils.system import setup_sudoers
setup_sudoers(user)
@click.command("nginx", help="Generate configuration files for NGINX") @click.command("nginx", help="Generate configuration files for NGINX")
@click.option("--yes", help="Yes to regeneration of nginx config file", default=False, is_flag=True) @click.option("--yes", help="Yes to regeneration of nginx config file", default=False, is_flag=True)
def setup_nginx(yes=False): def setup_nginx(yes=False):
import bench.config.nginx from bench.config.nginx import make_nginx_conf
bench.config.nginx.make_nginx_conf(bench_path=".", yes=yes) make_nginx_conf(bench_path=".", yes=yes)
@click.command("reload-nginx", help="Checks NGINX config file and reloads service") @click.command("reload-nginx", help="Checks NGINX config file and reloads service")
def reload_nginx(): def reload_nginx():
import bench.config.production_setup from bench.config.production_setup import reload_nginx
bench.config.production_setup.reload_nginx() reload_nginx()
@click.command("supervisor", help="Generate configuration for supervisor") @click.command("supervisor", help="Generate configuration for supervisor")
@ -45,27 +41,30 @@ def reload_nginx():
@click.option("--yes", help="Yes to regeneration of supervisor config", is_flag=True, default=False) @click.option("--yes", help="Yes to regeneration of supervisor config", is_flag=True, default=False)
@click.option("--skip-redis", help="Skip redis configuration", is_flag=True, default=False) @click.option("--skip-redis", help="Skip redis configuration", is_flag=True, default=False)
def setup_supervisor(user=None, yes=False, skip_redis=False): def setup_supervisor(user=None, yes=False, skip_redis=False):
bench.config.supervisor.update_supervisord_config(user=user, yes=yes) from bench.config.supervisor import update_supervisord_config, generate_supervisor_config
bench.config.supervisor.generate_supervisor_config(bench_path=".", user=user, yes=yes, skip_redis=skip_redis)
update_supervisord_config(user=user, yes=yes)
generate_supervisor_config(bench_path=".", user=user, yes=yes, skip_redis=skip_redis)
@click.command("redis", help="Generates configuration for Redis") @click.command("redis", help="Generates configuration for Redis")
def setup_redis(): def setup_redis():
bench.config.redis.generate_config(".") from bench.config.redis import generate_config
generate_config(".")
@click.command("fonts", help="Add Frappe fonts to system") @click.command("fonts", help="Add Frappe fonts to system")
def setup_fonts(): def setup_fonts():
bench.utils.setup_fonts() from bench.utils.system import setup_fonts
setup_fonts()
@click.command("production", help="Setup Frappe production environment for specific user") @click.command("production", help="Setup Frappe production environment for specific user")
@click.argument("user") @click.argument("user")
@click.option("--yes", help="Yes to regeneration config", is_flag=True, default=False) @click.option("--yes", help="Yes to regeneration config", is_flag=True, default=False)
def setup_production(user, yes=False): def setup_production(user, yes=False):
import bench.config.production_setup from bench.config.production_setup import setup_production
setup_production(user=user, yes=yes)
bench.config.production_setup.setup_production(user=user, yes=yes)
@click.command("backups", help="Add cronjob for bench backups") @click.command("backups", help="Add cronjob for bench backups")
@ -109,9 +108,8 @@ def set_ssh_port(port, force=False):
@click.option("--custom-domain") @click.option("--custom-domain")
@click.option('-n', '--non-interactive', default=False, is_flag=True, help="Run command non-interactively. This flag restarts nginx and runs certbot non interactively. Shouldn't be used on 1'st attempt") @click.option('-n', '--non-interactive', default=False, is_flag=True, help="Run command non-interactively. This flag restarts nginx and runs certbot non interactively. Shouldn't be used on 1'st attempt")
def setup_letsencrypt(site, custom_domain, non_interactive): def setup_letsencrypt(site, custom_domain, non_interactive):
import bench.config.lets_encrypt from bench.config.lets_encrypt import setup_letsencrypt
setup_letsencrypt(site, custom_domain, bench_path=".", interactive=not non_interactive)
bench.config.lets_encrypt.setup_letsencrypt(site, custom_domain, bench_path=".", interactive=not non_interactive)
@click.command("wildcard-ssl", help="Setup wildcard SSL certificate for multi-tenant bench") @click.command("wildcard-ssl", help="Setup wildcard SSL certificate for multi-tenant bench")
@ -119,14 +117,14 @@ def setup_letsencrypt(site, custom_domain, non_interactive):
@click.option("--email") @click.option("--email")
@click.option("--exclude-base-domain", default=False, is_flag=True, help="SSL Certificate not applicable for base domain") @click.option("--exclude-base-domain", default=False, is_flag=True, help="SSL Certificate not applicable for base domain")
def setup_wildcard_ssl(domain, email, exclude_base_domain): def setup_wildcard_ssl(domain, email, exclude_base_domain):
import bench.config.lets_encrypt from bench.config.lets_encrypt import setup_wildcard_ssl
setup_wildcard_ssl(domain, email, bench_path=".", exclude_base_domain=exclude_base_domain)
bench.config.lets_encrypt.setup_wildcard_ssl(domain, email, bench_path=".", exclude_base_domain=exclude_base_domain)
@click.command("procfile", help="Generate Procfile for bench start") @click.command("procfile", help="Generate Procfile for bench start")
def setup_procfile(): def setup_procfile():
bench.config.procfile.setup_procfile(".") from bench.config.procfile import setup_procfile
setup_procfile(".")
@click.command("socketio", help="[DEPRECATED] Setup node dependencies for socketio server") @click.command("socketio", help="[DEPRECATED] Setup node dependencies for socketio server")
@ -213,7 +211,8 @@ def add_domain(domain, site=None, ssl_certificate=None, ssl_certificate_key=None
print("Please specify site") print("Please specify site")
sys.exit(1) sys.exit(1)
bench.config.site_config.add_domain(site, domain, ssl_certificate, ssl_certificate_key, bench_path=".") from bench.config.site_config import add_domain
add_domain(site, domain, ssl_certificate, ssl_certificate_key, bench_path=".")
@click.command("remove-domain", help="Remove custom domain from a site") @click.command("remove-domain", help="Remove custom domain from a site")
@ -224,7 +223,8 @@ def remove_domain(domain, site=None):
print("Please specify site") print("Please specify site")
sys.exit(1) sys.exit(1)
bench.config.site_config.remove_domain(site, domain, bench_path=".") from bench.config.site_config import remove_domain
remove_domain(site, domain, bench_path=".")
@click.command("sync-domains", help="Check if there is a change in domains. If yes, updates the domains list.") @click.command("sync-domains", help="Check if there is a change in domains. If yes, updates the domains list.")
@ -241,7 +241,8 @@ def sync_domains(domain=None, site=None):
print("Domains should be a json list of strings or dictionaries") print("Domains should be a json list of strings or dictionaries")
sys.exit(1) sys.exit(1)
changed = bench.config.site_config.sync_domains(site, domains, bench_path=".") from bench.config.site_config import sync_domains
changed = sync_domains(site, domains, bench_path=".")
# if changed, success, else failure # if changed, success, else failure
sys.exit(0 if changed else 1) sys.exit(0 if changed else 1)

View File

@ -12,11 +12,12 @@ from shlex import split
import click import click
# imports - module imports # imports - module imports
import bench from bench import PROJECT_NAME, VERSION
from bench.exceptions import InvalidRemoteException, ValidationError from bench.exceptions import InvalidRemoteException, ValidationError
logger = logging.getLogger(bench.PROJECT_NAME) logger = logging.getLogger(PROJECT_NAME)
bench_cache_file = '.bench.cmd' bench_cache_file = '.bench.cmd'
paths_in_app = ('hooks.py', 'modules.txt', 'patches.txt', 'public') paths_in_app = ('hooks.py', 'modules.txt', 'patches.txt', 'public')
paths_in_bench = ('apps', 'sites', 'config', 'logs', 'config/pids') paths_in_bench = ('apps', 'sites', 'config', 'logs', 'config/pids')
@ -65,7 +66,7 @@ def log(message, level=0):
def check_latest_version(): def check_latest_version():
if bench.VERSION.endswith("dev"): if VERSION.endswith("dev"):
return return
import requests import requests
@ -81,7 +82,7 @@ def check_latest_version():
if pypi_request.status_code == 200: if pypi_request.status_code == 200:
pypi_version_str = pypi_request.json().get('info').get('version') pypi_version_str = pypi_request.json().get('info').get('version')
pypi_version = Version(pypi_version_str) pypi_version = Version(pypi_version_str)
local_version = Version(bench.VERSION) local_version = Version(VERSION)
if pypi_version > local_version: if pypi_version > local_version:
log(f"A newer version of bench is available: {local_version}{pypi_version}") log(f"A newer version of bench is available: {local_version}{pypi_version}")
@ -137,7 +138,7 @@ def setup_logging(bench_path='.'):
else: else:
hdlr = logging.NullHandler() hdlr = logging.NullHandler()
logger = logging.getLogger(bench.PROJECT_NAME) logger = logging.getLogger(PROJECT_NAME)
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
hdlr.setFormatter(formatter) hdlr.setFormatter(formatter)
logger.addHandler(hdlr) logger.addHandler(hdlr)
@ -180,6 +181,7 @@ def is_root():
def run_frappe_cmd(*args, **kwargs): def run_frappe_cmd(*args, **kwargs):
from bench.cli import from_command_line from bench.cli import from_command_line
from bench.utils.bench import get_env_cmd
bench_path = kwargs.get('bench_path', '.') bench_path = kwargs.get('bench_path', '.')
f = get_env_cmd('python', bench_path=bench_path) f = get_env_cmd('python', bench_path=bench_path)
@ -395,6 +397,9 @@ def is_git_url(url):
def drop_privileges(uid_name='nobody', gid_name='nogroup'): def drop_privileges(uid_name='nobody', gid_name='nogroup'):
import grp
import pwd
# from http://stackoverflow.com/a/2699996 # from http://stackoverflow.com/a/2699996
if os.getuid() != 0: if os.getuid() != 0:
# We're not root so, like, whatever dude # We're not root so, like, whatever dude