code: def get_mupdf_internal(out, location=None, sha=None, local_tgz=None):
      """
      Gets MuPDF as either a .tgz or a local directory.
  
      Args:
          out:
              Either 'dir' (we return name of local directory containing mupdf) or 'tgz' (we return
              name of local .tgz file containing mupdf).
          location:
              First, if None we set to hard-coded default URL or git location.
              If starts with 'git:', should be remote git location.
              Otherwise if containg '://' should be URL for .tgz.
              Otherwise shuld path of local mupdf checkout.
          sha:
              If not None and we use git clone, we checkout this sha.
          local_tgz:
              If not None, must be local .tgz file.
      Returns:
          Absolute path of local directory or .tgz containing MuPDF, or None if
          we are to use system MuPDF.
      """
      log(f"get_mupdf_internal(): {out=} {location=} {sha=}")
      assert out in ("dir", "tgz")
      if location is None:
          location = "https://mupdf.com/downloads/archive/mupdf-1.23.9-source.tar.gz"
          # location = 'git:--branch master https://github.com/ArtifexSoftware/mupdf.git'
  
      if location == "":
          # Use system mupdf.
          return
  
      local_dir = None
      if local_tgz:
          assert os.path.isfile(local_tgz)
      elif location.startswith("git:"):
          location_git = location[4:]
          local_dir = "mupdf-git"
  
          # Try to update existing checkout.
          e = run(
              f"cd {local_dir} && git pull && git submodule update --init",
              check=False,
          ).returncode
          if e:
              # No existing git checkout, so do a fresh clone.
              _fs_remove(local_dir)
              run(
                  f"git clone --recursive --depth 1 --shallow-submodules {location[4:]} {local_dir}"
              )
  
          # Show sha of checkout.
          run(
              f"cd {local_dir} && git show --pretty=oneline|head -n 1",
              check=False,
          )
          if sha:
              command = f"cd mupdf && git checkout {sha}"
              log(f"Running: {command}")
              run(f"cd mupdf && git checkout {sha}")
      elif "://" in location:
          # Download .tgz.
          local_tgz = os.path.basename(location)
          suffix = ".tar.gz"
          assert location.endswith(
              suffix
          ), f"Unrecognised suffix in remote URL {location=}."
          name = local_tgz[: -len(suffix)]
          log(f"Download {location=} {local_tgz=} {name=}")
          if os.path.exists(local_tgz):
              try:
                  tar_check(local_tgz, "r:gz", prefix=f"{name}/")
              except Exception as e:
                  log(
                      f"Not using existing file {local_tgz} because invalid tar data: {e}"
                  )
                  _fs_remove(local_tgz)
          if os.path.exists(local_tgz):
              log(
                  f"Not downloading from {location} because already present: {local_tgz!r}"
              )
          else:
              log(f"Downloading from {location=} to {local_tgz=}.")
              urllib.request.urlretrieve(location, local_tgz)
              assert os.path.exists(local_tgz)
              tar_check(local_tgz, "r:gz", prefix=f"{name}/")
      else:
          assert os.path.isdir(
              location
          ), f"Local MuPDF does not exist: {location=}"
          local_dir = location
  
      assert bool(local_dir) != bool(local_tgz)
      if out == "dir":
          if not local_dir:
              assert local_tgz
              local_dir = tar_extract(local_tgz, exists="return")
          return os.path.abspath(local_dir)
      elif out == "tgz":
          if not local_tgz:
              # Create .tgz containing git files in `local_dir`.
              assert local_dir
              if local_dir.endswith("/"):
                  local_dir = local_dir[:-1]
              top = os.path.basename(local_dir)
              local_tgz = f"{local_dir}.tgz"
              log(
                  f"Creating .tgz from git files. {top=} {local_dir=} {local_tgz=}"
              )
              _fs_remove(local_tgz)
              with tarfile.open(local_tgz, "w:gz") as f:
                  for name in pipcl.git_items(
                      local_dir, submodules=True
                  ):
                      path = os.path.join(local_dir, name)
                      if os.path.isfile(path):
                          path2 = f"{top}/{name}"
                          log(f"Adding {path=} {path2=}.")
                          f.add(path, path2, recursive=False)
          return os.path.abspath(local_tgz)
      else:
          assert 0, f"Unrecognised {out=}"
  
imports: import glob
  import io
  import os
  import textwrap
  import time
  import platform
  import shlex
  import shutil
  import stat
  import subprocess
  import sys
  import tarfile
  import urllib.request
  import zipfile
  import pipcl
  import sys
label_desc: Use image node_id 8 for calling a global function with example usage: get_mupdf_internal(out, location, sha, local_tgz) and returns: os, os