"""Prototype sysconfig module that loads information when run as a script,
but only defines constants when imported.

This should be run as a script as one of the last steps of the Python
installation process.

Written by:   Fred L. Drake, Jr.
Email:        <fdrake@acm.org>
Initial date: 17-Dec-1998
"""

__version__ = "$Revision$"

import os
import re
import string
import sys


def get_config_h_filename():
    return os.path.join(sys.exec_prefix, "lib", "python" + sys.version[:3],
                        "config", "config.h")

def get_makefile_filename():
    return os.path.join(sys.exec_prefix, "lib", "python" + sys.version[:3],
                        "config", "Makefile")

def parse_config_h(fp, g=None):
    if g is None:
        g = {}
    define_rx = re.compile("#define ([A-Z][A-Z0-9_]+) (.*)\n")
    undef_rx = re.compile("/[*] #undef ([A-Z][A-Z0-9_]+) [*]/\n")
    #
    while 1:
        line = fp.readline()
        if not line:
            break
        m = define_rx.match(line)
        if m:
            n, v = m.group(1, 2)
            try: v = string.atoi(v)
            except ValueError: pass
            g[n] = v
        else:
            m = undef_rx.match(line)
            if m:
                g[m.group(1)] = 0
    return g

def parse_makefile(fp, g=None):
    if g is None:
        g = {}
    variable_rx = re.compile("([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)\n")
    done = {}
    notdone = {}
    #
    while 1:
        line = fp.readline()
        if not line:
            break
        m = variable_rx.match(line)
        if m:
            n, v = m.group(1, 2)
            v = string.strip(v)
            if "$" in v:
                notdone[n] = v
            else:
                try: v = string.atoi(v)
                except ValueError: pass
                done[n] = v

    # do variable interpolation here
    findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)")
    findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}")
    while notdone:
        for name in notdone.keys():
            value = notdone[name]
            m = findvar1_rx.search(value)
            if not m:
                m = findvar2_rx.search(value)
            if m:
                n = m.group(1)
                if done.has_key(n):
                    after = value[m.end():]
                    value = value[:m.start()] + done[n] + after
                    if "$" in after:
                        notdone[name] = value
                    else:
                        try: value = string.atoi(value)
                        except ValueError: pass
                        done[name] = string.strip(value)
                        del notdone[name]
                elif notdone.has_key(n):
                    # get it on a subsequent round
                    pass
                else:
                    done[n] = ""
                    after = value[m.end():]
                    value = value[:m.start()] + after
                    if "$" in after:
                        notdone[name] = value
                    else:
                        try: value = string.atoi(value)
                        except ValueError: pass
                        done[name] = string.strip(value)
                        del notdone[name]
            else:
                # bogus variable reference; just drop it since we can't deal
                del notdone[name]

    # save the results in the global dictionary
    g.update(done)
    return g


def _init_posix():
    g = globals()
    # load the installed config.h:
    parse_config_h(open(get_config_h_filename()), g)
    # load the installed Makefile.pre.in:
    parse_makefile(open(get_makefile_filename()), g)



try:
    exec "_init_" + os.name
except NameError:
    # not needed for this platform
    pass
else:
    exec "_init_%s()" % os.name

del _init_posix

@@ -3,210 +3,149 @@
 %22Pro
-totype sysconfig module that loads information when run as a script,%0Abut only defines constants when imported.%0A%0AThis should be run as a script as one of
+vide access to Python's configuration information.  The specific names%0Adefined in the module depend heavily on
  the 
+p
 la
-st steps of the Python%0Ainstallation process
+tform and configuration
 .%0A%0AW
@@ -334,32 +334,91 @@
 g_h_filename():%0A
+    %22%22%22Return full pathname of installed config.h file.%22%22%22%0A
     return os.pa
@@ -549,24 +549,100 @@
 filename():%0A
+    %22%22%22Return full pathname of installed Makefile from the Python build.%22%22%22%0A
     return o
@@ -776,32 +776,243 @@
 _h(fp, g=None):%0A
+    %22%22%22Parse a config.h-style file.  A dictionary containing name/value%0A    pairs is returned.  If an optional dictionary is passed in as the second%0A    argument, it is used instead of a new dictionary.%0A    %22%22%22%0A
     if g is None
@@ -1535,16 +1535,16 @@
 turn g%0A%0A
-
 def pars
@@ -1563,24 +1563,235 @@
 p, g=None):%0A
+    %22%22%22Parse a Makefile-style file.  A dictionary containing name/value%0A    pairs is returned.  If an optional dictionary is passed in as the second%0A    argument, it is used instead of a new dictionary.%0A    %22%22%22%0A
     if g is 
@@ -3970,24 +3970,90 @@
 it_posix():%0A
+    %22%22%22Initialize the module as appropriate for POSIX systems.%22%22%22%0A
     g = glob

Update and add docstrings.