diff options
author | Greg Ward <gward@python.net> | 2000-09-17 00:53:02 (GMT) |
---|---|---|
committer | Greg Ward <gward@python.net> | 2000-09-17 00:53:02 (GMT) |
commit | d283ce73641c59013ddcfd29064181dd2c96bfcb (patch) | |
tree | 92be4971603f1e9501e4024267d790171ba799ce | |
parent | 41ed12ffe9de3ddf387641d384a63edf95bfa322 (diff) | |
download | cpython-d283ce73641c59013ddcfd29064181dd2c96bfcb.zip cpython-d283ce73641c59013ddcfd29064181dd2c96bfcb.tar.gz cpython-d283ce73641c59013ddcfd29064181dd2c96bfcb.tar.bz2 |
Added 'expand_makefile_vars()' to (duh) expand make-style variables
in a string (gives you something to do with the dictionary returned
by 'parse_makefile()').
Pulled the regexes in 'parse_makefile()' out -- they're now globals,
as 'expand_makefile_vars()' needs (two of) them.
Cosmetic tweaks to 'parse_makefile()'.
-rw-r--r-- | Lib/distutils/sysconfig.py | 48 |
1 files changed, 39 insertions, 9 deletions
diff --git a/Lib/distutils/sysconfig.py b/Lib/distutils/sysconfig.py index de35d96..605e95d 100644 --- a/Lib/distutils/sysconfig.py +++ b/Lib/distutils/sysconfig.py @@ -157,6 +157,13 @@ def parse_config_h(fp, g=None): g[m.group(1)] = 0 return g + +# 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_]*)}") + def parse_makefile(fn, g=None): """Parse a Makefile-style file. @@ -166,19 +173,18 @@ def parse_makefile(fn, g=None): """ from distutils.text_file import TextFile - fp = TextFile(fn, strip_comments=1, join_lines=1) + fp = TextFile(fn, strip_comments=1, skip_blanks=1, join_lines=1) if g is None: g = {} - variable_rx = re.compile("([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)") done = {} notdone = {} while 1: line = fp.readline() - if line is None: + if line is None: # eof break - m = variable_rx.match(line) + m = _variable_rx.match(line) if m: n, v = m.group(1, 2) v = string.strip(v) @@ -190,14 +196,10 @@ def parse_makefile(fn, g=None): 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) + m = _findvar1_rx.search(value) or _findvar2_rx.search(value) if m: n = m.group(1) if done.has_key(n): @@ -228,11 +230,39 @@ def parse_makefile(fn, g=None): # bogus variable reference; just drop it since we can't deal del notdone[name] + fp.close() + # save the results in the global dictionary g.update(done) return g +def expand_makefile_vars(s, vars): + """Expand Makefile-style variables -- "${foo}" or "$(foo)" -- in + 'string' according to 'vars' (a dictionary mapping variable names to + values). Variables not present in 'vars' are silently expanded to the + empty string. The variable values in 'vars' should not contain further + variable expansions; if 'vars' is the output of 'parse_makefile()', + you're fine. Returns a variable-expanded version of 's'. + """ + + # This algorithm does multiple expansion, so if vars['foo'] contains + # "${bar}", it will expand ${foo} to ${bar}, and then expand + # ${bar}... and so forth. This is fine as long as 'vars' comes from + # 'parse_makefile()', which takes care of such expansions eagerly, + # according to make's variable expansion semantics. + + while 1: + m = _findvar1_rx.search(s) or _findvar2_rx.search(s) + if m: + name = m.group(1) + (beg, end) = m.span() + s = s[0:beg] + vars.get(m.group(1)) + s[end:] + else: + break + return s + + _config_vars = None def _init_posix(): |