From e08a12477ddbd1cfac08cdd8e95c7251816d4dce Mon Sep 17 00:00:00 2001 From: Gavin D'souza Date: Sat, 13 Nov 2021 00:19:12 +0530 Subject: [PATCH] chore: Get rid of import * utils --- bench/app.py | 4 ++-- bench/bench.py | 11 ++++------- bench/commands/install.py | 3 ++- bench/commands/make.py | 5 +++-- bench/commands/update.py | 4 ++-- bench/commands/utils.py | 18 +++++++++--------- bench/config/lets_encrypt.py | 3 ++- bench/config/production_setup.py | 3 ++- bench/release.py | 2 ++ bench/utils/__init__.py | 26 ++++++++++++++++++++------ bench/utils/system.py | 21 --------------------- 11 files changed, 48 insertions(+), 52 deletions(-) diff --git a/bench/app.py b/bench/app.py index 5872189a..d3aee384 100755 --- a/bench/app.py +++ b/bench/app.py @@ -267,7 +267,7 @@ def new_app(app, bench_path='.'): def install_app(app, bench_path=".", verbose=False, no_cache=False, restart_bench=True, skip_assets=False): from bench.bench import Bench - from bench.utils import get_env_cmd + from bench.utils.bench import get_env_cmd install_text = f'Installing {app}' click.secho(install_text, fg="yellow") @@ -288,7 +288,7 @@ def install_app(app, bench_path=".", verbose=False, no_cache=False, restart_benc conf = Bench(bench_path).conf if conf.get("developer_mode"): - from bench.utils import install_python_dev_dependencies + from bench.utils.bench import install_python_dev_dependencies install_python_dev_dependencies(apps=app) if not skip_assets: diff --git a/bench/bench.py b/bench/bench.py index 9153dc52..e1db12b6 100644 --- a/bench/bench.py +++ b/bench/bench.py @@ -1,18 +1,17 @@ import os import shutil import sys -import typing import logging -from typing import MutableSequence +from typing import MutableSequence, TYPE_CHECKING import bench from bench.exceptions import ValidationError from bench.config.common_site_config import setup_config -from bench.utils import paths_in_bench, get_venv_path, exec_cmd, get_env_cmd, is_frappe_app, get_git_version, run_frappe_cmd -from bench.utils.bench import validate_app_installed_on_sites, restart_supervisor_processes, restart_systemd_processes, remove_backups_crontab +from bench.utils import paths_in_bench, exec_cmd, is_frappe_app, get_git_version, run_frappe_cmd +from bench.utils.bench import validate_app_installed_on_sites, restart_supervisor_processes, restart_systemd_processes, remove_backups_crontab, get_venv_path, get_env_cmd -if typing.TYPE_CHECKING: +if TYPE_CHECKING: from bench.app import App logger = logging.getLogger(bench.PROJECT_NAME) @@ -231,8 +230,6 @@ class BenchSetup(Base): return setup_logging(bench_path=self.bench.name) def patches(self): - import shutil - shutil.copy( os.path.join(os.path.dirname(os.path.abspath(__file__)), 'patches', 'patches.txt'), os.path.join(self.bench.name, 'patches.txt') diff --git a/bench/commands/install.py b/bench/commands/install.py index e1fc93bb..132d2255 100644 --- a/bench/commands/install.py +++ b/bench/commands/install.py @@ -1,5 +1,6 @@ # imports - module imports -from bench.utils import run_playbook, setup_sudoers +from bench.utils import run_playbook +from bench.utils.system import setup_sudoers # imports - third party imports import click diff --git a/bench/commands/make.py b/bench/commands/make.py index 53908e9e..618af9ad 100755 --- a/bench/commands/make.py +++ b/bench/commands/make.py @@ -18,7 +18,8 @@ import click @click.option('--verbose',is_flag=True, help="Verbose output during install") def init(path, apps_path, frappe_path, frappe_branch, no_procfile, no_backups, clone_from, verbose, skip_redis_config_generation, clone_without_update, ignore_exist=False, skip_assets=False, python='python3'): import os - from bench.utils import init, log + from bench.utils import log + from bench.utils.system import init if not ignore_exist and os.path.exists(path): log(f"Bench instance already exists at {path}", level=2) @@ -122,6 +123,6 @@ def include_app_for_update(app_name): def pip(ctx, args): "Run pip commands in bench env" import os - from bench.utils import get_env_cmd + from bench.utils.bench import get_env_cmd env_py = get_env_cmd('python') os.execv(env_py, (env_py, '-m', 'pip') + args) diff --git a/bench/commands/update.py b/bench/commands/update.py index 1e1979cb..5d97d1d8 100755 --- a/bench/commands/update.py +++ b/bench/commands/update.py @@ -3,7 +3,7 @@ import click # imports - module imports from bench.app import pull_apps -from bench.utils import post_upgrade, patch_sites, build_assets +from bench.utils.bench import post_upgrade, patch_sites, build_assets @click.command('update', help="Performs an update operation on current bench. Without any flags will backup, pull, setup requirements, build, run patches and restart bench. Using specific flags will only do certain tasks instead of all") @@ -19,7 +19,7 @@ from bench.utils import post_upgrade, patch_sites, build_assets @click.option('--force', is_flag=True, help="Forces major version upgrades") @click.option('--reset', is_flag=True, help="Hard resets git branch's to their new states overriding any changes and overriding rebase on pull") def update(pull, apps, patch, build, requirements, restart_supervisor, restart_systemd, no_backup, no_compile, force, reset): - from bench.utils import update + from bench.utils.bench import update update(pull=pull, apps=apps, patch=patch, build=build, requirements=requirements, restart_supervisor=restart_supervisor, restart_systemd=restart_systemd, backup=not no_backup, compile=not no_compile, force=force, reset=reset) diff --git a/bench/commands/utils.py b/bench/commands/utils.py index 5ab0e3d9..a5beb614 100644 --- a/bench/commands/utils.py +++ b/bench/commands/utils.py @@ -22,8 +22,8 @@ def start(no_dev, concurrency, procfile, no_prefix, man): @click.option('--supervisor', is_flag=True, default=False) @click.option('--systemd', is_flag=True, default=False) def restart(web, supervisor, systemd): - from bench.utils import restart_supervisor_processes, restart_systemd_processes from bench.bench import Bench + from bench.utils.bench import restart_supervisor_processes, restart_systemd_processes bench = Bench(".") @@ -68,7 +68,7 @@ def set_url_root(site, url_root): @click.command('set-mariadb-host', help="Set MariaDB host for bench") @click.argument('host') def set_mariadb_host(host): - from bench.utils import set_mariadb_host + from bench.utils.bench import set_mariadb_host set_mariadb_host(host) @@ -78,7 +78,7 @@ def set_redis_cache_host(host): """ Usage: bench set-redis-cache-host localhost:6379/1 """ - from bench.utils import set_redis_cache_host + from bench.utils.bench import set_redis_cache_host set_redis_cache_host(host) @@ -88,7 +88,7 @@ def set_redis_queue_host(host): """ Usage: bench set-redis-queue-host localhost:6379/2 """ - from bench.utils import set_redis_queue_host + from bench.utils.bench import set_redis_queue_host set_redis_queue_host(host) @@ -98,14 +98,14 @@ def set_redis_socketio_host(host): """ Usage: bench set-redis-socketio-host localhost:6379/3 """ - from bench.utils import set_redis_socketio_host + from bench.utils.bench import set_redis_socketio_host set_redis_socketio_host(host) @click.command('download-translations', help="Download latest translations") def download_translations(): - from bench.utils import download_translations_p + from bench.utils.translation import download_translations_p download_translations_p() @@ -119,7 +119,7 @@ def renew_lets_encrypt(): @click.argument('site') def backup_site(site): from bench.bench import Bench - from bench.utils import backup_site + from bench.utils.system import backup_site if site not in Bench(".").sites: print(f'Site `{site}` not found') sys.exit(1) @@ -128,7 +128,7 @@ def backup_site(site): @click.command('backup-all-sites', help="Backup all sites in current bench") def backup_all_sites(): - from bench.utils import backup_all_sites + from bench.utils.system import backup_all_sites backup_all_sites(bench_path='.') @@ -178,7 +178,7 @@ def find_benches(location): @click.argument('python', type=str) @click.option('--no-backup', 'backup', is_flag=True, default=True) def migrate_env(python, backup=True): - from bench.utils import migrate_env + from bench.utils.bench import migrate_env migrate_env(python=python, backup=backup) diff --git a/bench/config/lets_encrypt.py b/bench/config/lets_encrypt.py index c69b6121..45a61308 100755 --- a/bench/config/lets_encrypt.py +++ b/bench/config/lets_encrypt.py @@ -10,7 +10,8 @@ from bench.config.nginx import make_nginx_conf from bench.config.production_setup import service from bench.config.site_config import get_domains, remove_domain, update_site_config from bench.bench import Bench -from bench.utils import exec_cmd, update_common_site_config +from bench.utils import exec_cmd +from bench.utils.bench import update_common_site_config from bench.exceptions import CommandFailedError diff --git a/bench/config/production_setup.py b/bench/config/production_setup.py index 10ecc191..7632f830 100755 --- a/bench/config/production_setup.py +++ b/bench/config/production_setup.py @@ -9,7 +9,8 @@ from bench.config.nginx import make_nginx_conf from bench.config.supervisor import generate_supervisor_config, update_supervisord_config from bench.config.systemd import generate_systemd_config from bench.bench import Bench -from bench.utils import exec_cmd, which, fix_prod_setup_perms, get_bench_name, get_cmd_output, log +from bench.utils import exec_cmd, which, get_bench_name, get_cmd_output, log +from bench.utils.system import fix_prod_setup_perms from bench.exceptions import CommandFailedError logger = logging.getLogger(bench.PROJECT_NAME) diff --git a/bench/release.py b/bench/release.py index 98911cd7..d236e1c4 100755 --- a/bench/release.py +++ b/bench/release.py @@ -7,6 +7,8 @@ import git import getpass import re from time import sleep + +from bench.exceptions import ValidationError from .config.common_site_config import get_config import click diff --git a/bench/utils/__init__.py b/bench/utils/__init__.py index b52640b1..dc51e446 100644 --- a/bench/utils/__init__.py +++ b/bench/utils/__init__.py @@ -13,7 +13,7 @@ import click # imports - module imports import bench -from bench.exceptions import InvalidRemoteException +from bench.exceptions import InvalidRemoteException, ValidationError logger = logging.getLogger(bench.PROJECT_NAME) @@ -393,8 +393,22 @@ def is_git_url(url): return bool(re.match(pattern, url)) -# to avoid circular imports -from .app import * -from .bench import * -from .system import * -from .translation import * +def drop_privileges(uid_name='nobody', gid_name='nogroup'): + # from http://stackoverflow.com/a/2699996 + if os.getuid() != 0: + # We're not root so, like, whatever dude + return + + # Get the uid/gid from the name + running_uid = pwd.getpwnam(uid_name).pw_uid + running_gid = grp.getgrnam(gid_name).gr_gid + + # Remove group privileges + os.setgroups([]) + + # Try setting the new uid/gid + os.setgid(running_gid) + os.setuid(running_uid) + + # Ensure a very conservative umask + os.umask(0o22) diff --git a/bench/utils/system.py b/bench/utils/system.py index 33e94cce..d0a2c428 100644 --- a/bench/utils/system.py +++ b/bench/utils/system.py @@ -135,27 +135,6 @@ def backup_all_sites(bench_path='.'): backup_site(site, bench_path=bench_path) -def drop_privileges(uid_name='nobody', gid_name='nogroup'): - # from http://stackoverflow.com/a/2699996 - if os.getuid() != 0: - # We're not root so, like, whatever dude - return - - # Get the uid/gid from the name - running_uid = pwd.getpwnam(uid_name).pw_uid - running_gid = grp.getgrnam(gid_name).gr_gid - - # Remove group privileges - os.setgroups([]) - - # Try setting the new uid/gid - os.setgid(running_gid) - os.setuid(running_uid) - - # Ensure a very conservative umask - os.umask(0o22) - - def fix_prod_setup_perms(bench_path='.', frappe_user=None): from glob import glob from bench.bench import Bench