• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

zopefoundation / ZODB / 8679957491

30 Mar 2024 11:42AM CUT coverage: 83.744%. Remained the same
8679957491

push

github

dataflake
- vb [ci skip]

2878 of 4051 branches covered (71.04%)

13348 of 15939 relevant lines covered (83.74%)

0.84 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/ZODB/scripts/checkbtrees.py
1
#!/usr/bin/env python
2
"""Check the consistency of BTrees in a Data.fs
×
3

4
usage: checkbtrees.py data.fs
5

6
Try to find all the BTrees in a Data.fs, call their _check() methods,
7
and run them through BTrees.check.check().
8
"""
9

10
from BTrees.check import check
×
11

12
import ZODB
×
13
from ZODB.FileStorage import FileStorage
×
14

15

16
# Set of oids we've already visited.  Since the object structure is
17
# a general graph, this is needed to prevent unbounded paths in the
18
# presence of cycles.  It's also helpful in eliminating redundant
19
# checking when a BTree is pointed to by many objects.
20
oids_seen = {}
×
21

22
# Append (obj, path) to L if and only if obj is a persistent object
23
# and we haven't seen it before.
24

25

26
def add_if_new_persistent(L, obj, path):
×
27
    global oids_seen
28

29
    getattr(obj, '_', None)  # unghostify
×
30
    if hasattr(obj, '_p_oid'):
×
31
        oid = obj._p_oid
×
32
        if oid not in oids_seen:
×
33
            L.append((obj, path))
×
34
            oids_seen[oid] = 1
×
35

36

37
def get_subobjects(obj):
×
38
    getattr(obj, '_', None)  # unghostify
×
39
    sub = []
×
40
    try:
×
41
        attrs = obj.__dict__.items()
×
42
    except AttributeError:
×
43
        attrs = ()
×
44
    for pair in attrs:
×
45
        sub.append(pair)
×
46

47
    # what if it is a mapping?
48
    try:
×
49
        items = obj.items()
×
50
    except AttributeError:
×
51
        items = ()
×
52
    for k, v in items:
×
53
        if not isinstance(k, int):
×
54
            sub.append(("<key>", k))
×
55
        if not isinstance(v, int):
×
56
            sub.append(("[%s]" % repr(k), v))
×
57

58
    # what if it is a sequence?
59
    i = 0
×
60
    while 1:
61
        try:
×
62
            elt = obj[i]
×
63
        except:  # noqa: E722 do not use bare 'except'
×
64
            break
×
65
        sub.append(("[%d]" % i, elt))
×
66
        i += 1
×
67

68
    return sub
×
69

70

71
def main(fname=None):
×
72
    if fname is None:
×
73
        import sys
×
74
        try:
×
75
            fname, = sys.argv[1:]
×
76
        except:  # noqa: E722 do not use bare 'except'
×
77
            print(__doc__)
×
78
            sys.exit(2)
×
79

80
    fs = FileStorage(fname, read_only=1)
×
81
    cn = ZODB.DB(fs).open()
×
82
    rt = cn.root()
×
83
    todo = []
×
84
    add_if_new_persistent(todo, rt, '')
×
85

86
    found = 0
×
87
    while todo:
×
88
        obj, path = todo.pop(0)
×
89
        found += 1
×
90
        if not path:
×
91
            print("<root>", repr(obj))
×
92
        else:
93
            print(path, repr(obj))
×
94

95
        mod = str(obj.__class__.__module__)
×
96
        if mod.startswith("BTrees"):
×
97
            if hasattr(obj, "_check"):
×
98
                try:
×
99
                    obj._check()
×
100
                except AssertionError as msg:
×
101
                    print("*" * 60)
×
102
                    print(msg)
×
103
                    print("*" * 60)
×
104

105
                try:
×
106
                    check(obj)
×
107
                except AssertionError as msg:
×
108
                    print("*" * 60)
×
109
                    print(msg)
×
110
                    print("*" * 60)
×
111

112
        if found % 100 == 0:
×
113
            cn.cacheMinimize()
×
114

115
        for k, v in get_subobjects(obj):
×
116
            if k.startswith('['):
×
117
                # getitem
118
                newpath = "{}{}".format(path, k)
×
119
            else:
120
                newpath = "{}.{}".format(path, k)
×
121
            add_if_new_persistent(todo, v, newpath)
×
122

123
    print("total", len(fs._index), "found", found)
×
124

125

126
if __name__ == "__main__":
×
127
    main()
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc