+# a dict with some useful additional getters which can convert types and handle one-element lists like their single member
+class ConfigDict(dict):
+ def getstr(self, name, default = None):
+ if not name in self: return default
+ val = self[name]
+ if isinstance(val, list):
+ if len(val) != 1: raise Exception('%s is a list, but it should not' % name)
+ return val[0]
+ else:
+ return val
+
+ def getint(self, name, default = None):
+ return int(self.getstr(name, default))
+
+ def getbool(self, name, default = None):
+ val = self.getstr(name, default)
+ if isinstance(val, bool): return val # already a bool
+ return val.lower() in ('true', 'yes', 'on', '1')
+
+# create a safe-to-call shell command from the array
+def safeCall(*args):
+ res = ""
+ for arg in args:
+ assert arg.find("'") < 0 # ' is not supported
+ if len(res): res += " "
+ res += "'"+arg+"'"
+ return res
+
+# Load a section-less config file: maps parameter names to space-separated lists of strings (with shell quotation)
+# Lines starting with spaces are continuation lines
+def loadConfigFile(file):
+ # read config file
+ linenr = 0
+ with open(file) as file:
+ result = ConfigDict()
+ curKey = None
+ for line in file:
+ linenr += 1
+ isCont = len(line) and line[0].isspace() # remember if we were a continuation line
+ if isCont and curKey is None:
+ raise Exception("Invalid config, line %d: Starting with continuation line" % linenr)
+ line = line.strip()
+ if not len(line) or line.startswith("#"): continue # skip empty and comment lines
+ try:
+ if isCont:
+ # continuation line
+ result[curKey] += shlex.split(line)
+ else:
+ # option line
+ pos = line.index("=") # will raise exception when substring is not found
+ curKey = line[:pos].strip()
+ result[curKey] = shlex.split(line[pos+1:]) # shlex.split also strips
+ except Exception:
+ raise Exception("Invalid config, line %d: Error parsing line (quoting issue?)" % linenr)
+ # add some convencience get functions
+ return result
+
+# representation of a build system
+class BuildSystem:
+ def __init__(self, ruleMaker, buildDepends = [], binaryDepends = []):
+ self.ruleMaker = ruleMaker
+ self.buildDepends = buildDepends
+ self.binaryDepends = binaryDepends
+