components/policy/tools/syntax_check_policy_template_json.py
def _CheckPolicyDefinitionsChangeCompatibility(
      self, policy_definitions, original_file_contents, current_version):
    '''
    Checks if all the |policy_definitions| in the modified policy templates file
    are compatible with the policy definitions defined in the original policy
    templates file with |original_file_contents| .

    |policy_definitions|: The policy definition as it is in the modified policy
      templates file.
    |original_file_contents|: The full contents of the original policy templates
      file.
    |current_version|: The current major version of the branch as stored in
      chrome/VERSION.
    '''
    try:
      original_container = eval(original_file_contents)
    except:
      import traceback
      traceback.print_exc(file=sys.stdout)
      self._Error('Invalid Python/JSON syntax in original file.')
      return

    if original_container == None:
      self._Error('Invalid Python/JSON syntax in original file.')
      return

    original_policy_definitions = self._CheckContains(
        original_container,
        'policy_definitions',
        list,
        parent_element=None,
        optional=True,
        container_name='The root element',
        offending=None)

    if original_policy_definitions is None:
      return

    # Sort the new policies by name for faster searches.
    policy_definitions_dict = {
        policy['name']: policy
        for policy in policy_definitions
        if policy['type'] != 'group'
    }

    original_policy_name_set = {
        policy['name']
        for policy in original_policy_definitions if policy['type'] != 'group'
    }

    for original_policy in original_policy_definitions:
      # Check change compatibility for all non-group policy definitions.
      if original_policy['type'] == 'group':
        continue

      original_released_platforms, original_rolling_out_platforms = \
              self._GetReleasedPlatforms( original_policy, current_version)

      new_policy = policy_definitions_dict.get(original_policy['name'])

      # A policy that has at least one released platform cannot be removed.
      if new_policy is None and original_released_platforms:
        self._Error('Released policy \'%s\' has been removed.' %
                    original_policy['name'])
        continue

      new_released_platforms, new_rolling_out_platform = \
              self._GetReleasedPlatforms(new_policy, current_version)

      # Check policy compatibility if there is at least one released platform.
      if original_released_platforms:
        self._CheckPolicyDefinitionChangeCompatibility(
            original_policy, original_released_platforms, new_policy,
            new_released_platforms, current_version)

      # New released platforms should always use the current version unless they
      # are going to be merged into previous milestone.
      if new_released_platforms or new_rolling_out_platform:
        self._CheckNewReleasedPlatforms(
            MergeDict(original_released_platforms,
                      original_rolling_out_platforms),
            MergeDict(new_released_platforms, new_rolling_out_platform),
            current_version, original_policy['name'])

      if new_policy:
        self._CheckDeprecatedFutureField(original_policy, new_policy,
                                         original_policy['name'])

    # Check brand new policies:
    for new_policy_name in set(
        policy_definitions_dict.keys()) - original_policy_name_set:
      new_policy = policy_definitions_dict[new_policy_name]
      new_released_platforms, new_rolling_out_platform = \
              self._GetReleasedPlatforms(new_policy, current_version)
      if new_released_platforms or new_rolling_out_platform:
        self._CheckNewReleasedPlatforms({},
                                        MergeDict(new_released_platforms,
                                                  new_rolling_out_platform),
                                        current_version, new_policy_name)
      self._CheckDeprecatedFutureField(None, new_policy, new_policy_name)

      # TODO(crbug.com/1139046): This default check should apply to all
      # policies instead of just new ones.
      if self._NeedsDefault(new_policy) and not 'default' in new_policy:
        self._Error("Definition of policy %s must include a 'default'"
                    " field." % (new_policy_name))

      # TODO(crbug.com/1139306): This item check should apply to all policies
      # instead of just new ones.
      if self._NeedsItems(new_policy) and new_policy.get('items', None) == None:
        self._Error(('Missing items field for policy %s') % (new_policy_name))

    Checks if all the |policy_definitions| in the modified policy templates file
    are compatible with the policy definitions defined in the original policy
    templates file with |original_file_contents| .

    |policy_definitions|: The policy definition as it is in the modified policy
      templates file.
    |original_file_contents|: The full contents of the original policy templates
      file.
    |current_version|: The current major version of the branch as stored in
      chrome/VERSION.
    
