Commit 9860b7dc authored by Antoine Catton's avatar Antoine Catton

Remove any time.sleep() from execute. Use inotify instead.

parent 0940ba76
...@@ -4,6 +4,46 @@ import signal ...@@ -4,6 +4,46 @@ import signal
import subprocess import subprocess
import time import time
import inotifyx
def _wait_files_creation(file_list):
# Etablish a list of directory and subfiles
directories = dict()
for dirname, filename in [os.path.split(f) for f in file_list]:
directories.setdefault(dirname, dict())
directories[dirname][filename] = False
def all_files_exists():
return all([all(files.values()) for files in directories.values()])
fd = inotifyx.init()
try:
# Watch every directories where the file are
watchdescriptors = dict()
for dirname in directories.keys():
wd = inotifyx.add_watch(fd,
dirname,
inotifyx.IN_CREATE | inotifyx.IN_DELETE)
watchdescriptors[wd] = dirname
# Set to True the file wich exists
for dirname, filename in [os.path.split(f) for f in file_list]:
directories[dirname][filename] = os.path.exists(os.path.join(dirname,
filename))
# Let's wait for every file creation
while not all_files_exists():
events_list = inotifyx.get_events(fd)
for event in events_list:
dirname = watchdescriptors[event.wd]
if event.name in directories[dirname]:
# One of watched file was created or deleted
if event.mask & inotifyx.IN_DELETE:
directories[dirname][event.name] = False
else:
directories[dirname][event.name] = True
finally:
os.close(fd)
def execute(args): def execute(args):
"""Portable execution with process replacement""" """Portable execution with process replacement"""
...@@ -14,18 +54,7 @@ def execute_wait(args): ...@@ -14,18 +54,7 @@ def execute_wait(args):
"""Execution but after all files in args[1] exists""" """Execution but after all files in args[1] exists"""
exec_list = list(args[0]) exec_list = list(args[0])
file_list = list(args[1]) file_list = list(args[1])
sleep = 60 _wait_files_creation(file_list)
while True:
ready = True
for f in file_list:
if not os.path.exists(f):
print 'File %r does not exists, sleeping for %s' % (f, sleep)
ready = False
if ready:
break
# XXX: It's the same as ../ca/certificate_authoritiy.py
# We should use pyinotify as well. Or select() on socket.
time.sleep(sleep)
os.execv(exec_list[0], exec_list + sys.argv[1:]) os.execv(exec_list[0], exec_list + sys.argv[1:])
...@@ -49,16 +78,7 @@ def executee_wait(args): ...@@ -49,16 +78,7 @@ def executee_wait(args):
env = os.environ.copy() env = os.environ.copy()
for k,v in environment.iteritems(): for k,v in environment.iteritems():
env[k] = v env[k] = v
sleep = 60 _wait_files_creation(file_list)
while True:
ready = True
for f in file_list:
if not os.path.exists(f):
print 'File %r does not exists, sleeping for %s' % (f, sleep)
ready = False
if ready:
break
time.sleep(sleep)
os.execve(exec_list[0], exec_list + sys.argv[1:], env) os.execve(exec_list[0], exec_list + sys.argv[1:], env)
def sig_handler(signal, frame): def sig_handler(signal, frame):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment