Exemplo n.º 1
0
    def get_writable_fsobj(self, fsobj, prefer_reuse=True, empty=False):

        path = source = None
        if fsobj:
            source = fsobj.data
            if source.mutable:
                return fsobj
            if self.allow_reuse and prefer_reuse:
                path = source.path

                # XXX: this should be doing abspath fs intersection probably,
                # although the paths generated are from triggers/engine- still.

                if path is not None and not path.startswith(self.tempdir):
                    # the fsobj pathway isn't in temp space; force a transfer.
                    path = None

            if path:
                # ok, it's tempspace, and reusable.
                obj = data_source.local_source(path,
                                               True,
                                               encoding=source.encoding)

                if empty:
                    obj.bytes_fileobj(True).truncate(0)
                return obj

        # clone it into tempspace; it's required we control the tempspace,
        # so this function is safe in our usage.
        fd, path = tempfile.mkstemp(prefix='merge-engine-', dir=self.tempdir)

        # XXX: annoying quirk of python, we don't want append mode, so 'a+'
        # isn't viable; wr will truncate the file, so data_source uses r+.
        # this however doesn't allow us to state "create if missing"
        # so we create it ourselves.  Annoying, but so it goes.
        # just touch the filepath.
        open(path, 'w').close()
        new_source = data_source.local_source(path,
                                              True,
                                              encoding=getattr(
                                                  fsobj, 'encoding', None))

        if source and not empty:
            data_source.transfer(source.bytes_fsobj(),
                                 new_source.bytes_fsobj(True))
        return new_source
Exemplo n.º 2
0
    def get_writable_fsobj(self, fsobj, prefer_reuse=True, empty=False):

        path = source = None
        if fsobj:
            source = fsobj.data
            if source.mutable:
                return fsobj
            if self.allow_reuse and prefer_reuse:
                path = source.path

                # XXX: this should be doing abspath fs intersection probably,
                # although the paths generated are from triggers/engine- still.

                if path is not None and not path.startswith(self.tempdir):
                    # the fsobj pathway isn't in temp space; force a transfer.
                    path = None

            if path:
                # ok, it's tempspace, and reusable.
                obj = data_source.local_source(path, True,
                    encoding=source.encoding)

                if empty:
                    obj.bytes_fileobj(True).truncate(0)
                return obj

        # clone it into tempspace; it's required we control the tempspace,
        # so this function is safe in our usage.
        fd, path = tempfile.mkstemp(prefix='merge-engine-', dir=self.tempdir)

        # XXX: annoying quirk of python, we don't want append mode, so 'a+'
        # isn't viable; wr will truncate the file, so data_source uses r+.
        # this however doesn't allow us to state "create if missing"
        # so we create it ourselves.  Annoying, but so it goes.
        # just touch the filepath.
        open(path, 'w').close()
        new_source = data_source.local_source(path, True,
            encoding=getattr(fsobj, 'encoding', None))

        if source and not empty:
            data_source.transfer(source.bytes_fsobj(), new_source.bytes_fsobj(True))
        return new_source