Exemplo n.º 1
0
def _has_hash(path: str, expected_hash: str) -> bool:
    """Check if the provided path has the expected hash.

    Parameters
    ----------
    path: str
    expected_hash: str

    Returns
    -------
    bool
        True if the file hash and the expected one are equal
    """
    if not os.path.exists(path):
        return False
    return file_hash(path) == expected_hash
Exemplo n.º 2
0
    def fetch(self, fname, processor=None, downloader=None):
        self._assert_file_in_registry(fname)

        # Create the local data directory if it doesn't already exist
        if not self.abspath.exists():
            os.makedirs(str(self.abspath))

        full_path = self.abspath / fname

        in_storage = full_path.exists()
        if not in_storage:
            action = 'download'
        elif in_storage and file_hash(str(full_path)) != self.registry[fname]:
            action = 'update'
        else:
            action = 'fetch'

        if action in ('download', 'update'):
            action_word = dict(download='Downloading', update='Updating')
            warn("{} data file '{}' from remote data store '{}' to '{}'.".
                 format(action_word[action], fname, self.get_url(fname),
                        str(self.path)))
            if downloader is None:
                downloader = Downloader()
            # Stream the file to a temporary so that we can safely check its hash before
            # overwriting the original
            tmp = tempfile.NamedTemporaryFile(delete=False,
                                              dir=str(self.abspath))
            # Close the temp file so that the downloader can decide how to opened it
            tmp.close()
            try:
                downloader(self.get_url(fname), tmp.name, self)
                self._check_download_hash(fname, tmp.name)
                # Ensure the parent directory exists in case the file is in a
                # subdirectory. Otherwise, move will cause an error.
                if not os.path.exists(str(full_path.parent)):
                    os.makedirs(str(full_path.parent))
                shutil.move(tmp.name, str(full_path))
            finally:
                if os.path.exists(tmp.name):
                    os.remove(tmp.name)

        if processor is not None:
            return processor(str(full_path), action, self)

        return str(full_path)
Exemplo n.º 3
0
def _has_hash(path, expected_hash):
    """Check if the provided path has the expected hash."""
    if not osp.exists(path):
        return False
    return file_hash(path) == expected_hash