| 1 |
from io import open |
| 2 |
from os.path import isdir |
| 3 |
import re |
| 4 |
|
| 5 |
def filterLines(lines, regex): |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
matcher = re.compile(regex) |
| 11 |
for line in lines: |
| 12 |
if line.endswith('\n'): |
| 13 |
line = line[ : -1] |
| 14 |
match = matcher.match(line) |
| 15 |
if match: |
| 16 |
yield match.groups() |
| 17 |
|
| 18 |
def filterFile(filePath, regex): |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
with open(filePath, 'r', encoding='utf-8') as inp: |
| 24 |
for groups in filterLines(inp, regex): |
| 25 |
yield groups |
| 26 |
|
| 27 |
def joinContinuedLines(lines): |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
buf = '' |
| 32 |
for line in lines: |
| 33 |
if line.endswith('\\\n'): |
| 34 |
buf += line[ : -2] |
| 35 |
elif line.endswith('\\'): |
| 36 |
buf += line[ : -1] |
| 37 |
else: |
| 38 |
yield buf + line |
| 39 |
buf = '' |
| 40 |
if buf: |
| 41 |
raise ValueError('Continuation on last line') |
| 42 |
|
| 43 |
_reEval = re.compile(r'(\$\(|\))') |
| 44 |
def evalMakeExpr(expr, makeVars): |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
stack = [ [] ] |
| 50 |
for part in _reEval.split(expr): |
| 51 |
if part == '$(': |
| 52 |
stack.append([]) |
| 53 |
elif part == ')' and len(stack) != 1: |
| 54 |
name = ''.join(stack.pop()) |
| 55 |
if name.startswith('addprefix '): |
| 56 |
prefix, args = name[len('addprefix') : ].split(',') |
| 57 |
prefix = prefix.strip() |
| 58 |
value = ' '.join(prefix + arg for arg in args.split()) |
| 59 |
elif name.startswith('addsuffix '): |
| 60 |
suffix, args = name[len('addsuffix') : ].split(',') |
| 61 |
suffix = suffix.strip() |
| 62 |
value = ' '.join(arg + suffix for arg in args.split()) |
| 63 |
elif name.startswith('shell '): |
| 64 |
|
| 65 |
value = '?' |
| 66 |
elif name.isdigit(): |
| 67 |
|
| 68 |
|
| 69 |
value = '?' |
| 70 |
else: |
| 71 |
value = makeVars[name] |
| 72 |
stack[-1].append(value) |
| 73 |
else: |
| 74 |
stack[-1].append(part) |
| 75 |
if len(stack) != 1: |
| 76 |
raise ValueError('Open without close in "%s"' % expr) |
| 77 |
return ''.join(stack.pop()) |
| 78 |
|
| 79 |
def extractMakeVariables(filePath, makeVars = None): |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
makeVars = {} if makeVars is None else dict(makeVars) |
| 87 |
with open(filePath, 'r', encoding='utf-8') as inp: |
| 88 |
for name, assign, value in filterLines( |
| 89 |
joinContinuedLines(inp), |
| 90 |
r'[ ]*([A-Za-z0-9_]+)[ ]*([+:]?=)(.*)' |
| 91 |
): |
| 92 |
if assign == '=': |
| 93 |
makeVars[name] = value.strip() |
| 94 |
elif assign == ':=': |
| 95 |
makeVars[name] = evalMakeExpr(value, makeVars).strip() |
| 96 |
elif assign == '+=': |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
makeVars[name] = makeVars[name] + ' ' + value.strip() |
| 101 |
else: |
| 102 |
assert False, assign |
| 103 |
return makeVars |
| 104 |
|
| 105 |
def parseBool(valueStr): |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
if valueStr == 'true': |
| 110 |
return True |
| 111 |
elif valueStr == 'false': |
| 112 |
return False |
| 113 |
else: |
| 114 |
raise ValueError('Invalid boolean "%s"' % valueStr) |