# 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 time import FileSummary from Misc import * _PROPERTIES_DATA = { FileSummary.INODE: { "name": "inode", "value": lambda f: f.inode }, FileSummary.MODE: { "name": "mode", "value": lambda f: oct(f.mode) }, FileSummary.LINK: { "name": "link target", "value": lambda f: f.link_target_filename }, FileSummary.LINKS: { "name": "number of links", "value": lambda f: f.links }, FileSummary.UID: { "name": "uid", "value": lambda f: f.uid }, FileSummary.GID: { "name": "gid", "value": lambda f: f.gid }, FileSummary.SIZE: { "name": "size", "value": lambda f: f.size }, FileSummary.ATIME: { "name": "access time", "value": lambda f: time_to_string(f.atime) }, FileSummary.MTIME: { "name": "modification time", "value": lambda f: time_to_string(f.mtime) }, FileSummary.CTIME: { "name": "change time", "value": lambda f: time_to_string(f.ctime) }, FileSummary.MD5: { "name": "md5 digest", "value": lambda f: f.md5_digest }, FileSummary.SHA: { "name": "sha digest", "value": lambda f: f.sha_digest } } _PROPERTIES = FileSummary.INODE, FileSummary.MODE, FileSummary.LINKS, \ FileSummary.UID, FileSummary.GID, FileSummary.SIZE, FileSummary.ATIME, \ FileSummary.MTIME, FileSummary.CTIME, FileSummary.MD5, FileSummary.SHA _MAX_PROPERTY_NAME_LEN = reduce(lambda x, y: (x > y) and x or y, map(lambda x: len(x["name"]), _PROPERTIES_DATA.values())) class Report: def __init__(self): self._added = 0 self._removed = 0 self._modified = 0 def __del__(self): added = self._added > 1 and "s" or "" removed = self._removed > 1 and "s" or "" modified = self._modified > 1 and "s" or "" message = "Summary: %d added file%s, %d removed file%s, %d modified file%s" % \ (self._added, added, self._removed, removed, self._modified, modified) print print "-" * len(message) print stylize(message, bold=True) def _printFileMessage(self, message, filename): format_string = "%%-%ds" % _MAX_PROPERTY_NAME_LEN print format_string % ("[" + stylize(message, foreground="red", bold=True) + "]"), print stylize(filename, foreground="green") def _printPropertyName(self, property): format_string = "%%-%ds:" % (_MAX_PROPERTY_NAME_LEN + 1) print format_string % _PROPERTIES_DATA[property]["name"], def _printProperties(self, file): for property in _PROPERTIES: value = _PROPERTIES_DATA[property]["value"](file) if not value: continue self._printPropertyName(property) print value def fileAdded(self, file): self._added += 1 self._printFileMessage("ADDED FILE", file.filename) self._printProperties(file) print def fileRemoved(self, file): self._removed += 1 self._printFileMessage("REMOVED FILE", file.filename) self._printProperties(file) print def fileModified(self, changed_properties, original_file, current_file): self._modified += 1 self._printFileMessage("MODIFIED FILE", current_file.filename) for property in _PROPERTIES: if changed_properties & property: get_value = _PROPERTIES_DATA[property]["value"] self._printPropertyName(property) print "%-35s, %s" % (get_value(original_file), get_value(current_file)) print