Update of /cvsroot/python/distutils/distutils In directory slayer.i.sourceforge.net:/tmp/cvs-serv8403 Modified Files: sysconfig.py Log Message: 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()'. Index: sysconfig.py =================================================================== RCS file: /cvsroot/python/distutils/distutils/sysconfig.py,v retrieving revision 1.27 retrieving revision 1.28 diff -C2 -r1.27 -r1.28 *** sysconfig.py 2000/09/15 01:15:08 1.27 --- sysconfig.py 2000/09/17 00:53:02 1.28 *************** *** 158,161 **** --- 158,168 ---- 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. *************** *** 167,175 **** """ from distutils.text_file import TextFile ! fp = TextFile(fn, strip_comments=1, join_lines=1) if g is None: g = {} - variable_rx = re.compile("([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)") done = {} notdone = {} --- 174,181 ---- """ 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 = {} *************** *** 177,183 **** while 1: line = fp.readline() ! if line is None: break ! m = variable_rx.match(line) if m: n, v = m.group(1, 2) --- 183,189 ---- while 1: line = fp.readline() ! if line is None: # eof break ! m = _variable_rx.match(line) if m: n, v = m.group(1, 2) *************** *** 191,202 **** # 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) --- 197,204 ---- # 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) *************** *** 229,235 **** --- 231,265 ---- 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
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4