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 e
(s, vars)
| 756 | |
| 757 | |
| 758 | def expand_makefile_vars(s, vars): |
| 759 | """Expand Makefile-style variables -- "${foo}" or "$(foo)" -- in |
| 760 | 'string' according to 'vars' (a dictionary mapping variable names to |
| 761 | values). Variables not present in 'vars' are silently expanded to the |
| 762 | empty string. The variable values in 'vars' should not contain further |
| 763 | variable expansions; if 'vars' is the output of 'parse_makefile()', |
| 764 | you're fine. Returns a variable-expanded version of 's'. |
| 765 | """ |
| 766 | |
| 767 | import warnings |
| 768 | warnings.warn( |
| 769 | 'sysconfig.expand_makefile_vars is deprecated and will be removed in ' |
| 770 | 'Python 3.16. Use sysconfig.get_paths(vars=...) instead.', |
| 771 | DeprecationWarning, |
| 772 | stacklevel=2, |
| 773 | ) |
| 774 | |
| 775 | import re |
| 776 | |
| 777 | _findvar1_rx = r"\$\(([A-Za-z][A-Za-z0-9_]*)\)" |
| 778 | _findvar2_rx = r"\${([A-Za-z][A-Za-z0-9_]*)}" |
| 779 | |
| 780 | # This algorithm does multiple expansion, so if vars['foo'] contains |
| 781 | # "${bar}", it will expand ${foo} to ${bar}, and then expand |
| 782 | # ${bar}... and so forth. This is fine as long as 'vars' comes from |
| 783 | # 'parse_makefile()', which takes care of such expansions eagerly, |
| 784 | # according to make's variable expansion semantics. |
| 785 | |
| 786 | while True: |
| 787 | m = re.search(_findvar1_rx, s) or re.search(_findvar2_rx, s) |
| 788 | if m: |
| 789 | (beg, end) = m.span() |
| 790 | s = s[0:beg] + vars.get(m.group(1)) + s[end:] |
| 791 | else: |
| 792 | break |
| 793 | return s |