contrib/lock-checker.py
author Augie Fackler <raf@durin42.com>
Wed, 01 Aug 2012 22:13:27 -0500
changeset 17669 405b5f8fdad7
child 17738 b8424c92ba2b
permissions -rw-r--r--
lock-checker: new contrib extension based on work done by Mads This makes it possible to do lock validation as part of a normal test run. I didn't attempt any wlock validation because that's a bit more subtle to detect properly. Thanks to the initial patch from Mads for the idea.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17669
405b5f8fdad7 lock-checker: new contrib extension based on work done by Mads
Augie Fackler <raf@durin42.com>
parents:
diff changeset
     1
"""Extension to verify locks are obtained in the required places.
405b5f8fdad7 lock-checker: new contrib extension based on work done by Mads
Augie Fackler <raf@durin42.com>
parents:
diff changeset
     2
405b5f8fdad7 lock-checker: new contrib extension based on work done by Mads
Augie Fackler <raf@durin42.com>
parents:
diff changeset
     3
This works by wrapping functions that should be surrounded by a lock
405b5f8fdad7 lock-checker: new contrib extension based on work done by Mads
Augie Fackler <raf@durin42.com>
parents:
diff changeset
     4
and asserting the lock is held. Missing locks are called out with a
405b5f8fdad7 lock-checker: new contrib extension based on work done by Mads
Augie Fackler <raf@durin42.com>
parents:
diff changeset
     5
traceback printed to stderr.
405b5f8fdad7 lock-checker: new contrib extension based on work done by Mads
Augie Fackler <raf@durin42.com>
parents:
diff changeset
     6
405b5f8fdad7 lock-checker: new contrib extension based on work done by Mads
Augie Fackler <raf@durin42.com>
parents:
diff changeset
     7
This currently only checks store locks, not working copy locks.
405b5f8fdad7 lock-checker: new contrib extension based on work done by Mads
Augie Fackler <raf@durin42.com>
parents:
diff changeset
     8
"""
405b5f8fdad7 lock-checker: new contrib extension based on work done by Mads
Augie Fackler <raf@durin42.com>
parents:
diff changeset
     9
import os
405b5f8fdad7 lock-checker: new contrib extension based on work done by Mads
Augie Fackler <raf@durin42.com>
parents:
diff changeset
    10
import traceback
405b5f8fdad7 lock-checker: new contrib extension based on work done by Mads
Augie Fackler <raf@durin42.com>
parents:
diff changeset
    11
405b5f8fdad7 lock-checker: new contrib extension based on work done by Mads
Augie Fackler <raf@durin42.com>
parents:
diff changeset
    12
def _warnstack(ui, msg, skip=1):
405b5f8fdad7 lock-checker: new contrib extension based on work done by Mads
Augie Fackler <raf@durin42.com>
parents:
diff changeset
    13
    '''issue warning with the message and the current stack, skipping the
405b5f8fdad7 lock-checker: new contrib extension based on work done by Mads
Augie Fackler <raf@durin42.com>
parents:
diff changeset
    14
    skip last entries'''
405b5f8fdad7 lock-checker: new contrib extension based on work done by Mads
Augie Fackler <raf@durin42.com>
parents:
diff changeset
    15
    ui.warn('%s at:\n' % msg)
405b5f8fdad7 lock-checker: new contrib extension based on work done by Mads
Augie Fackler <raf@durin42.com>
parents:
diff changeset
    16
    entries = traceback.extract_stack()[:-skip]
405b5f8fdad7 lock-checker: new contrib extension based on work done by Mads
Augie Fackler <raf@durin42.com>
parents:
diff changeset
    17
    fnmax = max(len(entry[0]) for entry in entries)
405b5f8fdad7 lock-checker: new contrib extension based on work done by Mads
Augie Fackler <raf@durin42.com>
parents:
diff changeset
    18
    for fn, ln, func, _text in entries:
405b5f8fdad7 lock-checker: new contrib extension based on work done by Mads
Augie Fackler <raf@durin42.com>
parents:
diff changeset
    19
        ui.warn(' %*s:%-4s in %s\n' % (fnmax, fn, ln, func))
405b5f8fdad7 lock-checker: new contrib extension based on work done by Mads
Augie Fackler <raf@durin42.com>
parents:
diff changeset
    20
405b5f8fdad7 lock-checker: new contrib extension based on work done by Mads
Augie Fackler <raf@durin42.com>
parents:
diff changeset
    21
def _checklock(repo):
405b5f8fdad7 lock-checker: new contrib extension based on work done by Mads
Augie Fackler <raf@durin42.com>
parents:
diff changeset
    22
    l = repo._lockref and repo._lockref()
405b5f8fdad7 lock-checker: new contrib extension based on work done by Mads
Augie Fackler <raf@durin42.com>
parents:
diff changeset
    23
    if l is None or not l.held:
