Example #1
0
 def write(self, bytes):
     try:
         #Using pump_string_file seems to make things crash
         osutils.pumpfile(StringIO(bytes), self.stream)
     except gio.Error, e:
         #self.transport._translate_gio_error(e,self.relpath)
         raise errors.BzrError(str(e))
 def write(self, bytes):
     try:
         #Using pump_string_file seems to make things crash
         osutils.pumpfile(StringIO(bytes), self.stream)
     except gio.Error, e:
         #self.transport._translate_gio_error(e,self.relpath)
         raise errors.BzrError(str(e))
Example #3
0
 def perform_save(self, window):
     try:
         save_path = self.window._get_save_path(osutils.basename(self.path))
     except SelectCancelled:
         return
     source = open(self.path, 'rb')
     try:
         target = open(save_path, 'wb')
         try:
             osutils.pumpfile(source, target)
         finally:
             target.close()
     finally:
         source.close()
Example #4
0
    def _add_compressed(self, fn, f):
        from cStringIO import StringIO
        from bzrlib.osutils import pumpfile

        if isinstance(f, basestring):
            f = StringIO(f)

        sio = StringIO()
        gf = gzip.GzipFile(mode='wb', fileobj=sio)
        # if pumpfile handles files that don't fit in ram,
        # so will this function
        pumpfile(f, gf)
        gf.close()
        sio.seek(0)
        self._try_put(fn, sio)
Example #5
0
 def _add_compressed(self, fn, f):
     from cStringIO import StringIO
     from bzrlib.osutils import pumpfile
     
     if isinstance(f, basestring):
         f = StringIO(f)
         
     sio = StringIO()
     gf = gzip.GzipFile(mode='wb', fileobj=sio)
     # if pumpfile handles files that don't fit in ram,
     # so will this function
     pumpfile(f, gf)
     gf.close()
     sio.seek(0)
     self._try_put(fn, sio)
Example #6
0
    def write_files(self, rev_tree, file_id_list):
        """
    Find the desired revision of each file, write it to our temporary
    directory.  The directory will be removed when the ScratchArea is
    deleted.
    """
        for file_id in file_id_list:
            if rev_tree.has_id(file_id):
                base_name = osutils.basename(rev_tree.id2path(file_id))
                # write in binary mode, to avoid OS-specific translations:
                tmp_file = open(osutils.pathjoin(self.path, base_name), "wb")
                osutils.pumpfile(rev_tree.get_file(file_id), tmp_file)
                tmp_file.close()

        self._make_readonly()

        return
Example #7
0
    def write_files(self, rev_tree, file_id_list):
        """
    Find the desired revision of each file, write it to our temporary
    directory.  The directory will be removed when the ScratchArea is
    deleted.
    """
        for file_id in file_id_list:
            if rev_tree.has_id(file_id):
                base_name = osutils.basename(rev_tree.id2path(file_id))
                # write in binary mode, to avoid OS-specific translations:
                tmp_file = open(osutils.pathjoin(self.path, base_name), 'wb')
                osutils.pumpfile(rev_tree.get_file(file_id), tmp_file)
                tmp_file.close()

        self._make_readonly()

        return
Example #8
0
    def read(self, size=-1):
        """Read size bytes from the current position in the file.

        Reading across ranges is not supported. We rely on the underlying http
        client to clean the socket if we leave bytes unread. This may occur for
        the final boundary line of a multipart response or for any range
        request not entirely consumed by the client (due to offset coalescing)

        :param size:  The number of bytes to read.  Leave unspecified or pass
            -1 to read to EOF.
        """
        if (self._size > 0
            and self._pos == self._start + self._size):
            if size == 0:
                return ''
            else:
                self._seek_to_next_range()
        elif self._pos < self._start:
            raise errors.InvalidRange(
                self._path, self._pos,
                "Can't read %s bytes before range (%s, %s)"
                % (size, self._start, self._size))
        if self._size > 0:
            if size > 0 and self._pos + size > self._start + self._size:
                raise errors.InvalidRange(
                    self._path, self._pos,
                    "Can't read %s bytes across range (%s, %s)"
                    % (size, self._start, self._size))

        # read data from file
        buf = StringIO()
        limited = size
        if self._size > 0:
            # Don't read past the range definition
            limited = self._start + self._size - self._pos
            if size >= 0:
                limited = min(limited, size)
        osutils.pumpfile(self._file, buf, limited, self._max_read_size)
        data = buf.getvalue()

        # Update _pos respecting the data effectively read
        self._pos += len(data)
        return data
Example #9
0
    def read(self, size=-1):
        """Read size bytes from the current position in the file.

        Reading across ranges is not supported. We rely on the underlying http
        client to clean the socket if we leave bytes unread. This may occur for
        the final boundary line of a multipart response or for any range
        request not entirely consumed by the client (due to offset coalescing)

        :param size:  The number of bytes to read.  Leave unspecified or pass
            -1 to read to EOF.
        """
        if (self._size > 0 and self._pos == self._start + self._size):
            if size == 0:
                return ''
            else:
                self._seek_to_next_range()
        elif self._pos < self._start:
            raise errors.InvalidRange(
                self._path, self._pos,
                "Can't read %s bytes before range (%s, %s)" %
                (size, self._start, self._size))
        if self._size > 0:
            if size > 0 and self._pos + size > self._start + self._size:
                raise errors.InvalidRange(
                    self._path, self._pos,
                    "Can't read %s bytes across range (%s, %s)" %
                    (size, self._start, self._size))

        # read data from file
        buf = StringIO()
        limited = size
        if self._size > 0:
            # Don't read past the range definition
            limited = self._start + self._size - self._pos
            if size >= 0:
                limited = min(limited, size)
        osutils.pumpfile(self._file, buf, limited, self._max_read_size)
        data = buf.getvalue()

        # Update _pos respecting the data effectively read
        self._pos += len(data)
        return data
