def _download_google_java_formatter_jar(version: str) -> str: # pragma: no cover def get_urls(_version: str) -> typing.List[str]: # Links extracted from https://github.com/google/google-java-format/ return [ "https://github.com/google/google-java-format/releases/download/" "v{version}/google-java-format-{version}-all-deps.jar".format( version=_version, ), # Versions older than 1.10 have a different template "https://github.com/google/google-java-format/releases/download/" "google-java-format-{version}/google-java-format-{version}-all-deps.jar".format( version=_version, ), ] possible_urls = get_urls(version) try: for url_to_download in possible_urls: try: return download_url(url_to_download, "google-java-formatter{version}.jar".format(version=version)) except requests.HTTPError as e: if e.response.status_code != 404: # If the url was not found then move forward with the next links raise raise RuntimeError("Failed to load any of the provided links") except: # noqa: E722 (allow usage of bare 'except') raise RuntimeError( "Failed to download any of: {urls}. Probably the requested version, " "{version}, is not valid or you have some network issue.".format( urls=", ".join(possible_urls), version=version, ), )
def download_kotlin_formatter_jar(version=KTLINT_VERSION): # pragma: no cover def get_url(_version): # Links extracted from https://github.com/shyiko/ktlint/ return \ 'https://github.com/pinterest/ktlint/releases/download/{version}/ktlint'.format( version=_version, ) return download_url(get_url(version), 'ktlint{version}.jar'.format(version=version))
def download_google_java_formatter_jar( version=GOOGLE_JAVA_FORMATTER_VERSION): # pragma: no cover def get_url(_version): # Links extracted from https://github.com/google/google-java-format/ return \ 'https://github.com/google/google-java-format/releases/download/' \ 'google-java-format-{version}/google-java-format-{version}-all-deps.jar'.format( version=_version, ) return download_url(get_url(version))
def test_download_url(mock_requests, mock_shutil, tmpdir, url, does_file_already_exist): if does_file_already_exist: with open(os.path.join(tmpdir.strpath, basename(url)), "w") as f: f.write(str("")) with mock.patch.dict(os.environ, {"PRE_COMMIT_HOME": tmpdir.strpath}): assert download_url(url) == os.path.join(tmpdir.strpath, basename(url)) if does_file_already_exist: assert not mock_requests.get.called else: mock_requests.get.assert_called_once_with(url, stream=True)
def __download_kotlin_formatter_jar(version: str) -> str: # pragma: no cover def get_url(_version: str) -> str: # Links extracted from https://github.com/pinterest/ktlint/ return "https://github.com/pinterest/ktlint/releases/download/{version}/ktlint".format( version=_version, ) url_to_download = get_url(version) try: return download_url(get_url(version), "ktlint{version}.jar".format(version=version)) except: # noqa: E722 (allow usage of bare 'except') raise RuntimeError( "Failed to download {url}. Probably the requested version, {version}, is " "not valid or you have some network issue.".format( url=url_to_download, version=version, ), )
def __download_google_java_formatter_jar( version: str) -> str: # pragma: no cover def get_url(_version: str) -> str: # Links extracted from https://github.com/google/google-java-format/ return ( "https://github.com/google/google-java-format/releases/download/" "google-java-format-{version}/google-java-format-{version}-all-deps.jar" .format(version=_version, )) url_to_download = get_url(version) try: return download_url( get_url(version), "google-java-formatter{version}.jar".format(version=version)) except: # noqa: E722 (allow usage of bare 'except') raise RuntimeError( "Failed to download {url}. Probably the requested version, {version}, is " "not valid or you have some network issue.".format( url=url_to_download, version=version, ), )