Belle II Software  release-05-02-19
test_validation_scripts.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 '''
5 Check if the validation scripts can be executed using basf2 with "--dry-run".
6 '''
7 
8 import basf2
9 import b2test_utils as b2u
10 import argparse
11 import glob
12 import os
13 import subprocess as sp
14 import multiprocessing as mp
15 from concurrent.futures import ProcessPoolExecutor
16 
17 __authors__ = ['Giacomo De Pietro']
18 
19 
20 def arg_parser():
21  '''
22  A very simple argument parser.
23  '''
24  parser = argparse.ArgumentParser(description=__doc__)
25  parser.add_argument('-j', '--jobs',
26  default=None,
27  type=int,
28  help='Number of workers to be used by ProcessPoolExecutor.',
29  metavar='JOBS')
30  return parser
31 
32 
33 def dry_run(validation_file):
34  '''
35  Check if the steering file at the given path can be run with the "--dry-run" option.
36  '''
37  result = sp.call(['basf2', validation_file, '--dry-run', '-i', 'input.root', '-o', 'output.root', '-l', 'ERROR'])
38  return (result, validation_file)
39 
40 
41 if __name__ == '__main__':
42  # First, let's check in a smart way how many cores we can use (or override it).
43  args = arg_parser().parse_args()
44  num_workers = None
45  if args.jobs is None:
46  num_workers = max(int(mp.cpu_count() / (1. + os.getloadavg()[1])), 1)
47  else: # Ok, we override the number of workers...
48  num_workers = args.jobs
49  basf2.B2INFO(f'The test will be executed using {num_workers} workers.')
50  # Then, let's run the test!
51  validation_path = basf2.find_file('skim/validation/')
52  failed_files = []
53  with b2u.clean_working_directory():
54  with ProcessPoolExecutor(max_workers=num_workers) as pool:
55  for result, validation_file in pool.map(dry_run, glob.glob(f'{validation_path}*.py')):
56  if not result == 0:
57  failed_files.append(f'skim/validation/{os.path.basename(validation_file)}')
58  if len(failed_files) > 0:
59  basf2.B2FATAL('The following validation scripts in the skim package failed, check them:\n'
60  + ' \n'.join(failed_files))