Coverage for src/meshpy/utils/environment.py: 100%
32 statements
« prev ^ index » next coverage.py v7.9.0, created at 2025-06-13 04:26 +0000
« prev ^ index » next coverage.py v7.9.0, created at 2025-06-13 04:26 +0000
1# The MIT License (MIT)
2#
3# Copyright (c) 2018-2025 MeshPy 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 MeshPy environment."""
24import os as _os
25import shutil as _shutil
26import subprocess as _subprocess # nosec B404
27from importlib.util import find_spec as _find_spec
28from pathlib import Path as _Path
29from typing import Optional as _Optional
30from typing import Tuple as _Tuple
33def cubitpy_is_available() -> bool:
34 """Check if CubitPy is installed.
36 Returns:
37 True if CubitPy is installed, False otherwise
38 """
40 if _find_spec("cubitpy") is None:
41 return False
42 return True
45def is_mybinder():
46 """Check if the current environment is running on mybinder."""
47 return "BINDER_LAUNCH_HOST" in _os.environ.keys()
50def is_testing():
51 """Check if the current environment is a pytest testing run."""
52 return "PYTEST_CURRENT_TEST" in _os.environ
55def get_env_variable(name, *, default="default_not_set"):
56 """Return the value of an environment variable.
58 Args
59 ----
60 name: str
61 Name of the environment variable
62 default:
63 Value to be returned if the given named environment variable does
64 not exist. If this is not set and the name is not in the env
65 variables, then an error will be thrown.
66 """
67 if name in _os.environ.keys():
68 return _os.environ[name]
69 elif default == "default_not_set":
70 raise ValueError(f"Environment variable {name} is not set")
71 return default
74def get_git_data(repo_path: _Path) -> _Tuple[_Optional[str], _Optional[str]]:
75 """Return the hash and date of the current git commit of a git repo for a
76 given repo path.
78 Args:
79 repo_path: Path to the git repository.
80 Returns:
81 A tuple with the hash and date of the current git commit
82 if available, otherwise None.
83 """
85 git = _shutil.which("git")
86 if git is None:
87 raise RuntimeError("Git executable not found")
89 out_sha = _subprocess.run( # nosec B603
90 [git, "rev-parse", "HEAD"],
91 cwd=repo_path,
92 stdout=_subprocess.PIPE,
93 stderr=_subprocess.DEVNULL,
94 )
96 out_date = _subprocess.run( # nosec B603
97 [git, "show", "-s", "--format=%ci"],
98 cwd=repo_path,
99 stdout=_subprocess.PIPE,
100 stderr=_subprocess.DEVNULL,
101 )
103 if not out_sha.returncode + out_date.returncode == 0:
104 return None, None
106 git_sha = out_sha.stdout.decode("ascii").strip()
107 git_date = out_date.stdout.decode("ascii").strip()
109 return git_sha, git_date