def _downloadToCache(stream, hashinfo={}, origin_info=dict()):
hash_name  = None<EOL>hash_value = None<EOL>m = None<EOL>if len(hashinfo):<EOL><INDENT>for h in ('<STR_LIT>',):<EOL><INDENT>if h in hashinfo:<EOL><INDENT>hash_name  = h<EOL>hash_value = hashinfo[h]<EOL>m = getattr(hashlib, h)()<EOL>break<EOL><DEDENT><DEDENT>if not hash_name:<EOL><INDENT>logger.warning('<STR_LIT>', hashinfo)<EOL><DEDENT><DEDENT>cache_dir = folders.cacheDirectory()<EOL>fsutils.mkDirP(cache_dir)<EOL>file_size = <NUM_LIT:0><EOL>(download_file, download_fname) = tempfile.mkstemp(dir=cache_dir, suffix='<STR_LIT>')<EOL>with os.fdopen(download_file, '<STR_LIT:wb>') as f:<EOL><INDENT>f.seek(<NUM_LIT:0>)<EOL>for chunk in stream.iter_content(<NUM_LIT>):<EOL><INDENT>f.write(chunk)<EOL>if hash_name:<EOL><INDENT>m.update(chunk)<EOL><DEDENT><DEDENT>if hash_name:<EOL><INDENT>calculated_hash = m.hexdigest()<EOL>logger.debug(<EOL>'<STR_LIT>' % (<EOL>hash_name, calculated_hash, hash_value<EOL>)<EOL>)<EOL>if hash_value and (hash_value != calculated_hash):<EOL><INDENT>raise Exception('<STR_LIT>')<EOL><DEDENT><DEDENT>file_size = f.tell()<EOL>logger.debug('<STR_LIT>', file_size, download_fname)<EOL>f.truncate()<EOL><DEDENT>extended_origin_info = {<EOL>'<STR_LIT>': hashinfo,<EOL>'<STR_LIT:size>': file_size<EOL>}<EOL>extended_origin_info.update(origin_info)<EOL>ordered_json.dump(download_fname + '<STR_LIT>', extended_origin_info)<EOL>return os.path.basename(download_fname)<EOL>
Download the specified stream to a temporary cache directory, and
        returns a cache key that can be used to access/remove the file.
        You should use either removeFromCache(cache_key) or _moveCachedFile to
        move the downloaded file to a known key after downloading.