ia64/xen-unstable

view tools/python/xen/xend/XendDB.py @ 6552:a9873d384da4

Merge.
author adsharma@los-vmm.sc.intel.com
date Thu Aug 25 12:24:48 2005 -0700 (2005-08-25)
parents 112d44270733 fa0754a9f64f
children dfaf788ab18c
line source
1 #============================================================================
2 # This library is free software; you can redistribute it and/or
3 # modify it under the terms of version 2.1 of the GNU Lesser General Public
4 # License as published by the Free Software Foundation.
5 #
6 # This library is distributed in the hope that it will be useful,
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 # Lesser General Public License for more details.
10 #
11 # You should have received a copy of the GNU Lesser General Public
12 # License along with this library; if not, write to the Free Software
13 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14 #============================================================================
15 # Copyright (C) 2004, 2005 Mike Wray <mike.wray@hp.com>
16 #============================================================================
18 import os
19 import os.path
20 import errno
21 import dircache
22 import time
24 import sxp
25 import XendRoot
26 xroot = XendRoot.instance()
28 class XendDB:
29 """Persistence for Xend. Stores data in files and directories.
30 """
32 def __init__(self, path=None):
33 self.dbpath = xroot.get_dbroot()
34 if path:
35 self.dbpath = os.path.join(self.dbpath, path)
36 pass
38 def listdir(self, dpath):
39 try:
40 return dircache.listdir(dpath)
41 except:
42 return []
44 def filepath(self, path):
45 return os.path.join(self.dbpath, path)
47 def fetch(self, path):
48 fpath = self.filepath(path)
49 return self.fetchfile(fpath)
51 def fetchfile(self, fpath):
52 pin = sxp.Parser()
53 fin = file(fpath, "rb")
54 try:
55 while 1:
56 try:
57 buf = fin.read(1024)
58 except IOError, ex:
59 if ex.errno == errno.EINTR:
60 continue
61 else:
62 raise
63 pin.input(buf)
64 if buf == '':
65 pin.input_eof()
66 break
67 finally:
68 fin.close()
69 return pin.get_val()
71 def save(self, path, sxpr):
72 fpath = self.filepath(path)
73 return self.savefile(fpath, sxpr)
75 def savefile(self, fpath, sxpr):
76 backup = False
77 fdir = os.path.dirname(fpath)
78 if not os.path.isdir(fdir):
79 os.makedirs(fdir)
80 if os.path.exists(fpath):
81 backup = True
82 real_fpath = fpath
83 fpath += ".new."
85 fout = file(fpath, "wb+")
86 try:
87 try:
88 t = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
89 fout.write("# %s %s\n" % (fpath, t))
90 sxp.show(sxpr, out=fout)
91 finally:
92 fout.close()
93 except:
94 if backup:
95 try:
96 os.unlink(fpath)
97 except:
98 pass
99 raise
100 if backup:
101 os.rename(fpath, real_fpath)
103 def fetchall(self, path):
104 dpath = self.filepath(path)
105 d = {}
106 for k in self.listdir(dpath):
107 try:
108 v = self.fetchfile(os.path.join(dpath, k))
109 d[k] = v
110 except:
111 pass
112 return d
114 def saveall(self, path, d):
115 for (k, v) in d.items():
116 self.save(os.path.join(path, k), v)
118 def delete(self, path):
119 dpath = self.filepath(path)
120 os.unlink(dpath)
122 def ls(self, path):
123 dpath = self.filepath(path)
124 return self.listdir(dpath)