scripts/gestion/lock.py
bernat 3311711bb5 Option quiet pour le lock
darcs-hash:20041205123023-d1718-526a0448dff827f459a187594773375cf6224610.gz
2004-12-05 13:30:23 +01:00

106 lines
2.7 KiB
Python
Executable file

#!/usr/bin/env python
# -*- coding: iso-8859-15 -*-
""" Gestion de lock
Copyright (C) Frédéric Pauget
Licence : GPLv2
"""
import os,string,time,sys, affich_tools
from commands import getoutput
from user_tests import getuser
from fcntl import lockf, LOCK_EX, LOCK_NB, LOCK_UN
import errno
def make_lock(lock_name, lock_comment='',nowait=1, quiet=False) :
""" Création d'un lock
si nowait=1 fait un sys.exit(254) quand un ancien lock actif est rencontré
"""
lock_file = '/var/lock/' + lock_name
# On créé une zone d'exclusion
lock_fd_dl=open("%s-dotlock" % lock_file, "w")
# On demande un verrou exclusif
try:
lockf(lock_fd_dl, LOCK_EX | LOCK_NB)
except IOError, e:
if e.errno not in [errno.EACCESS, errno.EAGAIN]:
raise
# La procédure de lock est deja en cours d'execution, on essaie un peu plus tard
time.sleep(0.5)
return make_lock(lock_name, lock_comment)
if os.path.isfile(lock_file) :
### Lock existant
# Lecture du lock
fd = open(lock_file, "r")
pid= fd.readline().strip()
user = fd.readline().strip()
fd.close()
# Informations sur le processus lockant
if os.system( "ps %s > /dev/null 2>&1" % pid ) :
# Le script lockant ne tourne plus
os.remove(lock_file)
elif nowait :
if not quiet:
sys.stderr.write('Lock : %s\n' % lock_file)
l=getoutput('ps -o etime --no-headers %s' % pid)
data = [ user , pid , l.strip() ]
# Formatate de etime
s = data[-1].split('-')
if len(s)==2 :
txt = '%s jour(s) ' % s[0]
s=s[1]
else :
txt = ''
s=s[0]
s = s.split(':')
if len(s) == 3 :
txt = '%sh%smin%ss' % tuple(s)
elif len(s) == 2 :
txt = '%smin%ss' % tuple(s)
else :
txt = '???'
data[-1]=txt
if not quiet:
sys.stderr.write('\tpropriétaire : %s\n\tpid : %s\n\tdémarré depuis %s\n' % tuple(data) )
sys.exit(254)
else:
# On va plutot lever une exception
raise AssertionError(tuple(data))
else :
# Il faut attendre
a = affich_tools.anim('\tattente du lock')
for i in range(8) :
time.sleep(1)
a.cycle()
sys.stdout.write('\r')
return make_lock(lock_name, lock_comment)
### Prise du lock
lock_fd = file(lock_file,"w")
lock_fd.write("%s\n%s\n%s" % (os.getpid(), getuser(), lock_comment) )
lock_fd.close()
# On enleve le verrou système
lockf(lock_fd_dl, LOCK_UN)
lock_fd_dl.close()
def remove_lock( lock_name ) :
""" Destruction du lock """
lock_file = '/var/lock/' + lock_name
try :
fd = open(lock_file, "r")
if fd.readline().strip()=="%s" % os.getpid():
os.remove(lock_file)
fd.close()
except :
None