@@ -470,8 +470,44 @@ def update_resource_estimates(workflow, resource_json):
470470# a python dictionary
471471def get_alienv_software_environment (packagestring ):
472472 """
473- packagestring is something like O2::v202298081-1,O2Physics::xxx
473+ packagestring is something like O2::v202298081-1,O2Physics::xxx representing packages
474+ published on CVMFS ... or ... a file containing directly the software environment to apply
474475 """
476+
477+ # the trivial cases do nothing
478+ if packagestring == None or packagestring == "" or packagestring == "None" :
479+ return {}
480+
481+ def load_env_file (env_file ):
482+ """Transform an environment file generated with 'export > env.txt' into a python dictionary."""
483+ env_vars = {}
484+ with open (env_file , "r" ) as f :
485+ for line in f :
486+ line = line .strip ()
487+
488+ # Ignore empty lines or comments
489+ if not line or line .startswith ("#" ):
490+ continue
491+
492+ # Remove 'declare -x ' if present
493+ if line .startswith ("declare -x " ):
494+ line = line .replace ("declare -x " , "" , 1 )
495+
496+ # Handle case: "FOO" without "=" (assign empty string)
497+ if "=" not in line :
498+ key , value = line .strip (), ""
499+ else :
500+ key , value = line .split ("=" , 1 )
501+ value = value .strip ('"' ) # Remove surrounding quotes if present
502+
503+ env_vars [key .strip ()] = value
504+ return env_vars
505+
506+ # see if this is a file
507+ if os .path .exists (packagestring ) and os .path .isfile (packagestring ):
508+ actionlogger .info ("Taking software environment from file " + packagestring )
509+ return load_env_file (packagestring )
510+
475511 # alienv printenv packagestring --> dictionary
476512 # for the moment this works with CVMFS only
477513 cmd = "/cvmfs/alice.cern.ch/bin/alienv printenv " + packagestring
@@ -1089,19 +1125,28 @@ def submit(self, tid, nice):
10891125 return subprocess .Popen (['/bin/bash' ,'-c' ,drycommand ], cwd = workdir )
10901126
10911127 taskenv = os .environ .copy ()
1092- # add task specific environment
1093- if self .workflowspec ['stages' ][tid ].get ('env' )!= None :
1094- taskenv .update (self .workflowspec ['stages' ][tid ]['env' ])
1095-
10961128 # apply specific (non-default) software version, if any
10971129 # (this was setup earlier)
10981130 alternative_env = self .alternative_envs .get (tid , None )
1099- if alternative_env != None :
1131+ if alternative_env != None and len ( alternative_env ) > 0 :
11001132 actionlogger .info ('Applying alternative software environment to task ' + self .idtotask [tid ])
1101- for entry in alternative_env :
1133+ if alternative_env .get ('TERM' ) != None :
1134+ # the environment is a complete environment
1135+ taskenv = {}
1136+ taskenv = alternative_env
1137+ else :
1138+ for entry in alternative_env :
11021139 # overwrite what is present in default
11031140 taskenv [entry ] = alternative_env [entry ]
11041141
1142+ # add task specific environment
1143+ if self .workflowspec ['stages' ][tid ].get ('env' )!= None :
1144+ taskenv .update (self .workflowspec ['stages' ][tid ]['env' ])
1145+
1146+ # envfilename = "taskenv_" + str(tid) + ".json"
1147+ # with open(envfilename, "w") as file:
1148+ # json.dump(taskenv, file, indent=2)
1149+
11051150 p = psutil .Popen (['/bin/bash' ,'-c' ,c ], cwd = workdir , env = taskenv )
11061151 try :
11071152 p .nice (nice )
@@ -1406,7 +1451,7 @@ def get_tar_command(dir='./', flags='cf', findtype='f', filename='checkpoint.tar
14061451
14071452 def init_alternative_software_environments (self ):
14081453 """
1409- Initiatialises alternative software environments for specific tasks, if there
1454+ Initialises alternative software environments for specific tasks, if there
14101455 is an annotation in the workflow specificiation.
14111456 """
14121457
0 commit comments