Index: infra/bots/win_toolchain_utils.py |
diff --git a/infra/bots/win_toolchain_utils.py b/infra/bots/win_toolchain_utils.py |
new file mode 100644 |
index 0000000000000000000000000000000000000000..43f62863cde3de8a457830d6339c50bc8bc47e4e |
--- /dev/null |
+++ b/infra/bots/win_toolchain_utils.py |
@@ -0,0 +1,53 @@ |
+#!/usr/bin/env python |
+# |
+# Copyright 2016 Google Inc. |
+# |
+# Use of this source code is governed by a BSD-style license that can be |
+# found in the LICENSE file. |
+ |
+ |
+"""Utilities for manipulating the win_toolchain.json file.""" |
+ |
+ |
+import json |
+ |
+ |
+PLACEHOLDER = '<(TOOLCHAIN_BASE_DIR)' |
+ |
+ |
+def _replace_prefix(val, before, after): |
+ """Replace the given prefix with the given string.""" |
+ if val.startswith(before): |
+ return val.replace(before, after, 1) |
+ return val |
+ |
+ |
+def _replace(val, before, after): |
+ """Replace occurrences of one string with another within the data.""" |
+ if isinstance(val, basestring): |
+ return _replace_prefix(val, before, after) |
+ elif isinstance(val, (list, tuple)): |
+ return [_replace(elem, before, after) for elem in val] |
+ elif isinstance(val, dict): |
+ return {_replace(k, before, after): |
+ _replace(v, before, after) for k, v in val.iteritems()} |
+ raise Exception('Cannot replace variable: %s' % val) |
+ |
+ |
+def _replace_in_file(filename, before, after): |
+ """Replace occurrences of one string with another within the file.""" |
+ with open(filename) as f: |
+ contents = json.load(f) |
+ new_contents = _replace(contents, before, after) |
+ with open(filename, 'w') as f: |
+ json.dump(new_contents, f) |
+ |
+ |
+def abstract(win_toolchain_json, old_path): |
+ """Replace absolute paths in win_toolchain.json with placeholders.""" |
+ _replace_in_file(win_toolchain_json, old_path, PLACEHOLDER) |
+ |
+ |
+def resolve(win_toolchain_json, new_path): |
+ """Replace placeholders in win_toolchain.json with absolute paths.""" |
+ _replace_in_file(win_toolchain_json, PLACEHOLDER, new_path) |