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
39 decfile as a path relative to the working directory. Therefore, in
40 order to test decfiles using GENERIC_DALITZ, basf2 must be run from
41 the directory containing the decfile, otherwise EvtGen will be
42 unable to find the XML. This function returns True if the decfile
45 with open(decfile_path)
as decfile:
47 if not ln.startswith(
'#')
and 'GENERIC_DALITZ' in ln:
52if __name__ ==
'__main__':
59 if not os.environ.get(
'BELLE2_LOCAL_DIR'):
62 topdir = Path(os.environ[
'BELLE2_LOCAL_DIR'])
63 assert topdir.is_dir()
66 merge_base = repo.merge_base(
'origin/main', repo.head)
67 diff_to_main = repo.head.commit.diff(merge_base)
69 added_or_modified_decfiles = [topdir / new_file.a_path
for new_file
in diff_to_main
70 if (Path(new_file.a_path).suffix ==
'.dec')
71 and (Path(
'decfiles/dec')
in Path(new_file.a_path).parents)]
75 added_or_modified_decfiles = [decfile
for decfile
in added_or_modified_decfiles
76 if basf2.find_file(decfile.as_posix(), silent=
True)]
78 steering_file = basf2.find_file(
'decfiles/tests/test_changed_decfiles.py_noexec')
79 custom_evtpdl = basf2.find_file(
"decfiles/tests/test_changed_decfiles.pdl")
80 default_evtpdl = basf2.find_file(
'data/framework/particledb/evt.pdl')
83 if added_or_modified_decfiles:
84 changed_file_string =
'\n'.join(str(p)
for p
in added_or_modified_decfiles)
85 print(f
"Changed decayfiles: \n{changed_file_string}")
87 with NamedTemporaryFile(mode=
'w', suffix=
'.pdl')
as tempfile:
89 for fname
in [custom_evtpdl, default_evtpdl]:
90 with open(fname)
as infile:
91 tempfile.write(infile.read())
93 for decfile
in added_or_modified_decfiles:
94 cwd = decfile.parent
if decfile_uses_generic_dalitz(decfile)
else None
96 run_results.append(subprocess.run([
'basf2', steering_file, str(decfile), tempfile.name],
97 capture_output=
True, cwd=cwd))
99 files_and_errors = [f
'Decfile {added_or_modified_decfiles[i]} failed with output \n'
100 f
'{ret.stdout.decode()} \n and error \n {add_hint(ret.stderr.decode())}'
101 for i, ret
in enumerate(run_results)
if ret.returncode != 0]
103 if len(files_and_errors):
104 raise RuntimeError(
"At least one added decfile has failed.\n"
105 +
'\n'.join(files_and_errors))
skip_test(reason, py_case=None)
clean_working_directory()
configure_logging_for_tests(user_replacements=None, replace_cdb_provider=True)