Checks if all the |policy_definitions| in the modified policy templates file
are compatible with the policy definitions defined in the original policy
templates file with |original_file_contents| .

|policy_definitions|: The policy definition as it is in the modified policy
templates file.
|original_file_contents|: The full contents of the original policy templates
file.
|current_version|: The current major version of the branch as stored in
chrome/VERSION.
Checks if all the |policy_definitions| in the modified policy templates file are compatible with the policy definitions defined in the original policy templates file with |original_file_contents| . |policy_definitions| : The policy definition as it is in the modified policy templates file . |original_file_contents| : The full contents of the original policy templates file . |current_version| : The current major version of the branch as stored in chrome / VERSION .
def _CheckPolicyDefinitionsChangeCompatibility(
      self, policy_definitions, original_file_contents, current_version):
    try:
      original_container = eval(original_file_contents)
    except:
      import traceback
      traceback.print_exc(file=sys.stdout)
      self._Error('Invalid Python/JSON syntax in original file.')
      return
    if original_container == None:
      self._Error('Invalid Python/JSON syntax in original file.')
      return
    original_policy_definitions = self._CheckContains(
        original_container,
        'policy_definitions',
        list,
        parent_element=None,
        optional=True,
        container_name='The root element',
        offending=None)
    if original_policy_definitions is None:
      return
    policy_definitions_dict = {
        policy['name']: policy
        for policy in policy_definitions
        if policy['type'] != 'group'
    }
    original_policy_name_set = {
        policy['name']
        for policy in original_policy_definitions if policy['type'] != 'group'
    }
    for original_policy in original_policy_definitions:
      if original_policy['type'] == 'group':
        continue
      original_released_platforms, original_rolling_out_platforms = \
              self._GetReleasedPlatforms( original_policy, current_version)
      new_policy = policy_definitions_dict.get(original_policy['name'])
      if new_policy is None and original_released_platforms:
        self._Error('Released policy \'%s\' has been removed.' %
                    original_policy['name'])
        continue
      new_released_platforms, new_rolling_out_platform = \
              self._GetReleasedPlatforms(new_policy, current_version)
      if original_released_platforms:
        self._CheckPolicyDefinitionChangeCompatibility(
            original_policy, original_released_platforms, new_policy,
            new_released_platforms, current_version)
      if new_released_platforms or new_rolling_out_platform:
        self._CheckNewReleasedPlatforms(
            MergeDict(original_released_platforms,
                      original_rolling_out_platforms),
            MergeDict(new_released_platforms, new_rolling_out_platform),
            current_version, original_policy['name'])
      if new_policy:
        self._CheckDeprecatedFutureField(original_policy, new_policy,
                                         original_policy['name'])
    for new_policy_name in set(
        policy_definitions_dict.keys()) - original_policy_name_set:
      new_policy = policy_definitions_dict[new_policy_name]
      new_released_platforms, new_rolling_out_platform = \
              self._GetReleasedPlatforms(new_policy, current_version)
      if new_released_platforms or new_rolling_out_platform:
        self._CheckNewReleasedPlatforms({},
                                        MergeDict(new_released_platforms,
                                                  new_rolling_out_platform),
                                        current_version, new_policy_name)
      self._CheckDeprecatedFutureField(None, new_policy, new_policy_name)
      if self._NeedsDefault(new_policy) and not 'default' in new_policy:
        self._Error("Definition of policy %s must include a 'default'"
                    " field." % (new_policy_name))
      if self._NeedsItems(new_policy) and new_policy.get('items', None) == None:
        self._Error(('Missing items field for policy %s') % (new_policy_name))
