示例#1
0
def real_size(path):
    """
    Return size for `path`, which is a (link to a) file or directory

    Raise ReadError on failure
    """
    if os.path.isdir(os.path.realpath(path)):

        def onerror(exc):
            raise error.ReadError(getattr(exc, 'errno', None),
                                  getattr(exc, 'filename', None))

        size = 0
        walker = os.walk(path, followlinks=True, onerror=onerror)
        for dirpath, dirnames, filenames in walker:
            for filename in filenames:
                filepath = os.path.join(dirpath, filename)
                size += os.path.getsize(filepath)
        return size
    else:
        try:
            return os.path.getsize(path)
        except OSError as exc:
            raise error.ReadError(getattr(exc, 'errno', None),
                                  getattr(exc, 'filename', None))
示例#2
0
def read_chunks(filepath, chunksize, prepend=bytes()):
    """
    Generator that yields chunks from file

    `prepend` is prepended to the content of `filepath`.
    """
    chunk = b''
    for pos in range(0, len(prepend), chunksize):
        chunk = prepend[pos:pos + chunksize]
        if len(chunk) == chunksize:
            yield chunk
            chunk = b''
    try:
        with open(filepath, 'rb') as f:
            # Fill last chunk from prepended bytes with first bytes from file
            if chunk:
                chunk += f.read(chunksize - len(chunk))
                yield chunk
            while True:
                chunk = f.read(chunksize)
                if chunk:
                    yield chunk
                else:
                    break  # EOF
    except OSError as e:
        raise error.ReadError(e.errno, filepath)
示例#3
0
 def insert(self, index, path):
     path = self._coerce(path)
     try:
         path_is_dir = path.is_dir()
     except OSError as exc:
         raise error.ReadError(getattr(exc, 'errno', None),
                               getattr(exc, 'filename', None))
     if path_is_dir:
         # Add files in directory recursively
         with self._callback_disabled():
             for i,child in enumerate(sorted(path.iterdir())):
                 self.insert(index + i, child)
         if self._callback is not None:
             self._callback(self)
     else:
         super().insert(index, path)
示例#4
0
 def assert_readable(path):
     os_supports_effective_ids = os.access in os.supports_effective_ids
     if not os.access(path, os.R_OK, effective_ids=os_supports_effective_ids):
         raise error.ReadError(errno.EACCES, path)
示例#5
0
 def onerror(exc):
     raise error.ReadError(getattr(exc, 'errno', None),
                           getattr(exc, 'filename', None))