From ddf34e4e918a543f60d9ec18b1d53189e2025a36 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Mon, 29 Nov 2021 09:54:05 +0100 Subject: [PATCH] Adding translation margers to lib/pp_admintools/deploy_zones_from_pdns.py --- lib/pp_admintools/deploy_zones_from_pdns.py | 130 +++++++++++--------- 1 file changed, 71 insertions(+), 59 deletions(-) diff --git a/lib/pp_admintools/deploy_zones_from_pdns.py b/lib/pp_admintools/deploy_zones_from_pdns.py index 12c7978..2b0786b 100644 --- a/lib/pp_admintools/deploy_zones_from_pdns.py +++ b/lib/pp_admintools/deploy_zones_from_pdns.py @@ -37,9 +37,13 @@ from .pdns_app import PpPDNSAppError, PpPDNSApplication from .pidfile import PidFileError, PidFile -__version__ = '0.6.0' +from .xlate import XLATOR + +__version__ = '0.6.1' LOG = logging.getLogger(__name__) +_ = XLATOR.gettext + # ============================================================================= class PpDeployZonesError(PpPDNSAppError): @@ -131,9 +135,7 @@ class PpDeployZonesApp(PpPDNSApplication): self.files2replace = {} self.moved_files = {} - description = textwrap.dedent('''\ - Generation of the BIND9 configuration file for slave zones. - ''') + description = _('Generation of the BIND9 configuration file for slave zones.') super(PpDeployZonesApp, self).__init__( appname=appname, version=version, description=description, @@ -167,12 +169,12 @@ class PpDeployZonesApp(PpPDNSApplication): self.arg_parser.add_argument( '-B', '--backup', dest="keep_backup", action='store_true', - help=("Keep a backup file for each changed configuration file."), + help=_("Keep a backup file for each changed configuration file."), ) self.arg_parser.add_argument( '-K', '--keep-tempdir', dest='keep_tempdir', action='store_true', - help=( + help=_( "Keeping the temporary directory instead of removing it at the end " "(e.g. for debugging purposes)"), ) @@ -200,7 +202,7 @@ class PpDeployZonesApp(PpPDNSApplication): for section_name in self.cfg.keys(): if self.verbose > 3: - LOG.debug("Checking config section {!r} ...".format(section_name)) + LOG.debug(_("Checking config section {!r} ...").format(section_name)) section = self.cfg[section_name] @@ -224,8 +226,8 @@ class PpDeployZonesApp(PpPDNSApplication): def set_named_options(self, section, section_name): if self.verbose > 2: - LOG.debug("Evaluating config section {n!r}:\n{s}".format( - n=section_name, s=pp(section))) + LOG.debug( + _("Evaluating config section {!r}:").format(section_name) + '\n' + pp(section)) # Configuration files and directories self._check_path_config( @@ -250,7 +252,7 @@ class PpDeployZonesApp(PpPDNSApplication): value = value.strip() if not value: - msg = "No masters given in [{}]/masters.".format(section_name) + msg = _("No masters given in [{}]/masters.").format(section_name) LOG.error(msg) self.config_has_errors = True return @@ -260,38 +262,38 @@ class PpDeployZonesApp(PpPDNSApplication): for m in self.re_split_addresses.split(value): if m: m = m.strip().lower() - LOG.debug("Checking given master address {!r} ...".format(m)) + LOG.debug(_("Checking given master address {!r} ...").format(m)) try: addr_infos = socket.getaddrinfo( m, 53, proto=socket.IPPROTO_TCP) for addr_info in addr_infos: addr = addr_info[4][0] if not self.named_listen_on_v6 and addr_info[0] == socket.AF_INET6: - msg = ( + msg = _( "Not using {!r} as a master IP address, because " "we are not using IPv6.").format(addr) LOG.debug(msg) continue if addr in masters: - LOG.debug("Address {!r} already in masters yet.".format(addr)) + LOG.debug(_("Address {!r} are already in masters yet.").format(addr)) else: - LOG.debug("Address {!r} not in masters yet.".format(addr)) + LOG.debug(_("Address {!r} are not in masters yet.").format(addr)) masters.append(addr) except socket.gaierror as e: - msg = ( - "Invalid hostname or address {!r} found in " - "[{}]/masters: {}").format(m, section_name, e) + msg = _( + "Invalid hostname or address {a!r} found in [{s}]/masters: {e}").format( + a=m, s=section_name, e=e) LOG.error(msg) self.config_has_errors = True m = None if masters: if self.verbose > 2: - LOG.debug("Using configured masters: {}".format(pp(masters))) + LOG.debug(_("Using configured masters: {}").format(pp(masters))) self.zone_masters = masters self.masters_configured = True else: - LOG.warn("No valid masters found in configuration.") + LOG.warn(_("No valid masters found in configuration.")) # ------------------------------------------------------------------------- def post_init(self): @@ -302,20 +304,20 @@ class PpDeployZonesApp(PpPDNSApplication): if not self.quiet: print('') - LOG.debug("Post init phase.") + LOG.debug(_("Post init phase.")) - LOG.debug("Checking for masters, which are local addresses ...") + LOG.debug(_("Checking for masters, which are local addresses ...")) ext_masters = [] for addr in self.zone_masters: if addr in self.local_addresses: LOG.debug( - "Address {!r} IS in list of local addresses.".format(addr)) + _("Address {!r} is in list of local addresses.").format(addr)) else: LOG.debug( - "Address {!r} not in list of local addresses.".format(addr)) + _("Address {!r} is not in list of local addresses.").format(addr)) ext_masters.append(addr) self.zone_masters = ext_masters - LOG.info("Using masters for slave zones: {}".format( + LOG.info(_("Using masters for slave zones: {}").format( ', '.join(map(lambda x: '{!r}'.format(x), self.zone_masters)))) self.pidfile = PidFile( @@ -335,9 +337,9 @@ class PpDeployZonesApp(PpPDNSApplication): super(PpDeployZonesApp, self).pre_run() if self.environment == 'global': - LOG.error( + LOG.error(_( "Using the global DNS master is not supported, " - "please use 'local' or 'public'") + "please use 'local' or 'public'")) self.exit(1) # ------------------------------------------------------------------------- @@ -349,12 +351,12 @@ class PpDeployZonesApp(PpPDNSApplication): try: local_tz = timezone(local_tz_name) except UnknownTimeZoneError: - LOG.error("Unknown time zone: {!r}.".format(local_tz_name)) + LOG.error(_("Unknown time zone: {!r}.").format(local_tz_name)) self.exit(6) my_uid = os.geteuid() if my_uid: - msg = "You must be root to execute this script." + msg = _("You must be root to execute this script.") if self.simulate: LOG.warn(msg) time.sleep(1) @@ -365,13 +367,13 @@ class PpDeployZonesApp(PpPDNSApplication): try: self.pidfile.create() except PidFileError as e: - LOG.error("Could not occupy pidfile: {}".format(e)) + LOG.error(_("Could not occupy pidfile: {}").format(e)) self.exit(7) return try: - LOG.info("Starting: {}".format( + LOG.info(_("Starting: {}").format( datetime.datetime.now(local_tz).strftime('%Y-%m-%d %H:%M:%S %Z'))) self.zones = self.get_api_zones() @@ -394,38 +396,38 @@ class PpDeployZonesApp(PpPDNSApplication): finally: self.cleanup() self.pidfile = None - LOG.info("Ending: {}".format( + LOG.info(_("Ending: {}").format( datetime.datetime.now(local_tz).strftime('%Y-%m-%d %H:%M:%S %Z'))) # ------------------------------------------------------------------------- def cleanup(self): - LOG.info("Cleaning up ...") + LOG.info(_("Cleaning up ...")) for tgt_file in self.moved_files.keys(): backup_file = self.moved_files[tgt_file] - LOG.debug("Searching for {!r}.".format(backup_file)) + LOG.debug(_("Searching for {!r}.").format(backup_file)) if os.path.exists(backup_file): if self.keep_backup: - LOG.info("Keep existing backup file {!r}.".format(backup_file)) + LOG.info(_("Keep existing backup file {!r}.").format(backup_file)) else: - LOG.info("Removing {!r} ...".format(backup_file)) + LOG.info(_("Removing {!r} ...").format(backup_file)) if not self.simulate: os.remove(backup_file) # ----------------------- def emit_rm_err(function, path, excinfo): - LOG.error("Error removing {!r} - {}: {}".format( - path, excinfo[1].__class__.__name__, excinfo[1])) + LOG.error(_("Error removing {p!r} - {c}: {e}").format( + p=path, c=excinfo[1].__class__.__name__, e=excinfo[1])) if self.tempdir: if self.keep_tempdir: - msg = ( + msg = _( "Temporary directory {!r} will not be removed. " "It's on yours to remove it manually.").format(self.tempdir) LOG.warn(msg) else: - LOG.debug("Destroying temporary directory {!r} ...".format(self.tempdir)) + LOG.debug(_("Destroying temporary directory {!r} ...").format(self.tempdir)) shutil.rmtree(self.tempdir, False, emit_rm_err) self.tempdir = None @@ -436,18 +438,18 @@ class PpDeployZonesApp(PpPDNSApplication): self.tempdir = tempfile.mkdtemp( prefix=(self.appname + '.'), suffix='.tmp.d' ) - LOG.debug("Temporary directory: {!r}.".format(self.tempdir)) + LOG.debug(_("Temporary directory: {!r}.").format(self.tempdir)) self.temp_zones_cfg_file = os.path.join( self.tempdir, self.default_named_zones_cfg_file) if self.verbose > 1: - LOG.debug("Temporary zones conf: {!r}".format(self.temp_zones_cfg_file)) + LOG.debug(_("Temporary zones conf: {!r}").format(self.temp_zones_cfg_file)) # ------------------------------------------------------------------------- def generate_slave_cfg_file(self): - LOG.info("Generating {} ...".format(self.default_named_zones_cfg_file)) + LOG.info(_("Generating {} ...").format(self.default_named_zones_cfg_file)) cur_date = datetime.datetime.now().isoformat(' ') re_rev = re.compile(r'^rev\.', re.IGNORECASE) @@ -474,7 +476,7 @@ class PpDeployZonesApp(PpPDNSApplication): prefix = self._get_ipv4_prefix(match.group(1)) if prefix: if prefix == '127.0.0': - LOG.debug("Pure local zone {!r} will not be considered.".format(prefix)) + LOG.debug(_("Pure local zone {!r} will not be considered.").format(prefix)) continue canonical_name = 'rev.' + prefix else: @@ -512,7 +514,9 @@ class PpDeployZonesApp(PpPDNSApplication): fh.write(content) if self.verbose > 2: - LOG.debug("Generated {!r}:\n{}".format(self.temp_zones_cfg_file, content.strip())) + LOG.debug( + _("Generated file {!r}:").format( + self.temp_zones_cfg_file) + '\n' + content.strip()) # ------------------------------------------------------------------------- def _get_ipv4_prefix(self, match): @@ -522,7 +526,7 @@ class PpDeployZonesApp(PpPDNSApplication): if t: tuples.insert(0, t) if self.verbose > 2: - LOG.debug("Got IPv4 tuples: {}".format(pp(tuples))) + LOG.debug(_("Got IPv4 tuples: {}").format(pp(tuples))) return '.'.join(tuples) # ------------------------------------------------------------------------- @@ -546,46 +550,55 @@ class PpDeployZonesApp(PpPDNSApplication): del tuples[0:4] if self.verbose > 2: - LOG.debug("Got IPv6 tokens: {}".format(pp(tokens))) + LOG.debug(_("Got IPv6 tokens: {}").format(pp(tokens))) return ':'.join(tokens) # ------------------------------------------------------------------------- def compare_files(self): - LOG.info("Comparing generated files with existing ones.") + 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))) + LOG.debug(_("Files to replace:") + '\n' + 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.") + raise PpDeployZonesError(_("Source file not defined.")) if not file_tgt: - raise PpDeployZonesError("Target file not defined.") + raise PpDeployZonesError(_("Target file not defined.")) + + LOG.debug(_("Comparing {one!r} with {two!r} ...").format( + one=file_src, two=file_tgt)) if not os.path.exists(file_src): - raise PpDeployZonesError("Source file {!r} does not exists.".format(file_src)) + msg = _("{what} {f!r} does not exists.").format( + what=_("Source file"), f=file_src) + raise PpDeployZonesError(msg) if not os.path.isfile(file_src): - raise PpDeployZonesError("Source file {!r} is not a regular file.".format(file_src)) + msg = _("{what} {f!r} is not a regular file.").format( + what=_("Source file"), f=file_src) + raise PpDeployZonesError(msg) if not os.path.exists(file_tgt): - LOG.debug("Target file {!r} does not exists.".format(file_tgt)) + msg = _("{what} {f!r} does not exists.").format( + what=_("Target file"), f=file_tgt) + LOG.debug(msg) return False if not os.path.isfile(file_tgt): - raise PpDeployZonesError("Target file {!r} is not a regular file.".format(file_tgt)) + msg = _("{what} {f!r} is not a regular file.").format( + what=_("Target file"), f=file_tgt) + raise PpDeployZonesError(msg) content_src = '' if self.verbose > 2: - LOG.debug("Reading {!r} ...".format(file_src)) + 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) @@ -596,8 +609,7 @@ class PpDeployZonesApp(PpPDNSApplication): if line: lines_src.append(line) if self.verbose > 3: - LOG.debug("Cleaned version of {!r}:\n{}".format( - file_src, '\n'.join(lines_src))) + LOG.debug(_("Cleaned version of {!r}:").format(file_src) + '\n' + '\n'.join(lines_src)) content_tgt = '' if self.verbose > 2: -- 2.39.5