| 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 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 policy_templates.json file. | 44 policy_templates.json file. |
| 45 policy_list: A list that has policy definitions appended to it. | 45 policy_list: A list that has policy definitions appended to it. |
| 46 """ | 46 """ |
| 47 for policy in policy_definitions: | 47 for policy in policy_definitions: |
| 48 if policy['type'] == 'group': | 48 if policy['type'] == 'group': |
| 49 FlattenPolicies(policy['policies'], policy_list) | 49 FlattenPolicies(policy['policies'], policy_list) |
| 50 else: | 50 else: |
| 51 policy_list.append(policy) | 51 policy_list.append(policy) |
| 52 | 52 |
| 53 | 53 |
| 54 def ParsePlaceholders(text): | |
| 55 """Parse placeholders in |text|, making it more human-readable. The format of | |
| 56 |text| is exactly the same as in captions in policy_templates.json: it can | |
| 57 contain XML tags (ph, ex) and $1-like substitutions. Note that this function | |
| 58 does only a very simple parsing that is not fully correct, but should be | |
| 59 enough for all practical situations. | |
| 60 | |
| 61 Args: | |
| 62 text: A string containing placeholders. | |
| 63 | |
| 64 Returns: | |
| 65 |text| with placeholders removed or replaced by readable text. | |
| 66 """ | |
| 67 text = re.sub(r'\$\d+', '', text) # Remove $1-like substitutions. | |
| 68 text = re.sub(r'<[^>]+>', '', text) # Remove XML tags. | |
| 69 return text | |
| 70 | |
| 71 | |
| 72 def UpdateHistogramDefinitions(policy_templates, doc): | 54 def UpdateHistogramDefinitions(policy_templates, doc): |
| 73 """Sets the children of <enum name="EnterprisePolicies" ...> node in |doc| to | 55 """Sets the children of <enum name="EnterprisePolicies" ...> node in |doc| to |
| 74 values generated from policy ids contained in |policy_templates|. | 56 values generated from policy ids contained in |policy_templates|. |
| 75 | 57 |
| 76 Args: | 58 Args: |
| 77 policy_templates: A list of dictionaries, defining policies or policy | 59 policy_templates: A list of dictionaries, defining policies or policy |
| 78 groups. The format is exactly the same as in | 60 groups. The format is exactly the same as in |
| 79 policy_templates.json file. | 61 policy_templates.json file. |
| 80 doc: A minidom.Document object representing parsed histogram definitions | 62 doc: A minidom.Document object representing parsed histogram definitions |
| 81 XML file. | 63 XML file. |
| (...skipping 14 matching lines...) Expand all Loading... |
| 96 comment = ' Generated from {0} '.format(POLICY_TEMPLATES_PATH) | 78 comment = ' Generated from {0} '.format(POLICY_TEMPLATES_PATH) |
| 97 policy_enum_node.appendChild(doc.createComment(comment)) | 79 policy_enum_node.appendChild(doc.createComment(comment)) |
| 98 | 80 |
| 99 # Add values generated from policy templates. | 81 # Add values generated from policy templates. |
| 100 ordered_policies = [] | 82 ordered_policies = [] |
| 101 FlattenPolicies(policy_templates['policy_definitions'], ordered_policies) | 83 FlattenPolicies(policy_templates['policy_definitions'], ordered_policies) |
| 102 ordered_policies.sort(key=lambda policy: policy['id']) | 84 ordered_policies.sort(key=lambda policy: policy['id']) |
| 103 for policy in ordered_policies: | 85 for policy in ordered_policies: |
| 104 node = doc.createElement('int') | 86 node = doc.createElement('int') |
| 105 node.attributes['value'] = str(policy['id']) | 87 node.attributes['value'] = str(policy['id']) |
| 106 node.attributes['label'] = ParsePlaceholders(policy['caption']) | 88 node.attributes['label'] = policy['name'] |
| 107 policy_enum_node.appendChild(node) | 89 policy_enum_node.appendChild(node) |
| 108 | 90 |
| 109 | 91 |
| 110 def main(): | 92 def main(): |
| 111 if len(sys.argv) > 1: | 93 if len(sys.argv) > 1: |
| 112 print >>sys.stderr, 'No arguments expected!' | 94 print >>sys.stderr, 'No arguments expected!' |
| 113 sys.stderr.write(__doc__) | 95 sys.stderr.write(__doc__) |
| 114 sys.exit(1) | 96 sys.exit(1) |
| 115 | 97 |
| 116 with open(path_util.GetInputFile(POLICY_TEMPLATES_PATH), 'rb') as f: | 98 with open(path_util.GetInputFile(POLICY_TEMPLATES_PATH), 'rb') as f: |
| 117 policy_templates = literal_eval(f.read()) | 99 policy_templates = literal_eval(f.read()) |
| 118 with open(HISTOGRAMS_PATH, 'rb') as f: | 100 with open(HISTOGRAMS_PATH, 'rb') as f: |
| 119 histograms_doc = minidom.parse(f) | 101 histograms_doc = minidom.parse(f) |
| 120 f.seek(0) | 102 f.seek(0) |
| 121 xml = f.read() | 103 xml = f.read() |
| 122 | 104 |
| 123 UpdateHistogramDefinitions(policy_templates, histograms_doc) | 105 UpdateHistogramDefinitions(policy_templates, histograms_doc) |
| 124 new_xml = print_style.GetPrintStyle().PrettyPrintNode(histograms_doc) | 106 new_xml = print_style.GetPrintStyle().PrettyPrintNode(histograms_doc) |
| 125 if PromptUserToAcceptDiff(xml, new_xml, 'Is the updated version acceptable?'): | 107 if PromptUserToAcceptDiff(xml, new_xml, 'Is the updated version acceptable?'): |
| 126 with open(HISTOGRAMS_PATH, 'wb') as f: | 108 with open(HISTOGRAMS_PATH, 'wb') as f: |
| 127 f.write(new_xml) | 109 f.write(new_xml) |
| 128 | 110 |
| 129 | 111 |
| 130 if __name__ == '__main__': | 112 if __name__ == '__main__': |
| 131 try: | 113 try: |
| 132 main() | 114 main() |
| 133 except UserError as e: | 115 except UserError as e: |
| 134 print >>sys.stderr, e.message | 116 print >>sys.stderr, e.message |
| 135 sys.exit(1) | 117 sys.exit(1) |
| OLD | NEW |