--- /usr/lib/portage/pym/portage_contents.py 2004-11-07 23:53:58.000000000 +0000 +++ portage_contents.py 2004-11-14 03:02:45.000000000 +0000 @@ -3,9 +3,10 @@ # Distributed under the terms of the GNU General Public License v2 # $Header: /var/cvsroot/gentoo-src/portage/pym/portage_contents.py,v 1.3 2004/10/04 14:07:40 vapier Exp $ -import os,string,types,sys,copy +import os,string,types,sys,copy,stat import portage_exception import portage_const +import portage_checksum #import gettext #gettext_t = gettext.translation('portage.contents', portage_const.LOCALE_DATA_PATH) @@ -17,6 +18,102 @@ FILES_KEY = "\0FILES\0" OWNER_KEY = "\0OWNER\0" +class ContentsManager: + """Manage a CONTENTS file.""" + def __init__(self,filename): + + self.contents = [] + + self.filename = filename + self.readFile(filename) + + def readFile(self,filename): + infile = open(filename) + + mylines = infile.readlines() + infile.close() + + contents = self.contents + + for line in mylines: + if line[-1] == '\n': + line = line[:-1] + parts = string.split(line) + + mytype = parts[0] + + if mytype in ["dir","dev","fif"]: + mypath = string.join(parts[1:]) + contents.append({'type': mytype, 'path': mypath}) + elif mytype == "obj": + mypath = string.join(parts[1:-2]) + mymd5 = parts[-2] + mymtime = parts[-1] + contents.append({'type': mytype, 'path': mypath, 'md5': mymd5, 'mtime': mymtime}) + elif mytype == "sym": + sl = string.join(parts[1:-1]) + sl = string.split(sl, " -> ") + + mypath = sl[0] + mytarget = sl[1] + mymtime = parts[-1] + contents.append({'type': mytype, 'path': mypath, 'target': mytarget, 'mtime': mymtime}) + else: + print _("Unknown type:"),mytype + + def writeFile(self,filename=""): + contents=self.contents + if filename == "": + filename = self.filename + + outfile=open(filename,"w") + for x in contents: + if x['type'] in ["dir","dev","fif"]: + outfile.write(x['type']+"\t"+x['path']+"\n") + elif x['type'] == "obj": + outfile.write(x['type']+"\t"+x['path']+"\t"+x['md5']+" "+x['mtime']+"\n") + elif x['type'] == "sym": + outfile.write(x['type']+"\t"+x['path']+"\t->\t"+x['target']+" "+x['mtime']+"\n") + outfile.close() + return + + def getMD5(self,filename): + contents = self.contents + + for i in contents: + if i['path'] == filename: + return i['md5'] + + def getMtime(self,filename): + contents = self.contents + + for i in contents: + if i['path'] == filename: + return i['mtime'] + + def updateAllInfos(self,filename): + self.changeMD5(filename) + self.changeMtime(filename) + + def changeMD5(self,filename, newmd5=""): + contents = self.contents + if newmd5 == "": + newmd5=portage_checksum.perform_md5(filename) + + for i in contents: + if i['path'] == filename: + i['md5'] = newmd5 + + def changeMtime(self, filename, newmtime=""): + contents = self.contents + if newmtime == "": + pathstat = os.stat(filename) + newmtime = pathstat[stat.ST_MTIME] + + for i in contents: + if i['path'] == filename: + i['mtime'] = str(newmtime) + def ContentsHandler(filename): infile = open(filename)