Commit 5ebc95e6 authored by David Jaša's avatar David Jaša
Browse files

p-b-l: add 2s delay between copying, parallelize

run/runtest.sh needs some time to delete invalid reports, e.g. from
skipped scenarios. This could make p-b-l miss some reports so the
wait and copying is done in parallel Process() and in order to speed up
copying of already existing files (to reduce time from service start to
notifier start), initial copying is done by Pool() of processes.
parent c6d5432f
Pipeline #428734 passed with stages
in 8 minutes and 12 seconds
......@@ -6,16 +6,20 @@ new file name for clearer orientation and to make sure we keep logs of older run
of the same case
"""
from glob import glob
from os.path import basename, getmtime, isdir
import pyinotify
from selinux import restorecon
from multiprocessing import Pool, Process
from os.path import basename, exists, getmtime, isdir
from shutil import copy2
from sys import argv, stdout
from time import localtime, strftime
from time import localtime, sleep, strftime
from xml.etree.ElementTree import parse as et_parse
from selinux import restorecon
import pyinotify
def copy_file(src):
'''copy the file, add FAIL_ prefix if failed or not (x)html'''
prefix = 'FAIL_'
try:
tree = et_parse(src)
......@@ -31,43 +35,58 @@ def copy_file(src):
file_name = basename(src).rsplit('.', 1)[0]
file_ext = basename(src).rsplit('.', 1)[1]
mtime = strftime('%m%d-%H%M%S', localtime(getmtime(src)))
target = '{}/{}{}-{}.{}'.format(document_root, prefix, file_name, mtime, file_ext)
target = '{}/{}{}-{}.{}'.format(DOCUMENT_ROOT, prefix, file_name, mtime, file_ext)
copy2(src, target)
restorecon(target)
print('Written file: {}'.format(target))
stdout.flush()
def process_close_write(event):
if (event.pathname.startswith('/tmp/report_') and
event.pathname.endswith('.html')):
copy_file(event.pathname)
def delay_copy(newfile):
'''Copy the file if still exists after 2 s delay so run/runtest.sh has
a chance to delete empty reports of skipped tests. Intended to run in
a separate new process to prevent blocking.'''
sleep(2)
if exists(newfile):
copy_file(newfile)
if __name__ == '__main__':
if len(argv) == 1:
document_root = '/var/www/html'
elif len(argv) == 2:
document_root = argv[1]
else:
raise SystemExit('specify just one destination directory as command argument!')
if not isdir(document_root):
raise SystemExit('Directory {} must exist!'.format(document_root))
def process_close_write(event):
'''Called when file in /tmp is close()-d. If it matches the pattern, new
process that will copy it after 2 s delay will be created'''
newfile = event.pathname
if (newfile.startswith('/tmp/report_') and
newfile.endswith('.html')):
Process(target=delay_copy, args=(newfile,)).start()
def main():
wm = pyinotify.WatchManager()
notifier = pyinotify.Notifier(wm, default_proc_fun=process_close_write)
wm.add_watch('/tmp', pyinotify.IN_CLOSE_WRITE)
# copy already existing files
for f in glob('/tmp/report_*html'):
copy_file(f)
with Pool() as p:
p.map(copy_file, glob('/tmp/report_*html'))
try:
notifier.loop()
except KeyboardInterrupt:
print('\n\n')
print('Caught interrupt, quitting.')
finally:
notifier.stop()
if __name__ == '__main__':
global DOCUMENT_ROOT
if len(argv) == 1:
DOCUMENT_ROOT = '/var/www/html'
elif len(argv) == 2:
DOCUMENT_ROOT = argv[1]
else:
raise SystemExit('specify just one destination directory as command argument!')
if not isdir(DOCUMENT_ROOT):
raise SystemExit('Directory {} must exist!'.format(DOCUMENT_ROOT))
notifier.stop()
main()
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