Home | History | Annotate | Download | only in diff
      1 # Copyright 2016, Tresys Technology, LLC
      2 #
      3 # This file is part of SETools.
      4 #
      5 # SETools is free software: you can redistribute it and/or modify
      6 # it under the terms of the GNU Lesser General Public License as
      7 # published by the Free Software Foundation, either version 2.1 of
      8 # the License, or (at your option) any later version.
      9 #
     10 # SETools is distributed in the hope that it will be useful,
     11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
     12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13 # GNU Lesser General Public License for more details.
     14 #
     15 # You should have received a copy of the GNU Lesser General Public
     16 # License along with SETools.  If not, see
     17 # <http://www.gnu.org/licenses/>.
     18 #
     19 from collections import namedtuple
     20 
     21 from .descriptors import DiffResultDescriptor
     22 from .difference import Difference, SymbolWrapper
     23 
     24 
     25 modified_bool_record = namedtuple("modified_boolean", ["added_state", "removed_state"])
     26 
     27 
     28 class BooleansDifference(Difference):
     29 
     30     """Determine the difference in type attributes between two policies."""
     31 
     32     added_booleans = DiffResultDescriptor("diff_booleans")
     33     removed_booleans = DiffResultDescriptor("diff_booleans")
     34     modified_booleans = DiffResultDescriptor("diff_booleans")
     35 
     36     def diff_booleans(self):
     37         """Generate the difference in type attributes between the policies."""
     38 
     39         self.log.info("Generating Boolean differences from {0.left_policy} to {0.right_policy}".
     40                       format(self))
     41 
     42         self.added_booleans, self.removed_booleans, matched_booleans = \
     43             self._set_diff(
     44                 (SymbolWrapper(b) for b in self.left_policy.bools()),
     45                 (SymbolWrapper(b) for b in self.right_policy.bools()))
     46 
     47         self.modified_booleans = dict()
     48 
     49         for left_boolean, right_boolean in matched_booleans:
     50             # Criteria for modified booleans
     51             # 1. change to default state
     52             if left_boolean.state != right_boolean.state:
     53                 self.modified_booleans[left_boolean] = modified_bool_record(right_boolean.state,
     54                                                                             left_boolean.state)
     55 
     56     #
     57     # Internal functions
     58     #
     59     def _reset_diff(self):
     60         """Reset diff results on policy changes."""
     61         self.log.debug("Resetting Boolean differences")
     62         self.added_booleans = None
     63         self.removed_booleans = None
     64         self.modified_booleans = None
     65