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

Contents of /portage/pym/portage_db_flat.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.19 - (show annotations) (download) (as text)
Wed Mar 9 09:10:18 2005 UTC (9 years, 5 months ago) by ferringb
Branch: MAIN
CVS Tags: HEAD
Changes since 1.18: +2 -2 lines
File MIME type: text/x-python
FILE REMOVED
banish the old cache classes to the attic.

1 # Copyright 2004 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3 # $Header: /var/cvsroot/gentoo-src/portage/pym/portage_db_flat.py,v 1.18 2005/02/26 06:35:20 jstubbs Exp $
4 cvs_id_string="$Id: portage_db_flat.py,v 1.18 2005/02/26 06:35:20 jstubbs Exp $"[5:-2]
5
6 import types
7 import os
8 import stat
9
10 import portage_db_template
11
12 class database(portage_db_template.database):
13 def module_init(self):
14 self.lastkey = None # Cache
15 self.lastval = None # Cache
16
17 self.fullpath = os.path.join(self.path,self.category)
18
19 if not os.path.exists(self.fullpath):
20 prevmask=os.umask(0)
21 os.makedirs(self.fullpath, 02775)
22 os.umask(prevmask)
23 try:
24 os.chown(self.fullpath, self.uid, self.gid)
25 os.chmod(self.fullpath, 02775)
26 except SystemExit, e:
27 raise
28 except:
29 pass
30
31 self.flushCache()
32
33 def __addMcache(self,key,val):
34 del self.__mcache_list[2]
35 self.__mcache_list.insert(0,val)
36 del self.__mcache_keys[2]
37 self.__mcache_keys.insert(0,key)
38
39 def __delMache(self,key):
40 i = self.__mcache_list.index(key)
41 self.__mcache_list[i] = None
42 self.__mcache_keys[i] = None
43
44 def flushCache(self):
45 portage_db_template.database.flushCache(self)
46 self.__mcache_list = [None,None,None]
47 self.__mcache_keys = [None,None,None]
48
49 def has_key(self,key):
50 if os.path.exists(os.path.join(self.fullpath,key)):
51 return 1
52 return 0
53
54 def keys(self):
55 # XXX: NEED TOOLS SEPERATED
56 # return portage.listdir(self.fullpath,filesonly=1)
57 mykeys = []
58 for x in os.listdir(self.fullpath):
59 if os.path.isfile(self.fullpath+x) and not x.beginswith(".update."):
60 mykeys += [x]
61 return mykeys
62
63 def get_timestamp(self,key,locking=True):
64 import traceback
65 traceback.print_stack()
66 if key in self.__mcache_keys:
67 return self.__mcache_list[self.__mcache_keys.index(key)]
68 lock=portage_locks.lockfile(os.path.join(self.fullpath,key),wantnewlockfile=1)
69 try: x=os.stat(os.path.join(self.fullpath,key))[stat.ST_MTIME]
70 except OSError: x=None
71 self.__addMcache(key,x)
72 portage_locks.unlockfile(lock)
73 return x
74
75 def get_values(self,key):
76 if not key:
77 raise KeyError, "key is not set to a valid value"
78
79 # mylock = portage_locks.lockfile(self.fullpath+key, wantnewlockfile=1)
80 # if self.has_key(key):
81 try:
82 # self.get_timestamp(key,locking=False)
83 myf = open(os.path.join(self.fullpath,key),"r")
84 mtime = os.fstat(myf.fileno()).st_mtime
85 myl = myf.readlines()
86 myf.close()
87
88 dict = {"_mtime_":mtime}
89
90 if len(myl) != len(self.dbkeys):
91 raise ValueError, "Key count mismatch"
92 for x in range(0,len(myl)):
93 if myl[x] and myl[x][-1] == "\n":
94 dict[self.dbkeys[x]] = myl[x][:-1]
95 else:
96 dict[self.dbkeys[x]] = myl[x]
97
98 return dict
99 except OSError:
100 return None
101
102 def set_values(self,key,val):
103 if not key:
104 raise KeyError, "No key provided. key:%s val:%s" % (key,val)
105 if not val:
106 raise ValueError, "No value provided. key:%s val:%s" % (key,val)
107 update_fp = os.path.join(self.fullpath, ".update.%i.%s" % (os.getpid(), key))
108 myf = open(update_fp, "w")
109 myf.writelines( [ val[x] +"\n" for x in self.dbkeys] )
110 myf.close()
111
112 os.chown(update_fp, self.uid, self.gid)
113 os.chmod(update_fp, 0664)
114 os.utime(update_fp, (-1,long(val["_mtime_"])))
115 os.rename(update_fp, os.path.join(self.fullpath,key))
116
117 def del_key(self,key):
118 self.lastkey = None
119 self.lastval = None
120 try:
121 os.unlink(os.path.join(self.fullpath,key))
122 except OSError:
123 # either someone beat us to it, or the key doesn't exist.
124 # either way, it's gone, so we return false
125 return False
126 return True
127
128 def sync(self):
129 return
130
131 def close(self):
132 return
133

  ViewVC Help
Powered by ViewVC 1.1.20