def test_read_file_section(self):
        _, fname = self.create_file()
        self.assertEqual(read_file_section(fname, 2, 4), b"llo ")

        _, dirname = self.create_directory()
        self.assertEqual(read_file_section(f"{dirname}/README.md", 2, 4),
                         b"llo ")
Beispiel #2
0
    def read_file_section(self, target, offset, length, gzipped):
        """
        Reads length bytes of the file at the given path in the bundle.
        The result is gzipped if gzipped is True.
        """
        if self._is_available_locally(target):
            file_path = self._get_target_path(target)
            bytestring = file_util.read_file_section(file_path, offset, length)
            if gzipped:
                bytestring = file_util.gzip_bytestring(bytestring)
            return bytestring
        else:
            worker = self._bundle_model.get_bundle_worker(target.bundle_uuid)
            response_socket_id = self._worker_model.allocate_socket(
                worker['user_id'], worker['worker_id']
            )
            try:
                read_args = {'type': 'read_file_section', 'offset': offset, 'length': length}
                self._send_read_message(worker, response_socket_id, target, read_args)
                bytestring = self._get_read_response(response_socket_id)
            finally:
                self._worker_model.deallocate_socket(response_socket_id)

            # Note: all data from the worker is gzipped (see `local_reader.py`).
            if not gzipped:
                bytestring = file_util.un_gzip_bytestring(bytestring)
            return bytestring
Beispiel #3
0
 def read_file_section_thread(final_path):
     bytestring = gzip_bytestring(
         read_file_section(final_path, args['offset'], args['length']))
     reply_fn(None, {}, bytestring)
 def test_read_file_section(self):
     with tempfile.NamedTemporaryFile(delete=False) as f:
         f.write(b"hello world")
     self.assertEqual(read_file_section(f.name, 2, 4), b"llo ")