Coverage for python/lsst/ctrl/execute/envString.py: 22%

14 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-04-13 10:34 +0000

1#!/usr/bin/env python 

2 

3# 

4# LSST Data Management System 

5# Copyright 2008-2016 LSST Corporation. 

6# 

7# This product includes software developed by the 

8# LSST Project (http://www.lsst.org/). 

9# 

10# This program is free software: you can redistribute it and/or modify 

11# it under the terms of the GNU General Public License as published by 

12# the Free Software Foundation, either version 3 of the License, or 

13# (at your option) any later version. 

14# 

15# This program is distributed in the hope that it will be useful, 

16# but WITHOUT ANY WARRANTY; without even the implied warranty of 

17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

18# GNU General Public License for more details. 

19# 

20# You should have received a copy of the LSST License Statement and 

21# the GNU General Public License along with this program. If not, 

22# see <http://www.lsstcorp.org/LegalNotices/>. 

23# 

24 

25import os 

26import re 

27import sys 

28 

29# Given a string, look for any $ prefixed word, attempt to substitute 

30# an environment variable with that name. 

31# @throw exception if the environment variable doesn't exist 

32# @return the resulting string 

33 

34 

35def resolve(strVal): 

36 p = re.compile(r"\$[a-zA-Z0-9_]+") 

37 retVal = strVal 

38 exprs = p.findall(retVal) 

39 for i in exprs: 

40 var = i[1:] 

41 val = os.getenv(var, None) 

42 if val is None: 

43 raise RuntimeError("couldn't find environment variable " + i) 

44 sys.exit(120) 

45 retVal = p.sub(val, retVal, 1) 

46 return retVal