~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/check.py

  • Committer: mbp at sourcefrog
  • Date: 2005-04-06 14:06:32 UTC
  • Revision ID: mbp@sourcefrog.net-20050406140632-b44f572e110216f266a43b71
pychecker fixups

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#! /usr/bin/python
2
 
 
3
 
 
4
1
# Copyright (C) 2004, 2005 by Martin Pool
5
2
# Copyright (C) 2005 by Canonical Ltd
6
3
 
7
 
 
8
4
# This program is free software; you can redistribute it and/or modify
9
5
# it under the terms of the GNU General Public License as published by
10
6
# the Free Software Foundation; either version 2 of the License, or
24
20
######################################################################
25
21
# consistency checks
26
22
 
27
 
def check():
28
 
    """Consistency check of tree."""
29
 
    assert_in_tree()
30
 
    mutter("checking tree")
31
 
    check_patches_exist()
32
 
    check_patch_chaining()
33
 
    check_patch_uniqueness()
34
 
    check_inventory()
35
 
    mutter("tree looks OK")
36
 
    ## TODO: Check that previous-inventory and previous-manifest
37
 
    ## are the same as those stored in the previous changeset.
38
 
 
39
 
    ## TODO: Check all patches present in patch directory are
40
 
    ## mentioned in patch history; having an orphaned patch only gives
41
 
    ## a warning.
42
 
 
43
 
    ## TODO: Check cached data is consistent with data reconstructed
44
 
    ## from scratch.
45
 
 
46
 
    ## TODO: Check no control files are versioned.
47
 
 
48
 
    ## TODO: Check that the before-hash of each file in a later
49
 
    ## revision matches the after-hash in the previous revision to
50
 
    ## touch it.
51
 
 
52
 
 
53
 
def check_inventory():
54
 
    mutter("checking inventory file and ids...")
55
 
    seen_ids = Set()
56
 
    seen_names = Set()
57
 
    
58
 
    for l in controlfile('inventory').readlines():
59
 
        parts = l.split()
60
 
        if len(parts) != 2:
61
 
            bailout("malformed inventory line: " + `l`)
62
 
        file_id, name = parts
63
 
        
64
 
        if file_id in seen_ids:
65
 
            bailout("duplicated file id " + file_id)
66
 
        seen_ids.add(file_id)
67
 
 
68
 
        if name in seen_names:
69
 
            bailout("duplicated file name in inventory: " + quotefn(name))
70
 
        seen_names.add(name)
71
 
        
72
 
        if is_control_file(name):
73
 
            raise BzrError("control file %s present in inventory" % quotefn(name))
74
 
 
75
 
 
76
 
def check_patches_exist():
77
 
    """Check constraint of current version: all patches exist"""
78
 
    mutter("checking all patches are present...")
79
 
    for pid in revision_history():
80
 
        read_patch_header(pid)
81
 
 
82
 
 
83
 
def check_patch_chaining():
84
 
    """Check ancestry of patches and history file is consistent"""
85
 
    mutter("checking patch chaining...")
86
 
    prev = None
87
 
    for pid in revision_history():
88
 
        log_prev = read_patch_header(pid).precursor
89
 
        if log_prev != prev:
90
 
            bailout("inconsistent precursor links on " + pid)
91
 
        prev = pid
92
 
 
93
 
 
94
 
def check_patch_uniqueness():
95
 
    """Make sure no patch is listed twice in the history.
96
 
 
97
 
    This should be implied by having correct ancestry but I'll check it
98
 
    anyhow."""
99
 
    mutter("checking history for duplicates...")
100
 
    seen = Set()
101
 
    for pid in revision_history():
102
 
        if pid in seen:
103
 
            bailout("patch " + pid + " appears twice in history")
104
 
        seen.add(pid)
105
 
        
 
23
import sys
 
24
from sets import Set
 
25
 
 
26
from trace import mutter
 
27
from errors import bailout
 
28
import osutils
 
