def download_configuration_file(file_to_download, config_file, max_len,
                                verbose, insecure):
    kwargs = {}
    if insecure:
        kwargs = {"verify": False}
    if verbose:
        info(
            f"Downloading {config_file} from {file_to_download} with option {kwargs}"
        )
    with warnings.catch_warnings(record=True) as messages:
        result = requests.get(file_to_download, **kwargs)
        for msg in messages:
            if not insecure or msg.category is not InsecureRequestWarning:
                warn(msg.message)
    path = Path(file_to_download)
    with open(path.name, "wb") as f:
        f.write(result.content)
    if result.status_code != 200:
        error_msg = f"download failed 💥\nHTTP status {result.status_code} !"
        if result.status_code == 404:
            error_msg = "not found. Are you sure it exists ? 💥"
        error(f"💥 '{file_to_download}' {error_msg}")
    else:
        formatted_config = "{:{align}{width}}".format(config_file,
                                                      align="<",
                                                      width=max_len)
        success("✨ Successfully retrieved {} ✨".format(formatted_config))
    return result.status_code == 200
def download_configuration(config: confuse.Configuration) -> None:
    results = {}
    config_files = config["configuration_files"].get(list)
    url = get_url_from_args(
        config["repository"].get(str),
        config["branch"].get(str),
        config["path"].get(str),
    )
    insecure = config["insecure"].get(bool)
    verbose = config["verbose"].get(bool)
    for config_file in config_files:
        result = Result()
        config_file_url = f"{url}/{config_file}"
        if verbose:
            info(f"Downloading '{config_file}' from '{config_file_url}'")
        request_result = recover_new_content(config_file_url, insecure)
        result.downloaded = request_result.status_code == 200
        if not result.downloaded:
            result.failed_download = request_result
            results[config_file] = result
            continue
        path = Path(config_file_url)
        old_content = None
        file_already_exists = os.path.exists(config_file)
        if file_already_exists:
            old_content = read_current_file(path)
        result.new_content = old_content != request_result.content
        if not file_already_exists or (result.new_content and
                                       config["replace_existing"].get(bool)):
            result.replaced = True
            write_new_content(path, request_result)
        results[config_file] = result
    display_results(results)
def install_pre_commit(verbose: bool) -> None:
    if not Path(".pre-commit-config.yaml").exists():
        warn(
            "No '.pre-commit-config.yaml' found, we can't install pre-commit.")
        sys.exit(ExitCode.PRE_COMMIT_CONF_NOT_FOUND)
    init_pre_commit = ["pre-commit", "install"]
    if verbose:
        info(f"Launching : {init_pre_commit}")
    subprocess.run(init_pre_commit, capture_output=True, check=False)
    success(
        "🎉 pre-commit installed locally with the current configuration. 🎉"
    )
예제 #4
0
def install_pre_commit(verbose):
    if not Path(".pre-commit-config.yaml").exists():
        warn(
            "No '.pre-commit-config.yaml' found, we can't install pre-commit.")
        sys.exit(ExitCode.PRE_COMMIT_CONF_NOT_FOUND)
    init_pre_commit = ["pre-commit", "install"]
    if verbose:
        info(f"Launching : {init_pre_commit}")
    subprocess_compat_mode(init_pre_commit)
    success(
        "🎉 pre-commit installed locally with the current configuration. 🎉"
    )
def run() -> None:
    config = confuse.Configuration(APPLICATION_NAME, __name__)
    try:
        config = parse_args(config)
    except confuse.ConfigError as e:
        error("Problem with your configuration file in "
              f"{[s.filename for s in config.sources]}: {e}")
        sys.exit(ExitCode.PRE_COMMIT_CONF_NOT_FOUND.value)
    config_path = Path(config.config_dir()).resolve() / "config.yaml"
    if not config_path.exists():
        info(f"You can set the option system wide in {config_path}.")
    if config["verbose"].get(bool):
        info(f"Installing with the following options : {config}.")
    install(config)
    sys.exit(ExitCode.OK.value)
예제 #6
0
def update_gitignore(config_files: Iterable[str],
                     verbose: bool,
                     path: Union[Path, str] = ".gitignore") -> None:
    """Set up the .gitignore for the whole team."""
    if not os.path.isfile(path):
        warn(f" 🔧 We created '{path}' please commit it. 🔧")
        return write_config_file_to_add(set(config_files), "", path=path)
    config_files_to_add = set()
    with open(path, encoding="utf8") as git_ignore:
        gitignore_content = git_ignore.read().split("\n")
    for config_file in config_files:
        if config_file not in gitignore_content:
            if verbose:
                info(f"{config_file} is not in the .gitignore")
            config_files_to_add.add(config_file)
    return write_config_file_to_add(config_files_to_add,
                                    "\n".join(gitignore_content),
                                    path=path)
def run():
    config = confuse.Configuration(APPLICATION_NAME, __name__)
    try:
        config = parse_args(config)
    except confuse.ConfigError as e:
        error(
            f"Problem with your configuration file in {[s.filename for s in config.sources]}: {e}"
        )
        sys.exit(ExitCode.PRE_COMMIT_CONF_NOT_FOUND.value)
    url = get_url_from_args(config["repository"].get(str),
                            config["branch"].get(str), config["path"].get(str))
    config_files = config["configuration_files"].get(list)
    verbose = config["verbose"].get(bool)
    replace_existing = config["replace_existing"].get(bool)
    insecure = config["insecure"].get(bool)
    if verbose:
        info(f"Installing with the following options : {config}.")
        config_path = Path(config.config_dir()).resolve() / "config.yaml"
        info(f"You can set the option system wide in {config_path}.")
        info(f"Configuration files to fetch : {config_files}.")
    install(url=url,
            config_files=config_files,
            replace_existing=replace_existing,
            verbose=verbose,
            insecure=insecure)
    sys.exit(ExitCode.OK.value)