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

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

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

#!/usr/bin/python 

# 

# Copyright (C) Citrix Systems Inc. 

# 

# This program is free software; you can redistribute it and/or modify  

# it under the terms of the GNU Lesser General Public License as published  

# by the Free Software Foundation; version 2.1 only. 

# 

# This program is distributed in the hope that it will be useful,  

# but WITHOUT ANY WARRANTY; without even the implied warranty of  

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the  

# GNU Lesser General Public License for more details. 

# 

# You should have received a copy of the GNU Lesser General Public License 

# along with this program; if not, write to the Free Software Foundation, Inc., 

# 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA 

# 

# File-based journaling 

 

import os 

import errno 

 

import util 

from journaler import JournalerException 

 

SEPARATOR = "_" 

 

class Journaler: 

    """Simple file-based journaler. A journal is a id-value pair, and there 

    can be only one journal for a given id.""" 

 

    def __init__(self, dir): 

        self.dir = dir 

 

    def create(self, type, id, val): 

        """Create an entry of type "type" for "id" with the value "val". 

        Error if such an entry already exists.""" 

        valExisting = self.get(type, id) 

        if valExisting: 

            raise JournalerException("Journal already exists for '%s:%s': %s" \ 

                    % (type, id, valExisting)) 

        path = self._getPath(type, id) 

        f = open(path, "w") 

        f.write(val) 

        f.close() 

 

    def remove(self, type, id): 

        """Remove the entry of type "type" for "id". Error if the entry doesn't 

        exist.""" 

        val = self.get(type, id) 

        if not val: 

            raise JournalerException("No journal for '%s:%s'" % (type, id)) 

        path = self._getPath(type, id) 

        os.unlink(path) 

 

    def get(self, type, id): 

        """Get the value for the journal entry of type "type" for "id". 

        Return None if no such entry exists""" 

        path = self._getPath(type, id) 

        if not util.pathexists(path): 

            return None 

        try: 

            f = open(path, "r") 

        except IOError, e: 

            if e.errno == errno.ENOENT: 

                # the file can disappear any time, since there is no locking 

                return None 

            raise 

        val = f.readline() 

        return val 

 

    def getAll(self, type): 

        """Get a mapping id->value for all entries of type "type" """ 

        fileList = os.listdir(self.dir) 

        entries = dict() 

        for fileName in fileList: 

            if not fileName.startswith(type): 

                continue 

            parts = fileName.split(SEPARATOR, 2) 

            if len(parts) != 2: 

                raise JournalerException("Bad file name: %s" % fileName) 

            t, id = parts 

            if t != type: 

                continue 

            val = self.get(type, id) 

            if val: 

                entries[id] = val 

        return entries 

 

    def _getPath(self, type, id): 

        name = "%s%s%s" % (type, SEPARATOR, id) 

        path = os.path.join(self.dir, name) 

        return path 

 

 

########################################################################### 

# 

#  Unit tests 

# 

def _runTests(): 

    """Unit testing""" 

    dir = "/tmp" 

    print "Running unit tests..." 

 

    j = Journaler(dir) 

    if j.get("clone", "1"): 

        print "get non-existing failed" 

        return 1 

    j.create("clone", "1", "a") 

    val = j.get("clone", "1") 

    if val != "a": 

        print "create-get failed" 

        return 1 

    j.remove("clone", "1") 

    if j.get("clone", "1"): 

        print "remove failed" 

        return 1 

    j.create("modify", "X", "831_3") 

    j.create("modify", "Z", "831_4") 

    j.create("modify", "Y", "53_0") 

    val = j.get("modify", "X") 

    if val != "831_3": 

        print "create underscore_val failed" 

        return 1 

    val = j.get("modify", "Y") 

    if val != "53_0": 

        print "create multiple id's failed" 

        return 1 

    entries = j.getAll("modify") 

    if not entries.get("X") or not entries.get("Y") or \ 

            entries["X"] != "831_3"  or entries["Y"] != "53_0": 

        print "getAll failed: %s" % entries 

        return 1 

    j.remove("modify", "X") 

    val = j.getAll("modify") 

    if val.get("X") or not val.get("Y") or val["Y"] != "53_0": 

        print "remove(X) failed" 

        return 1 

    j.remove("modify", "Y") 

    j.remove("modify", "Z") 

    if j.get("modify", "Y"): 

        print "remove(Y) failed" 

        return 1 

    if j.get("modify", "Z"): 

        print "remove(Z) failed" 

        return 1 

    print "All tests passed" 

    return 0 

 

151if __name__ == '__main__': 

    import sys 

    ret = _runTests() 

    sys.exit(ret)