code: def run_wrapper(
      job_rule,
      input,
      output,
      params,
      wildcards,
      threads,
      resources,
      log,
      benchmark,
      benchmark_repeats,
      conda_env,
      container_img,
      singularity_args,
      env_modules,
      use_singularity,
      linemaps,
      debug,
      cleanup_scripts,
      shadow_dir,
      jobid,
      edit_notebook,
      conda_base_path,
      basedir,
      sourcecache_path,
      runtime_sourcecache_path,
  ):
      """
      Wrapper around the run method that handles exceptions and benchmarking.
  
      Arguments
      job_rule   -- the ``job.rule`` member
      input      -- a list of input files
      output     -- a list of output files
      wildcards  -- so far processed wildcards
      threads    -- usable threads
      log        -- a list of log files
      shadow_dir -- optional shadow directory root
      """
      # get shortcuts to job_rule members
      run = job_rule.run_func
      rule = job_rule.name
      is_shell = job_rule.shellcmd is not None
  
      if os.name == "posix" and debug:
          sys.stdin = open("/dev/stdin")
  
      if benchmark is not None:
          from snakemake.benchmark import (
              BenchmarkRecord,
              benchmarked,
              write_benchmark_records,
          )
  
      # Change workdir if shadow defined and not using singularity.
      # Otherwise, we do the change from inside the container.
      passed_shadow_dir = None
      if use_singularity and container_img:
          passed_shadow_dir = shadow_dir
          shadow_dir = None
  
      try:
          with change_working_directory(shadow_dir):
              if benchmark:
                  bench_records = []
                  for bench_iteration in range(benchmark_repeats):
                      # Determine whether to benchmark this process or do not
                      # benchmarking at all.  We benchmark this process unless the
                      # execution is done through the ``shell:``, ``script:``, or
                      # ``wrapper:`` stanza.
                      is_sub = (
                          job_rule.shellcmd
                          or job_rule.script
                          or job_rule.wrapper
                          or job_rule.cwl
                      )
                      if is_sub:
                          # The benchmarking through ``benchmarked()`` is started
                          # in the execution of the shell fragment, script, wrapper
                          # etc, as the child PID is available there.
                          bench_record = BenchmarkRecord()
                          run(
                              input,
                              output,
                              params,
                              wildcards,
                              threads,
                              resources,
                              log,
                              rule,
                              conda_env,
                              container_img,
                              singularity_args,
                              use_singularity,
                              env_modules,
                              bench_record,
                              jobid,
                              is_shell,
                              bench_iteration,
                              cleanup_scripts,
                              passed_shadow_dir,
                              edit_notebook,
                              conda_base_path,
                              basedir,
                              sourcecache_path,
                              runtime_sourcecache_path,
                          )
                      else:
                          # The benchmarking is started here as we have a run section
                          # and the generated Python function is executed in this
                          # process' thread.
                          with benchmarked() as bench_record:
                              run(
                                  input,
                                  output,
                                  params,
                                  wildcards,
                                  threads,
                                  resources,
                                  log,
                                  rule,
                                  conda_env,
                                  container_img,
                                  singularity_args,
                                  use_singularity,
                                  env_modules,
                                  bench_record,
                                  jobid,
                                  is_shell,
                                  bench_iteration,
                                  cleanup_scripts,
                                  passed_shadow_dir,
                                  edit_notebook,
                                  conda_base_path,
                                  basedir,
                                  sourcecache_path,
                                  runtime_sourcecache_path,
                              )
                      # Store benchmark record for this iteration
                      bench_records.append(bench_record)
              else:
                  run(
                      input,
                      output,
                      params,
                      wildcards,
                      threads,
                      resources,
                      log,
                      rule,
                      conda_env,
                      container_img,
                      singularity_args,
                      use_singularity,
                      env_modules,
                      None,
                      jobid,
                      is_shell,
                      None,
                      cleanup_scripts,
                      passed_shadow_dir,
                      edit_notebook,
                      conda_base_path,
                      basedir,
                      sourcecache_path,
                      runtime_sourcecache_path,
                  )
      except (KeyboardInterrupt, SystemExit) as e:
          # Re-raise the keyboard interrupt in order to record an error in the
          # scheduler but ignore it
          raise e
      except BaseException as ex:
          # this ensures that exception can be re-raised in the parent thread
          origin = get_exception_origin(ex, linemaps)
          if origin is not None:
              log_verbose_traceback(ex)
              lineno, file = origin
              raise RuleException(
                  format_error(
                      ex,
                      lineno,
                      linemaps=linemaps,
                      snakefile=file,
                      show_traceback=True,
                  )
              )
          else:
              # some internal bug, just reraise
              raise ex
  
      if benchmark is not None:
          try:
              write_benchmark_records(bench_records, benchmark)
          except Exception as ex:
              raise WorkflowError(ex)
  
imports: import os
  from pathlib import Path
  import sys
  import time
  import shlex
  import concurrent.futures
  import subprocess
  from functools import partial
  from snakemake.common import async_run
  from snakemake.executors import change_working_directory
  from snakemake.settings import DeploymentMethod
  from snakemake_interface_executor_plugins.executors.base import SubmittedJobInfo
  from snakemake_interface_executor_plugins.executors.real import RealExecutor
  from snakemake_interface_executor_plugins.dag import DAGExecutorInterface
  from snakemake_interface_executor_plugins.workflow import WorkflowExecutorInterface
  from snakemake_interface_executor_plugins.logging import LoggerExecutorInterface
  from snakemake_interface_executor_plugins.jobs import JobExecutorInterface, SingleJobExecutorInterface, GroupJobExecutorInterface
  from snakemake_interface_executor_plugins.settings import ExecMode, CommonSettings
  from snakemake.shell import shell
  from snakemake.logging import logger
  from snakemake.exceptions import print_exception, get_exception_origin
  from snakemake.exceptions import format_error, RuleException, log_verbose_traceback
  from snakemake.exceptions import WorkflowError, SpawnedJobError, CacheMissException
label_desc: Use image node_id 1 for calling a global function with example usage: run_wrapper(job_rule, input, output, params, wildcards, threads, resources, log, benchmark, benchmark_repeats, conda_env, container_img, singularity_args, env_modules, use_singularity, linemaps, debug, cleanup_scripts, shadow_dir, jobid, edit_notebook, conda_base_path, basedir, sourcecache_path, runtime_sourcecache_path) without return types