From cd77b4225583687922f74c48b186d7589e5b412b Mon Sep 17 00:00:00 2001 From: Gavin D'souza Date: Thu, 26 Mar 2020 14:15:59 +0530 Subject: [PATCH] refactor: avoid redefining name from different scopes ref: deepsource https://deepsource.io/gh/frappe/bench/run/c9ee328a-6f59-49a9-b21c-8e770bd14091/python/PYL-W0621/ --- bench/commands/setup.py | 75 ++++++++++++-------------------- bench/config/production_setup.py | 12 +++-- bench/utils.py | 24 +++++----- 3 files changed, 49 insertions(+), 62 deletions(-) diff --git a/bench/commands/setup.py b/bench/commands/setup.py index 84e267c7..96360bea 100755 --- a/bench/commands/setup.py +++ b/bench/commands/setup.py @@ -2,12 +2,21 @@ import os import sys -# imports - module imports -from bench.utils import exec_cmd - # imports - third party imports -from six import PY3 import click +from six import PY3 + +# imports - module imports +import bench.config.lets_encrypt +import bench.config.nginx +import bench.config.procfile +import bench.config.production_setup +import bench.config.redis +import bench.config.site_config +import bench.config.supervisor + +import bench.utils +from bench.utils import exec_cmd, run_playbook @click.group(help="Setup command group for enabling setting up a Frappe environment") @@ -18,70 +27,59 @@ def setup(): @click.command("sudoers", help="Add commands to sudoers list for execution without password") @click.argument("user") def setup_sudoers(user): - from bench.utils import setup_sudoers - setup_sudoers(user) + bench.utils.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): - from bench.config.nginx import make_nginx_conf - make_nginx_conf(bench_path=".", yes=yes) + bench.config.nginx.make_nginx_conf(bench_path=".", yes=yes) @click.command("reload-nginx", help="Checks NGINX config file and reloads service") def reload_nginx(): - from bench.config.production_setup import reload_nginx - reload_nginx() + bench.config.production_setup.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) def setup_supervisor(user=None, yes=False): - from bench.config.supervisor import generate_supervisor_config - generate_supervisor_config(bench_path=".", user=user, yes=yes) + bench.config.supervisor.generate_supervisor_config(bench_path=".", user=user, yes=yes) @click.command("redis", help="Generates configuration for Redis") def setup_redis(): - from bench.config.redis import generate_config - generate_config(".") + bench.config.redis.generate_config(".") @click.command("fonts", help="Add Frappe fonts to system") def setup_fonts(): - from bench.utils import setup_fonts - setup_fonts() + bench.utils.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): - 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") def setup_backups(): - from bench.utils import setup_backups - setup_backups() + bench.utils.setup_backups() @click.command("env", help="Setup virtualenv for bench") @click.option("--python", type = str, default = "python3", help = "Path to Python Executable.") def setup_env(python="python3"): - from bench.utils import setup_env - setup_env(python=python) + bench.utils.setup_env(python=python) @click.command("firewall", help="Setup firewall for system") @click.option("--ssh_port") @click.option("--force") def setup_firewall(ssh_port=None, force=False): - from bench.utils import run_playbook - if not force: click.confirm("Setting up the firewall will block all ports except 80, 443 and {0}\nDo you want to continue?".format(ssh_port), abort=True) @@ -95,8 +93,6 @@ def setup_firewall(ssh_port=None, force=False): @click.argument("port") @click.option("--force") def set_ssh_port(port, force=False): - from bench.utils import run_playbook - if not force: click.confirm("This will change your SSH Port to {}\nDo you want to continue?".format(port), abort=True) @@ -108,8 +104,7 @@ 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): - 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") @@ -117,20 +112,17 @@ def setup_letsencrypt(site, custom_domain, non_interactive): @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): - 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") def setup_procfile(): - from bench.config.procfile import setup_procfile - setup_procfile(".") + bench.config.procfile.setup_procfile(".") @click.command("socketio", help="Setup node dependencies for socketio server") def setup_socketio(): - from bench.utils import setup_socketio - setup_socketio() + bench.utils.setup_socketio() @click.command("requirements", help="Setup Python and Node dependencies") @@ -203,34 +195,28 @@ def setup_config(): @click.option("--ssl-certificate-key", help="Absolute path to SSL Certificate Key") def add_domain(domain, site=None, ssl_certificate=None, ssl_certificate_key=None): """Add custom domain to site""" - from bench.config.site_config import add_domain - if not site: print("Please specify site") sys.exit(1) - add_domain(site, domain, ssl_certificate, ssl_certificate_key, bench_path=".") + bench.config.site_config.add_domain(site, domain, ssl_certificate, ssl_certificate_key, bench_path=".") @click.command("remove-domain", help="Remove custom domain from a site") @click.argument("domain") @click.option("--site", prompt=True) def remove_domain(domain, site=None): - from bench.config.site_config import remove_domain - if not site: print("Please specify site") sys.exit(1) - remove_domain(site, domain, bench_path=".") + bench.config.site_config.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.option("--domain", multiple=True) @click.option("--site", prompt=True) def sync_domains(domain=None, site=None): - from bench.config.site_config import sync_domains - if not site: print("Please specify site") sys.exit(1) @@ -241,7 +227,7 @@ def sync_domains(domain=None, site=None): print("Domains should be a json list of strings or dictionaries") sys.exit(1) - changed = sync_domains(site, domains, bench_path=".") + changed = bench.config.site_config.sync_domains(site, domains, bench_path=".") # if changed, success, else failure sys.exit(0 if changed else 1) @@ -253,8 +239,6 @@ def sync_domains(domain=None, site=None): @click.option("--mysql_root_password") @click.option("--container", is_flag=True, default=False) def setup_roles(role, **kwargs): - from bench.utils import run_playbook - extra_vars = {"production": True} extra_vars.update(kwargs) @@ -269,7 +253,6 @@ def setup_roles(role, **kwargs): @click.option("--bantime", default=600, help="The counter is set to zero if no match is found within 'findtime' seconds. Default is 600 seconds") @click.option("--findtime", default=600, help="Duration (in seconds) for IP to be banned for. Negative number for 'permanent' ban. Default is 600 seconds") def setup_nginx_proxy_jail(**kwargs): - from bench.utils import run_playbook run_playbook("roles/fail2ban/tasks/configure_nginx_jail.yml", extra_vars=kwargs) diff --git a/bench/config/production_setup.py b/bench/config/production_setup.py index 9dc4af69..dff6a402 100755 --- a/bench/config/production_setup.py +++ b/bench/config/production_setup.py @@ -83,17 +83,21 @@ def disable_production(bench_path='.'): reload_nginx() -def service(service, option): +def service(service_name, service_option): if os.path.basename(find_executable('systemctl') or '') == 'systemctl' and is_running_systemd(): - exec_cmd("sudo {service_manager} {option} {service}".format(service_manager='systemctl', option=option, service=service)) + systemctl_cmd = "sudo {service_manager} {service_option} {service_name}" + exec_cmd(systemctl_cmd.format(service_manager='systemctl', service_option=service_option, service_name=service_name)) + elif os.path.basename(find_executable('service') or '') == 'service': - exec_cmd("sudo {service_manager} {service} {option} ".format(service_manager='service', service=service, option=option)) + service_cmd = "sudo {service_manager} {service_name} {service_option}" + exec_cmd(service_cmd.format(service_manager='service', service_name=service_name, service_option=service_option)) + else: # look for 'service_manager' and 'service_manager_command' in environment service_manager = os.environ.get("BENCH_SERVICE_MANAGER") if service_manager: service_manager_command = (os.environ.get("BENCH_SERVICE_MANAGER_COMMAND") - or "{service_manager} {option} {service}").format(service_manager=service_manager, service=service, option=option) + or "{service_manager} {service_option} {service}").format(service_manager=service_manager, service=service, service_option=service_option) exec_cmd(service_manager_command) else: diff --git a/bench/utils.py b/bench/utils.py index 19fe9036..6362f305 100755 --- a/bench/utils.py +++ b/bench/utils.py @@ -66,10 +66,10 @@ def log(message, level=0): 2: color.red + 'ERROR', # fail 3: color.yellow + 'WARN' # warn/suggest } - start = (levels.get(level) + ': ') if level in levels else '' - end = '\033[0m' + start_line = (levels.get(level) + ': ') if level in levels else '' + end_line = '\033[0m' - print(start + message + end) + print(start_line + message + end_line) def safe_decode(string, encoding = 'utf-8'): @@ -1110,8 +1110,8 @@ def migrate_env(python, backup=False): from bench.config.common_site_config import get_config from bench.app import get_apps - log = logging.getLogger(__name__) - log.setLevel(logging.DEBUG) + logger = logging.getLogger(__name__) + logger.setLevel(logging.DEBUG) nvenv = 'env' path = os.getcwd() @@ -1127,12 +1127,12 @@ def migrate_env(python, backup=False): redis = '{redis} -p {port}'.format(redis=which('redis-cli'), port=rredis.port) - log.debug('Clearing Redis Cache...') + logger.debug('Clearing Redis Cache...') exec_cmd('{redis} FLUSHALL'.format(redis = redis)) - log.debug('Clearing Redis DataBase...') + logger.debug('Clearing Redis DataBase...') exec_cmd('{redis} FLUSHDB'.format(redis = redis)) except: - log.warn('Please ensure Redis Connections are running or Daemonized.') + logger.warn('Please ensure Redis Connections are running or Daemonized.') # Backup venv: restore using `virtualenv --relocatable` if needed if backup: @@ -1143,7 +1143,7 @@ def migrate_env(python, backup=False): source = os.path.join(path, 'env') target = parch - log.debug('Backing up Virtual Environment') + logger.debug('Backing up Virtual Environment') stamp = datetime.now().strftime('%Y%m%d_%H%M%S') dest = os.path.join(path, str(stamp)) @@ -1152,15 +1152,15 @@ def migrate_env(python, backup=False): # Create virtualenv using specified python try: - log.debug('Setting up a New Virtual {} Environment'.format(python)) + logger.debug('Setting up a New Virtual {} Environment'.format(python)) exec_cmd('{virtualenv} --python {python} {pvenv}'.format(virtualenv=virtualenv, python=python, pvenv=pvenv)) apps = ' '.join(["-e {}".format(os.path.join("apps", app)) for app in get_apps()]) exec_cmd('{0} install -q -U {1}'.format(pip, apps)) - log.debug('Migration Successful to {}'.format(python)) + logger.debug('Migration Successful to {}'.format(python)) except: - log.debug('Migration Error') + logger.debug('Migration Error') raise