Skip to content

Commit

Permalink
fix: Cleanup import hell
Browse files Browse the repository at this point in the history
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
  • Loading branch information
gavindsouza committed Nov 12, 2021
1 parent b2e0fd1 commit e2fd9de
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 33 deletions.
2 changes: 1 addition & 1 deletion bench/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
find_parent_bench,
generate_command_cache,
get_cmd_output,
get_env_cmd,
is_bench_directory,
is_dist_editable,
is_root,
log,
setup_logging,
)
from bench.utils.bench import get_env_cmd

from_command_line = False
change_uid_msg = "You should not run this command as root"
Expand Down
55 changes: 28 additions & 27 deletions bench/commands/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@
import click

# 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


Expand All @@ -22,50 +17,54 @@ def setup():
@click.command("sudoers", help="Add commands to sudoers list for execution without password")
@click.argument("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.option("--yes", help="Yes to regeneration of nginx config file", default=False, is_flag=True)
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")
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.option("--user", help="optional user argument")
@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)
def setup_supervisor(user=None, yes=False, skip_redis=False):
bench.config.supervisor.update_supervisord_config(user=user, yes=yes)
bench.config.supervisor.generate_supervisor_config(bench_path=".", user=user, yes=yes, skip_redis=skip_redis)
from bench.config.supervisor import update_supervisord_config, generate_supervisor_config

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")
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")
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.argument("user")
@click.option("--yes", help="Yes to regeneration config", is_flag=True, default=False)
def setup_production(user, yes=False):
import bench.config.production_setup

bench.config.production_setup.setup_production(user=user, yes=yes)
from bench.config.production_setup import setup_production
setup_production(user=user, yes=yes)


@click.command("backups", help="Add cronjob for bench backups")
Expand Down Expand Up @@ -109,24 +108,23 @@ def set_ssh_port(port, force=False):
@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")
def setup_letsencrypt(site, custom_domain, non_interactive):
import bench.config.lets_encrypt

bench.config.lets_encrypt.setup_letsencrypt(site, custom_domain, bench_path=".", interactive=not non_interactive)
from bench.config.lets_encrypt import setup_letsencrypt
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.argument("domain")
@click.option("--email")
@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):
import bench.config.lets_encrypt

bench.config.lets_encrypt.setup_wildcard_ssl(domain, email, bench_path=".", exclude_base_domain=exclude_base_domain)
from bench.config.lets_encrypt import setup_wildcard_ssl
setup_wildcard_ssl(domain, email, bench_path=".", exclude_base_domain=exclude_base_domain)


@click.command("procfile", help="Generate Procfile for bench start")
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")
Expand Down Expand Up @@ -213,7 +211,8 @@ def add_domain(domain, site=None, ssl_certificate=None, ssl_certificate_key=None
print("Please specify site")
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")
Expand All @@ -224,7 +223,8 @@ def remove_domain(domain, site=None):
print("Please specify site")
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.")
Expand All @@ -241,7 +241,8 @@ def sync_domains(domain=None, site=None):
print("Domains should be a json list of strings or dictionaries")
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
sys.exit(0 if changed else 1)
Expand Down
15 changes: 10 additions & 5 deletions bench/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
import click

# imports - module imports
import bench
from bench import PROJECT_NAME, VERSION

from bench.exceptions import InvalidRemoteException, ValidationError


logger = logging.getLogger(bench.PROJECT_NAME)
logger = logging.getLogger(PROJECT_NAME)
bench_cache_file = '.bench.cmd'
paths_in_app = ('hooks.py', 'modules.txt', 'patches.txt', 'public')
paths_in_bench = ('apps', 'sites', 'config', 'logs', 'config/pids')
Expand Down Expand Up @@ -65,7 +66,7 @@ def log(message, level=0):


def check_latest_version():
if bench.VERSION.endswith("dev"):
if VERSION.endswith("dev"):
return

import requests
Expand All @@ -81,7 +82,7 @@ def check_latest_version():
if pypi_request.status_code == 200:
pypi_version_str = pypi_request.json().get('info').get('version')
pypi_version = Version(pypi_version_str)
local_version = Version(bench.VERSION)
local_version = Version(VERSION)

if pypi_version > local_version:
log(f"A newer version of bench is available: {local_version}{pypi_version}")
Expand Down Expand Up @@ -137,7 +138,7 @@ def logv(self, message, *args, **kws):
else:
hdlr = logging.NullHandler()

logger = logging.getLogger(bench.PROJECT_NAME)
logger = logging.getLogger(PROJECT_NAME)
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
Expand Down Expand Up @@ -180,6 +181,7 @@ def is_root():

def run_frappe_cmd(*args, **kwargs):
from bench.cli import from_command_line
from bench.utils.bench import get_env_cmd

bench_path = kwargs.get('bench_path', '.')
f = get_env_cmd('python', bench_path=bench_path)
Expand Down Expand Up @@ -395,6 +397,9 @@ def is_git_url(url):


def drop_privileges(uid_name='nobody', gid_name='nogroup'):
import grp
import pwd

# from http://stackoverflow.com/a/2699996
if os.getuid() != 0:
# We're not root so, like, whatever dude
Expand Down

0 comments on commit e2fd9de

Please sign in to comment.