2
0
mirror of https://github.com/frappe/bench.git synced 2025-01-10 00:37:51 +00:00

chore: Get rid of import * utils

This commit is contained in:
Gavin D'souza 2021-11-13 00:19:12 +05:30
parent a2ecb398dd
commit e08a12477d
11 changed files with 48 additions and 52 deletions

View File

@ -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): def install_app(app, bench_path=".", verbose=False, no_cache=False, restart_bench=True, skip_assets=False):
from bench.bench import Bench 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}' install_text = f'Installing {app}'
click.secho(install_text, fg="yellow") 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 conf = Bench(bench_path).conf
if conf.get("developer_mode"): 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) install_python_dev_dependencies(apps=app)
if not skip_assets: if not skip_assets:

View File

@ -1,18 +1,17 @@
import os import os
import shutil import shutil
import sys import sys
import typing
import logging import logging
from typing import MutableSequence from typing import MutableSequence, TYPE_CHECKING
import bench import bench
from bench.exceptions import ValidationError from bench.exceptions import ValidationError
from bench.config.common_site_config import setup_config 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 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 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 from bench.app import App
logger = logging.getLogger(bench.PROJECT_NAME) logger = logging.getLogger(bench.PROJECT_NAME)
@ -231,8 +230,6 @@ class BenchSetup(Base):
return setup_logging(bench_path=self.bench.name) return setup_logging(bench_path=self.bench.name)
def patches(self): def patches(self):
import shutil
shutil.copy( shutil.copy(
os.path.join(os.path.dirname(os.path.abspath(__file__)), 'patches', 'patches.txt'), os.path.join(os.path.dirname(os.path.abspath(__file__)), 'patches', 'patches.txt'),
os.path.join(self.bench.name, 'patches.txt') os.path.join(self.bench.name, 'patches.txt')

View File

@ -1,5 +1,6 @@
# imports - module imports # 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 # imports - third party imports
import click import click

View File

@ -18,7 +18,8 @@ import click
@click.option('--verbose',is_flag=True, help="Verbose output during install") @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'): 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 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): if not ignore_exist and os.path.exists(path):
log(f"Bench instance already exists at {path}", level=2) 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): def pip(ctx, args):
"Run pip commands in bench env" "Run pip commands in bench env"
import os import os
from bench.utils import get_env_cmd from bench.utils.bench import get_env_cmd
env_py = get_env_cmd('python') env_py = get_env_cmd('python')
os.execv(env_py, (env_py, '-m', 'pip') + args) os.execv(env_py, (env_py, '-m', 'pip') + args)

View File

@ -3,7 +3,7 @@ import click
# imports - module imports # imports - module imports
from bench.app import pull_apps 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") @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('--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") @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): 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) 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)

View File

@ -22,8 +22,8 @@ def start(no_dev, concurrency, procfile, no_prefix, man):
@click.option('--supervisor', is_flag=True, default=False) @click.option('--supervisor', is_flag=True, default=False)
@click.option('--systemd', is_flag=True, default=False) @click.option('--systemd', is_flag=True, default=False)
def restart(web, supervisor, systemd): def restart(web, supervisor, systemd):
from bench.utils import restart_supervisor_processes, restart_systemd_processes
from bench.bench import Bench from bench.bench import Bench
from bench.utils.bench import restart_supervisor_processes, restart_systemd_processes
bench = Bench(".") 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.command('set-mariadb-host', help="Set MariaDB host for bench")
@click.argument('host') @click.argument('host')
def set_mariadb_host(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) set_mariadb_host(host)
@ -78,7 +78,7 @@ def set_redis_cache_host(host):
""" """
Usage: bench set-redis-cache-host localhost:6379/1 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) set_redis_cache_host(host)
@ -88,7 +88,7 @@ def set_redis_queue_host(host):
""" """
Usage: bench set-redis-queue-host localhost:6379/2 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) set_redis_queue_host(host)
@ -98,14 +98,14 @@ def set_redis_socketio_host(host):
""" """
Usage: bench set-redis-socketio-host localhost:6379/3 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) set_redis_socketio_host(host)
@click.command('download-translations', help="Download latest translations") @click.command('download-translations', help="Download latest translations")
def download_translations(): def download_translations():
from bench.utils import download_translations_p from bench.utils.translation import download_translations_p
download_translations_p() download_translations_p()
@ -119,7 +119,7 @@ def renew_lets_encrypt():
@click.argument('site') @click.argument('site')
def backup_site(site): def backup_site(site):
from bench.bench import Bench from bench.bench import Bench
from bench.utils import backup_site from bench.utils.system import backup_site
if site not in Bench(".").sites: if site not in Bench(".").sites:
print(f'Site `{site}` not found') print(f'Site `{site}` not found')
sys.exit(1) sys.exit(1)
@ -128,7 +128,7 @@ def backup_site(site):
@click.command('backup-all-sites', help="Backup all sites in current bench") @click.command('backup-all-sites', help="Backup all sites in current bench")
def backup_all_sites(): def backup_all_sites():
from bench.utils import backup_all_sites from bench.utils.system import backup_all_sites
backup_all_sites(bench_path='.') backup_all_sites(bench_path='.')
@ -178,7 +178,7 @@ def find_benches(location):
@click.argument('python', type=str) @click.argument('python', type=str)
@click.option('--no-backup', 'backup', is_flag=True, default=True) @click.option('--no-backup', 'backup', is_flag=True, default=True)
def migrate_env(python, backup=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) migrate_env(python=python, backup=backup)

View File

@ -10,7 +10,8 @@ from bench.config.nginx import make_nginx_conf
from bench.config.production_setup import service from bench.config.production_setup import service
from bench.config.site_config import get_domains, remove_domain, update_site_config from bench.config.site_config import get_domains, remove_domain, update_site_config
from bench.bench import Bench 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 from bench.exceptions import CommandFailedError

View File

@ -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.supervisor import generate_supervisor_config, update_supervisord_config
from bench.config.systemd import generate_systemd_config from bench.config.systemd import generate_systemd_config
from bench.bench import Bench 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 from bench.exceptions import CommandFailedError
logger = logging.getLogger(bench.PROJECT_NAME) logger = logging.getLogger(bench.PROJECT_NAME)

View File

@ -7,6 +7,8 @@ import git
import getpass import getpass
import re import re
from time import sleep from time import sleep
from bench.exceptions import ValidationError
from .config.common_site_config import get_config from .config.common_site_config import get_config
import click import click

View File

@ -13,7 +13,7 @@ import click
# imports - module imports # imports - module imports
import bench import bench
from bench.exceptions import InvalidRemoteException from bench.exceptions import InvalidRemoteException, ValidationError
logger = logging.getLogger(bench.PROJECT_NAME) logger = logging.getLogger(bench.PROJECT_NAME)
@ -393,8 +393,22 @@ def is_git_url(url):
return bool(re.match(pattern, url)) return bool(re.match(pattern, url))
# to avoid circular imports def drop_privileges(uid_name='nobody', gid_name='nogroup'):
from .app import * # from http://stackoverflow.com/a/2699996
from .bench import * if os.getuid() != 0:
from .system import * # We're not root so, like, whatever dude
from .translation import * 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)

View File

@ -135,27 +135,6 @@ def backup_all_sites(bench_path='.'):
backup_site(site, bench_path=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): def fix_prod_setup_perms(bench_path='.', frappe_user=None):
from glob import glob from glob import glob
from bench.bench import Bench from bench.bench import Bench