-
Notifications
You must be signed in to change notification settings - Fork 167
/
__init__.py
37 lines (25 loc) · 1.03 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import os
import shutil
__all__ = ["ToolNotFound", "tool_env_var", "has_tool", "require_tool"]
class ToolNotFound(Exception):
pass
def tool_env_var(name):
return name.upper().replace("-", "_").replace("+", "X")
def _get_tool(name):
return os.environ.get(tool_env_var(name), name)
def has_tool(name):
return shutil.which(_get_tool(name)) is not None
def require_tool(name):
env_var = tool_env_var(name)
path = _get_tool(name)
if shutil.which(path) is None:
if env_var in os.environ:
raise ToolNotFound("Could not find required tool {} in {} as "
"specified via the {} environment variable".
format(name, path, env_var))
else:
raise ToolNotFound("Could not find required tool {} in PATH. Place "
"it directly in PATH or specify path explicitly "
"via the {} environment variable".
format(name, env_var))
return path