code: def get_preprocessed_entry_point(
      entry_point,
      chief_config,
      worker_config,
      worker_count,
      distribution_strategy,
      called_from_notebook=False,
      return_file_descriptor=False,
  ):
      """Creates python script for distribution based on the given `entry_point`.
  
      This utility creates a new python script called `preprocessed_entry_point`
      based on the given `entry_point` and `distribution_strategy` inputs. This
      script will become the new Docker entry point python program.
  
      1. If `entry_point` is a python file name and `distribution_strategy` is
      auto, then `preprocessed_entry_point` will have the user given
      `entry_point` code wrapped in a Tensorflow distribution strategy.
  
      2. If `entry_point` is None and `run` is invoked inside of a python script,
      then `preprocessed_entry_point` will be this python script (sys.args[0]).
  
      3. If `entry_point` is an `ipynb` file, then `preprocessed_entry_point`
      will be the code from the notebook. This utility uses `nbconvert`
      to get the code from notebook.
  
      4. If `entry_point` is None and `run` is invoked inside of an `ipynb`
      notebook, then `preprocessed_entry_point` will be the code from the
      notebook. This urility uses `google.colab` client API to fetch the code.
  
      For cases 2, 3 & 4, if `distribution_strategy` is auto, then this script
      will be wrapped in a Tensorflow distribution strategy.
  
      The distribution strategy instance created is based on the machine
      configurations provided using the `chief_config`, `worker_count` params.
      - If the number of workers > 0,
          - If accelerator type is TPU, we will create an instance of
          `tf.distribute.experimental.TPUStrategy`.
          - Otherwise, we will create a default instance of
          `tf.distribute.experimental.MultiWorkerMirroredStrategy`.
      - If number of GPUs > 0, we will create a default instance of
          `tf.distribute.MirroredStrategy`
      - Otherwise, we will use `tf.distribute.OneDeviceStrategy`
  
      Args:
          entry_point: Optional string. File path to the python file or iPython
              notebook that contains the TensorFlow code.
              Note) This path must be in the current working directory tree.
              Example) 'train.py', 'training/mnist.py', 'mnist.ipynb'
              If `entry_point` is not provided, then
              - If you are in an iPython notebook environment, then the
                  current notebook is taken as the `entry_point`.
              - Otherwise, the current python script is taken as the
                  `entry_point`.
          chief_config: `MachineConfig` that represents the configuration
              for the chief worker in a distribution cluster.
          worker_config: `MachineConfig` that represents the configuration
              for the workers in a distribution cluster.
          worker_count: Integer that represents the number of general workers
              in a distribution cluster. This count does not include the chief
              worker.
          distribution_strategy: 'auto' or None. Defaults to 'auto'.
              'auto' means we will take care of creating a Tensorflow
              distribution strategy instance based on the machine configurations
              provided using the `chief_config`, `worker_config` and
              `worker_count` params.
          called_from_notebook: Boolean. True if the API is run in a
              notebook environment.
          return_file_descriptor: Boolean. True if the file descriptor for the
              temporary file is also returned.
  
      Returns:
          The `preprocessed_entry_point` file path.
  
      Raises:
          RuntimeError: If invoked from Notebook but unable to access it.
              Typically, this is due to missing the `nbconvert` package.
      """
  
      # Set `TF_KERAS_RUNNING_REMOTELY` env variable. This is required in order
      # to prevent running `tfc.run` if we are already in a cloud environment.
      # This is applicable only when `entry_point` is None.
      script_lines = [
          "import os\n",
          "import tensorflow as tf\n",
          'os.environ["TF_KERAS_RUNNING_REMOTELY"]="1"\n',
      ]
  
      # Capture job trace log when SIGSEGV occures
  
      if worker_count > 0 and machine_config.is_tpu_config(
          worker_config
      ):
          # faulthandler is not supported in TPU v1.x images
          logger.info(
              "Faulthandler is not supported with TF < v2.2 images."
          )
  
      else:
          script_lines.extend(
              [
                  "import faulthandler\n",
                  "faulthandler.enable()\n",
              ]
          )
  
      # Setting default Tuner_ID if one is provided in args
      script_lines.extend(
          [
              "import sys\n",
              "for flag in sys.argv[1:]:\n",
              '  if flag.startswith("TUNER_ID"):\n',
              '    os.environ["KERASTUNER_TUNER_ID"]=flag\n',
          ]
      )
  
      # Auto wrap in distribution strategy.
      if distribution_strategy == "auto":
          if worker_count > 0:
              if machine_config.is_tpu_config(worker_config):
                  strategy = get_tpu_cluster_resolver_fn()
                  strategy.extend(
                      [
                          "resolver = wait_for_tpu_cluster_resolver_ready()\n",
                          "tf.config.experimental_connect_to_cluster(resolver)\n",
                          "tf.tpu.experimental.initialize_tpu_system(resolver)\n",
                          "strategy = tf.distribute.experimental.TPUStrategy("
                          "resolver)\n",
                      ]
                  )
              else:
                  strategy = [
                      "strategy = tf.distribute.experimental."
                      "MultiWorkerMirroredStrategy()\n"
                  ]
          elif chief_config.accelerator_count > 1:
              strategy = [
                  "strategy = tf.distribute.MirroredStrategy()\n"
              ]
          else:
              strategy = [
                  "strategy = tf.distribute.OneDeviceStrategy(device='/gpu:0')\n"
              ]
          script_lines.extend(strategy)
          script_lines.append(
              "tf.distribute.experimental_set_strategy(strategy)\n"
          )
  
      # If `entry_point` is not provided, detect if we are in a notebook
      # or a python script. Fetch the `entry_point`.
      if entry_point is None and not called_from_notebook:
          # Current python script is assumed to be the entry_point.
          entry_point = sys.argv[0]
  
      # Add user's code.
      if entry_point is not None and entry_point.endswith("py"):
          # We are using exec here to execute the user code object.
          # This will support use case where the user's program has a
          # main method.
          _, entry_point_file_name = os.path.split(entry_point)
          script_lines.append(
              'exec(open("{}").read())\n'.format(entry_point_file_name)
          )
      else:
          if called_from_notebook:
              # Kaggle integration
              if os.getenv("KAGGLE_CONTAINER_NAME"):
                  logger.info("Preprocessing Kaggle notebook...")
                  py_content = _get_kaggle_notebook_content()
              else:
                  # Colab integration
                  py_content = _get_colab_notebook_content()
          else:
              if PythonExporter is None:
                  raise RuntimeError(
                      "Unable to access iPython notebook. "
                      "Please make sure you have installed `nbconvert` package."
                  )
  
              # Get the python code from the iPython notebook.
              (py_content, _) = PythonExporter().from_filename(
                  entry_point
              )
              py_content = py_content.splitlines(keepends=True)
  
          # Remove any iPython special commands and add the python code
          # to script_lines.
          for line in py_content:
              if line.strip().startswith("%"):
                  raise RuntimeError(
                      "Magic commands '%' are not supported."
                  )
  
              elif line.strip().startswith("!"):
                  commands_list = line.strip()[1:].split(" ")
                  script_lines.extend(
                      [
                          "import sys\n",
                          "import subprocess\n",
                          f"print(subprocess.run({commands_list}",
                          ",capture_output=True, text=True).stdout)\n",
                      ]
                  )
  
              elif not (
                  line.strip().startswith("get_ipython().system(")
              ):
                  script_lines.append(line)
  
      # Create a tmp wrapped entry point script file.
      file_descriptor, output_file = tempfile.mkstemp(suffix=".py")
      with open(output_file, "w") as f:
          f.writelines(script_lines)
  
      # Returning file descriptor could be necessary for some os.close calls
      if return_file_descriptor:
          return (output_file, file_descriptor)
      else:
          return output_file
  
file_path: cloud/src/python/tensorflow_cloud/core/preprocess.py
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import io
  import logging
  import os
  import sys
  import tempfile
  from .None import machine_config
label_desc: Use image node_id 1 for calling a global function with example usage: get_preprocessed_entry_point(entry_point, chief_config, worker_config, worker_count, distribution_strategy, called_from_notebook, return_file_descriptor) and returns: output_file,  file_descriptor, output_file