OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # |
| 3 # Copyright 2016 Google Inc. |
| 4 # |
| 5 # Use of this source code is governed by a BSD-style license that can be |
| 6 # found in the LICENSE file. |
| 7 |
| 8 |
| 9 """Utilities for manipulating the win_toolchain.json file.""" |
| 10 |
| 11 |
| 12 import json |
| 13 |
| 14 |
| 15 PLACEHOLDER = '<(TOOLCHAIN_BASE_DIR)' |
| 16 |
| 17 |
| 18 def _replace_prefix(val, before, after): |
| 19 """Replace the given prefix with the given string.""" |
| 20 if val.startswith(before): |
| 21 return val.replace(before, after, 1) |
| 22 return val |
| 23 |
| 24 |
| 25 def _replace(val, before, after): |
| 26 """Replace occurrences of one string with another within the data.""" |
| 27 if isinstance(val, basestring): |
| 28 return _replace_prefix(val, before, after) |
| 29 elif isinstance(val, (list, tuple)): |
| 30 return [_replace(elem, before, after) for elem in val] |
| 31 elif isinstance(val, dict): |
| 32 return {_replace(k, before, after): |
| 33 _replace(v, before, after) for k, v in val.iteritems()} |
| 34 raise Exception('Cannot replace variable: %s' % val) |
| 35 |
| 36 |
| 37 def _replace_in_file(filename, before, after): |
| 38 """Replace occurrences of one string with another within the file.""" |
| 39 with open(filename) as f: |
| 40 contents = json.load(f) |
| 41 new_contents = _replace(contents, before, after) |
| 42 with open(filename, 'w') as f: |
| 43 json.dump(new_contents, f) |
| 44 |
| 45 |
| 46 def abstract(win_toolchain_json, old_path): |
| 47 """Replace absolute paths in win_toolchain.json with placeholders.""" |
| 48 _replace_in_file(win_toolchain_json, old_path, PLACEHOLDER) |
| 49 |
| 50 |
| 51 def resolve(win_toolchain_json, new_path): |
| 52 """Replace placeholders in win_toolchain.json with absolute paths.""" |
| 53 _replace_in_file(win_toolchain_json, PLACEHOLDER, new_path) |
OLD | NEW |