code: def shellcheck(bazel_aquery, *shellcheck_argv):
      """Runs shellcheck with the provided argument(s) on all targets that match
      the given Bazel aquery.
  
      Args:
          bazel_aquery: A Bazel aquery expression (e.g. "//:*")
          shellcheck_argv: The command-line arguments to call for shellcheck.
              Note that the first entry should be the shellcheck program itself.
              If omitted, will simply call "shellcheck".
  
      Returns:
          The exit code of shellcheck.
      """
      bazel = Bazel()
      shellcheck_argv = list(shellcheck_argv) or ["shellcheck"]
      all_script_infos = defaultdict(lambda: [])
      aquery_results = bazel.aquery(
          "--include_artifacts=false", bazel_aquery
      )
      shell_calls = list(parse_aquery_shell_calls(aquery_results))
      for shell_args in shell_calls:
          shname = os.path.basename(
              os.path.splitext(shell_args[0])[0]
          ).lower()
          finished_options = False
          i = 1
          while i < len(shell_args):
              if finished_options or not shell_args[i].startswith("-"):
                  all_script_infos[shname].append((shell_args[i], None))
              elif shell_args[i] == "--":
                  finished_options = True
              elif shell_args[i] in ("-o", "+o"):
                  i += 1
              elif shell_args[i] == "-c":
                  all_script_infos[shname].append(
                      (None, shell_args[i + 1])
                  )
                  break
              i += 1
  
      result = 0
      bazel_execution_root = None
      for shell, script_infos in all_script_infos.items():
          scripts_combined = []
          has_stdin = False
          filenames = []
          for script_file, script_text in script_infos:
              if script_file is not None:
                  filenames.append(script_file)
              if script_text is not None:
                  has_stdin = True
                  flatc = "host/bin/external/com_github_google_flatbuffers/flatc"
                  if flatc not in script_text:
                      statements = [
                          "if test -t 0; then",
                          script_text,
                          "fi",
                      ]
                      scripts_combined.append("\n".join(statements))
          if has_stdin:
              filenames.insert(0, "-")
          if shell.endswith("sh"):
              if bazel_execution_root is None:
                  bazel_execution_root = bazel.info()["execution_root"]
              cwd = bazel_execution_root
              cmdargs = [
                  "--shell=" + shell,
                  "--external-sources",
              ] + filenames
              cmdargs = shellcheck_argv + cmdargs
              proc = subprocess.Popen(
                  cmdargs, stdin=subprocess.PIPE, cwd=cwd
              )
              try:
                  proc.communicate(
                      "\n".join(scripts_combined).encode("utf-8")
                  )
              finally:
                  proc.wait()
              result = result or proc.returncode
      return result
  
imports: import ast
  import errno
  import json
  import os
  import re
  import subprocess
  import stat
  import sys
  from collections import defaultdict, OrderedDict
label_desc: Use image node_id 8 for calling a global function with example usage: shellcheck(bazel_aquery) and returns: result