| OLD | NEW |
| 1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 """Updates EnterprisePolicies enum in histograms.xml file with policy | 5 """Updates EnterprisePolicies enum in histograms.xml file with policy |
| 6 definitions read from policy_templates.json. | 6 definitions read from policy_templates.json. |
| 7 | 7 |
| 8 If the file was pretty-printed, the updated version is pretty-printed too. | 8 If the file was pretty-printed, the updated version is pretty-printed too. |
| 9 """ | 9 """ |
| 10 | 10 |
| 11 import os | 11 import os |
| 12 import re | 12 import re |
| 13 import sys | 13 import sys |
| 14 | 14 |
| 15 from ast import literal_eval | 15 from ast import literal_eval |
| 16 from optparse import OptionParser | 16 from optparse import OptionParser |
| 17 from xml.dom import minidom | 17 from xml.dom import minidom |
| 18 | 18 |
| 19 import print_style | 19 sys.path.append(os.path.join(os.path.dirname(__file__), '..')) # tools/metrics |
| 20 from common.diff_util import PromptUserToAcceptDiff |
| 21 from common import path_util |
| 22 from histograms import print_style |
| 20 | 23 |
| 21 # Import the metrics/common module. | 24 HISTOGRAMS_PATH = path_util.GetHistogramsFile() |
| 22 sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'common')) | 25 POLICY_TEMPLATES_PATH = 'components/policy/resources/policy_templates.json' |
| 23 from diff_util import PromptUserToAcceptDiff | |
| 24 | |
| 25 HISTOGRAMS_PATH = 'histograms.xml' | |
| 26 POLICY_TEMPLATES_PATH = ( | |
| 27 '../../../components/policy/resources/policy_templates.json') | |
| 28 ENUM_NAME = 'EnterprisePolicies' | 26 ENUM_NAME = 'EnterprisePolicies' |
| 29 | 27 |
| 30 class UserError(Exception): | 28 class UserError(Exception): |
| 31 def __init__(self, message): | 29 def __init__(self, message): |
| 32 Exception.__init__(self, message) | 30 Exception.__init__(self, message) |
| 33 | 31 |
| 34 @property | 32 @property |
| 35 def message(self): | 33 def message(self): |
| 36 return self.args[0] | 34 return self.args[0] |
| 37 | 35 |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 node.attributes['label'] = ParsePlaceholders(policy['caption']) | 105 node.attributes['label'] = ParsePlaceholders(policy['caption']) |
| 108 policy_enum_node.appendChild(node) | 106 policy_enum_node.appendChild(node) |
| 109 | 107 |
| 110 | 108 |
| 111 def main(): | 109 def main(): |
| 112 if len(sys.argv) > 1: | 110 if len(sys.argv) > 1: |
| 113 print >>sys.stderr, 'No arguments expected!' | 111 print >>sys.stderr, 'No arguments expected!' |
| 114 sys.stderr.write(__doc__) | 112 sys.stderr.write(__doc__) |
| 115 sys.exit(1) | 113 sys.exit(1) |
| 116 | 114 |
| 117 with open(POLICY_TEMPLATES_PATH, 'rb') as f: | 115 with open(path_util.GetInputFile(POLICY_TEMPLATES_PATH), 'rb') as f: |
| 118 policy_templates = literal_eval(f.read()) | 116 policy_templates = literal_eval(f.read()) |
| 119 with open(HISTOGRAMS_PATH, 'rb') as f: | 117 with open(HISTOGRAMS_PATH, 'rb') as f: |
| 120 histograms_doc = minidom.parse(f) | 118 histograms_doc = minidom.parse(f) |
| 121 f.seek(0) | 119 f.seek(0) |
| 122 xml = f.read() | 120 xml = f.read() |
| 123 | 121 |
| 124 UpdateHistogramDefinitions(policy_templates, histograms_doc) | 122 UpdateHistogramDefinitions(policy_templates, histograms_doc) |
| 125 new_xml = print_style.GetPrintStyle().PrettyPrintNode(histograms_doc) | 123 new_xml = print_style.GetPrintStyle().PrettyPrintNode(histograms_doc) |
| 126 if PromptUserToAcceptDiff(xml, new_xml, 'Is the updated version acceptable?'): | 124 if PromptUserToAcceptDiff(xml, new_xml, 'Is the updated version acceptable?'): |
| 127 with open(HISTOGRAMS_PATH, 'wb') as f: | 125 with open(HISTOGRAMS_PATH, 'wb') as f: |
| 128 f.write(new_xml) | 126 f.write(new_xml) |
| 129 | 127 |
| 130 | 128 |
| 131 if __name__ == '__main__': | 129 if __name__ == '__main__': |
| 132 try: | 130 try: |
| 133 main() | 131 main() |
| 134 except UserError as e: | 132 except UserError as e: |
| 135 print >>sys.stderr, e.message | 133 print >>sys.stderr, e.message |
| 136 sys.exit(1) | 134 sys.exit(1) |
| OLD | NEW |