Esempio n. 1
0
def read_from_filesystem(run, path, read_args, socket):
    def reply_error(code, message):
        message = {
            'error_code': code,
            'error_message': message,
        }
        socket.reply(message)

    try:
        read_type = read_args['type']
        if read_type == 'get_target_info':
            # At the top-level directory, we should ignore dependencies.
            if path and os.path.normpath(path) in run.dependency_paths:
                target_info = None
            else:
                try:
                    target_info = get_target_info(
                        run.bundle_path, run.bundle['uuid'], path, read_args['depth'])
                except PathException as e:
                    reply_error(httplib.BAD_REQUEST, e.message)
                    return

                if not path and read_args['depth'] > 0:
                    target_info['contents'] = [
                        child for child in target_info['contents']
                        if child['name'] not in run.dependency_paths]

            socket.reply({'target_info': target_info})
        else:
            try:
                final_path = get_target_path(run.bundle_path, run.bundle['uuid'], path)
            except PathException as e:
                reply_error(httplib.BAD_REQUEST, e.message)
                return

            if read_type == 'stream_directory':
                if path:
                    exclude_names = []
                else:
                    exclude_names = run.dependency_paths
                with closing(tar_gzip_directory(final_path, exclude_names=exclude_names)) as fileobj:
                    socket.reply_data({}, fileobj)
            elif read_type == 'stream_file':
                with closing(gzip_file(final_path)) as fileobj:
                    socket.reply_data({}, fileobj)
            elif read_type == 'read_file_section':
                string = gzip_string(read_file_section(
                    final_path, read_args['offset'], read_args['length']))
                socket.reply_data({}, string)
            elif read_type == 'summarize_file':
                string = gzip_string(summarize_file(
                    final_path, read_args['num_head_lines'],
                    read_args['num_tail_lines'], read_args['max_line_length'],
                    read_args['truncation_text']))
                socket.reply_data({}, string)
    except BundleServiceException:
        traceback.print_exc()
    except Exception as e:
        traceback.print_exc()
        reply_error(httplib.INTERNAL_SERVER_ERROR, e.message)
Esempio n. 2
0
    def read(self, socket_id, path, read_args):
        def reply_error(code, message):
            message = {
                'error_code': code,
                'error_message': message,
            }
            self._bundle_service.reply(self._worker.id, socket_id, message)

        try:
            read_type = read_args['type']
            if read_type == 'get_target_info':
                # At the top-level directory, we should ignore dependencies.
                if path and os.path.normpath(path) in self._dep_paths:
                    target_info = None
                else:
                    try:
                        target_info = get_target_info(
                            self._bundle_path, self._uuid, path, read_args['depth'])
                    except PathException as e:
                        reply_error(httplib.BAD_REQUEST, e.message)
                        return

                    if not path and read_args['depth'] > 0:
                        target_info['contents'] = [
                            child for child in target_info['contents']
                            if child['name'] not in self._dep_paths]

                self._bundle_service.reply(self._worker.id, socket_id,
                                           {'target_info': target_info})
            else:
                try:
                    final_path = get_target_path(self._bundle_path, self._uuid, path)
                except PathException as e:
                    reply_error(httplib.BAD_REQUEST, e.message)
                    return

                if read_type == 'stream_directory':
                    if path:
                        exclude_names = []
                    else:
                        exclude_names = self._dep_paths
                    with closing(tar_gzip_directory(final_path, exclude_names=exclude_names)) as fileobj:
                        self._bundle_service.reply_data(self._worker.id, socket_id, {}, fileobj)
                elif read_type == 'stream_file':
                    with closing(gzip_file(final_path)) as fileobj:
                        self._bundle_service.reply_data(self._worker.id, socket_id, {}, fileobj)
                elif read_type == 'read_file_section':
                    string = gzip_string(read_file_section(
                        final_path, read_args['offset'], read_args['length']))
                    self._bundle_service.reply_data(self._worker.id, socket_id, {}, string)
                elif read_type == 'summarize_file':
                    string = gzip_string(summarize_file(
                        final_path, read_args['num_head_lines'],
                        read_args['num_tail_lines'], read_args['max_line_length'],
                        read_args['truncation_text']))
                    self._bundle_service.reply_data(self._worker.id, socket_id, {}, string)
        except BundleServiceException:
            traceback.print_exc()
        except Exception as e:
            traceback.print_exc()
            reply_error(httplib.INTERNAL_SERVER_ERROR, e.message)