Exemple #1
0
def version_upgrade_xform(n, envoy_internal_shadow, file_proto, params):
    """Transform a FileDescriptorProto from vN[alpha\d] to v(N+1).

    Args:
        n: version N to upgrade from.
        envoy_internal_shadow: generate a shadow for Envoy internal use containing deprecated fields.
        file_proto: vN[alpha\d] FileDescriptorProto message.
        params: plugin parameters.

    Returns:
        v(N+1) FileDescriptorProto message.
    """
    # Load type database.
    if params['type_db_path']:
        utils.load_type_db(params['type_db_path'])
    typedb = utils.get_type_db()
    # If this isn't a proto in an upgraded package, return None.
    if file_proto.name not in typedb.next_version_protos or not typedb.next_version_protos[
            file_proto.name]:
        return None
    # Otherwise, this .proto needs upgrading, do it.
    freeze = 'extra_args' in params and params['extra_args'] == 'freeze'
    existing_pkg_version_status = file_proto.options.Extensions[
        status_pb2.file_status].package_version_status
    # Normally, we are generating the NEXT_MAJOR_VERSION_CANDIDATE. However, if
    # freezing and previously this was the active major version, the migrated
    # version is now the ACTIVE version.
    if freeze and existing_pkg_version_status == status_pb2.ACTIVE:
        package_version_status = status_pb2.ACTIVE
    else:
        package_version_status = status_pb2.NEXT_MAJOR_VERSION_CANDIDATE
    return traverse.traverse_file(
        file_proto, UpgradeVisitor(n, typedb, envoy_internal_shadow, package_version_status))
Exemple #2
0
    def __init__(self, params):
        if params['type_db_path']:
            utils.load_type_db(params['type_db_path'])

        self.clang_format_config = pathlib.Path(params[".clang-format"])
        if not self.clang_format_config.exists():
            raise ProtoPrintError(
                f"Unable to find .clang-format file: {self.clang_format_config}"
            )

        if extra_args := params.get("extra_args"):
            if extra_args.startswith("api_version:"):
                self._api_version = extra_args.split(":")[1]
Exemple #3
0
 def __init__(self, active_or_frozen, params):
     if params['type_db_path']:
         utils.load_type_db(params['type_db_path'])
     self._freeze = 'extra_args' in params and params['extra_args'] == 'freeze'
     self._active_or_frozen = active_or_frozen
Exemple #4
0
            formatted_msgs, reserved_fields, fields)

    def visit_file(self, file_proto, type_context, services, msgs, enums):
        empty_file = len(services) == 0 and len(enums) == 0 and len(msgs) == 0
        header = format_header_from_file(
            type_context.source_code_info, file_proto, empty_file,
            self._requires_deprecation_annotation_import)
        formatted_services = format_block('\n'.join(services))
        formatted_enums = format_block('\n'.join(enums))
        formatted_msgs = format_block('\n'.join(msgs))
        return clang_format(header + formatted_services + formatted_enums + formatted_msgs)


if __name__ == '__main__':
    proto_desc_path = sys.argv[1]

    utils.load_protos(PROTO_PACKAGES)

    file_proto = descriptor_pb2.FileDescriptorProto()
    input_text = pathlib.Path(proto_desc_path).read_text()
    if not input_text:
        sys.exit(0)
    text_format.Merge(input_text, file_proto)
    dst_path = pathlib.Path(sys.argv[2])
    utils.load_type_db(sys.argv[3])
    api_version_file_path = pathlib.Path(sys.argv[4])
    frozen_proto = file_proto.options.Extensions[
        status_pb2.file_status].package_version_status == status_pb2.FROZEN
    dst_path.write_bytes(
        traverse.traverse_file(file_proto, ProtoFormatVisitor(api_version_file_path, frozen_proto)))