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