/[gentoo-src]/portage/pym/dcache.py
Gentoo

Contents of /portage/pym/dcache.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (show annotations) (download) (as text)
Sun Nov 14 01:32:00 2004 UTC (9 years, 10 months ago) by ferringb
Branch: MAIN
CVS Tags: portage_041204, HEAD
Branch point for: portage_2_1
Changes since 1.2: +2 -2 lines
File MIME type: text/x-python
make it less noisy.

1 """list based, symlink aware fs listdir caching implementation"""
2 import os,stat,weakref
3
4 def norm_path(mypath):
5 newpath = os.path.normpath(mypath)
6 if len(mypath) > 1:
7 if newpath[:2] == "//":
8 newpath=newpath.replace("//","/")
9 return newpath
10
11 def abs_link(sym):
12 # note this doesn't parse every single entry. that's up to you.
13 l=os.readlink(sym)
14 if l[0] != '/':
15 l = os.path.dirname(sym)+"/"+l
16 return norm_path(l)
17
18 class node:
19 """class representing a directory"""
20 __slots__=["dir_node_name","dir_node_ref","dirs","files","others","sdirs","sfiles","mtime"]
21 # instances = 0
22 def __init__(self):#,dirs=[],others=[],files=[],mtime=-1):
23 self.dir_node_ref=[]
24 self.dir_node_name=[]
25 self.dirs=[]
26 self.files=[]
27 self.others=[]
28 self.sdirs=[]
29 self.sfiles=[]
30 self.mtime=-1
31
32 # def __del__(self):
33 # print "del'ing a node"
34 # del self.dir_node_n
35 # del self.sdirs
36 # del self.sfiles
37 # del self.dirs
38 # del self.files
39 # del self.others
40 # del self.mtime
41
42 def get_node(self,nnode):
43 """retrieve directory node if exists"""
44 try:
45 x=self.dir_node_name.index(nnode)
46 return self.dir_node_ref[x]
47 except (ReferenceError,ValueError),e :
48 # print "ref exception",e
49 return None
50
51 def add_node(self,nnode,ref_node=None,symlink=False):
52 self.dir_node_name.append(nnode)
53 if ref_node:
54 if symlink:
55 self.dir_node_ref.append(weakref.proxy(ref_node))
56 else:
57 self.dir_node_ref.append(ref_node)
58 else:
59 self.dir_node_ref.append(node())
60 return self.dir_node_ref[-1]
61
62 def del_node(self,nnode):
63 try:
64 x=self.dir_node_name.index(nnode)
65 del self.dir_node_name[x]
66 del self.dir_node_ref[x]
67 except Exception,e:
68 print "warning, dcache was asked to delete a non_existant node: lacks node %s" % nnode
69 print "e=",e
70
71 def rec_del_node(self):
72 while len(self.dir_node_n):
73 self.dir_node_name[0].rec_del_node()
74 del self.dir_node_name[0][0]
75 del self.dir_node_ref[0]
76
77 class dcache:
78 def __init__(self):
79 self.cache=node()
80
81 def _get_dir_ent(self, mypath,symlink=False):
82 p=mypath.split("/")
83 if not p[0]:
84 p = p[1:]
85 if not p[-1]:
86 p = p[0:-1]
87 fullpath=''
88 c = self.cache
89 if not len(p):
90 return self.cache
91 while len(p):
92 fullpath += '/' + p[0]
93
94 g=c.get_node(p[0])
95 if not g:
96 st=os.lstat(fullpath)
97 if stat.S_ISLNK(st.st_mode):
98 # yippee
99 fullpath=abs_link(fullpath)
100 # print "hijacking %s to %s" % (p[0], fullpath)
101 g=self._get_dir_ent(fullpath)
102 g=c.add_node(p[0],ref_node=self._get_dir_ent(fullpath))
103 elif stat.S_ISDIR(st.st_mode):
104 g=c.add_node(p[0])
105 else:
106 raise Exception("%s is not a dir!" % fullpath)
107 c=g
108 p.pop(0)
109 if symlink:
110 c["actpath"]=fullpath
111 return c
112
113 def cacheddir(self, mypath):
114 mypath=norm_path(mypath)
115 if mypath=="/":
116 ent=self.cache
117 else:
118 ent = self._get_dir_ent(mypath)
119 # print "nodes mtime=",ent.mtime
120 listing_needed = (ent.mtime == -1)
121
122 mtime = os.stat(mypath).st_mtime
123 if not listing_needed:
124 # print "nost listing needed"
125 if mtime != ent.mtime:
126 print "mtime flipped it"
127 self._invalidate_dir_node(ent)
128 listing_needed = True
129
130 # print "ent = ",ent,"listing_needed=",listing_needed
131 if listing_needed:
132 ent.mtime=mtime
133 # print "set nodes mtime to",ent.mtime,"from",mtime
134 l = os.listdir(mypath)
135 for x in l:
136 try:
137 ps=os.lstat(mypath+"/"+x).st_mode
138 if stat.S_ISREG(ps):
139 ent.files.append(x)
140 elif stat.S_ISDIR(ps):
141 ent.dirs.append(x)
142 elif stat.S_ISLNK(ps):
143 ps=os.stat(mypath+"/"+x).st_mode
144 if stat.S_ISDIR(ps):
145 ent.sdirs.append(x)
146 else:
147 ent.sfiles.append(x)
148 else:
149 ent.others.append(x)
150 except:
151 ent.others.append(x)
152 return list(ent.dirs + ent.sdirs + ent.files + ent.sfiles + ent.others),\
153 [1 for x in ent.dirs]+[3 for x in ent.sdirs]+ \
154 [0 for x in ent.files]+ [2 for x in ent.sfiles]+[4 for x in ent.others]

  ViewVC Help
Powered by ViewVC 1.1.20