| 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 | |
| 12 import re | 11 import re |
| 13 import sys | 12 import sys |
| 14 | 13 |
| 15 from ast import literal_eval | 14 from ast import literal_eval |
| 16 from optparse import OptionParser | 15 from optparse import OptionParser |
| 17 from xml.dom import minidom | 16 from xml.dom import minidom |
| 18 | 17 |
| 19 import print_style | 18 from diffutil import PromptUserToAcceptDiff |
| 20 | 19 from pretty_print import PrettyPrintNode |
| 21 # Import the metrics/common module. | |
| 22 sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'common')) | |
| 23 from diff_util import PromptUserToAcceptDiff | |
| 24 | 20 |
| 25 HISTOGRAMS_PATH = 'histograms.xml' | 21 HISTOGRAMS_PATH = 'histograms.xml' |
| 26 POLICY_TEMPLATES_PATH = ( | 22 POLICY_TEMPLATES_PATH = |
| 27 '../../../components/policy/resources/policy_templates.json') | 23 '../../../components/policy/resources/policy_templates.json' |
| 28 ENUM_NAME = 'EnterprisePolicies' | 24 ENUM_NAME = 'EnterprisePolicies' |
| 29 | 25 |
| 30 class UserError(Exception): | 26 class UserError(Exception): |
| 31 def __init__(self, message): | 27 def __init__(self, message): |
| 32 Exception.__init__(self, message) | 28 Exception.__init__(self, message) |
| 33 | 29 |
| 34 @property | 30 @property |
| 35 def message(self): | 31 def message(self): |
| 36 return self.args[0] | 32 return self.args[0] |
| 37 | 33 |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 sys.exit(1) | 111 sys.exit(1) |
| 116 | 112 |
| 117 with open(POLICY_TEMPLATES_PATH, 'rb') as f: | 113 with open(POLICY_TEMPLATES_PATH, 'rb') as f: |
| 118 policy_templates = literal_eval(f.read()) | 114 policy_templates = literal_eval(f.read()) |
| 119 with open(HISTOGRAMS_PATH, 'rb') as f: | 115 with open(HISTOGRAMS_PATH, 'rb') as f: |
| 120 histograms_doc = minidom.parse(f) | 116 histograms_doc = minidom.parse(f) |
| 121 f.seek(0) | 117 f.seek(0) |
| 122 xml = f.read() | 118 xml = f.read() |
| 123 | 119 |
| 124 UpdateHistogramDefinitions(policy_templates, histograms_doc) | 120 UpdateHistogramDefinitions(policy_templates, histograms_doc) |
| 125 new_xml = print_style.GetPrintStyle().PrettyPrintNode(histograms_doc) | 121 |
| 122 new_xml = PrettyPrintNode(histograms_doc) |
| 126 if PromptUserToAcceptDiff(xml, new_xml, 'Is the updated version acceptable?'): | 123 if PromptUserToAcceptDiff(xml, new_xml, 'Is the updated version acceptable?'): |
| 127 with open(HISTOGRAMS_PATH, 'wb') as f: | 124 with open(HISTOGRAMS_PATH, 'wb') as f: |
| 128 f.write(new_xml) | 125 f.write(new_xml) |
| 129 | 126 |
| 130 | 127 |
| 131 if __name__ == '__main__': | 128 if __name__ == '__main__': |
| 132 try: | 129 try: |
| 133 main() | 130 main() |
| 134 except UserError as e: | 131 except UserError as e: |
| 135 print >>sys.stderr, e.message | 132 print >>sys.stderr, e.message |
| 136 sys.exit(1) | 133 sys.exit(1) |
| OLD | NEW |