code: def cached_run(options, run_args, **run_kwargs):
      run_args = [str(arg) for arg in run_args]  # flatten PosixPaths
      args = parser.parse_args(run_args[1:])
  
      if args.imagename in ("stdin", "-"):
          return run(run_args, **run_kwargs)
  
      source_file = options.input_file
      cache_folder = get_cache_folder(source_file, run_args, args)
      cache_folder.mkdir(parents=True, exist_ok=True)
  
      log.debug(f"Using Tesseract cache {cache_folder}")
  
      if (cache_folder / "stderr.bin").exists():
          log.debug("Cache HIT")
  
          # Replicate stdout/err
          if args.outputbase != "stdout":
              if not args.configfiles:
                  args.configfiles.append("txt")
              for configfile in args.configfiles:
                  # cp cache -> output
                  tessfile = args.outputbase + "." + configfile
                  shutil.copy(
                      str(cache_folder / configfile) + ".bin", tessfile
                  )
          return CompletedProcess(
              args=run_args,
              returncode=0,
              stdout=(cache_folder / "stdout.bin").read_bytes(),
              stderr=(cache_folder / "stderr.bin").read_bytes(),
          )
  
      log.debug("Cache MISS")
  
      cache_kwargs = {
          k: v
          for k, v in run_kwargs.items()
          if k not in ("stdout", "stderr")
      }
      assert cache_kwargs["check"]
      try:
          p = run(run_args, stdout=PIPE, stderr=PIPE, **cache_kwargs)
      except CalledProcessError as e:
          log.exception(e)
          raise  # Pass exception onward
  
      # Update cache
      (cache_folder / "stdout.bin").write_bytes(p.stdout)
      (cache_folder / "stderr.bin").write_bytes(p.stderr)
  
      if args.outputbase != "stdout":
          if not args.configfiles:
              args.configfiles.append("txt")
  
          for configfile in args.configfiles:
              if configfile not in ("hocr", "pdf", "txt"):
                  continue
              # cp pwd/{outputbase}.{configfile} -> {cache}/{configfile}
              tessfile = args.outputbase + "." + configfile
              shutil.copy(
                  tessfile, str(cache_folder / configfile) + ".bin"
              )
  
      def clean_sys_argv():
          for arg in run_args[1:]:
              yield re.sub(
                  r".*/ocrmypdf[.]io[.][^/]+[/](.*)", r"$TMPDIR/\1", arg
              )
  
      manifest = {
          "tesseract_version": TesseractOcrEngine.version().replace(
              "\n", " "
          ),
          "system": platform.system(),
          "python": platform.python_version(),
          "argv_slug": cache_folder.name,
          "sourcefile": str(Path(source_file).relative_to(TESTS_ROOT)),
          "args": list(clean_sys_argv()),
      }
  
      with (Path(CACHE_ROOT) / "manifest.jsonl").open("a") as f:
          json.dump(manifest, f)
          f.write("\n")
          f.flush()
      return p
  
imports: from __future__ import annotations
  import argparse
  import json
  import logging
  import platform
  import re
  import shutil
  import threading
  from functools import partial
  from pathlib import Path
  from subprocess import PIPE, CalledProcessError, CompletedProcess
  from unittest.mock import patch
  from ocrmypdf import hookimpl
  from ocrmypdf.builtin_plugins.tesseract_ocr import TesseractOcrEngine
  from ocrmypdf.subprocess import run
label_desc: Use image node_id 2 for calling a global function with example usage: cached_run(options, run_args) and returns: p, run, CompletedProcess