code: def _get_script_handler(script, since, revision):
      ofh = sys.stdout if script.strip() == "-" else open(script, "w")
  
      def fn(dset, results):
          ds_repo = dset.repo
          header = """\
  #!/bin/sh
  #
  # This file was generated by running (the equivalent of)
  #
  #   datalad rerun --script={script}{since} {revision}
  #
  # in {ds}{path}\n"""
          ofh.write(
              header.format(
                  script=script,
                  since="" if since is None else " --since=" + since,
                  revision=ds_repo.get_hexsha(revision),
                  ds="dataset {} at ".format(dset.id)
                  if dset.id
                  else "",
                  path=dset.path,
              )
          )
  
          for res in results:
              if res["status"] != "ok":
                  yield res
                  return
  
              if "run_info" not in res:
                  continue
  
              run_info = res["run_info"]
              cmd = run_info["cmd"]
  
              expanded_cmd = format_command(
                  dset,
                  cmd,
                  **dict(
                      run_info,
                      dspath=dset.path,
                      pwd=op.join(dset.path, run_info["pwd"]),
                  ),
              )
  
              msg = res["run_message"]
              if msg == _format_cmd_shorty(expanded_cmd):
                  msg = ""
  
              ofh.write(
                  "\n"
                  + "".join("# " + ln for ln in msg.splitlines(True))
                  + "\n"
              )
              commit_descr = ds_repo.describe(res["commit"])
              ofh.write(
                  "# (record: {})\n".format(
                      commit_descr if commit_descr else res["commit"]
                  )
              )
  
              ofh.write(expanded_cmd + "\n")
          if ofh is not sys.stdout:
              ofh.close()
  
          if ofh is sys.stdout:
              yield None
          else:
              yield get_status_dict(
                  "run",
                  ds=dset,
                  status="ok",
                  path=script,
                  message=("Script written to %s", script),
              )
  
      return fn
  
imports: import json
  import logging
  import os.path
  import re
  import sys
  from copy import copy
  from functools import partial
  from itertools import dropwhile
  from datalad.consts import PRE_INIT_COMMIT_SHA
  from datalad.core.local.run import _format_cmd_shorty, assume_ready_opt, format_command, run_command
  from datalad.distribution.dataset import EnsureDataset, datasetmethod, require_dataset
  from datalad.interface.base import Interface, build_doc, eval_results
  from datalad.interface.results import get_status_dict
  from datalad.interface.common_opts import jobs_opt
  from datalad.support.constraints import EnsureNone, EnsureStr
  from datalad.support.exceptions import CapturedException
  from datalad.support.json_py import load_stream
  from datalad.support.param import Parameter
label_desc: Use image node_id 7 for calling a global function with example usage: _get_script_handler(script, since, revision) and returns: fn