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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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: