Coverage for gwcelery/tools/condor_submit_helper.py: 94%

17 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-11-14 05:52 +0000

1""" 

2Wrapper script to get the Redis broker URL from condor and launch another tool. 

3 

4In a job running under HTCondor, the environment variable `_CONDOR_MACHINE_AD` 

5is the path of a text file that contains the machine's ClassAd attributes. The 

6`ClientMachine` attribute records the hostname of the machine that has claimed 

7the machine on which the job is running, i.e., the submit machine. We assume 

8that the Redis server is running on the submit machine. 

9""" 

10import os 

11import re 

12import sys 

13 

14 

15def get_classad_attribute(filename, attrib): 

16 pattern = r'^' + re.escape(attrib) + r'\s*=\s*"(.*)"\s*$' 

17 regex = re.compile(pattern) 

18 with open(filename, 'r') as f: 

19 for line in f: 

20 match = regex.match(line) 

21 if match is not None: 

22 return match.group(1) 

23 raise ValueError( 

24 f'ClassAd attribute "{attrib}" not found in file "{filename}"') 

25 

26 

27def main(): 

28 filename = os.environ['_CONDOR_MACHINE_AD'] 

29 hostname = get_classad_attribute(filename, 'ClientMachine') 

30 os.environ['CELERY_BROKER_URL'] = f'redis://{hostname}' 

31 os.execvp(sys.argv[1], sys.argv[1:])