-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathconstants.py
60 lines (48 loc) · 1.84 KB
/
constants.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""
Constant values for the pact-python package.
This will default to the bundled Pact binaries bundled with the package, but
should these be unavailable or the environment variable `PACT_USE_SYSTEM_BINS` is
set to `TRUE` or `YES`, the system Pact binaries will be used instead.
"""
import os
import shutil
import warnings
from pathlib import Path
__all__ = [
"BROKER_CLIENT_PATH",
"MESSAGE_PATH",
"MOCK_SERVICE_PATH",
"VERIFIER_PATH",
]
_USE_SYSTEM_BINS = os.getenv("PACT_USE_SYSTEM_BINS", "").upper() in ("TRUE", "YES")
_BIN_DIR = Path(__file__).parent.resolve() / "bin"
def _find_executable(executable: str) -> str:
"""
Find the path to an executable.
This inspects the environment variable `PACT_USE_SYSTEM_BINS` to determine
whether to use the bundled Pact binaries or the system ones. Note that if
the local executables are not found, this will fall back to the system
executables (if found).
Args:
executable:
The name of the executable to find without the extension. Python
will automatically append the correct extension for the current
platform.
Returns:
The absolute path to the executable.
Warns:
RuntimeWarning:
If the executable cannot be found in the system path.
"""
if _USE_SYSTEM_BINS:
bin_path = shutil.which(executable)
else:
bin_path = shutil.which(executable, path=_BIN_DIR) or shutil.which(executable)
if bin_path is None:
msg = f"Unable to find {executable} binary executable."
warnings.warn(msg, RuntimeWarning, stacklevel=2)
return bin_path or ""
BROKER_CLIENT_PATH = _find_executable("pact-broker")
MESSAGE_PATH = _find_executable("pact-message")
MOCK_SERVICE_PATH = _find_executable("pact-mock-service")
VERIFIER_PATH = _find_executable("pact-provider-verifier")