Coverage for drivers/fjournaler.py : 89%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# Copyright (C) Citrix Systems Inc.
2#
3# This program is free software; you can redistribute it and/or modify
4# it under the terms of the GNU Lesser General Public License as published
5# by the Free Software Foundation; version 2.1 only.
6#
7# This program is distributed in the hope that it will be useful,
8# but WITHOUT ANY WARRANTY; without even the implied warranty of
9# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10# GNU Lesser General Public License for more details.
11#
12# You should have received a copy of the GNU Lesser General Public License
13# along with this program; if not, write to the Free Software Foundation, Inc.,
14# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15#
16# File-based journaling
18import os
19import errno
21import util
22from journaler import JournalerException
24SEPARATOR = "_"
27class Journaler:
28 """Simple file-based journaler. A journal is a id-value pair, and there
29 can be only one journal for a given id."""
31 def __init__(self, dir):
32 self.dir = dir
34 def create(self, type, id, val):
35 """Create an entry of type "type" for "id" with the value "val".
36 Error if such an entry already exists."""
37 valExisting = self.get(type, id)
38 if valExisting:
39 raise JournalerException("Journal already exists for '%s:%s': %s" \
40 % (type, id, valExisting))
41 path = self._getPath(type, id)
42 f = open(path, "w")
43 f.write(val)
44 f.close()
46 def remove(self, type, id):
47 """Remove the entry of type "type" for "id". Error if the entry doesn't
48 exist."""
49 val = self.get(type, id)
50 if not val:
51 raise JournalerException("No journal for '%s:%s'" % (type, id))
52 path = self._getPath(type, id)
53 os.unlink(path)
55 def get(self, type, id):
56 """Get the value for the journal entry of type "type" for "id".
57 Return None if no such entry exists"""
58 path = self._getPath(type, id)
59 if not util.pathexists(path):
60 return None
61 try:
62 f = open(path, "r")
63 except IOError as e:
64 if e.errno == errno.ENOENT:
65 # the file can disappear any time, since there is no locking
66 return None
67 raise
68 val = f.readline()
69 return val
71 def getAll(self, type):
72 """Get a mapping id->value for all entries of type "type" """
73 fileList = os.listdir(self.dir)
74 entries = dict()
75 for fileName in fileList:
76 if not fileName.startswith(type):
77 continue
78 parts = fileName.split(SEPARATOR, 2)
79 if len(parts) != 2: 79 ↛ 80line 79 didn't jump to line 80, because the condition on line 79 was never true
80 raise JournalerException("Bad file name: %s" % fileName)
81 t, id = parts
82 if t != type:
83 continue
84 val = self.get(type, id)
85 if val:
86 entries[id] = val
87 return entries
89 def _getPath(self, type, id):
90 name = "%s%s%s" % (type, SEPARATOR, id)
91 path = os.path.join(self.dir, name)
92 return path