3 ### Repository lock checker. Gets an 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 my_getopt
= getopt
.gnu_getopt
19 except AttributeError:
20 my_getopt
= getopt
.getopt
22 def usage_and_exit(retval
):
27 out
.write("""Usage: %s [OPTIONS] REPOS-PATH
30 --help (-h) : Show this usage message
31 --non-blocking : Don't wait for a lock that can't be immediately obtained
33 Obtain an exclusive lock (waiting for one unless --non-blocking is
34 passed) on REPOS-PATH, then check its lock usage counts. If there is
35 any accumulation present, report that accumulation to stdout.
36 """ % (os
.path
.basename(sys
.argv
[0])))
40 now_time
= time
.asctime()
45 optlist
, args
= my_getopt(sys
.argv
[1:], "h", ['non-blocking', 'help'])
46 for opt
, arg
in optlist
:
47 if opt
== '--help' or opt
== '-h':
49 if opt
== '--non-blocking':
54 # We need at least a path to work with, here.
56 if argc
< 1 or argc
> 1:
60 fd
= open(os
.path
.join(repos_path
, 'locks', 'db.lock'), 'a')
62 # Get an exclusive lock on the repository lock file, but maybe
67 mode
= mode | fcntl
.LOCK_NB
70 sys
.stderr
.write("Error obtaining exclusive lock.\n")
73 # Grab the db_stat results.
74 lines
= os
.popen('%s -ch %s' % (DB_STAT
, os
.path
.join(repos_path
, 'db')))
77 pieces
= line
.split('\t')
78 if (pieces
[1].find('current lock') != -1) and (int(pieces
[0]) > 0):
80 if not len(log_lines
):
81 log
= log
+ "[%s] Lock accumulation for '%s'\n" \
82 % (now_time
, repos_path
)
84 log
= log
+ "%s\t%s" % (pieces
[0], pieces
[1])
87 sys
.stdout
.write(''.join(log_lines
))
90 fcntl
.lockf(fd
, fcntl
.LOCK_UN
)
93 if __name__
== "__main__":