(fn, g=None)
return g
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.
Parse a Makefile-style file.
Parse a Makefile - style file .
def parse_makefile(fn, g=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.
    """
    from distutils.text_file import TextFile
    fp = TextFile(fn, strip_comments=1, skip_blanks=1, join_lines=1)

    if g is None:
        g = {}
    done = {}
    notdone = {}

    while 1:
        line = fp.readline()
        if line is None:  # eof
            break
        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]

    fp.close()

    # strip spurious spaces
    for k, v in done.items():
        if isinstance(v, str):
            done[k] = v.strip()

    # save the results in the global dictionary
    g.update(done)
    return g
def parse_makefile ( fn , g = None ) : from distutils . text_file import TextFile fp = TextFile ( fn , strip_comments = 1 , skip_blanks = 1 , join_lines = 1 ) if g is None : g = { } done = { } notdone = { } while 1 : line = fp . readline ( ) if line is None : # eof break 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 ] fp . close ( ) # strip spurious spaces for k , v in done . items ( ) : if isinstance ( v , str ) : done [ k ] = v . strip ( ) # save the results in the global dictionary g . update ( done ) return g