| 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 sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'common')) |
| 20 from diff_util import PromptUserToAcceptDiff |
| 21 import path_util |
| 22 |
| 19 import print_style | 23 import print_style |
| 20 | 24 |
| 21 # Import the metrics/common module. | 25 HISTOGRAMS_PATH = path_util.GetHistogramsFile() |
| 22 sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'common')) | 26 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' | 27 ENUM_NAME = 'EnterprisePolicies' |
| 29 | 28 |
| 30 class UserError(Exception): | 29 class UserError(Exception): |
| 31 def __init__(self, message): | 30 def __init__(self, message): |
| 32 Exception.__init__(self, message) | 31 Exception.__init__(self, message) |
| 33 | 32 |
| 34 @property | 33 @property |
| 35 def message(self): | 34 def message(self): |
| 36 return self.args[0] | 35 return self.args[0] |
| 37 | 36 |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 node.attributes['label'] = ParsePlaceholders(policy['caption']) | 106 node.attributes['label'] = ParsePlaceholders(policy['caption']) |
| 108 policy_enum_node.appendChild(node) | 107 policy_enum_node.appendChild(node) |
| 109 | 108 |
| 110 | 109 |
| 111 def main(): | 110 def main(): |
| 112 if len(sys.argv) > 1: | 111 if len(sys.argv) > 1: |
| 113 print >>sys.stderr, 'No arguments expected!' | 112 print >>sys.stderr, 'No arguments expected!' |
| 114 sys.stderr.write(__doc__) | 113 sys.stderr.write(__doc__) |
| 115 sys.exit(1) | 114 sys.exit(1) |
| 116 | 115 |
| 117 with open(POLICY_TEMPLATES_PATH, 'rb') as f: | 116 with open(path_util.GetInputFile(POLICY_TEMPLATES_PATH), 'rb') as f: |
| 118 policy_templates = literal_eval(f.read()) | 117 policy_templates = literal_eval(f.read()) |
| 119 with open(HISTOGRAMS_PATH, 'rb') as f: | 118 with open(HISTOGRAMS_PATH, 'rb') as f: |
| 120 histograms_doc = minidom.parse(f) | 119 histograms_doc = minidom.parse(f) |
| 121 f.seek(0) | 120 f.seek(0) |
| 122 xml = f.read() | 121 xml = f.read() |
| 123 | 122 |
| 124 UpdateHistogramDefinitions(policy_templates, histograms_doc) | 123 UpdateHistogramDefinitions(policy_templates, histograms_doc) |
| 125 new_xml = print_style.GetPrintStyle().PrettyPrintNode(histograms_doc) | 124 new_xml = print_style.GetPrintStyle().PrettyPrintNode(histograms_doc) |
| 126 if PromptUserToAcceptDiff(xml, new_xml, 'Is the updated version acceptable?'): | 125 if PromptUserToAcceptDiff(xml, new_xml, 'Is the updated version acceptable?'): |
| 127 with open(HISTOGRAMS_PATH, 'wb') as f: | 126 with open(HISTOGRAMS_PATH, 'wb') as f: |
| 128 f.write(new_xml) | 127 f.write(new_xml) |
| 129 | 128 |
| 130 | 129 |
| 131 if __name__ == '__main__': | 130 if __name__ == '__main__': |
| 132 try: | 131 try: |
| 133 main() | 132 main() |
| 134 except UserError as e: | 133 except UserError as e: |
| 135 print >>sys.stderr, e.message | 134 print >>sys.stderr, e.message |
| 136 sys.exit(1) | 135 sys.exit(1) |
| OLD | NEW |