code: def _prepare_dependencies_and_pre_execution_scripts(
      local_dependencies_path: str,
      pre_execution_commands: List[str],
      pre_execution_script_local_path: str,
      s3_base_uri: str,
      s3_kms_key: str,
      sagemaker_session: Session,
      tmp_dir: str,
  ):
      """Prepare pre-execution scripts and dependencies and upload them to s3.
  
      If pre execution commands are provided, a new bash file will be created
        with those commands in tmp directory.
      If pre execution script is provided, it copies that file from local file path
        to tmp directory.
      If local dependencies file is provided, it copies that file from local file path
        to tmp directory.
      If under pipeline context, tmp directory with copied dependencies and scripts is
        uploaded to S3.
      """
      from sagemaker.workflow.utilities import (
          load_step_compilation_context,
      )
  
      if not (
          local_dependencies_path
          or pre_execution_commands
          or pre_execution_script_local_path
      ):
          return None
  
      if local_dependencies_path:
          dst_path = shutil.copy2(local_dependencies_path, tmp_dir)
          logger.info(
              "Copied dependencies file at '%s' to '%s'",
              local_dependencies_path,
              dst_path,
          )
  
      if pre_execution_commands or pre_execution_script_local_path:
          pre_execution_script = os.path.join(
              tmp_dir, PRE_EXECUTION_SCRIPT_NAME
          )
          if pre_execution_commands:
              with open(pre_execution_script, "w") as target_script:
                  commands = [
                      cmd + "\n" for cmd in pre_execution_commands
                  ]
                  target_script.writelines(commands)
                  logger.info(
                      "Generated pre-execution script from commands to '%s'",
                      pre_execution_script,
                  )
          else:
              shutil.copy2(
                  pre_execution_script_local_path, pre_execution_script
              )
              logger.info(
                  "Copied pre-execution commands from script at '%s' to '%s'",
                  pre_execution_script_local_path,
                  pre_execution_script,
              )
  
      step_compilation_context = load_step_compilation_context()
      if step_compilation_context:
          upload_path = S3Uploader.upload(
              tmp_dir,
              s3_path_join(
                  s3_base_uri,
                  step_compilation_context.step_name,
                  step_compilation_context.pipeline_build_time,
                  SCRIPT_AND_DEPENDENCIES_CHANNEL_NAME,
              ),
              s3_kms_key,
              sagemaker_session,
          )
          logger.info(
              "Successfully uploaded dependencies and pre execution scripts to '%s'",
              upload_path,
          )
          return upload_path
      return None
  
imports: from __future__ import absolute_import
  import dataclasses
  import os
  import re
  import shutil
  import sys
  import json
  import secrets
  from typing import Callable, Dict, List, Optional, Tuple, Union, TYPE_CHECKING
  from urllib.parse import urlparse
  from io import BytesIO
  from sagemaker.config.config_schema import REMOTE_FUNCTION_ENVIRONMENT_VARIABLES, REMOTE_FUNCTION_IMAGE_URI, REMOTE_FUNCTION_DEPENDENCIES, REMOTE_FUNCTION_PRE_EXECUTION_COMMANDS, REMOTE_FUNCTION_PRE_EXECUTION_SCRIPT, REMOTE_FUNCTION_INCLUDE_LOCAL_WORKDIR, REMOTE_FUNCTION_INSTANCE_TYPE, REMOTE_FUNCTION_JOB_CONDA_ENV, REMOTE_FUNCTION_ROLE_ARN, REMOTE_FUNCTION_S3_ROOT_URI, REMOTE_FUNCTION_S3_KMS_KEY_ID, REMOTE_FUNCTION_VOLUME_KMS_KEY_ID, REMOTE_FUNCTION_TAGS, REMOTE_FUNCTION_VPC_CONFIG_SUBNETS, REMOTE_FUNCTION_VPC_CONFIG_SECURITY_GROUP_IDS, REMOTE_FUNCTION_ENABLE_INTER_CONTAINER_TRAFFIC_ENCRYPTION
  from sagemaker.experiments._run_context import _RunContext
  from sagemaker.experiments.run import Run
  from sagemaker.image_uris import get_base_python_image_uri
  from sagemaker import image_uris
  from sagemaker.remote_function.checkpoint_location import CheckpointLocation
  from sagemaker.session import get_execution_role, _logs_for_job, Session
  from sagemaker.utils import name_from_base, _tmpdir, resolve_value_from_config, format_tags, Tags
  from sagemaker.s3 import s3_path_join, S3Uploader
  from sagemaker import vpc_utils
  from sagemaker.remote_function.core.stored_function import StoredFunction, _SerializedData
  from sagemaker.remote_function.core.pipeline_variables import Context
  from sagemaker.remote_function.runtime_environment.runtime_environment_manager import RuntimeEnvironmentManager, _DependencySettings
  from sagemaker.remote_function import logging_config
  from sagemaker.remote_function.spark_config import SparkConfig
  from sagemaker.spark import defaults
  from sagemaker.remote_function.custom_file_filter import CustomFileFilter, copy_workdir, resolve_custom_file_filter_from_config_file
label_desc: Use image node_id 3 for calling a global function with example usage: _prepare_dependencies_and_pre_execution_scripts(local_dependencies_path, pre_execution_commands, pre_execution_script_local_path, s3_base_uri, s3_kms_key, sagemaker_session, tmp_dir) and returns: None, None, upload_path