10Simple launcher for decfiles/tests/test_changed_decfiles.py_noexec. This tests whether new decay files work.
19from pathlib
import Path
20from tempfile
import NamedTemporaryFile
23def add_hint(errorstring: str):
25 hint =
"\nDon't forget to add custom (i.e. not yet discovered or measured) particles to " \
26 "decfiles/tests/test_changed_decfiles.pdl, otherwise the test will not pass." \
27 " Already discovered particles should go in framework/particledb/data/evt.pdl instead."
29 if 'Unknown particle name' in errorstring:
30 return errorstring + hint
35def decfile_uses_generic_dalitz(decfile_path) -> bool:
37 Decay files using the GENERIC_DALITZ model need an additional XML
38 file specifying the resonances. The XML's path is given in the decfile as a path relative to the working directory. Therefore,
in
39 order to test decfiles using GENERIC_DALITZ, basf2 must be run
from
40 the directory containing the decfile, otherwise EvtGen will be
41 unable to find the XML. This function returns
True if the decfile
44 with open(decfile_path)
as decfile:
46 if not ln.startswith(
'#')
and 'GENERIC_DALITZ' in ln:
51if __name__ ==
'__main__':
58 if not os.environ.get(
'BELLE2_LOCAL_DIR'):
61 topdir = Path(os.environ[
'BELLE2_LOCAL_DIR'])
62 assert topdir.is_dir()
65 merge_base = repo.merge_base(
'origin/main', repo.head)
66 diff_to_main = repo.head.commit.diff(merge_base)
68 added_or_modified_decfiles = [topdir / new_file.a_path
for new_file
in diff_to_main
69 if (Path(new_file.a_path).suffix ==
'.dec')
70 and (Path(
'decfiles/dec')
in Path(new_file.a_path).parents)]
74 added_or_modified_decfiles = [decfile
for decfile
in added_or_modified_decfiles
75 if basf2.find_file(decfile.as_posix(), silent=
True)]
77 steering_file = basf2.find_file(
'decfiles/tests/test_changed_decfiles.py_noexec')
78 custom_evtpdl = basf2.find_file(
"decfiles/tests/test_changed_decfiles.pdl")
79 default_evtpdl = basf2.find_file(
'data/framework/particledb/evt.pdl')
82 if added_or_modified_decfiles:
83 changed_file_string =
'\n'.join(str(p)
for p
in added_or_modified_decfiles)
84 print(f
"Changed decayfiles: \n{changed_file_string}")
86 with NamedTemporaryFile(mode=
'w', suffix=
'.pdl')
as tempfile:
88 for fname
in [custom_evtpdl, default_evtpdl]:
89 with open(fname)
as infile:
90 tempfile.write(infile.read())
92 for decfile
in added_or_modified_decfiles:
93 cwd = decfile.parent
if decfile_uses_generic_dalitz(decfile)
else None
95 run_results.append(subprocess.run([
'basf2', steering_file, str(decfile), tempfile.name],
96 capture_output=
True, cwd=cwd))
98 files_and_errors = [f
'Decfile {added_or_modified_decfiles[i]} failed with output \n'
99 f
'{ret.stdout.decode()} \n and error \n {add_hint(ret.stderr.decode())}'
100 for i, ret
in enumerate(run_results)
if ret.returncode != 0]
102 if len(files_and_errors):
103 raise RuntimeError(
"At least one added decfile has failed.\n"
104 +
'\n'.join(files_and_errors))
def clean_working_directory()
def configure_logging_for_tests(user_replacements=None)
def skip_test(reason, py_case=None)