Skip to content

Commit 309b449

Browse files
committed
Ability to query DPL options in a specific environment
Also fixing subprocess invocation to prevent DPL hang
1 parent 9399914 commit 309b449

File tree

3 files changed

+57
-7
lines changed

3 files changed

+57
-7
lines changed

MC/bin/o2dpg_sim_config.py

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from functools import lru_cache
22
import subprocess
33
import re
4+
import os
45

56
def create_sim_config(args):
67
# creates a generic simulation config
@@ -143,11 +144,43 @@ def constructConfigKeyArg(config):
143144
arg = arg + '"'
144145
return arg
145146

147+
def load_env_file(env_file):
148+
"""Transform an environment file generated with 'export > env.txt' into a python dictionary."""
149+
env_vars = {}
150+
with open(env_file, "r") as f:
151+
for line in f:
152+
line = line.strip()
153+
154+
# Ignore empty lines or comments
155+
if not line or line.startswith("#"):
156+
continue
157+
158+
# Remove 'declare -x ' if present
159+
if line.startswith("declare -x "):
160+
line = line.replace("declare -x ", "", 1)
161+
162+
# Handle case: "FOO" without "=" (assign empty string)
163+
if "=" not in line:
164+
key, value = line.strip(), ""
165+
else:
166+
key, value = line.split("=", 1)
167+
value = value.strip('"') # Remove surrounding quotes if present
168+
169+
env_vars[key.strip()] = value
170+
return env_vars
171+
146172
# some functions to determine dpl option availability on the fly
147-
def parse_dpl_help_output(executable):
173+
def parse_dpl_help_output(executable, envfile):
148174
"""Parses the --help full output of an executable to extract available options."""
149175
try:
150-
output = subprocess.check_output([executable, "--help", "full"], text=True)
176+
env = os.environ.copy()
177+
if envfile != None:
178+
print ("Loading from alternative environment")
179+
env = load_env_file(envfile)
180+
181+
# the DEVNULL is important for o2-dpl workflows not to hang on non-interactive missing tty environments
182+
# it is cleaner that the echo | trick
183+
output = subprocess.check_output([executable, "--help", "full"], env=env, text=True, stdin=subprocess.DEVNULL, timeout = 10)
151184
except subprocess.CalledProcessError:
152185
return {}, {}
153186

@@ -172,11 +205,11 @@ def parse_dpl_help_output(executable):
172205
return sections, inverse_lookup
173206

174207
@lru_cache(maxsize=10)
175-
def get_dpl_options_for_executable(executable):
208+
def get_dpl_options_for_executable(executable, envfile):
176209
"""Returns available options and inverse lookup for a given executable, caching the result."""
177-
return parse_dpl_help_output(executable)
210+
return parse_dpl_help_output(executable, envfile)
178211

179-
def option_if_available(executable, option):
212+
def option_if_available(executable, option, envfile = None):
180213
"""Checks if an option is available for a given executable and returns it as a string. Otherwise empty string"""
181-
_, inverse_lookup = get_dpl_options_for_executable(executable)
214+
_, inverse_lookup = get_dpl_options_for_executable(executable, envfile)
182215
return ' ' + option if option in inverse_lookup else ''

MC/bin/o2dpg_sim_workflow.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1154,11 +1154,12 @@ def getDigiTaskName(det):
11541154

11551155
# TODO: Is this still used?
11561156
tpc_corr_scaling_options = anchorConfig.get('tpc-corr-scaling','')
1157+
tpc_envfile = 'env_async.env' if environ.get('ALIEN_JDL_O2DPG_ASYNC_RECO_TAG') is not None else None
11571158
TPCRECOtask=createTask(name='tpcreco_'+str(tf), needs=tpcreconeeds, tf=tf, cwd=timeframeworkdir, lab=["RECO"], relative_cpu=3/8, mem='16000')
11581159
TPCRECOtask['cmd'] = '${O2_ROOT}/bin/o2-tpc-reco-workflow ' + getDPL_global_options(bigshm=True) + ' --input-type clusters --output-type tracks,send-clusters-per-sector ' \
11591160
+ putConfigValuesNew(["GPU_global","TPCGasParam", "TPCCorrMap", "GPU_rec_tpc", "trackTuneParams"], {"GPU_proc.ompThreads":NWORKERS_TF} | tpcLocalCFreco) + ('',' --disable-mc')[args.no_mc_labels] \
11601161
+ tpc_corr_scaling_options + tpc_corr_options_mc \
1161-
+ option_if_available('o2-tpc-reco-workflow', '--tpc-mc-time-gain')
1162+
+ option_if_available('o2-tpc-reco-workflow', '--tpc-mc-time-gain', envfile=tpc_envfile)
11621163

11631164
workflow['stages'].append(TPCRECOtask)
11641165

MC/run/ANCHOR/anchorMC.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,18 @@ SEED=${ALIEN_PROC_ID:-${SEED:-1}}
147147

148148
ONCVMFS=0
149149

150+
# TODO:
151+
# (a) detect if there was an O2DPG_OVERLOAD; because we need to handle this correctly during
152+
# purging, reloading
153+
# (b) apply "tpc-mc-time-gain" optionally to tpc reco --- but this is done; so we really need a replacement method
154+
and a 2-stage workflow production
155+
if [ "${ALIEN_JDL_O2DPG_OVERWRITE}" ]; then
156+
echo "Setting O2DPG_ROOT to overwritten path"
157+
export O2DPG_ROOT=${ALIEN_JDL_O2DPG_OVERWRITE}
158+
fi
159+
160+
export > env_base.env
161+
150162
if ! declare -F module > /dev/null; then
151163
module() {
152164
eval "$(/usr/bin/modulecmd bash "$@")";
@@ -249,6 +261,10 @@ if [ "${ALIEN_JDL_O2DPG_ASYNC_RECO_TAG}" ]; then
249261
echo "Restoring initial environment"
250262
module --no-pager restore initial_modules.list
251263
module saverm initial_modules.list
264+
if [ "${ALIEN_JDL_O2DPG_OVERWRITE}" ]; then
265+
echo "Setting back O2DPG_ROOT to overwritten path ${ALIEN_JDL_O2DPG_OVERWRITE}"
266+
export O2DPG_ROOT=${ALIEN_JDL_O2DPG_OVERWRITE}
267+
fi
252268
fi
253269
#<----- END OF part that should run under a clean alternative software environment if this was given ------
254270

0 commit comments

Comments
 (0)