Example #1
0
    def test_maybe_gzip(self):
        """Test MaybeGzip"""
        dirrp = rpath.RPath(self.lc, abs_output_dir)
        re_init_rpath_dir(dirrp)

        base_rp = dirrp.append('foo')
        fileobj = rpath.MaybeGzip(base_rp)
        fileobj.close()
        base_rp.setdata()
        self.assertTrue(base_rp.isreg())
        self.assertEqual(base_rp.getsize(), 0)
        base_rp.delete()

        base_gz = dirrp.append('foo.gz')
        self.assertFalse(base_gz.lstat())
        fileobj = rpath.MaybeGzip(base_rp)
        fileobj.write(b"lala")
        fileobj.close()
        base_rp.setdata()
        base_gz.setdata()
        self.assertFalse(base_rp.lstat())
        self.assertTrue(base_gz.isreg())
        data = base_gz.get_bytes(compressed=1)
        self.assertEqual(data, b"lala")
Example #2
0
    def test_maybe_gzip(self):
        """Test MaybeGzip"""
        dirrp = rpath.RPath(self.lc, abs_output_dir)
        re_init_rpath_dir(dirrp)

        base_rp = dirrp.append('foo')
        fileobj = rpath.MaybeGzip(base_rp)
        fileobj.close()
        base_rp.setdata()
        assert base_rp.isreg(), base_rp
        assert base_rp.getsize() == 0
        base_rp.delete()

        base_gz = dirrp.append('foo.gz')
        assert not base_gz.lstat()
        fileobj = rpath.MaybeGzip(base_rp)
        fileobj.write(b"lala")
        fileobj.close()
        base_rp.setdata()
        base_gz.setdata()
        assert not base_rp.lstat()
        assert base_gz.isreg(), base_gz
        data = base_gz.get_bytes(compressed=1)
        assert data == b"lala", data
Example #3
0
    def __init__(self,
                 rp_base,
                 mode,
                 check_path=1,
                 compress=None,
                 callback=None):
        """
        Open rp (or rp+'.gz') for reading ('r') or writing ('w')

        If callback is available, it will be called on the rp upon
        closing (because the rp may not be known in advance).
        """
        self.mode = mode
        self.callback = callback
        self._record_buffer = []
        if check_path:
            if not (rp_base.isincfile()
                    and rp_base.getincbase_bname() == self._prefix):
                log.Log.FatalError(
                    "Checking the path '{pa}' went wrong.".format(pa=rp_base))
        if mode == 'r' or mode == 'rb':
            self.rp = rp_base
            if compress is None:
                if self.rp.isinccompressed():
                    compress = True
                else:
                    compress = False
            self.fileobj = self.rp.open("rb", compress)
        elif mode == 'w' or mode == 'wb':
            if compress and check_path and not rp_base.isinccompressed():

                def callback(rp):
                    self.rp = rp

                self.fileobj = rpath.MaybeGzip(rp_base, callback)
            else:
                self.rp = rp_base
                assert not self.rp.lstat(), (
                    "Path '{rp}' can't exist before it's opened.".format(
                        rp=self.rp))
                self.fileobj = self.rp.open("wb", compress=compress)
        else:
            log.Log.FatalError(
                "File opening mode '{om}' should have been one of "
                "r, rb, w or wb".format(om=mode))