From 5290232c5bf8c0d4c853cfd24231d7c7ae0d1fc7 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Fri, 22 Apr 2011 10:56:18 +0000 Subject: [PATCH] Mit Parsing config weitergemacht git-svn-id: http://svn.brehm-online.com/svn/my-stuff/python/PyLogrotate/trunk@211 ec8d2aa5-1599-4edb-8739-2b3a1bc399aa --- LogRotateConfig.py | 153 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 121 insertions(+), 32 deletions(-) diff --git a/LogRotateConfig.py b/LogRotateConfig.py index a47dd18..c982bd2 100755 --- a/LogRotateConfig.py +++ b/LogRotateConfig.py @@ -150,6 +150,13 @@ class LogrotateConfigurationReader(object): ############################################# # the rest of instance variables: + self.search_path = ['/bin', '/usr/bin'] + ''' + @ivar: ordered list with directories, where executables are searched + @type: list + ''' + self._init_search_path() + self.shred_command = '/usr/bin/shred' ''' @ivar: the system command to shred aged rotated logfiles, if wanted @@ -244,6 +251,7 @@ class LogrotateConfigurationReader(object): 'default': self.default, 'new_log': self.new_log, 'local_dir': self.local_dir, + 'search_path': self.search_path, 'scripts': self.scripts, 'shred_command': self.shred_command, 'taboo': self.taboo, @@ -327,23 +335,17 @@ class LogrotateConfigurationReader(object): self.taboo.append(pattern) #------------------------------------------------------------ - def _get_std_search_path(self, include_current = False): + def _init_search_path(self): ''' - Returns a list with all search directories from $PATH and some additionally - directiories. - - @param include_current: include the current working directory - at the end of the list - @type include_current: bool + Initialises the internal list of search pathes - @return: list of search directories - @rtype: list + @return: None ''' _ = self.t.lgettext - pp = pprint.PrettyPrinter(indent=4) dir_included = {} + # Including default path list from environment $PATH def_path = os.environ['PATH'] if not def_path: def_path = '' @@ -351,40 +353,72 @@ class LogrotateConfigurationReader(object): path_list = [] for item in def_path.split(sep): if item: - if not item in dir_included: - path_list.append(item) - dir_included[item] = True - if self.verbose > 2: - self.logger.debug( _("Path list from $PATH:") + "\n" - + pp.pformat(path_list) - ) - + if os.path.isdir(item): + real_dir = os.path.abspath(item) + if not real_dir in dir_included: + path_list.append(real_dir) + dir_included[real_dir] = True + else: + self.logger.debug( + _("'%s' is not a directory") % (item) + ) + + # Including default path list from python def_path = os.defpath for item in def_path.split(sep): if item: - if not item in dir_included: - path_list.append(item) - dir_included[item] = True + if os.path.isdir(item): + real_dir = os.path.abspath(item) + if not real_dir in dir_included: + path_list.append(real_dir) + dir_included[real_dir] = True + else: + self.logger.debug( + _("'%s' is not a directory") % (item) + ) + + # Including own defined directories for item in ('/usr/local/bin', '/sbin', '/usr/sbin', '/usr/local/sbin'): - if not item in dir_included: - path_list.append(item) - dir_included[item] = True + if os.path.isdir(item): + real_dir = os.path.abspath(item) + if not real_dir in dir_included: + path_list.append(real_dir) + dir_included[real_dir] = True + else: + self.logger.debug( + _("'%s' is not a directory") % (item) + ) + + self.search_path = path_list + + #------------------------------------------------------------ + def _get_std_search_path(self, include_current = False): + ''' + Returns a list with all search directories from $PATH and some additionally + directiories. + + @param include_current: include the current working directory + at the end of the list + @type include_current: bool + + @return: list of search directories + @rtype: list + ''' + + #_ = self.t.lgettext + + path_list = self.search_path if include_current: item = os.getcwd() - if not item in dir_included: - path_list.append(item) - dir_included[item] = True - if self.verbose > 2: - self.logger.debug( _("Path list after standard path:") + "\n" - + pp.pformat(path_list) - ) + real_dir = os.path.abspath(item) + path_list.append(real_dir) return path_list #------------------------------------------------------------ def check_shred_command(self): ''' - Checks the availibility of a check command. Sets self.shred_command to + Checks the availability of a check command. Sets self.shred_command to this system command or to None, if not found (including a warning). ''' @@ -416,6 +450,61 @@ class LogrotateConfigurationReader(object): self.shred_command = None return False + #------------------------------------------------------------ + def check_compress_command(self, command): + ''' + Checks the availability of the given compress command. + + 'internal_gzip' and 'internal_bzip2' are accepted as valid compress + commands for compressing with the appropriate python modules. + + @param command: command to validate (absolute or relative for + searching in standard search path) + @type command: str + + @return: absolute path of the compress command, 'internal_gzip', + 'internal_bzip2' or None if not found or invalid + @rtype: str or None + ''' + + _ = self.t.lgettext + path_list = self._get_std_search_path(True) + + match = re.search(r'^\s*internal[\-_\s]?gzip\s*', command, re.IGNORECASE) + if match: + return 'internal_gzip' + + match = re.search(r'^\s*internal[\-_\s]?bzip2\s*', command, re.IGNORECASE) + if match: + return 'internal_bzip2' + + if os.path.isabs(command): + if os.access(command, os.X_OK): + return os.path.abspath(command) + else: + return None + + cmd = None + found = False + for search_dir in path_list: + if os.path.isdir(search_dir): + cmd = os.path.join(search_dir, command) + if not os.path.isfile(cmd): + continue + if os.access(cmd, os.X_OK): + found = True + break + else: + self.logger.debug( _("Search path '%s' doesn't exists " + + "or is not a directory") + % (search_dir) + ) + + if found: + return os.path.abspath(cmd) + else: + return None + #------------------------------------------------------------ def get_config(self): ''' -- 2.39.5