code: def main(args=None):
      """Run CellProfiler
  
      args - command-line arguments, e.g., sys.argv
      """
      if args is None:
          args = sys.argv
  
      set_awt_headless(True)
  
      exit_code = 0
  
      switches = ("--work-announce", "--knime-bridge-address")
  
      if any(
          [
              any([arg.startswith(switch) for switch in switches])
              for arg in args
          ]
      ):
          set_headless()
          aw_parse_args()
          activate_readers()
          worker_main()
          return exit_code
  
      options, args = parse_args(args)
  
      if options.print_version:
          set_headless()
          __version__(exit_code)
  
      if (not options.show_gui) or options.write_schema_and_exit:
          set_headless()
  
          options.run_pipeline = True
  
      if options.batch_commands_file or options.new_batch_commands_file:
          set_headless()
          options.run_pipeline = False
          options.show_gui = False
  
      # must be run after last possible invocation of set_headless()
      activate_readers()
  
      if options.temp_dir is not None:
          if not os.path.exists(options.temp_dir):
              os.makedirs(options.temp_dir)
          set_temporary_directory(options.temp_dir, globally=False)
  
      temp_dir = get_temporary_directory()
  
      to_clean = []
  
      if options.pipeline_filename:
          o = urllib.parse.urlparse(options.pipeline_filename)
          if o[0] in ("ftp", "http", "https"):
              from urllib.request import urlopen
  
              temp_pipe_file = tempfile.NamedTemporaryFile(
                  mode="w+b",
                  suffix=".cppipe",
                  dir=temp_dir,
                  delete=False,
              )
              downloaded_pipeline = urlopen(options.pipeline_filename)
              for line in downloaded_pipeline:
                  temp_pipe_file.write(line)
              options.pipeline_filename = temp_pipe_file.name
              to_clean.append(
                  os.path.join(temp_dir, temp_pipe_file.name)
              )
  
      if options.image_set_file:
          o = urllib.parse.urlparse(options.image_set_file)
          if o[0] in ("ftp", "http", "https"):
              from urllib.request import urlopen
  
              temp_set_file = tempfile.NamedTemporaryFile(
                  mode="w+b", suffix=".csv", dir=temp_dir, delete=False
              )
              downloaded_set_csv = urlopen(options.image_set_file)
              for line in downloaded_set_csv:
                  temp_set_file.write(line)
              options.image_set_file = temp_set_file.name
              to_clean.append(
                  os.path.join(temp_dir, temp_set_file.name)
              )
  
      if options.data_file:
          o = urllib.parse.urlparse(options.data_file)
          if o[0] in ("ftp", "http", "https"):
              from urllib.request import urlopen
  
              temp_data_file = tempfile.NamedTemporaryFile(
                  mode="w+b", suffix=".csv", dir=temp_dir, delete=False
              )
              downloaded_data_csv = urlopen(options.data_file)
              for line in downloaded_data_csv:
                  temp_data_file.write(line)
              options.data_file = temp_data_file.name
              to_clean.append(
                  os.path.join(temp_dir, temp_data_file.name)
              )
  
      set_log_level(options)
  
      if options.print_groups_file is not None:
          print_groups(options.print_groups_file)
  
      if options.batch_commands_file is not None:
          try:
              nr_per_batch = int(options.images_per_batch)
          except ValueError:
              LOGGER.warning(
                  "non-integer argument to --images-per-batch. Defaulting to 1."
              )
              nr_per_batch = 1
          get_batch_commands(options.batch_commands_file, nr_per_batch)
  
      if options.new_batch_commands_file is not None:
          try:
              nr_per_batch = int(options.images_per_batch)
          except ValueError:
              LOGGER.warning(
                  "non-integer argument to --images-per-batch. Defaulting to 1."
              )
              nr_per_batch = 1
          get_batch_commands_new(
              options.new_batch_commands_file, nr_per_batch
          )
  
      # TODO: disabled until CellProfiler/CellProfiler#4684 is resolved
      # if options.omero_credentials is not None:
      #     set_omero_credentials_from_string(options.omero_credentials)
  
      if options.plugins_directory is not None:
          set_plugin_directory(
              options.plugins_directory, globally=False
          )
  
      if options.conserve_memory is not None:
          set_conserve_memory(options.conserve_memory, globally=False)
  
      if options.force_bioformats is not None:
          set_force_bioformats(options.force_bioformats, globally=False)
  
      if options.always_continue is not None:
          set_always_continue(options.always_continue, globally=False)
  
      if not options.allow_schema_write:
          set_allow_schema_write(False)
  
      if options.output_directory:
          if not os.path.exists(options.output_directory):
              os.makedirs(options.output_directory)
  
          set_default_output_directory(options.output_directory)
  
      if options.image_directory:
          set_default_image_directory(options.image_directory)
  
      if options.run_pipeline and not options.pipeline_filename:
          raise ValueError(
              "You must specify a pipeline filename to run"
          )
  
      if options.data_file is not None:
          set_data_file(os.path.abspath(options.data_file))
  
      if options.widget_inspector:
          set_widget_inspector(True, globally=False)
  
      try:
          if options.image_set_file is not None:
              set_image_set_file(options.image_set_file)
  
          #
          # Handle command-line tasks that that need to load the modules to run
          #
          if options.print_measurements:
              print_measurements(options)
  
          if options.write_schema_and_exit:
              write_schema(options.pipeline_filename)
  
          if options.show_gui:
              matplotlib.use("WXAgg")
  
              import cellprofiler.gui.app
  
              if options.pipeline_filename:
                  if is_workspace_file(options.pipeline_filename):
                      workspace_path = os.path.expanduser(
                          options.pipeline_filename
                      )
  
                      pipeline_path = None
                  else:
                      pipeline_path = os.path.expanduser(
                          options.pipeline_filename
                      )
  
                      workspace_path = None
              else:
                  workspace_path = None
  
                  pipeline_path = None
  
              app = cellprofiler.gui.app.App(
                  0,
                  workspace_path=workspace_path,
                  pipeline_path=pipeline_path,
              )
  
              if options.run_pipeline:
                  app.frame.pipeline_controller.do_analyze_images()
  
              app.MainLoop()
  
              return
          elif options.run_pipeline:
              exit_code = run_pipeline_headless(options, args)
  
      finally:
          # Cleanup the temp files we made, if any
          if len(to_clean) > 0:
              for each_temp in to_clean:
                  os.remove(each_temp)
          # If anything goes wrong during the startup sequence headlessly, the JVM needs
          # to be explicitly closed
          if not options.show_gui:
              stop_cellprofiler()
  
      return exit_code
  