405b5f8fdad7 lock-checker: new contrib extension based on work done by Mads
Augie Fackler <raf@durin42.com>
parents:
diff changeset
    24
        _warnstack(repo.ui, 'missing lock', skip=2)
405b5f8fdad7 lock-checker: new contrib extension based on work done by Mads
Augie Fackler <raf@durin42.com>
parents:
diff changeset
    25
405b5f8fdad7 lock-checker: new contrib extension based on work done by Mads
Augie Fackler <raf@durin42.com>
parents:
diff changeset
    26
def reposetup(ui, repo):
405b5f8fdad7 lock-checker: new contrib extension based on work done by Mads
Augie Fackler <raf@durin42.com>
parents:
diff changeset
    27
    orig = repo.__class__
405b5f8fdad7 lock-checker: new contrib extension based on work done by Mads
Augie Fackler <raf@durin42.com>
parents:
diff changeset
    28
    class lockcheckrepo(repo.__class__):
405b5f8fdad7 lock-checker: new contrib extension based on work done by Mads
Augie Fackler <raf@durin42.com>
parents:
diff changeset
    29
        def _writejournal(self, *args, **kwargs):
405b5f8fdad7 lock-checker: new contrib extension based on work done by Mads
Augie Fackler <raf@durin42.com>
parents:
diff changeset
    30
            _checklock(self)
405b5f8fdad7 lock-checker: new contrib extension based on work done by Mads
Augie Fackler <raf@durin42.com>
parents:
diff changeset
    31
            return orig._writejournal(self, *args, **kwargs)
405b5f8fdad7 lock-checker: new contrib extension based on work done by Mads
Augie Fackler <raf@durin42.com>
parents:
diff changeset
    32
405b5f8fdad7 lock-checker: new contrib extension based on work done by Mads
Augie Fackler <raf@durin42.com>
parents:
diff changeset
    33
        def transaction(self, *args, **kwargs):
405b5f8fdad7 lock-checker: new contrib extension based on work done by Mads
Augie Fackler <raf@durin42.com>
parents:
diff changeset
    34
            _checklock(self)
405b5f8fdad7 lock-checker: new contrib extension based on work done by Mads
Augie Fackler <raf@durin42.com>
parents:
diff changeset
    35
            return orig.transaction(self, *args, **kwargs)
405b5f8fdad7 lock-checker: new contrib extension based on work done by Mads
Augie Fackler <raf@durin42.com>
parents:
diff changeset
    36
405b5f8fdad7 lock-checker: new contrib extension based on work done by Mads
Augie Fackler <raf@durin42.com>
parents:
diff changeset
    37
        # TODO(durin42): kiilerix had a commented-out lock check in
405b5f8fdad7 lock-checker: new contrib extension based on work done by Mads
Augie Fackler <raf@durin42.com>
parents:
diff changeset
    38
        # writebranchcache and _writerequirements
405b5f8fdad7 lock-checker: new contrib extension based on work done by Mads
Augie Fackler <raf@durin42.com>
parents:
diff changeset
    39
405b5f8fdad7 lock-checker: new contrib extension based on work done by Mads
Augie Fackler <raf@durin42.com>
parents:
diff changeset
    40
        def _tag(self, *args, **kwargs):
405b5f8fdad7 lock-checker: new contrib extension based on work done by Mads
Augie Fackler <raf@durin42.com>
parents:
diff changeset
    41
            _checklock(self)
405b5f8fdad7 lock-checker: new contrib extension based on work done by Mads
Augie Fackler <raf@durin42.com>
parents:
diff changeset
    42
            return orig._tag(self, *args, **kwargs)
405b5f8fdad7 lock-checker: new contrib extension based on work done by Mads
Augie Fackler <raf@durin42.com>
parents:
diff changeset
    43
405b5f8fdad7 lock-checker: new contrib extension based on work done by Mads
Augie Fackler <raf@durin42.com>
parents:
diff changeset
    44
        def write(self, *args, **kwargs):
405b5f8fdad7 lock-checker: new contrib extension based on work done by Mads
Augie Fackler <raf@durin42.com>
parents:
diff changeset
    45
            assert os.path.lexists(self._join('.hg/wlock'))
405b5f8fdad7 lock-checker: new contrib extension based on work done by Mads
Augie Fackler <raf@durin42.com>
parents:
diff changeset
    46
            return orig.write(self, *args, **kwargs)
405b5f8fdad7 lock-checker: new contrib extension based on work done by Mads
Augie Fackler <raf@durin42.com>
parents:
diff changeset
    47
405b5f8fdad7 lock-checker: new contrib extension based on work done by Mads
Augie Fackler <raf@durin42.com>
parents:
diff changeset
    48
    repo.__class__ = lockcheckrepo