29
 
 
30
def check(branch, progress=True):
 
31
    out = sys.stdout
 
32
 
 
33
    if progress:
 
34
        def p(m):
 
35
            mutter('checking ' + m)
 
36
            out.write('\rchecking: %-50.50s' % m)
 
37
            out.flush()
 
38
    else:
 
39
        def p(m):
 
40
            mutter('checking ' + m)
 
41
 
 
42
    p('history of %r' % branch.base)
 
43
    last_ptr = None
 
44
    checked_revs = Set()
 
45
    
 
46
    history = branch.revision_history()
 
47
    revno = 0
 
48
    revcount = len(history)
 
49
 
 
50
    checked_texts = {}
 
51
    
 
52
    for rid in history:
 
53
        revno += 1
 
54
        p('revision %d/%d' % (revno, revcount))
 
55
        mutter('    revision {%s}' % rid)
 
56
        rev = branch.get_revision(rid)
 
57
        if rev.revision_id != rid:
 
58
            bailout('wrong internal revision id in revision {%s}' % rid)
 
59
        if rev.precursor != last_ptr:
 
60
            bailout('mismatched precursor in revision {%s}' % rid)
 
61
        last_ptr = rid
 
62
        if rid in checked_revs:
 
63
            bailout('repeated revision {%s}' % rid)
 
64
        checked_revs.add(rid)
 
65
 
 
66
        ## TODO: Check all the required fields are present on the revision.
 
67
 
 
68
        inv = branch.get_inventory(rev.inventory_id)
 
69
        seen_ids = Set()
 
70
        seen_names = Set()
 
71
 
 
72
        p('revision %d/%d file ids' % (revno, revcount))
 
73
        for file_id in inv:
 
74
            if file_id in seen_ids:
 
75
                bailout('duplicated file_id {%s} in inventory for revision {%s}'
 
76
                        % (file_id, rid))
 
77
            seen_ids.add(file_id)
 
78
 
 
79
        i = 0
 
80
        len_inv = len(inv)
 
81
        for file_id in inv:
 
82
            i += 1
 
83
            if (i % 100) == 0:
 
84
                p('revision %d/%d file text %d/%d' % (revno, revcount, i, len_inv))
 
85
 
 
86
            ie = inv[file_id]
 
87
 
 
88
            if ie.parent_id != None:
 
89
                if ie.parent_id not in seen_ids:
 
90
                    bailout('missing parent {%s} in inventory for revision {%s}'
 
91
                            % (ie.parent_id, rid))
 
92
 
 
93
            if ie.kind == 'file':
 
94
                if ie.text_id in checked_texts:
 
95
                    fp = checked_texts[ie.text_id]
 
96
                else:
 
97
                    if not ie.text_id in branch.text_store:
 
98
                        bailout('text {%s} not in text_store' % ie.text_id)
 
99
 
 
100
                    tf = branch.text_store[ie.text_id]
 
101
                    fp = osutils.fingerprint_file(tf)
 
102
                    checked_texts[ie.text_id] = fp
 
103
 
 
104
                if ie.text_size != fp['size']:
 
105
                    bailout('text {%s} wrong size' % ie.text_id)
 
106
                if ie.text_sha1 != fp['sha1']:
 
107
                    bailout('text {%s} wrong sha1' % ie.text_id)
 
108
            elif ie.kind == 'directory':
 
109
                if ie.text_sha1 != None or ie.text_size != None or ie.text_id != None:
 
110
                    bailout('directory {%s} has text in revision {%s}'
 
111
                            % (file_id, rid))
 
112
 
 
113
        p('revision %d/%d file paths' % (revno, revcount))
 
114
        for path, ie in inv.iter_entries():
 
115
            if path in seen_names:
 
116
                bailout('duplicated path %r in inventory for revision {%s}' % (path, revid))
 
117
            seen_names.add(path)
 
118
 
 
119
 
 
120
    p('done')
 
121
    if progress:
 
122
        print 
 
123
 
106
124