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