Coverage for src/beamme/utils/environment.py: 97%

37 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-07-28 15:20 +0000

1# The MIT License (MIT) 

2# 

3# Copyright (c) 2018-2026 BeamMe Authors 

4# 

5# Permission is hereby granted, free of charge, to any person obtaining a copy 

6# of this software and associated documentation files (the "Software"), to deal 

7# in the Software without restriction, including without limitation the rights 

8# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 

9# copies of the Software, and to permit persons to whom the Software is 

10# furnished to do so, subject to the following conditions: 

11# 

12# The above copyright notice and this permission notice shall be included in 

13# all copies or substantial portions of the Software. 

14# 

15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 

16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 

17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 

18# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 

19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 

20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 

21# THE SOFTWARE. 

22"""Helper functions to interact with the BeamMe environment.""" 

23 

24import os as _os 

25import shutil as _shutil 

26import subprocess as _subprocess # nosec B404 

27import sys as _sys 

28from importlib.util import find_spec as _find_spec 

29from pathlib import Path as _Path 

30 

31 

32def cubitpy_is_available() -> bool: 

33 """Check if CubitPy is installed. 

34 

35 Returns: 

36 True if CubitPy is installed, False otherwise 

37 """ 

38 if _find_spec("cubitpy") is None: 

39 return False 

40 return True 

41 

42 

43def is_mybinder(): 

44 """Check if the current environment is running on mybinder.""" 

45 return "BINDER_LAUNCH_HOST" in _os.environ 

46 

47 

48def is_nbsphinx(): 

49 """Check if the current environment is running in nbsphinx.""" 

50 return "IS_NBSPHINX" in _os.environ 

51 

52 

53def is_testing(): 

54 """Check if the current environment is a pytest testing run.""" 

55 return "PYTEST_CURRENT_TEST" in _os.environ 

56 

57 

58def get_env_variable(name, *, default="default_not_set"): 

59 """Return the value of an environment variable. 

60 

61 Args 

62 ---- 

63 name: str 

64 Name of the environment variable 

65 default: 

66 Value to be returned if the given named environment variable does 

67 not exist. If this is not set and the name is not in the env 

68 variables, then an error will be thrown. 

69 """ 

70 if name in _os.environ.keys(): 

71 return _os.environ[name] 

72 elif default == "default_not_set": 

73 raise ValueError(f"Environment variable {name} is not set") 

74 return default 

75 

76 

77def get_git_data(repo_path: _Path) -> tuple[str | None, str | None]: 

78 """Return the hash and date of the current git commit of a git repo for a given repo 

79 path. 

80 

81 Args: 

82 repo_path: Path to the git repository. 

83 Returns: 

84 A tuple with the hash and date of the current git commit 

85 if available, otherwise None. 

86 """ 

87 git = _shutil.which("git") 

88 if git is None: 

89 raise RuntimeError("Git executable not found") 

90 

91 out_sha = _subprocess.run( # nosec B603 

92 [git, "rev-parse", "HEAD"], 

93 cwd=repo_path, 

94 stdout=_subprocess.PIPE, 

95 stderr=_subprocess.DEVNULL, 

96 ) 

97 

98 out_date = _subprocess.run( # nosec B603 

99 [git, "show", "-s", "--format=%ci"], 

100 cwd=repo_path, 

101 stdout=_subprocess.PIPE, 

102 stderr=_subprocess.DEVNULL, 

103 ) 

104 

105 if not out_sha.returncode + out_date.returncode == 0: 

106 return None, None 

107 

108 git_sha = out_sha.stdout.decode("ascii").strip() 

109 git_date = out_date.stdout.decode("ascii").strip() 

110 

111 return git_sha, git_date 

112 

113 

114def get_application_path() -> _Path | None: 

115 """Returns the application path which created this input file and ensures that the 

116 file exists. 

117 

118 Returns: 

119 A path to the file, which created this input file or None. 

120 """ 

121 # return valid application path if it exists. 

122 if _Path(_sys.argv[0]).resolve().exists(): 

123 return _Path(_sys.argv[0]).resolve() 

124 

125 return None