# Copyright (C) 2004 Nicolas Delon # All Rights Reserved # # This file is part of the Prelude program. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2, or (at your option) # any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; see the file COPYING. If not, write to # the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. import MyConfigParser, FileSummary, Input import glob import os from stat import * MONITORING = "monitoring" ALIASES = "aliases" def strpbrk(s, accept): "Act like the corresponding C function" index = 0 for c in s: if c in accept: return index index += 1 return -1 class Error(Exception): pass class UnknownProperty(Error): def __init__(self, property, option): self.property = property self.lineno = option.getLineno() self.line = option.getLine() def __str__(self): return "unknown property '%s' in \"%s\" at line %d" % (self.property, self.line.rstrip(), self.lineno) class Conf: ATTR_TABLE = { "protection": FileSummary.MODE, "inode": FileSummary.INODE, "links": FileSummary.LINKS, "uid": FileSummary.UID, "gid": FileSummary.GID, "mtime": FileSummary.MTIME, "atime": FileSummary.ATIME, "ctime": FileSummary.CTIME, "md5": FileSummary.MD5, "sha": FileSummary.SHA, } def __init__(self, filename="../../etc/prelude-fic/prelude-fic.conf"): self._filename = filename self._files = { } self._aliases = { } self._load() def getFiles(self): return self._files.values() def __str__(self): ret = "[aliases]\n" for alias in self._aliases: ret += "%s = %#x\n" % (alias, self._aliases[alias]) ret += "\n[monitoring]\n" for file in self._files.values(): ret += "%s\n" % file return ret def _getPropertyByName(self, name): if self._aliases.has_key(name): return self._aliases[name] try: return self.ATTR_TABLE[name] except KeyError: raise UnknownProperty(name, self._current_option) def _setGroupProperties(self, property_names, group): if not property_names[0] in "-+": index = strpbrk(property_names, "-+") if index == -1: group.properties = self._getPropertyByName(property_names) return group.properties = self._getPropertyByName(property_names[:index]) property_names = property_names[index:] while len(property_names) > 0: operator = property_names[0] property_names = property_names[1:] index = strpbrk(property_names, "-+") if index == -1: property_name = property_names property_names = "" else: property_name = property_names[:index] property_names = property_names[index:] if operator == "-": group.properties &= ~self._getPropertyByName(property_name) else: group.properties |= self._getPropertyByName(property_name) def _loadAliases(self, conf): for alias in conf.getSection(ALIASES).getOptions(): self._aliases[alias.getName()] = self._getFileAttrs(alias.getValue()) def _loadMonitoring(self, conf): for option in conf.getSection(MONITORING).getOptions(): self._current_option = option group_name, property_names = option.getName(), option.getValue() add = True if group_name[0] == "!": group_name = group_name[1:] add = False recursive = True if group_name[0] == "=": group_name = group_name[1:] recursive = False group = Input.new_file_group(group_name, recursive) if add: if property_names: self._setGroupProperties(property_names, group) for file in group.expand(): self._files[file.filename] = file else: for file in group.expand(): del self._files[file.filename] def _load(self): conf = MyConfigParser.ConfigParser(self._filename) conf.load() self._loadAliases(conf) self._loadMonitoring(conf) if __name__ == "__main__": conf = Conf() print conf