aboutsummaryrefslogtreecommitdiff
blob: 5f3242e7e3a99d49048a1de47d650f57a84b4438 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# Copyright: 2005 Gentoo Foundation
# Author(s): Brian Harring (ferringb@gentoo.org)
# License: GPL2
# $Header: /local/data/ulm/cvs/history/var/cvsroot/gentoo-src/portage/portage/cache/anydbm.py,v 1.1 2005/07/10 09:21:05 ferringb Exp $

anydbm_module = __import__("anydbm")
import cPickle, os
import fs_template
import cache_errors


class database(fs_template.FsBased):

	autocommits = True

	def __init__(self, *args, **config):
		super(database,self).__init__(*args, **config)

		default_db = config.get("dbtype","anydbm")
		if not default_db.startswith("."):
			default_db = '.' + default_db

		self._db_path = os.path.join(self.location, fs_template.gen_label(self.location, self.label)+default_db)
		print "opening self._db_path=",self._db_path
		self.__db = None
		try:
			self.__db = anydbm_module.open(self._db_path, "w", self._perms)
			try:
				self._ensure_dirs()
				self._ensure_dirs(self._db_path)
				self._ensure_access(self._db_path)
				
			except (OSError, IOError), e:
				raise cache_errors.InitializationError(self.__clas__, e)
			# try again if failed
			if self.__db == None:
				self.__db = anydbm_module.open(self._db_path, "c", self._perms)


		except anydbm_module.error, e:
			# XXX handle this at some point
			raise


	def __getitem__(self, cpv):
		# we override getitem because it's just a cpickling of the data handed in.
		return cPickle.loads(self.__db[cpv])


	def _setitem(self, cpv, values):
		self.__db[cpv] = cPickle.dumps(values,cPickle.HIGHEST_PROTOCOL)

	def _delitem(self, cpv):
		del self.__db[cpv]


	def iterkeys(self):
		return iter(self.__db)


	def has_key(self, cpv):
		return cpv in self.__db

	def commit(self):	pass

	def __del__(self):
		print "keys=",self.__db.keys()
		self.__db.sync()
		self.__db.close()