Exemplo n.º 1
0
def __handle_new_tool_arg(in_args):
    if len(in_args) < 1:
        log.error("--new_tool requires a value.")
        log.normal("\t--new_tool [config_type]")
        return
    module_dir = os.path.relpath(__file__)
    module_dir = os.path.dirname(module_dir)
    config_type = in_args.pop(0)
    config_type_dir = os.path.join(module_dir, config_type)
    if not os.path.exists(config_type_dir):
        module_dir = os.path.join(module_dir, "new_tool")
        os.mkdir(config_type_dir)
        files = [
            "__init__.py", "help.md", "default.py", "config/__init__.py",
            "options/__init__.py", "options/tool.py"
        ]
        for file_name in files:
            with open(os.path.join(module_dir, file_name)) as in_file:
                file_str = in_file.read()
                file_str = file_str.format_map({"config_type": config_type})
                tool_file = os.path.join(config_type_dir, file_name)
                os.makedirs(os.path.dirname(tool_file), exist_ok=True)
                with open(tool_file, "w") as out_file:
                    out_file.write(file_str)
        log.success("created " + config_type + " config_type!")
        log.normal("generate a config file by calling: " +
                   "'./confply.py --new_config " + config_type +
                   " my_config.py'")
    else:
        log.error(config_type + " already exists. (--new_tool)")
        pass
Exemplo n.º 2
0
def __handle_new_config_arg(in_args):
    if len(in_args) < 2:
        log.error("--config requires two values:")
        log.normal("\t--new_config [config_type] [new_config_file]")
        log.normal("")
        log.normal("valid tool types:")
        module_dir = os.path.dirname(__file__)
        files = os.listdir(module_dir)
        for dir in files:
            if (os.path.isdir(os.path.join(module_dir, dir))
                    and not dir == "__pycache__" and not dir == "new_tool"):
                log.normal("\t" + dir)
        return

    confply_dir = __get_confply_dir()
    config_type_arg = in_args.pop(0)
    config_arg = in_args.pop(0)
    config_path = os.path.abspath(os.path.curdir) + "/" + config_arg
    config_type_dir = os.path.dirname(os.path.relpath(__file__))
    config_type_dir = os.path.join(config_type_dir, config_type_arg)

    if not os.path.exists(config_type_dir):
        log.error(config_type_arg + " is not a valid config_type, consider:")
        for dir_file in os.listdir(confply_dir + "/confply/"):
            if os.path.isdir(confply_dir + "/confply/" + dir_file):
                if not dir_file == "__pycache__":
                    log.normal("\t" + dir_file)
        return

    hash_file = os.path.join(config_type_dir, "config/__init__.py")
    config_hash = md5_file(hash_file)
    if not os.path.exists(config_path):
        with open(config_path, "w") as config_file:
            config_str = __new_config_str.format_map({
                "confply_dir": confply_dir,
                "config_type_arg": config_type_arg,
                "config_file": config_arg,
                "config_hash": config_hash
            })
            config_file.write(config_str)
        st = os.stat(config_path)
        os.chmod(config_path, st.st_mode | stat.S_IEXEC)
        log.success("wrote: " + config_path)
    else:
        log.error(config_path + " already exists!")
        log.normal("aborted --new_config.")
Exemplo n.º 3
0
def __run_shell_cmd(shell_cmd, cmd_env, tool):
    if confply.config.log_file:
        sys.stdout.flush()
        # #todo: check if this can be ansi-coloured
        result = subprocess.run(shell_cmd,
                                stdout=sys.stdout,
                                stderr=subprocess.STDOUT,
                                text=True,
                                shell=True,
                                env=cmd_env)
    else:
        result = subprocess.run(shell_cmd, shell=True, env=cmd_env)

    if result.returncode == 0:
        log.linebreak()
        log.success(tool + " succeeded!")
        return 0
    else:
        log.linebreak()
        log.error(tool + " failed.")
        log.error(tool + " return code: " + str(result.returncode))
        return -2
Exemplo n.º 4
0
def __handle_launcher_arg(in_args):
    if len(in_args) < 1:
        log.error("--launcher requires a value.")
        log.normal("\t--launcher [new_launcher_file]")
        return
    confply_dir = __get_confply_dir()
    arguement = in_args.pop(0)
    launcher_path = os.path.abspath(os.path.curdir) + "/" + arguement
    if not os.path.exists(launcher_path):
        with open(launcher_path, "w") as launcher_file:
            launcher_str = __new_launcher_str.format_map({
                "confply_dir":
                confply_dir,
                "launcher":
                arguement,
                "comment":
                "{\n    # 'myconfig':'path/to/config.py'\n}"
            })
            launcher_file.write(launcher_str)
        st = os.stat(launcher_path)
        os.chmod(launcher_path, st.st_mode | stat.S_IEXEC)
        log.success("wrote: " + launcher_path)
    else:
        log.error(launcher_path + " already exists!")
Exemplo n.º 5
0
def is_found():
    if _cl_found:
        log.success("cl found: "+_cl_path)
    return _cl_found
Exemplo n.º 6
0
def load_config(path):
    global config_modules
    if os.name == "nt":
        confply.config.platform = "windows"
    elif os.name == "posix":
        confply.config.platform = "linux"

    __load_vcs_info(path)
    # find group config in parent directories
    directory_paths = __get_group_configs(path)
    directory_paths.append(path)
    config_locals = {}
    # load and execute the config files
    for dir_path in directory_paths:
        if dir_path is None:
            continue
        if os.path.exists(dir_path) and os.path.isfile(dir_path):
            config_name = os.path.basename(dir_path)
            confply.config.config_name = config_name
            confply.config.modified = os.path.getmtime(dir_path).real
            with open(dir_path) as config_file:
                with pushd(os.path.dirname(dir_path)):
                    try:
                        exec(config_file.read(), {}, config_locals)
                        # find imported confply configs for cleanup
                        for k, v in config_locals.items():
                            m_valid = isinstance(v, types.ModuleType)
                            if not m_valid:
                                continue

                            v_name = v.__name__
                            m_valid = m_valid and v not in config_modules
                            m_valid = m_valid and v_name.startswith("confply.")
                            m_valid = m_valid and v_name.endswith(".config")
                            if m_valid:
                                config_modules.add(v)

                        # validate there are less than 2 imported configs
                        if len(config_modules) > 1:
                            log.error("too many confply configs imported:")
                            for c in config_modules:
                                log.normal("\t " + c)
                            log.normal(
                                "confply only supports one config import.")
                            clean_modules()
                            return None, None

                        log.linebreak()
                        log.success("loaded: " + str(dir_path))
                    except Exception:
                        log.error("failed to exec: " + str(dir_path))
                        trace = traceback.format_exc().replace(
                            "<string>", str(dir_path))
                        log.normal("traceback:\n\n" + trace)
                        return None, None

        else:
            log.error("failed to find: " + str(dir_path))
            return None, None

    return config_locals