Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def download_file(url, destination_path, blocksize=8192):
"""Downloads a file from HTTP, HTTPS or FTP URL.
Args:
url (str): URL to be downloaded.
destination_path (str): Path to where the downloaded file will be stored.
blocksize (int): Block size in bytes.
"""
r = DownloadHelper.request(url, stream=True)
if r is None:
raise DownloadError("An unexpected error occurred during the download.")
if not 200 <= r.status_code < 300:
raise DownloadError(r.reason)
file_size = int(r.headers.get('content-length', -1))
if r.headers.get('content-encoding', 'identity') != 'identity':
# use infinite progress bar in case of compressed content, there is no
# way to determine the uncompressed size in advance
file_size = -1
# file exists, check the size
if os.path.exists(destination_path):
if file_size < 0 or file_size != os.path.getsize(destination_path):
logger.verbose("The destination file '%s' exists, but sizes don't match! Removing it.",
def _get_version_using_pattern_api(cls, package_name):
r = DownloadHelper.request('{}/projects'.format(cls.API_URL), params=dict(pattern=package_name))
if r is None or not r.ok:
return None
data = r.json()
try:
versions = [p['version'] for p in data['projects'] if p['name'] == package_name and p['version']]
except KeyError:
return None
if not versions:
return None
# there can be multiple matching projects, just return the highest version of all of them
return sorted(versions, key=parse_version, reverse=True)[0]
def _get_version(cls, package_name):
if package_name.startswith('ghc-'):
package_name = package_name.replace('ghc-', '', 1)
r = DownloadHelper.request('{}/package/{}/preferred'.format(cls.API_URL, package_name),
headers={'Accept': 'application/json'})
if r is None or not r.ok:
return None
data = r.json()
return data.get('normal-version')[0]
def _get_version(cls, package_name):
# special-case "ruby", as https://rubygems.org/api/v1/gems/ruby.json returns nonsense
if package_name == 'ruby':
return None
r = DownloadHelper.request('{}/{}.json'.format(cls.API_URL, package_name))
if r is None or not r.ok:
# try to strip rubygem prefix
package_name = re.sub(r'^rubygem-', '', package_name)
r = DownloadHelper.request('{}/{}.json'.format(cls.API_URL, package_name))
if r is None or not r.ok:
return None
data = r.json()
return data.get('version')
def _get_version_using_distro_api(cls, package_name):
r = DownloadHelper.request('{}/project/Fedora/{}'.format(cls.API_URL, package_name))
if r is None or not r.ok:
return None
data = r.json()
return data.get('version')
def _get_commit_hash_from_github(cls, spec_file):
"""
Tries to find a commit using Github API
:param spec_file: SPEC file to base the search on
:return: SHA of a commit, or None
"""
m = re.match(r'^https?://github\.com/(?P[\w-]+)/(?P[\w-]+)/.*$', spec_file.sources[0])
if not m:
return None
baseurl = 'https://api.github.com/repos/{owner}/{project}'.format(**m.groupdict())
# try to get tag name from a release matching version
r = DownloadHelper.request('{}/releases'.format(baseurl))
if r is None:
return None
if not r.ok:
if r.status_code == 403 and r.headers.get('X-RateLimit-Remaining') == '0':
logger.warning("Rate limit exceeded on Github API! Try again later.")
return None
data = r.json()
version = spec_file.header.version
tag_name = None
for release in data:
if version in release.get('name', ''):
tag_name = release.get('tag_name')
break
r = DownloadHelper.request('{}/tags'.format(baseurl))
def _get_version(cls, package_name):
r = DownloadHelper.request('{}/{}/json'.format(cls.API_URL, package_name))
if r is None or not r.ok:
# try to strip python prefix
package_name = re.sub(r'^python[23]?-', '', package_name)
r = DownloadHelper.request('{}/{}/json'.format(cls.API_URL, package_name))
if r is None or not r.ok:
return None
data = r.json()
try:
return data['info']['version']
except KeyError:
return None
def _get_version(cls, package_name):
# gets the package name format needed in npm registry
if package_name.startswith('nodejs-'):
package_name = package_name.replace('nodejs-', '')
r = DownloadHelper.request('{}/{}'.format(cls.API_URL, package_name))
if r is None or not r.ok:
return None
data = r.json()
try:
return data.get('dist-tags').get('latest')
except TypeError:
return None