示例#1
0
文件: manager.py 项目: zimmaz/sunpy
            def wrapper(*args, **kwargs):
                replace = self._skip_file.get(name, None)
                if replace:
                    if replace['uri'].startswith('file://'):
                        file_path = replace['uri'][len('file://'):]
                        file_hash = hash_file(file_path)
                    else:
                        file_path, file_hash, _ = self._cache._download_and_hash(
                            [replace['uri']])
                    if replace['hash'] and file_hash != replace['hash']:
                        # if hash provided to replace function doesn't match the hash of the file
                        # raise error
                        raise ValueError(
                            "Hash provided to override_file does not match hash of the file."
                        )
                elif self._skip_hash_check:
                    file_path = self._cache.download(urls, redownload=True)
                else:
                    details = self._cache.get_by_hash(sha_hash)
                    if not details:
                        # In case we are matching by hash and file does not exist
                        # That might mean the wrong hash is supplied to decorator
                        # We match by urls to make sure that is not the case
                        if self._cache_has_file(urls):
                            raise ValueError(
                                " Hash provided does not match the hash in database."
                            )
                        file_path = self._cache.download(urls)
                        if hash_file(file_path) != sha_hash:
                            # the hash of the file downloaded does not match provided hash
                            # this means the file has changed on the server.
                            # the function should be updated to use the new hash. Raise an error to notify.
                            raise RuntimeError(
                                "Remote file on the server has changed. Update hash of the function."
                            )
                    else:
                        # This is to handle the case when the local file appears to be tampered/corrupted
                        if hash_file(
                                details['file_path']) != details['file_hash']:
                            warnings.warn(
                                "Hashes do not match, the file will be redownloaded (could be be tampered/corrupted)",
                                SunpyUserWarning)
                            file_path = self._cache.download(urls,
                                                             redownload=True)
                            # Recheck the hash again, if this fails, we will exit.
                            if hash_file(file_path) != details['file_hash']:
                                raise RuntimeError(
                                    "Redownloaded file also has the incorrect hash."
                                    "The remote file on the server might have changed."
                                )
                        else:
                            file_path = details['file_path']

                self._file_cache[name] = file_path
                return func(*args, **kwargs)
示例#2
0
            def wrapper(*args, **kwargs):
                replace = self._skip_file.get(name, None)
                if replace:
                    uri_parse = urlparse(replace['uri'])
                    if uri_parse.scheme in ("", "file"):
                        # If a relative file uri is specified (i.e.
                        # `file://sunpy/test`) this maintains compatibility
                        # with the original behaviour where this would be
                        # interpreted as `./sunpy/test` if no scheme is
                        # specified netloc will be '' by default.
                        file_path = uri_parse.netloc + uri_parse.path
                        file_hash = hash_file(file_path)
                    else:
                        file_path, file_hash, _ = self._cache._download_and_hash([replace['uri']])
                    if replace['hash'] and file_hash != replace['hash']:
                        # if hash provided to replace function doesn't match the hash of the file
                        # raise error
                        raise ValueError(
                            "Hash provided to override_file does not match hash of the file.")
                elif self._skip_hash_check:
                    file_path = self._cache.download(urls, redownload=True)
                else:
                    details = self._cache.get_by_hash(sha_hash)
                    if not details:
                        # In case we are matching by hash and file does not exist
                        # That might mean the wrong hash is supplied to decorator
                        # We match by urls to make sure that is not the case
                        if self._cache_has_file(urls):
                            raise ValueError(" Hash provided does not match the hash in database.")
                        file_path = self._cache.download(urls)
                        if hash_file(file_path) != sha_hash:
                            # the hash of the file downloaded does not match provided hash
                            # this means the file has changed on the server.
                            # the function should be updated to use the new
                            # hash. Raise an error to notify.
                            raise RuntimeError(
                                "Remote file on the server has changed. Update hash of the function.")
                    else:
                        # This is to handle the case when the local file
                        # appears to be tampered/corrupted
                        if hash_file(details['file_path']) != details['file_hash']:
                            warnings.warn("Hashes do not match, the file will be redownloaded (could be be tampered/corrupted)",
                                          SunpyUserWarning)
                            file_path = self._cache.download(urls, redownload=True)
                            # Recheck the hash again, if this fails, we will exit.
                            if hash_file(file_path) != details['file_hash']:
                                raise RuntimeError("Redownloaded file also has the incorrect hash."
                                                   "The remote file on the server might have changed.")
                        else:
                            file_path = details['file_path']

                self._file_cache[name] = file_path
                return func(*args, **kwargs)
示例#3
0
文件: cache.py 项目: zhims/sunpy
 def download(url):
     path = self._cache_dir / get_filename(urlopen(url), url)
     # replacement_filename returns a string and we want a Path object
     path = Path(replacement_filename(str(path)))
     self._downloader.download(url, path)
     shahash = hash_file(path)
     return path, shahash, url
示例#4
0
文件: cache.py 项目: wtbarnes/sunpy
 def download(url):
     path = self._cache_dir / (namespace +
                               get_filename(urlopen(url), url))
     self._downloader.download(url, path)
     shahash = hash_file(path)
     return path, shahash, url
示例#5
0
文件: cache.py 项目: zimmaz/sunpy
 def download(url):
     path = self._cache_dir / get_filename(urlopen(url), url)
     path = replacement_filename(path)
     self._downloader.download(url, path)
     shahash = hash_file(path)
     return path, shahash, url