code: def subst_vars(s, local_vars):
      """Perform shell/Perl-style variable substitution on 'string'.  Every
      occurrence of '$' followed by a name is considered a variable, and
      variable is substituted by the value found in the 'local_vars'
      dictionary, or in 'os.environ' if it's not in 'local_vars'.
      'os.environ' is first checked/augmented to guarantee that it contains
      certain values: see 'check_environ()'.  Raise ValueError for any
      variables not found in either 'local_vars' or 'os.environ'.
      """
      check_environ()
  
      def _subst(match, local_vars=local_vars):
          var_name = match.group(1)
          if var_name in local_vars:
              return str(local_vars[var_name])
          else:
              return os.environ[var_name]
  
      try:
          return re.sub(r"\$([a-zA-Z_][a-zA-Z_0-9]*)", _subst, s)
      except KeyError as var:
          raise ValueError("invalid variable '$%s'" % var)
  
imports: import os
  import re
  import importlib.util
  import string
  import sys
  import distutils
  from distutils.errors import DistutilsPlatformError
  from distutils.dep_util import newer
  from distutils.spawn import spawn
  from distutils import log
  from distutils.errors import DistutilsByteCompileError
label_desc: Use image node_id 6 for calling a global function with example usage: subst_vars(s, local_vars) and returns: re, str, os