Example #10
0
def dir_exporter(tree, dest, root):
    """Export this tree to a new directory.

    `dest` should not exist, and will be created holding the
    contents of this tree.

    TODO: To handle subdirectories we need to create the
           directories first.

    :note: If the export fails, the destination directory will be
           left in a half-assed state.
    """
    os.mkdir(dest)
    mutter('export version %r', tree)
    inv = tree.inventory
    entries = inv.iter_entries()
    entries.next() # skip root
    for dp, ie in entries:
        # The .bzr* namespace is reserved for "magic" files like
        # .bzrignore and .bzrrules - do not export these
        if dp.startswith(".bzr"):
            continue
        
        fullpath = osutils.pathjoin(dest, dp)
        if ie.kind == "file":
            fileobj = tree.get_file(ie.file_id)
            osutils.pumpfile(fileobj, file(fullpath, 'wb'))
            if tree.is_executable(ie.file_id):
                os.chmod(fullpath, 0755)
        elif ie.kind == "directory":
            os.mkdir(fullpath)
        elif ie.kind == "symlink":
            try:
                os.symlink(ie.symlink_target, fullpath)
            except OSError,e:
                raise errors.BzrError(
                    "Failed to create symlink %r -> %r, error: %s"
                    % (fullpath, self.symlink_target, e))
        else:
            raise errors.BzrError("don't know how to export {%s} of kind %r" %
               (ie.file_id, ie.kind))
Example #11
0
def dir_exporter(tree, dest, root):
    """Export this tree to a new directory.

    `dest` should not exist, and will be created holding the
    contents of this tree.

    TODO: To handle subdirectories we need to create the
           directories first.

    :note: If the export fails, the destination directory will be
           left in a half-assed state.
    """
    os.mkdir(dest)
    mutter('export version %r', tree)
    inv = tree.inventory
    entries = inv.iter_entries()
    entries.next()  # skip root
    for dp, ie in entries:
        # The .bzr* namespace is reserved for "magic" files like
        # .bzrignore and .bzrrules - do not export these
        if dp.startswith(".bzr"):
            continue

        fullpath = osutils.pathjoin(dest, dp)
        if ie.kind == "file":
            fileobj = tree.get_file(ie.file_id)
            osutils.pumpfile(fileobj, file(fullpath, 'wb'))
            if tree.is_executable(ie.file_id):
                os.chmod(fullpath, 0755)
        elif ie.kind == "directory":
            os.mkdir(fullpath)
        elif ie.kind == "symlink":
            try:
                os.symlink(ie.symlink_target, fullpath)
            except OSError, e:
                raise errors.BzrError(
                    "Failed to create symlink %r -> %r, error: %s" %
                    (fullpath, self.symlink_target, e))
        else:
            raise errors.BzrError("don't know how to export {%s} of kind %r" %
                                  (ie.file_id, ie.kind))
Example #12
0
    def put_with_check(self, path, content, action, cancellable=None):
        """Put a_file at path checking that as received it matches content.

        :param path: A relpath.
        :param content: A content description of a file.
        :param action: An action object which can supply file content.
        :param cancellable: A Cancellable object to use when the content is
            already present locally.
        :return: A callable that will execute the rename-into-place - all the
            IO has been done before returning.
        """
        tempname = '%s.lmirrortemp' % path
        self.ui.output_log(4, __name__, 'Checking %s %r' % (content.kind, path))
        if content.kind == 'dir':
            return lambda: self.ensure_dir(path)
        elif content.kind == 'symlink':
            realpath = self.contentdir.local_abspath(path)
            return lambda: self.ensure_link(realpath, content.target)
        elif content.kind != 'file':
            raise ValueError('unknown kind %r for %r' % (content.kind, path))
        # don't download content we don't need
        try:
            if self.check_file(path, content):
                action.ignore_file()
                if cancellable:
                    cancellable.cancelled = True
                return lambda:None
        except (ValueError, IOError):
            # If we can't read the file for some reason, we obviously need to
            # write it :).
            pass
        a_file = action.get_file()
        source = _ShaFile(a_file)
        try:
            # FIXME: mode should be supplied from above, or use 0600 and chmod
            # later.
            stream = self.contentdir.open_write_stream(tempname, 0644)
            try:
                size = osutils.pumpfile(source, stream)
            finally:
                stream.close()
            # TODO: here is where we should check for a mirror-is-updating
            # case.
            if size != content.length or source.sha1.hexdigest() != content.sha1:
                self.contentdir.delete(tempname)
                raise ValueError(
                    'read incorrect content for %r, got sha %r wanted %r' % (
                    path, source.sha1.hexdigest(), content.sha1))
            if content.mtime is not None:
                try:
                    temppath = self.contentdir.local_abspath(tempname)
                except errors.NotLocalUrl, e:
                    # swallow NotLocalUrl errors: they primarily indicate that
                    # the test suite is running against memory, with files that
                    # don't exist.
                    self.ui.output_log(4, __name__,
                        'Failed to set mtime for %r - nonlocal url %r.' % (
                        tempname, self.contentdir))
                else:
                    # Perhaps the first param - atime - should be 'now'.
                    os.utime(temppath, (content.mtime, content.mtime))
        finally:
            a_file.close()
        return lambda: self.ensure_file(tempname, path, content)