imports: import io
  import json
  import logging
  import logging.config
  import optparse
  import os
  import os.path
  import re
  import sys
  import tempfile
  import urllib.parse
  import h5py
  import matplotlib
  import numpy
  import pkg_resources
  from cellprofiler_core.constants.measurement import EXPERIMENT
  from cellprofiler_core.constants.measurement import GROUP_INDEX
  from cellprofiler_core.constants.measurement import GROUP_NUMBER
  from cellprofiler_core.constants.measurement import IMAGE
  from cellprofiler_core.constants.pipeline import M_PIPELINE, EXIT_STATUS
  from cellprofiler_core.measurement import Measurements
  from cellprofiler_core.object import ObjectSet
  from cellprofiler_core.pipeline import LoadException
  from cellprofiler_core.pipeline import Pipeline
  from cellprofiler_core.preferences import get_image_set_file
  from cellprofiler_core.preferences import get_temporary_directory
  from cellprofiler_core.preferences import set_conserve_memory
  from cellprofiler_core.preferences import set_force_bioformats
  from cellprofiler_core.preferences import set_allow_schema_write
  from cellprofiler_core.preferences import set_always_continue
  from cellprofiler_core.preferences import set_awt_headless
  from cellprofiler_core.preferences import set_data_file
  from cellprofiler_core.preferences import set_default_image_directory
  from cellprofiler_core.preferences import set_default_output_directory
  from cellprofiler_core.preferences import set_headless
  from cellprofiler_core.preferences import set_image_set_file
  from cellprofiler_core.preferences import set_plugin_directory
  from cellprofiler_core.preferences import set_temporary_directory
  from cellprofiler_core.preferences import set_widget_inspector
  from cellprofiler_core.utilities.core.workspace import is_workspace_file
  from cellprofiler_core.utilities.hdf5_dict import HDF5FileList
  from cellprofiler_core.utilities.java import start_java, stop_java
  from cellprofiler_core.utilities.measurement import load_measurements
  from cellprofiler_core.utilities.zmq import join_to_the_boundary
  from cellprofiler_core.worker import aw_parse_args
  from cellprofiler_core.worker import main
  from cellprofiler_core.workspace import Workspace
  from cellprofiler_core.reader import activate_readers
label_desc: Use image node_id 1 for calling a global function with example usage: main(args) and returns: exit_code, exit_code