Last active
June 23, 2023 05:30
-
-
Save dingp/badd9bc755924a0c42341f405aec4b5e to your computer and use it in GitHub Desktop.
A minimal example of detecting if the DUNE DAQ environment contains ND or FD components.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env python | |
import subprocess | |
import sys | |
def check_spack_pkg(pkg): | |
completed_process = subprocess.run(f"spack find --loaded {pkg}", | |
shell=True, check=False, | |
stdout=subprocess.DEVNULL, | |
stderr=subprocess.DEVNULL) | |
print(f"checking if '{pkg}' is a loaded spack module") # To-be-removed | |
if completed_process.returncode == 0: | |
return True | |
elif completed_process.returncode == 1: | |
return False | |
else: | |
print("spack command is not avaliable. Make sure you are in DUNE DAQ development/running environment.") | |
sys.exit(1) | |
class DAQRelease: | |
_isND = None | |
_isFD = None | |
@classmethod | |
def isND(self): | |
if self._isND is None: | |
if check_spack_pkg("ndreadoutlibs"): | |
self._isND = True | |
else: | |
self._isND = False | |
return self._isND | |
@classmethod | |
def isFD(self): | |
if self._isFD is None: | |
if check_spack_pkg("fdreadoutlibs"): | |
self._isFD = True | |
else: | |
self._isFD = False | |
return self._isFD | |
if DAQRelease.isND(): | |
print("import ND DAQ modules") | |
# import <nd_module_1> | |
# import <nd_module_2> | |
else: | |
print("Not in ND DAQ environment.") | |
if DAQRelease.isFD(): | |
print("import FD DAQ modules") | |
# import <fd_module_1> | |
# import <fd_module_2> | |
else: | |
print("Not in FD DAQ environment.") | |
if DAQRelease.isND(): | |
print("calling ND module") | |
# nd_module_1.class2() | |
if DAQRelease.isFD(): | |
print("calling FD module") | |
# fd_module_1.class2() |
Line 11 was for illustration only. and should be commented out. I put it there just to verify that the calling of spack
command is done only once for ND or FD.
Note that this only works with the new nightlies since N23-06-23 (NFD23-06-23 and NND23-06-23).
While testing this script, I found that I did not move ndreadoutlibs
, fdreadoutlibs
and a few other packages out of the base release into ND or FD release. It was a temporary measure put in place before Roland and I completed the splitting of readoutmodules
.
Alternatively, python 3.6 and later has a "ModuleNotFoundError" exception (we use 3.10.4 in DAQ). So it you prefer the pure python approach, you can do:
# at the top of the python script where the "import" statements are placed
try:
import <nd_module_1>
import <nd_module_2>
except ModuleNotFoundError:
notND = True
pass
try:
import <fd_module_1>
import <fd_module_2>
except ModuleNotFoundError:
notFD = True
pass
# encapsulate the usage of ND or FD modules' functionalities with if statements on the condition of the "notND" and "notFD" flags.
if not notND:
nd_module_1.class2.()
if not notFD:
fd_module_1.class2.()
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I used the class attribute together with
@classmethod
, so thatspack find --loaded <pkg>
command only needs to be run once for either ND or FD.I used
ndreadoutlibs
andfdreadoutlibs
as indicators of ND or FD packages in the environment because they are available both before and after the ND/FD release separation. So this will work across all nightly releases we are currently making.