from __future__ import absolute_import
# Standard modules
-import copy
+# import copy
import logging
import re
# import sys
from fb_tools.common import pp
from fb_tools.handler import BaseHandler
from fb_tools.multi_config import DEFAULT_ENCODING
-from fb_tools.xlate import format_list
+# from fb_tools.xlate import format_list
# Own modules
from .ldap import BaseLdapApplication
from ..xlate import XLATOR
-__version__ = '0.7.2'
+__version__ = '0.7.4'
LOG = logging.getLogger(__name__)
_ = XLATOR.gettext
postfix_config_dir = Path('/etc/postfix')
postfix_maps_dir = postfix_config_dir / 'maps'
- default_virtaliases_basename = 'virtual-aliases'
- default_virtaliases_files = [postfix_maps_dir / default_virtaliases_basename, ]
default_postfix_db_hashtype = 'hash'
postfix_filetype_extensions = {
default_origin = 'pixelpark.com'
re_virtaliases_line = re.compile(r'^([^#\s:]+)\s', re.MULTILINE)
+ re_pf_config = re.compile(r'^(?P<key>\S+)\s*=\s*(?P<value>\S.*)?')
+ re_pf_fieldsep = re.compile(r'\s*[,;]\s*')
+ re_empty_line = re.compile(r'^\s*(?:#.*|$)')
default_postmap_command = 'postmap'
default_postconf_command = 'postconf'
def __init__(self, appname=None, base_dir=None):
"""Constructz the application object."""
self.barracuda_base_dn = self.default_barracuda_base_dn
- self.virtaliases_files = copy.copy(self.default_virtaliases_files)
+ self.virtaliases_files = []
self.postfix_db_hashtype = self.default_postfix_db_hashtype
self.postconf_command = Path('/sbin') / self.default_postconf_command
self.postmap_command = Path('/sbin') / self.default_postmap_command
'It has to be exists. Default: {!r}.').format(str(self.postfix_maps_dir)),
)
- sync_group.add_argument(
- '-B', '--basename', '--virtalias-basename', nargs='*', dest='basename',
- metavar=_('NAME'),
- help=_(
- 'All possible basenames of the virtual aliases file below the latter '
- 'directory. All of these basenames are used as source of the virtual aliases. '
- 'Default: {!r}.').format(self.default_virtaliases_basename),
- )
-
- sync_group.add_argument(
- '--type', dest='hashtype', metavar=_('TYPE'),
- help=_(
- 'The used lookup table type of all virtual aliases table. '
- 'Default: {!r}.').format(self.default_postfix_db_hashtype),
- )
+# sync_group.add_argument(
+# '-B', '--basename', '--virtalias-basename', nargs='*', dest='basename',
+# metavar=_('NAME'),
+# help=_(
+# 'All possible basenames of the virtual aliases file below the latter '
+# 'directory. All of these basenames are used as source of the virtual aliases. '
+# 'Default: {!r}.').format(self.default_virtaliases_basename),
+# )
+
+# sync_group.add_argument(
+# '--type', dest='hashtype', metavar=_('TYPE'),
+# help=_(
+# 'The used lookup table type of all virtual aliases table. '
+# 'Default: {!r}.').format(self.default_postfix_db_hashtype),
+# )
sync_group.add_argument(
'--base-dn', dest='baase_dn', metavar='DN',
"""Execute this method before calling run()."""
super(BarracudaSyncApp, self).post_init()
- self._check_postfix()
- self._init_virtaliases_files()
- self._check_virtaliases_files()
+ self._check_postfix_commands()
+ self._check_postfix_table_types()
+ self._get_postfix_default_db_type()
+ # self._init_virtaliases_files()
+ # self._check_virtaliases_files()
# -------------------------------------------------------------------------
- def _check_postfix(self):
- """Check postfix commands and lookup table types."""
- LOG.debug(_('Checking postfix commands and lookup table types ...'))
+ def _check_postfix_commands(self):
+ """Check postfix commands."""
+ LOG.debug(_('Checking postfix commands ...'))
found_all_commands = True
LOG.err(_('Postfix seems not to be installed.'))
self.exit(5)
+ # -------------------------------------------------------------------------
+ def _check_postfix_table_types(self):
+ """Check postfix lookup table types."""
+ LOG.debug(_('Checking postfix lookup table types ...'))
+
LOG.debug(_('Evaluating lookup table types.'))
handler = BaseHandler(appname=self.appname, verbose=self.verbose)
LOG.err(msg)
self.exit(6)
- if self.args.hashtype:
- htype = self.args.hashtype
- if htype not in self.lookup_table_types:
- msg = _('Wrong lookup table type {!r} given. Valid types are:').format(htype)
- msg += ' ' + format_list(self.lookup_table_types, do_repr=True)
- LOG.error(msg)
- self.exit(1)
- self.postfix_db_hashtype = htype
- elif self.postfix_db_hashtype not in self.lookup_table_types:
- msg = _('Default lookup table type {!r} is invalid. Valid types are:').format(
- self.postfix_db_hashtype)
- LOG.error(msg)
- self.exit(6)
-
- if self.verbose > 1:
- LOG.debug(_('Candidate for lookup table type: {!r}.').format(self.postfix_db_hashtype))
+ # -------------------------------------------------------------------------
+ def _get_postfix_default_db_type(self):
+ """Evaluate default postfix lookup table type."""
+ LOG.debug(_('Evaluating default postfix lookup table type ...'))
- if self.postfix_db_hashtype not in self.usable_postfix_hashtypes:
- msg = _(
- 'The lookup table type {!r} cannot be used to get all '
- 'database elements.').format(self.postfix_db_hashtype)
- LOG.error(msg)
+ handler = BaseHandler(appname=self.appname, verbose=self.verbose)
+ pdata = handler.call([str(self.postconf_command), 'default_database_type'], quiet=True)
+ if pdata.returncode > 0:
+ msg = _('Error {} on evaluating default postfix lookup table type').format(
+ pdata.returncode)
+ if pdata.stderr:
+ msg += ': ' + pdata.stderr
+ else:
+ msg += '.'
+ LOG.err(msg)
self.exit(6)
+ for line in pdata.stdout.splitlines():
+ if self.re_empty_line.match(line):
+ continue
+ m = self.re_pf_config.match(line)
+ if m and m.group('key') == 'default_database_type':
+ table_type = m.group('value')
+ if table_type:
+ table_type = table_type.strip()
+ if table_type:
+ self.postfix_db_hashtype = table_type
+ self.debug(_('Found postfix default database type: {!r}.').format(table_type))
+
# -------------------------------------------------------------------------
def _init_virtaliases_files(self):
"""Collect all files used as database for virtual aliases."""