3
### Repository lock checker. Gets and exclusive lock on the provided
4
### repository, then runs db_stat to see if the lock counts have been
5
### reset to 0. If not, prints the timestamp of the run and a message
6
### about accumulation.
18
def usage_and_exit(retval):
23
out.write("""Usage: %s [OPTIONS] REPOS-PATH
26
--help (-h) : Show this usage message
27
--non-blocking : Don't wait for a lock that can't be immediately obtained
29
Obtain an exclusive lock (waiting for one unless --non-blocking is
30
passed) on REPOS-PATH, then check its lock usage counts. If there is
31
any accumulation present, report that accumulation to stdout.
32
""" % (os.path.basename(sys.argv[0])))
36
now_time = time.asctime()
41
optlist, args = getopt.getopt(sys.argv[1:], "h", ['non-blocking', 'help'])
42
for opt, arg in optlist:
43
if opt == '--help' or opt == '-h':
45
if opt == '--non-blocking':
50
# We need at least a path to work with, here.
52
if argc < 1 or argc > 1:
56
fd = open(os.path.join(repos_path, 'locks', 'db.lock'), 'a')
58
# Get an exclusive lock on the repository lock file, but maybe
63
mode = mode | fcntl.LOCK_NB
66
sys.stderr.write("Error obtaining exclusive lock.\n")
69
# Grab the db_stat results.
70
lines = os.popen('%s -ch %s' % (DB_STAT, os.path.join(repos_path, 'db')))
73
pieces = line.split('\t')
74
if (pieces[1].find('current lock') != -1) and (int(pieces[0]) > 0):
76
if not len(log_lines):
77
log = log + "[%s] Lock accumulation for '%s'\n" \
78
% (now_time, repos_path)
80
log = log + "%s\t%s" % (pieces[0], pieces[1])
83
sys.stdout.write(''.join(log_lines))
86
fcntl.lockf(fd, fcntl.LOCK_UN)
89
if __name__ == "__main__":