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_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 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. 🎉" )
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 write_config_file_to_add(config_files_to_add: Iterable[str], gitignore_content: str, path: Union[Path, str]) -> None: if not config_files_to_add: return config = confuse.Configuration(APPLICATION_NAME, __name__) gitignore_info_text = config["gitignore_info_text"].get(str) text, mode = get_updated_gitignore_content(gitignore_content, set(config_files_to_add), gitignore_info_text) with open(path, mode, encoding="utf8") as gitignore: gitignore.write(text) success(f"✨ Updated {path} successfully with {config_files_to_add}. ✨")
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. ✨🎉")