code: def prepare_contexts(
      metadata_handle: metadata.Metadata,
      node_contexts: pipeline_pb2.NodeContexts,
  ) -> List[metadata_store_pb2.Context]:
      """Creates the contexts given specification.
  
      Context types will be registered if not already exist.
  
      Args:
        metadata_handle: A handler to access MLMD store.
        node_contexts: A pipeline_pb2.NodeContext message that instructs registering
          of the contexts.
  
      Returns:
        A list of metadata_store_pb2.Context messages.
      """
      result = []
      pipeline_contexts = []
  
      # Makes sure pipeline context exists.
      for context_spec in node_contexts.contexts:
          if (
              context_spec.type.name
              == constants.PIPELINE_CONTEXT_TYPE_NAME
          ):
              pipeline_context = _register_context_if_not_exist(
                  metadata_handle=metadata_handle,
                  context_spec=context_spec,
              )
              pipeline_contexts.append(pipeline_context)
              result.append(pipeline_context)
  
      # Registers other contexts and also set parent-child relationship between
      # pipeline context and pipeline run context.
      for context_spec in node_contexts.contexts:
          if (
              context_spec.type.name
              == constants.PIPELINE_CONTEXT_TYPE_NAME
          ):
              continue
  
          if (
              context_spec.type.name
              == constants.PIPELINE_RUN_CONTEXT_TYPE_NAME
          ):
              parent_contexts = pipeline_contexts
          else:
              parent_contexts = []
          context = _register_context_if_not_exist(
              metadata_handle=metadata_handle,
              context_spec=context_spec,
              parent_contexts=parent_contexts,
          )
          result.append(context)
  
      return result
  
file_path: tfx/tfx/orchestration/portable/mlmd/context_lib.py
imports: from typing import List, Optional
  from absl import logging
  from tfx.dsl.compiler import constants
  from tfx.orchestration import data_types_utils
  from tfx.orchestration import metadata
  from tfx.orchestration.portable.mlmd import common_utils
  from tfx.utils import metrics_utils
  from tfx.proto.orchestration import pipeline_pb2
  from tfx.utils import telemetry_utils
  import ml_metadata
  from ml_metadata import errors
  from ml_metadata.proto import metadata_store_pb2
label_desc: Use image node_id 4 for calling a global function with example usage: prepare_contexts(metadata_handle, node_contexts) and returns: result