~svn/ubuntu/raring/subversion/ppa

« back to all changes in this revision

Viewing changes to tools/dev/lock-check.py

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-12-05 01:26:14 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20051205012614-qom4xfypgtsqc2xq
Tags: 1.2.3dfsg1-3ubuntu1
Merge with the final Debian release of 1.2.3dfsg1-3, bringing in
fixes to the clean target, better documentation of the libdb4.3
upgrade and build fixes to work with swig1.3_1.3.27.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
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.
 
7
 
 
8
DB_STAT = 'db_stat'
 
9
 
 
10
 
 
11
import sys
 
12
import os
 
13
import os.path
 
14
import time
 
15
import fcntl
 
16
import getopt
 
17
 
 
18
def usage_and_exit(retval):
 
19
  if retval:
 
20
    out = sys.stderr
 
21
  else:
 
22
    out = sys.stdout
 
23
  out.write("""Usage: %s [OPTIONS] REPOS-PATH
 
24
 
 
25
Options:
 
26
  --help (-h)    : Show this usage message
 
27
  --non-blocking : Don't wait for a lock that can't be immediately obtained
 
28
 
 
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])))
 
33
  sys.exit(retval)
 
34
  
 
35
def main():
 
36
  now_time = time.asctime()
 
37
  repos_path = None
 
38
  nonblocking = 0
 
39
 
 
40
  # Parse the options.
 
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':
 
44
      usage_and_exit(0)
 
45
    if opt == '--non-blocking':
 
46
      nonblocking = 1
 
47
    else:
 
48
      usage_and_exit(1)
 
49
 
 
50
  # We need at least a path to work with, here.
 
51
  argc = len(args)
 
52
  if argc < 1 or argc > 1:
 
53
    usage_and_exit(1)
 
54
  repos_path = args[0]
 
55
 
 
56
  fd = open(os.path.join(repos_path, 'locks', 'db.lock'), 'a')
 
57
  try:
 
58
    # Get an exclusive lock on the repository lock file, but maybe
 
59
    # don't wait for it.
 
60
    try:
 
61
      mode = fcntl.LOCK_EX
 
62
      if nonblocking:
 
63
        mode = mode | fcntl.LOCK_NB
 
64
      fcntl.lockf(fd, mode)
 
65
    except IOError:
 
66
      sys.stderr.write("Error obtaining exclusive lock.\n")
 
67
      sys.exit(1)
 
68
 
 
69
    # Grab the db_stat results.
 
70
    lines = os.popen('%s -ch %s' % (DB_STAT, os.path.join(repos_path, 'db')))
 
71
    log_lines = []
 
72
    for line in lines:
 
73
      pieces = line.split('\t')
 
74
      if (pieces[1].find('current lock') != -1) and (int(pieces[0]) > 0):
 
75
        log = ''
 
76
        if not len(log_lines):
 
77
          log = log + "[%s] Lock accumulation for '%s'\n" \
 
78
                % (now_time, repos_path)
 
79
        log = log + ' ' * 27
 
80
        log = log + "%s\t%s" % (pieces[0], pieces[1])
 
81
        log_lines.append(log)
 
82
    if len(log_lines):
 
83
      sys.stdout.write(''.join(log_lines))
 
84
  finally:
 
85
    # Unlock the lockfile
 
86
    fcntl.lockf(fd, fcntl.LOCK_UN)
 
87
  fd.close()
 
88
 
 
89
if __name__ == "__main__":
 
90
  main()