parameters: (filename, vars=None)
return_statement: return vars
docstring: Parse a Makefile-style file.
  
      A dictionary containing name/value pairs is returned.  If an
      optional dictionary is passed in as the second argument, it is
      used instead of a new dictionary.
docstring_summary: Parse a Makefile-style file.
docstring_tokens: Parse a Makefile - style file .
function: def _parse_makefile(filename, vars=None):
      """Parse a Makefile-style file.
  
      A dictionary containing name/value pairs is returned.  If an
      optional dictionary is passed in as the second argument, it is
      used instead of a new dictionary.
      """
      import re
      # Regexes needed for parsing Makefile (and similar syntaxes,
      # like old-style Setup files).
      _variable_rx = re.compile("([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)")
      _findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)")
      _findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}")
  
      if vars is None:
          vars = {}
      done = {}
      notdone = {}
  
      with open(filename) as f:
          lines = f.readlines()
  
      for line in lines:
          if line.startswith('#') or line.strip() == '':
              continue
          m = _variable_rx.match(line)
          if m:
              n, v = m.group(1, 2)
              v = v.strip()
              # `$$' is a literal `$' in make
              tmpv = v.replace('$$', '')
  
              if "$" in tmpv:
                  notdone[n] = v
              else:
                  try:
                      v = int(v)
                  except ValueError:
                      # insert literal `$'
                      done[n] = v.replace('$$', '$')
                  else:
                      done[n] = v
  
      # do variable interpolation here
      while notdone:
          for name in notdone.keys():
              value = notdone[name]
              m = _findvar1_rx.search(value) or _findvar2_rx.search(value)
              if m:
                  n = m.group(1)
                  found = True
                  if n in done:
                      item = str(done[n])
                  elif n in notdone:
                      # get it on a subsequent round
                      found = False
                  elif n in os.environ:
                      # do it like make: fall back to environment
                      item = os.environ[n]
                  else:
                      done[n] = item = ""
                  if found:
                      after = value[m.end():]
                      value = value[:m.start()] + item + after
                      if "$" in after:
                          notdone[name] = value
                      else:
                          try: value = int(value)
                          except ValueError:
                              done[name] = value.strip()
                          else:
                              done[name] = value
                          del notdone[name]
              else:
                  # bogus variable reference; just drop it since we can't deal
                  del notdone[name]
      # strip spurious spaces
      for k, v in done.items():
          if isinstance(v, str):
              done[k] = v.strip()
  
      # save the results in the global dictionary
      vars.update(done)
      return vars
function_tokens: def _parse_makefile ( filename , vars = None ) : import re # Regexes needed for parsing Makefile (and similar syntaxes, # like old-style Setup files). _variable_rx = re . compile ( "([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)" ) _findvar1_rx = re . compile ( r"\$\(([A-Za-z][A-Za-z0-9_]*)\)" ) _findvar2_rx = re . compile ( r"\${([A-Za-z][A-Za-z0-9_]*)}" ) if vars is None : vars = { } done = { } notdone = { } with open ( filename ) as f : lines = f . readlines ( ) for line in lines : if line . startswith ( '#' ) or line . strip ( ) == '' : continue m = _variable_rx . match ( line ) if m : n , v = m . group ( 1 , 2 ) v = v . strip ( ) # `$$' is a literal `$' in make tmpv = v . replace ( '$$' , '' ) if "$" in tmpv : notdone [ n ] = v else : try : v = int ( v ) except ValueError : # insert literal `$' done [ n ] = v . replace ( '$$' , '$' ) else : done [ n ] = v # do variable interpolation here while notdone : for name in notdone . keys ( ) : value = notdone [ name ] m = _findvar1_rx . search ( value ) or _findvar2_rx . search ( value ) if m : n = m . group ( 1 ) found = True if n in done : item = str ( done [ n ] ) elif n in notdone : # get it on a subsequent round found = False elif n in os . environ : # do it like make: fall back to environment item = os . environ [ n ] else : done [ n ] = item = "" if found : after = value [ m . end ( ) : ] value = value [ : m . start ( ) ] + item + after if "$" in after : notdone [ name ] = value else : try : value = int ( value ) except ValueError : done [ name ] = value . strip ( ) else : done [ name ] = value del notdone [ name ] else : # bogus variable reference; just drop it since we can't deal del notdone [ name ] # strip spurious spaces for k , v in done . items ( ) : if isinstance ( v , str ) : done [ k ] = v . strip ( ) # save the results in the global dictionary vars . update ( done ) return vars