From: Frank Brehm Date: Fri, 10 Nov 2017 09:03:19 +0000 (+0100) Subject: Adding comparision of existing and generated cfg file X-Git-Tag: 0.1.2~86 X-Git-Url: https://git.uhu-banane.net/?a=commitdiff_plain;h=6cdc97bcd64693d22f32b04c1947e0f4c356edfc;p=pixelpark%2Fadmin-tools.git Adding comparision of existing and generated cfg file --- diff --git a/pp_lib/deploy_zones_from_pdns.py b/pp_lib/deploy_zones_from_pdns.py index 3feeb51..611a5c1 100644 --- a/pp_lib/deploy_zones_from_pdns.py +++ b/pp_lib/deploy_zones_from_pdns.py @@ -43,7 +43,7 @@ from .pdns_record import compare_rrsets from .pidfile import PidFileError, InvalidPidFileError, PidFileInUseError, PidFile -__version__ = '0.3.1' +__version__ = '0.3.2' LOG = logging.getLogger(__name__) @@ -329,6 +329,7 @@ class PpDeployZonesApp(PpPDNSApplication): self.init_temp_objects() self.generate_slave_cfg_file() + self.compare_files() finally: self.cleanup() @@ -484,6 +485,89 @@ class PpDeployZonesApp(PpPDNSApplication): return ':'.join(tokens) + # ------------------------------------------------------------------------- + def compare_files(self): + + LOG.info("Comparing generated files with existing ones.") + + if not self.files_equal_content(self.temp_zones_cfg_file, self.named_zones_cfg_file): + self.reload_necessary = True + self.files2replace[self.named_zones_cfg_file] = self.temp_zones_cfg_file + + if self.verbose > 1: + LOG.debug("Files to replace:\n{}".format(pp(self.files2replace))) + + # ------------------------------------------------------------------------- + def files_equal_content(self, file_src, file_tgt): + + LOG.debug("Comparing {!r} with {!r} ...".format(file_src, file_tgt)) + + if not file_src: + raise PpDeployZonesError("Source file not defined.") + if not file_tgt: + raise PpDeployZonesError("Target file not defined.") + + if not os.path.exists(file_src): + raise PpDeployZonesError("Source file {!r} does not exists.".format(file_src)) + if not os.path.isfile(file_src): + raise PpDeployZonesError("Source file {!r} is not a regular file.".format(file_src)) + + if not os.path.exists(file_tgt): + LOG.debug("Target file {!r} does not exists.".format(file_tgt)) + return False + if not os.path.isfile(file_tgt): + raise PpDeployZonesError("Target file {!r} is not a regular file.".format(file_tgt)) + + content_src = '' + if self.verbose > 2: + LOG.debug("Reading {!r} ...".format(file_src)) + with open(file_src, 'r', **self.open_args) as fh: + content_src = fh.read() + lines_str_src = self.re_block_comment.sub('', content_src) + lines_str_src = self.re_line_comment.sub('', lines_str_src) + lines_src = [] + for line in lines_str_src.splitlines(): + line = line.strip() + if line: + lines_src.append(line) + if self.verbose > 3: + LOG.debug("Cleaned version of {!r}:\n{}".format( + file_src, '\n'.join(lines_src))) + + content_tgt = '' + if self.verbose > 2: + LOG.debug("Reading {!r} ...".format(file_tgt)) + with open(file_tgt, 'r', **self.open_args) as fh: + content_tgt = fh.read() + lines_str_tgt = self.re_block_comment.sub('', content_tgt) + lines_str_tgt = self.re_line_comment.sub('', lines_str_tgt) + lines_tgt = [] + for line in lines_str_tgt.splitlines(): + line = line.strip() + if line: + lines_tgt.append(line) + if self.verbose > 3: + LOG.debug("Cleaned version of {!r}:\n{}".format( + file_tgt, '\n'.join(lines_tgt))) + + if len(lines_src) != len(lines_tgt): + LOG.debug(( + "Source file {!r} has different number essential lines ({}) than " + "the target file {!r} ({} lines).").format( + file_src, len(lines_src), file_tgt, len(lines_tgt))) + return False + + i = 0 + while i < len(lines_src): + if lines_src[i] != lines_tgt[i]: + LOG.debug(( + "Source file {!r} has a different content than " + "the target file {!r}.").format(file_src, lines_tgt)) + return False + i += 1 + + return True + # =============================================================================