text
stringlengths
0
828
error_str = str(e)
msg = ""socket error, retrying after %ss"" % (timeout,)
print >> sys.stderr, msg
time.sleep(timeout)
retry_attempt += 1
else:
# When we get here, it means that the download was a success.
try:
finalize_download(url, download_to_file, content_type, request)
finally:
request.close()
return download_to_file
# All the attempts were used, but no successfull download - so raise error
msg = 'URL could not be opened: %s' % (error_str,)
raise InvenioFileDownloadError(msg, code=error_code)"
4798,"def finalize_download(url, download_to_file, content_type, request):
""""""
Finalizes the download operation by doing various checks, such as format
type, size check etc.
""""""
# If format is given, a format check is performed.
if content_type and content_type not in request.headers['content-type']:
msg = 'The downloaded file is not of the desired format'
raise InvenioFileDownloadError(msg)
# Save the downloaded file to desired or generated location.
to_file = open(download_to_file, 'w')
try:
try:
while True:
block = request.read(CFG_FILEUTILS_BLOCK_SIZE)
if not block:
break
to_file.write(block)
except Exception as e:
msg = ""Error when downloading %s into %s: %s"" % \
(url, download_to_file, e)
raise InvenioFileDownloadError(msg)
finally:
to_file.close()
# Check Size
filesize = os.path.getsize(download_to_file)
if filesize == 0:
raise InvenioFileDownloadError(""%s seems to be empty"" % (url,))
# download successful, return the new path
return download_to_file"
4799,"def download_local_file(filename, download_to_file):
""""""
Copies a local file to Invenio's temporary directory.
@param filename: the name of the file to copy
@type filename: string
@param download_to_file: the path to save the file to
@type download_to_file: string
@return: the path of the temporary file created
@rtype: string
@raise StandardError: if something went wrong
""""""
# Try to copy.
try:
path = urllib2.urlparse.urlsplit(urllib.unquote(filename))[2]
if os.path.abspath(path) != path:
msg = ""%s is not a normalized path (would be %s)."" \
% (path, os.path.normpath(path))
raise InvenioFileCopyError(msg)
allowed_path_list = current_app.config.get(
'CFG_BIBUPLOAD_FFT_ALLOWED_LOCAL_PATHS', []
)
allowed_path_list.append(current_app.config['CFG_TMPSHAREDDIR'])
for allowed_path in allowed_path_list:
if path.startswith(allowed_path):
shutil.copy(path, download_to_file)
if os.path.getsize(download_to_file) == 0:
os.remove(download_to_file)
msg = ""%s seems to be empty"" % (filename,)
raise InvenioFileCopyError(msg)
break
else:
msg = ""%s is not in one of the allowed paths."" % (path,)
raise InvenioFileCopyError()
except Exception as e:
msg = ""Impossible to copy the local file '%s' to %s: %s"" % \
(filename, download_to_file, str(e))
raise InvenioFileCopyError(msg)
return download_to_file"
4800,"def safe_mkstemp(suffix, prefix='filedownloadutils_'):
""""""Create a temporary filename that don't have any '.' inside a part
from the suffix.""""""
tmpfd, tmppath = tempfile.mkstemp(
suffix=suffix,
prefix=prefix,
dir=current_app.config['CFG_TMPSHAREDDIR']
)