# 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 urllib from stat import * import os.path from Misc import * INODE = 0x001 MODE = 0x002 LINKS = 0x004 UID = 0x008 GID = 0x010 SIZE = 0x020 ATIME = 0x040 MTIME = 0x080 CTIME = 0x100 LINK = 0x200 MD5 = 0x400 SHA = 0x800 class FileSummary: "Abstract base class for LocalFileSummary and RemoteFileSummary" def __init__(self, properties): self.properties = properties self.inode = None self.mode = None self.links = None self.uid = None self.gid = None self.size = None self.atime = None self.mtime = None self.ctime = None self.link_target_filename = None self.md5_digest = None self.sha_digest = None def __str__(self): property_handlers = ( (INODE, "inode", lambda f: f.inode), (MODE, "mode", lambda f: oct(f.mode)), (LINKS, "links", lambda f: f.links), (LINK, "link-target", lambda f: f.link_target_filename), (UID, "uid", lambda f: f.uid), (GID, "gid", lambda f: f.gid), (SIZE, "size", lambda f: f.size), (ATIME, "atime", lambda f: time.ctime(f.atime)), (CTIME, "ctime", lambda f: time.ctime(f.ctime)), (MTIME, "mtime", lambda f: time.ctime(f.mtime)), (MD5, "md5", lambda f: f.md5_digest), (SHA, "sha", lambda f: f.sha_digest), ) properties = [ ] for property, name, handler in property_handlers: if self.properties & property: properties.append("%s:%s" % (name, stylize(str(handler(self)), bold=True))) return stylize(self.filename, foreground="green", bold=True) + \ ", ".join(properties) + \ "\n" def setLinkTargetFilename(self, link_target_filename): self.properties |= LINK self.link_target_filename = link_target_filename def setMD5Digest(self, digest): self.properties |= MD5 self.md5_digest = digest def setSHADigest(self, digest): self.properties |= SHA self.sha_digest = digest class LocalFileSummary(FileSummary): DEFAULT_PROPERTIES = INODE|MODE|LINKS|UID|GID|SIZE|MTIME|CTIME|MD5 def __init__(self, filename, properties, stats): FileSummary.__init__(self, properties) self.filename = filename self.inode = stats[ST_INO] self.mode = stats[ST_MODE] self.links = stats[ST_NLINK] self.uid = stats[ST_UID] self.gid = stats[ST_GID] self.size = stats[ST_SIZE] self.atime = stats[ST_ATIME] self.mtime = stats[ST_MTIME] self.ctime = stats[ST_CTIME] if self.properties & LINK and S_ISLNK(self.mode): self.link_target_filename = os.path.abspath(os.readlink(self.filename)) def isLocal(self): return True def getPath(self): return os.path.dirname(self.filename) def getBaseName(self): return os.path.basename(self.filename) class RemoteFileSummary(FileSummary): DEFAULT_PROPERTIES = MTIME|SIZE|MD5 def __init__(self, filename, properties): FileSummary.__init__(self, properties) self.filename = filename self.protocol, other = urllib.splittype(filename) self.host, filename = urllib.splithost(other) self.path, self.basename = os.path.split(filename) self.host, self.port = urllib.splitnport(self.host, 80) def getPath(self): return self.path def getBaseName(self): return self.basename def setSize(self, size): self.size = size def setMtime(self, mtime): self.mtime = mtime def isLocal(self): return False def getProtocol(self): return self.protocol def getHost(self): return self.host def getPort(self): return self.port