Exemple #1
0
def change_uid():
	if is_root() and not cmd_requires_root():
		frappe_user = get_config(".").get('frappe_user')
		if frappe_user:
			drop_privileges(uid_name=frappe_user, gid_name=frappe_user)
			os.environ['HOME'] = pwd.getpwnam(frappe_user).pw_dir
		else:
			log('You should not run this command as root', level=3)
			sys.exit(1)
Exemple #2
0
def cli():
    global from_command_line
    from_command_line = True
    command = " ".join(sys.argv)

    change_working_directory()
    logger = setup_logging()
    logger.info(command)

    if len(sys.argv) > 1 and sys.argv[1] not in ("src", ):
        check_uid()
        change_uid()
        change_dir()

    if is_dist_editable(bench.PROJECT_NAME) and len(
            sys.argv) > 1 and sys.argv[1] != "src" and not get_config(".").get(
                "developer_mode"):
        log("bench is installed in editable mode!\n\nThis is not the recommended mode of installation for production. Instead, install the package from PyPI with: `pip install frappe-bench`\n",
            level=3)

    if not is_bench_directory() and not cmd_requires_root() and len(
            sys.argv) > 1 and sys.argv[1] not in ("init", "find", "src"):
        log("Command not being executed in bench directory", level=3)

    if len(sys.argv) > 2 and sys.argv[1] == "frappe":
        return old_frappe_cli()

    elif len(sys.argv) > 1:
        if sys.argv[1] in get_frappe_commands() + [
                "--site", "--verbose", "--force", "--profile"
        ]:
            return frappe_cmd()

        elif sys.argv[1] == "--help":
            print(click.Context(bench_command).get_help())
            print(get_frappe_help())
            return

        elif sys.argv[1] in get_apps():
            return app_cmd()

    if not (len(sys.argv) > 1 and sys.argv[1] == "src"):
        atexit.register(check_latest_version)

    try:
        bench_command()
    except BaseException as e:
        return_code = getattr(e, "code", 0)
        if return_code:
            logger.warning("{0} executed with exit code {1}".format(
                command, return_code))
        sys.exit(return_code)
Exemple #3
0
def get_frappe_commands(bench_path='.'):
	python = get_env_cmd('python', bench_path=bench_path)
	sites_path = os.path.join(bench_path, 'sites')
	if not os.path.exists(sites_path):
		log("Command not being executed in bench directory", level=3)
		return []
	try:
		output = get_cmd_output("{python} -m frappe.utils.bench_helper get-frappe-commands".format(python=python), cwd=sites_path)
		return json.loads(output)
	except subprocess.CalledProcessError as e:
		if hasattr(e, "stderr"):
			print(e.stderr.decode('utf-8'))
		return []
Exemple #4
0
def init(path,
         apps_path,
         frappe_path,
         frappe_branch,
         no_procfile,
         no_backups,
         no_auto_update,
         clone_from,
         verbose,
         skip_redis_config_generation,
         clone_without_update,
         ignore_exist=False,
         skip_assets=False,
         python='python3'):
    '''
	Create a New Bench Instance.
	'''
    from bench.utils import init, log

    try:
        init(
            path,
            apps_path=apps_path,
            no_procfile=no_procfile,
            no_backups=no_backups,
            no_auto_update=no_auto_update,
            frappe_path=frappe_path,
            frappe_branch=frappe_branch,
            verbose=verbose,
            clone_from=clone_from,
            skip_redis_config_generation=skip_redis_config_generation,
            clone_without_update=clone_without_update,
            ignore_exist=ignore_exist,
            skip_assets=skip_assets,
            python=python,
        )
        log('Bench {} initialized'.format(path), level=1)
    except SystemExit:
        pass
    except:
        import os, shutil, time, six
        # add a sleep here so that the traceback of other processes doesnt overlap with the prompts
        time.sleep(1)
        log("There was a problem while creating {}".format(path), level=2)
        if six.moves.input("Do you want to rollback these changes? [Y/n]: "
                           ).lower() == "y":
            print('Rolling back Bench "{}"'.format(path))
            if os.path.exists(path):
                shutil.rmtree(path)
def service(service_name, service_option):
	if os.path.basename(find_executable('systemctl') or '') == 'systemctl' and is_running_systemd():
		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':
		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} {service_option} {service}").format(service_manager=service_manager, service=service, service_option=service_option)
			exec_cmd(service_manager_command)

		else:
			log("No service manager found: '{0} {1}' failed to execute".format(service_name, service_option), level=2)
Exemple #6
0
def check_uid():
	if cmd_requires_root() and not is_root():
		log('superuser privileges required for this command', level=3)
		sys.exit(1)
Exemple #7
0
def switch_to_master():
    from bench.utils import log
    log("`switch-to-master` has been deprecated as master branches were renamed to version-11"
        )