Lines 1-11
Link Here
|
1 |
# portage_contents.py -- (Persistent) Contents File Management |
1 |
# portage_contents.py -- (Persistent) Contents File Management |
2 |
# Copyright 1998-2004 Gentoo Foundation |
2 |
# Copyright 1998-2004 Gentoo Foundation |
3 |
# Distributed under the terms of the GNU General Public License v2 |
3 |
# Distributed under the terms of the GNU General Public License v2 |
4 |
# $Header: /var/cvsroot/gentoo-src/portage/pym/portage_contents.py,v 1.3 2004/10/04 14:07:40 vapier Exp $ |
4 |
# $Header: $ |
5 |
|
5 |
|
6 |
import os,string,types,sys,copy |
6 |
import os,string,types,sys,copy,stat,re |
7 |
import portage_exception |
7 |
import portage_exception |
8 |
import portage_const |
8 |
import portage_const |
|
|
9 |
import portage_checksum |
9 |
|
10 |
|
10 |
#import gettext |
11 |
#import gettext |
11 |
#gettext_t = gettext.translation('portage.contents', portage_const.LOCALE_DATA_PATH) |
12 |
#gettext_t = gettext.translation('portage.contents', portage_const.LOCALE_DATA_PATH) |
Lines 16-22
Link Here
|
16 |
|
17 |
|
17 |
FILES_KEY = "\0FILES\0" |
18 |
FILES_KEY = "\0FILES\0" |
18 |
OWNER_KEY = "\0OWNER\0" |
19 |
OWNER_KEY = "\0OWNER\0" |
|
|
20 |
ATTRIBUTES_KEY = "\0ATTRIBUTES\0" |
19 |
|
21 |
|
|
|
22 |
class ContentsManager: |
23 |
"""Manage a CONTENTS file.""" |
24 |
def __init__(self,filename): |
25 |
|
26 |
self.contents = {} |
27 |
|
28 |
self.filename = filename |
29 |
self.readFile(filename) |
30 |
|
31 |
def readFile(self,filename): |
32 |
contents = self.contents |
33 |
pkgfiles = {} |
34 |
|
35 |
try: |
36 |
myc=open(filename,"r") |
37 |
except OSError, details: |
38 |
return (None, str(details)) |
39 |
except IOError, details: |
40 |
return (None, str(details)) |
41 |
mylines=myc.readlines() |
42 |
myc.close() |
43 |
pos=1 |
44 |
root="/" |
45 |
for line in mylines: |
46 |
mydat = string.split(line) |
47 |
# we do this so we can remove from non-root filesystems |
48 |
# (use the ROOT var to allow maintenance on other partitions) |
49 |
try: |
50 |
mydat[1]=os.path.normpath(root+mydat[1][1:]) |
51 |
if mydat[0]=="obj": |
52 |
#format: type, mtime, md5sum |
53 |
pkgfiles[string.join(mydat[1:-2]," ")]={'type' : mydat[0], 'md5' : mydat[-2], 'mtime' : mydat[-1]} |
54 |
elif mydat[0]=="dir": |
55 |
#format: type |
56 |
pkgfiles[string.join(mydat[1:])]={'type' : mydat[0] } |
57 |
elif mydat[0]=="sym": |
58 |
#format: type, mtime, dest |
59 |
x=len(mydat)-1 |
60 |
if (x >= 13) and (mydat[-1][-1]==')'): # Old/Broken symlink entry |
61 |
mydat = mydat[:-10]+[mydat[-10:][stat.ST_MTIME][:-1]] |
62 |
writemsg("FIXED SYMLINK LINE: %s\n" % mydat, 1) |
63 |
x=len(mydat)-1 |
64 |
splitter=-1 |
65 |
while(x>=0): |
66 |
if mydat[x]=="->": |
67 |
splitter=x |
68 |
break |
69 |
x=x-1 |
70 |
if splitter==-1: |
71 |
return None |
72 |
pkgfiles[string.join(mydat[1:splitter]," ")]={'type' : mydat[0], 'mtime' : mydat[-1], 'target' : string.join(mydat[(splitter+1):-1]," ")} |
73 |
elif mydat[0]=="dev": |
74 |
#format: type |
75 |
pkgfiles[string.join(mydat[1:]," ")]={'type' : mydat[0]} |
76 |
elif mydat[0]=="fif": |
77 |
#format: type |
78 |
pkgfiles[string.join(mydat[1:]," ")]={'type' : mydat[0]} |
79 |
else: |
80 |
return None |
81 |
except (KeyError,IndexError): |
82 |
print "portage: CONTENTS line",pos,"corrupt!" |
83 |
pos += 1 |
84 |
|
85 |
files = pkgfiles.keys() |
86 |
for file in files: |
87 |
elements = re.sub("^/*", "", file).split("/") |
88 |
subcontent = self.contents |
89 |
for element in elements: |
90 |
if not subcontent.has_key(element): |
91 |
subcontent[element] = {} |
92 |
subcontent = subcontent[element] |
93 |
subcontent[ATTRIBUTES_KEY] = pkgfiles[file] |
94 |
|
95 |
def writeContents(self, subcontent, key, path, outfile): |
96 |
|
97 |
if key == ATTRIBUTES_KEY: |
98 |
if subcontent['type'] in ["dir","dev","fif"]: |
99 |
outfile.write("%s\t%s\n" % (subcontent['type'], path)) |
100 |
elif subcontent['type'] == "obj": |
101 |
outfile.write("%s\t%s\t%s\t%s\n" % (subcontent['type'], path, subcontent['md5'], subcontent['mtime'])) |
102 |
elif subcontent['type'] == "sym": |
103 |
outfile.write("%s\t%s\t->\t%s\t%s\n" % (subcontent['type'], path, subcontent['target'], subcontent['mtime'])) |
104 |
else: |
105 |
if key != "": |
106 |
path = path+"/"+key |
107 |
keys = subcontent.keys() |
108 |
keys.sort() |
109 |
for key in keys: |
110 |
self.writeContents(subcontent[key], key, path, outfile) |
111 |
|
112 |
def writeFile(self,filename=""): |
113 |
contents=self.contents |
114 |
if filename == "": |
115 |
filename = self.filename |
116 |
|
117 |
newfilename = filename+".new" |
118 |
|
119 |
mykeys=contents.keys() |
120 |
mykeys.sort() |
121 |
|
122 |
outfile=open(newfilename,"w") |
123 |
self.writeContents(contents, "", "", outfile) |
124 |
outfile.close() |
125 |
os.rename(newfilename, filename) |
126 |
return |
127 |
|
128 |
def __getitem__(self, key): |
129 |
if re.match("^/", key): |
130 |
elements = re.sub("^/*", "", key).split("/") |
131 |
subcontent = self.contents |
132 |
for element in elements: |
133 |
subcontent = subcontent[element] |
134 |
return subcontent[ATTRIBUTES_KEY] |
135 |
else: |
136 |
return self.contents[key] |
137 |
|
138 |
def __setitem__(self, key, value): |
139 |
if re.match("^/", key): |
140 |
elements = re.sub("^/*", "", key).split("/") |
141 |
subcontent = self.contents |
142 |
for element in elements: |
143 |
if not subcontent.has_key(element): |
144 |
subcontent[element] = {} |
145 |
subcontent = subcontent[element] |
146 |
subcontent[ATTRIBUTES_KEY] = value |
147 |
|
148 |
def getMD5(self,filename): |
149 |
contents = self.contents |
150 |
return self.getAttribute(filename, 'md5') |
151 |
|
152 |
def getMtime(self,filename): |
153 |
contents = self.contents |
154 |
return self.getAttribute(filename, 'mtime') |
155 |
|
156 |
def getAttribute(self,filename, attribute): |
157 |
elements = re.sub("^/*", "", filename).split("/") |
158 |
subcontent = self.contents |
159 |
for element in elements: |
160 |
subcontent = subcontent[element] |
161 |
return subcontent[ATTRIBUTES_KEY][attribute] |
162 |
|
163 |
def setAttribute(self,filename, attribute, value): |
164 |
elements = re.sub("^/*", "", filename).split("/") |
165 |
subcontent = self.contents |
166 |
for element in elements: |
167 |
subcontent = subcontent[element] |
168 |
subcontent[ATTRIBUTES_KEY][attribute] = value |
169 |
|
170 |
def updateAllAttributes(self,filename): |
171 |
self.setMD5(filename) |
172 |
self.setMtime(filename) |
173 |
|
174 |
def setMD5(self,filename, newmd5=""): |
175 |
contents = self.contents |
176 |
if newmd5 == "": |
177 |
newmd5=portage_checksum.perform_md5(filename, calc_prelink=1) |
178 |
|
179 |
self.setAttribute(filename, 'md5', newmd5) |
180 |
|
181 |
def setMtime(self, filename, newmtime=""): |
182 |
contents = self.contents |
183 |
if newmtime == "": |
184 |
pathstat = os.lstat(filename) |
185 |
newmtime = pathstat[stat.ST_MTIME] |
186 |
|
187 |
self.setAttribute(filename, 'mtime', str(newmtime)) |
188 |
|
189 |
|
20 |
|
190 |
|
21 |
def ContentsHandler(filename): |
191 |
def ContentsHandler(filename): |
22 |
infile = open(filename) |
192 |
infile = open(filename) |