def _CheckPolicyDefinitionsChangeCompatibility ( self , policy_definitions , original_file_contents , current_version ) : try : original_container = eval ( original_file_contents ) except : import traceback traceback . print_exc ( file = sys . stdout ) self . _Error ( 'Invalid Python/JSON syntax in original file.' ) return if original_container == None : self . _Error ( 'Invalid Python/JSON syntax in original file.' ) return original_policy_definitions = self . _CheckContains ( original_container , 'policy_definitions' , list , parent_element = None , optional = True , container_name = 'The root element' , offending = None ) if original_policy_definitions is None : return policy_definitions_dict = { policy [ 'name' ] : policy for policy in policy_definitions if policy [ 'type' ] != 'group' } original_policy_name_set = { policy [ 'name' ] for policy in original_policy_definitions if policy [ 'type' ] != 'group' } for original_policy in original_policy_definitions : if original_policy [ 'type' ] == 'group' : continue original_released_platforms , original_rolling_out_platforms = self . _GetReleasedPlatforms ( original_policy , current_version ) new_policy = policy_definitions_dict . get ( original_policy [ 'name' ] ) if new_policy is None and original_released_platforms : self . _Error ( 'Released policy \'%s\' has been removed.' % original_policy [ 'name' ] ) continue new_released_platforms , new_rolling_out_platform = self . _GetReleasedPlatforms ( new_policy , current_version ) if original_released_platforms : self . _CheckPolicyDefinitionChangeCompatibility ( original_policy , original_released_platforms , new_policy , new_released_platforms , current_version ) if new_released_platforms or new_rolling_out_platform : self . _CheckNewReleasedPlatforms ( MergeDict ( original_released_platforms , original_rolling_out_platforms ) , MergeDict ( new_released_platforms , new_rolling_out_platform ) , current_version , original_policy [ 'name' ] ) if new_policy : self . _CheckDeprecatedFutureField ( original_policy , new_policy , original_policy [ 'name' ] ) for new_policy_name in set ( policy_definitions_dict . keys ( ) ) - original_policy_name_set : new_policy = policy_definitions_dict [ new_policy_name ] new_released_platforms , new_rolling_out_platform = self . _GetReleasedPlatforms ( new_policy , current_version ) if new_released_platforms or new_rolling_out_platform : self . _CheckNewReleasedPlatforms ( { } , MergeDict ( new_released_platforms , new_rolling_out_platform ) , current_version , new_policy_name ) self . _CheckDeprecatedFutureField ( None , new_policy , new_policy_name ) if self . _NeedsDefault ( new_policy ) and not 'default' in new_policy : self . _Error ( "Definition of policy %s must include a 'default'" " field." % ( new_policy_name ) ) if self . _NeedsItems ( new_policy ) and new_policy . get ( 'items' , None ) == None : self . _Error ( ( 'Missing items field for policy %s' ) % ( new_policy_name ) )
Checks if all the |policy_definitions| in the modified policy templates file
are compatible with the policy definitions defined in the original policy
templates file with |original_file_contents| .
Checks if all the |policy_definitions| in the modified policy templates file are compatible with the policy definitions defined in the original policy templates file with |original_file_contents| .
'''
    Checks if all the |policy_definitions| in the modified policy templates file
    are compatible with the policy definitions defined in the original policy
    templates file with |original_file_contents| .

    |policy_definitions|: The policy definition as it is in the modified policy
      templates file.
    |original_file_contents|: The full contents of the original policy templates
      file.
    |current_version|: The current major version of the branch as stored in
      chrome/VERSION.
    ''' # Sort the new policies by name for faster searches. # Check change compatibility for all non-group policy definitions. # A policy that has at least one released platform cannot be removed. # Check policy compatibility if there is at least one released platform. # New released platforms should always use the current version unless they # are going to be merged into previous milestone. # Check brand new policies: # TODO(crbug.com/1139046): This default check should apply to all # policies instead of just new ones. # TODO(crbug.com/1139306): This item check should apply to all policies # instead of just new ones.
self
policy_definitions
original_file_contents
current_version