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 recover_new_content(config_file_url: str,
                        insecure: bool) -> requests.Response:
    with warnings.catch_warnings(record=True) as messages:
        if insecure:
            result = requests.get(config_file_url, verify=False)
        else:
            result = requests.get(config_file_url)
        for msg in messages:
            if not insecure or msg.category is not InsecureRequestWarning:
                warn(msg.message)
    return result
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. 🎉"
    )
Exemplo n.º 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. 🎉"
    )
Exemplo n.º 5
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 download_configuration(config_files, replace_existing, url, verbose,
                           insecure):
    download_fail = 0
    download_success = 0
    for config_file in config_files:
        max_len = max(len(c) for c in config_files)
        if os.path.exists(config_file) and not replace_existing:
            formatted_config = "{:{align}{width}}".format(config_file,
                                                          align="<",
                                                          width=max_len)
            warn(
                f"Found existing {formatted_config} ��  Use '-f' or '--replace-existing' to force erase."
            )
            continue
        if download_configuration_file(f"{url}/{config_file}", config_file,
                                       max_len, verbose, insecure):
            download_success += 1
        else:
            download_fail += 1
    if download_fail == 0:
        if download_success > 0:
            plural = "s" if download_success > 1 else ""
            success(
                f"🎉 {download_success} configuration file{plural} recovered. 🎉"
            )
        else:
            warn("All configuration files already existed.")
    else:
        pluralization = "s were" if download_fail != 1 else " was"
        warn(
            f"🎻 {download_fail} configuration file{pluralization} not recovered correctly. 🎻"
        )
def display_results(results: Dict[str, Result]) -> None:
    max_len: int = max(len(c) for c in results)
    failed: int = 0
    no_new_content: int = 0
    not_replaced: int = 0
    replaced: int = 0
    for file, result in results.items():
        # pylint: disable-next=consider-using-f-string
        formatted_config: str = "{:{align}{width}}".format(file,
                                                           align="<",
                                                           width=max_len)
        if not result.downloaded:
            failed += 1
            details = (
                f"{result.failed_download.url} HTTP{result.failed_download.status_code}"
            )
            error(
                f"{formatted_config} : 🎻 Download failed 🎻 : {details}")
        elif not result.new_content:
            no_new_content += 1
            success(f"{formatted_config} : ✨ Already up to date ✨")
        elif result.replaced:
            replaced += 1
            success(
                f"{formatted_config} : 🎉✨ Updated with new content ✨🎉"
            )
        else:
            not_replaced += 1
            warn(f"{formatted_config} : 🔔 already exists 🔔")
    if not results:
        warn("Nothing to recover �")
        return
    if failed != 0:
        plural = "s were" if failed != 1 else " was"
        error(
            f"🎻 {failed} configuration file{plural} not recovered correctly. 🎻"
        )
    if no_new_content != 0:
        plural = "s" if no_new_content > 1 else ""
        success(
            f"✨ {no_new_content} configuration file{plural} already up to date ✨"
        )
    if not_replaced != 0:
        plural = "s" if not_replaced > 1 else ""
        warn(f"🔔 {not_replaced} file{plural} not replaced. Use '-f' or "
             "'--replace-existing' to force erase. 🔔")
    if replaced != 0:
        plural = "s" if replaced > 1 else ""
        success(
            f"🎉✨ {replaced} configuration file{plural} updated. ✨🎉")