File: update_policies.py

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (150 lines) | stat: -rw-r--r-- 4,963 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# Copyright 2013 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Updates EnterprisePolicies enum in histograms.xml file with policy
definitions read from policies.yaml.

If the file was pretty-printed, the updated version is pretty-printed too.
"""

from __future__ import print_function

import os
import re
import sys

from ast import literal_eval
from optparse import OptionParser
from xml.dom import minidom
sys.path.append(os.path.join(os.path.dirname(__file__), '../../../third_party'))
import pyyaml

sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'common'))
from diff_util import PromptUserToAcceptDiff
import path_util

import histogram_paths
import histogram_configuration_model

ENUMS_PATH = histogram_paths.ENUMS_XML
POLICY_LIST_PATH = 'components/policy/resources/templates/policies.yaml'
POLICIES_ENUM_NAME = 'EnterprisePolicies'
POLICY_ATOMIC_GROUPS_ENUM_NAME = 'PolicyAtomicGroups'

class UserError(Exception):
  def __init__(self, message):
    Exception.__init__(self, message)

  @property
  def message(self):
    return self.args[0]


def UpdatePoliciesHistogramDefinitions(policy_ids, doc):
  """Sets the children of <enum name="EnterprisePolicies" ...> node in |doc| to
  values generated from policy ids contained in |policy_templates|.

  Args:
    policy_ids: A dictionary mapping policy ids to their names.
    doc: A minidom.Document object representing parsed histogram definitions
         XML file.
  """
  # Find EnterprisePolicies enum.
  for enum_node in doc.getElementsByTagName('enum'):
    if enum_node.attributes['name'].value == POLICIES_ENUM_NAME:
      policy_enum_node = enum_node
      break
  else:
    raise UserError('No policy enum node found')

  # Remove existing values.
  while policy_enum_node.hasChildNodes():
    policy_enum_node.removeChild(policy_enum_node.lastChild)

  # Add a "Generated from (...)" comment
  comment = ' Generated from {0} '.format(POLICY_LIST_PATH)
  policy_enum_node.appendChild(doc.createComment(comment))

  # Add values generated from policy templates.
  ordered_policies = [{
      'id': id,
      'name': name
  } for id, name in policy_ids.items() if name]

  ordered_policies.sort(key=lambda policy: policy['id'])
  for policy in ordered_policies:
    node = doc.createElement('int')
    node.attributes['value'] = str(policy['id'])
    node.attributes['label'] = policy['name']
    policy_enum_node.appendChild(node)


def UpdateAtomicGroupsHistogramDefinitions(atomic_group_ids, doc):
  """Sets the children of <enum name="PolicyAtomicGroups" ...> node in |doc| to
  values generated from policy ids contained in |policy_templates|.

  Args:
    atomic_group_ids: A dictionary mapping atomic policy goupr ids to their
                      names.
    doc: A minidom.Document object representing parsed histogram definitions
         XML file.
  """
  # Find EnterprisePolicies enum.
  for enum_node in doc.getElementsByTagName('enum'):
    if enum_node.attributes['name'].value == POLICY_ATOMIC_GROUPS_ENUM_NAME:
      atomic_group_enum_node = enum_node
      break
  else:
    raise UserError('No policy atomic group enum node found')

  # Remove existing values.
  while atomic_group_enum_node.hasChildNodes():
    atomic_group_enum_node.removeChild(atomic_group_enum_node.lastChild)

  # Add a "Generated from (...)" comment
  comment = ' Generated from {0} '.format(POLICY_LIST_PATH)
  atomic_group_enum_node.appendChild(doc.createComment(comment))

  # Add values generated from policy templates.
  ordered_atomic_groups = [{
      'id': id,
      'name': name
  } for id, name in atomic_group_ids.items() if name]
  ordered_atomic_groups.sort(key=lambda group: group['id'])
  for group in ordered_atomic_groups:
    node = doc.createElement('int')
    node.attributes['value'] = str(group['id'])
    node.attributes['label'] = group['name']
    atomic_group_enum_node.appendChild(node)

def main():
  if len(sys.argv) > 1:
    print('No arguments expected!', file=sys.stderr)
    sys.stderr.write(__doc__)
    sys.exit(1)

  with open(os.path.join(POLICY_LIST_PATH), encoding='utf-8') as f:
    policy_list_content = pyyaml.safe_load(f)

  with open(ENUMS_PATH, 'rb') as f:
    histograms_doc = minidom.parse(f)
    f.seek(0)
    xml = f.read().decode('utf-8')

  UpdatePoliciesHistogramDefinitions(policy_list_content['policies'],
                                     histograms_doc)
  UpdateAtomicGroupsHistogramDefinitions(policy_list_content['atomic_groups'],
                                         histograms_doc)
  new_xml = histogram_configuration_model.PrettifyTree(histograms_doc)
  if PromptUserToAcceptDiff(xml, new_xml, 'Is the updated version acceptable?'):
    with open(ENUMS_PATH, 'wb') as f:
      f.write(new_xml.encode('utf-8'))


if __name__ == '__main__':
  try:
    main()
  except UserError as e:
    print(e.message, file=sys.stderr)
    sys.exit(1)