Chromium Code Reviews| Index: build/config/mac/gen_plist.py |
| diff --git a/build/config/mac/gen_plist.py b/build/config/mac/gen_plist.py |
| index 0004179505ee6e63bc5988c65b4aeba58f3d1b93..83d834655553980905bf7d7429b2a06109fcd83c 100644 |
| --- a/build/config/mac/gen_plist.py |
| +++ b/build/config/mac/gen_plist.py |
| @@ -2,6 +2,8 @@ |
| # Use of this source code is governed by a BSD-style license that can be |
| # found in the LICENSE file. |
| +from __future__ import print_function |
| + |
| import argparse |
| import plistlib |
| import os |
| @@ -21,6 +23,12 @@ SUBST_RE = re.compile(r'\$\{(?P<id>[^}]*?)(?P<modifier>:[^}]*)?\}') |
| IDENT_RE = re.compile(r'[_/\s]') |
| +class SubstitutionError(Exception): |
| + def __init__(self, key): |
| + super(SubstitutionError, self).__init__() |
| + self.key = key |
| + |
| + |
| class ArgumentParser(argparse.ArgumentParser): |
| """Subclass of argparse.ArgumentParser to work with GN response files. |
| @@ -34,27 +42,6 @@ class ArgumentParser(argparse.ArgumentParser): |
| return shlex.split(arg_line) |
| -def InterpolateList(values, substitutions): |
| - """Interpolates variable references into |value| using |substitutions|. |
| - |
| - Inputs: |
| - values: a list of values |
| - substitutions: a mapping of variable names to values |
| - |
| - Returns: |
| - A new list of values with all variables references ${VARIABLE} replaced |
| - by their value in |substitutions| or None if any of the variable has no |
| - subsitution. |
| - """ |
| - result = [] |
| - for value in values: |
| - interpolated = InterpolateValue(value, substitutions) |
| - if interpolated is None: |
| - return None |
| - result.append(interpolated) |
| - return result |
| - |
| - |
| def InterpolateString(value, substitutions): |
| """Interpolates variable references into |value| using |substitutions|. |
| @@ -64,29 +51,28 @@ def InterpolateString(value, substitutions): |
| Returns: |
| A new string with all variables references ${VARIABLES} replaced by their |
| - value in |substitutions| or None if any of the variable has no substitution. |
| + value in |substitutions|. Raises SubstitutionError if a variable has no |
| + substitution. |
| """ |
| - result = value |
| - for match in reversed(list(SUBST_RE.finditer(value))): |
| + def repl(match): |
| variable = match.group('id') |
| if variable not in substitutions: |
| - return None |
| + raise SubstitutionError(variable) |
| # Some values need to be identifier and thus the variables references may |
| # contains :modifier attributes to indicate how they should be converted |
| # to identifiers ("identifier" replaces all invalid characters by '_' and |
| # "rfc1034identifier" replaces them by "-" to make valid URI too). |
| modifier = match.group('modifier') |
| if modifier == ':identifier': |
| - interpolated = IDENT_RE.sub('_', substitutions[variable]) |
| + return IDENT_RE.sub('_', substitutions[variable]) |
| elif modifier == ':rfc1034identifier': |
| - interpolated = IDENT_RE.sub('-', substitutions[variable]) |
| + return IDENT_RE.sub('-', substitutions[variable]) |
| else: |
| - interpolated = substitutions[variable] |
| - result = result[:match.start()] + interpolated + result[match.end():] |
| - return result |
| + return substitutions[variable] |
| + return SUBST_RE.sub(repl, value) |
| -def InterpolateValue(value, substitutions): |
| +def Interpolate(value, substitutions): |
| """Interpolates variable references into |value| using |substitutions|. |
| Inputs: |
| @@ -95,38 +81,18 @@ def InterpolateValue(value, substitutions): |
| Returns: |
| A new value with all variables references ${VARIABLES} replaced by their |
| - value in |substitutions| or None if any of the variable has no substitution. |
| + value in |substitutions|. Raises SubstitutionError if a variable has no |
| + substitution. |
| """ |
| if isinstance(value, dict): |
| - return Interpolate(value, substitutions) |
| + return {k: Interpolate(v, substitutions) for k, v in value.iteritems()} |
| if isinstance(value, list): |
| - return InterpolateList(value, substitutions) |
| + return [Interpolate(v, substitutions) for v in value] |
| if isinstance(value, str): |
| return InterpolateString(value, substitutions) |
| return value |
| -def Interpolate(plist, substitutions): |
| - """Interpolates variable references into |value| using |substitutions|. |
| - |
| - Inputs: |
| - plist: a dictionary representing a Property List (.plist) file |
| - substitutions: a mapping of variable names to values |
| - |
| - Returns: |
| - A new plist with all variables references ${VARIABLES} replaced by their |
| - value in |substitutions|. All values that contains references with no |
| - substitutions will be removed and the corresponding key will be cleared |
| - from the plist (not recursively). |
| - """ |
| - result = {} |
| - for key in plist: |
| - value = InterpolateValue(plist[key], substitutions) |
| - if value is not None: |
| - result[key] = value |
| - return result |
| - |
| - |
| def LoadPList(path): |
| """Loads Plist at |path| and returns it as a dictionary.""" |
| fd, name = tempfile.mkstemp() |
| @@ -165,19 +131,12 @@ def MergePList(plist1, plist2): |
| |plist1| with |plist2|. If any value is a dictionary, they are merged |
| recursively, otherwise |plist2| value is used. |
| """ |
| - if not isinstance(plist1, dict) or not isinstance(plist2, dict): |
|
Robert Sesek
2016/09/23 20:55:55
Was this condition never hit?
Sidney San Martín
2016/09/26 18:38:25
It could get hit in the old code (:180 can call Me
|
| - if plist2 is not None: |
| - return plist2 |
| - else: |
| - return plist1 |
| - result = {} |
| - for key in set(plist1) | set(plist2): |
| - if key in plist2: |
| - value = plist2[key] |
| - else: |
| - value = plist1[key] |
| + result = plist1.copy() |
| + for key, value in plist2.iteritems(): |
| if isinstance(value, dict): |
| - value = MergePList(plist1.get(key, None), plist2.get(key, None)) |
| + old_value = result.get(key) |
| + if isinstance(old_value, dict): |
| + value = MergePList(old_value, value) |
| result[key] = value |
| return result |
| @@ -200,8 +159,14 @@ def main(): |
| substitutions[key] = value |
| data = {} |
| for filename in args.path: |
| - data = MergePList(data, LoadPList(filename)) |
| - data = Interpolate(data, substitutions) |
| + try: |
| + plist = LoadPList(filename) |
| + data = MergePList(data, Interpolate(plist, substitutions)) |
| + except SubstitutionError as e: |
| + print("SubstitutionError: No substitution found for '{0}' in {1}" |
|
Robert Sesek
2016/09/23 20:55:55
More idiomatic Chromium style is:
print >>sys.s
Sidney San Martín
2016/09/26 18:38:25
Done.
|
| + .format(e.key, filename), file=sys.stderr) |
| + return 1 |
| + |
| SavePList(args.output, args.format, data) |
| return 0 |