Skip to content

Commit bb8eae7

Browse files
authored
service: Get MeasurementLink install paths from registry instead of environment variables (#483)
* service: Get MeasurementLink install path from registry instead of environment variables * service: Fix mypy error * tests: Handle FileNotFoundError when checking for registration file * tox.ini: Remove ProgramW6432 and ProgramData from passenv
1 parent 92f5abe commit bb8eae7

3 files changed

Lines changed: 32 additions & 27 deletions

File tree

ni_measurementlink_service/discovery/_support.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
if sys.platform == "win32":
1717
import msvcrt
18+
import winreg
1819

1920
import win32con
2021
import win32file
@@ -55,13 +56,7 @@ def _get_discovery_service_location() -> pathlib.PurePath:
5556

5657
def _get_registration_json_file_path() -> pathlib.Path:
5758
if sys.platform == "win32":
58-
return (
59-
pathlib.Path(os.environ["ProgramW6432"])
60-
/ "National Instruments"
61-
/ "Shared"
62-
/ "MeasurementLink"
63-
/ "MeasurementLinkServices.json"
64-
)
59+
return _get_nipath("NISHAREDDIR64") / "MeasurementLink" / "MeasurementLinkServices.json"
6560
else:
6661
raise NotImplementedError("Platform not supported")
6762

@@ -123,13 +118,7 @@ def _get_key_file_path(cluster_id: Optional[str] = None) -> pathlib.Path:
123118
def _get_key_file_directory() -> pathlib.Path:
124119
version = discovery_service_pb2.DESCRIPTOR.package.split(".")[-1]
125120
if sys.platform == "win32":
126-
return (
127-
pathlib.Path(os.environ["ProgramData"])
128-
/ "National Instruments"
129-
/ "MeasurementLink"
130-
/ "Discovery"
131-
/ version
132-
)
121+
return _get_nipath("NIPUBAPPDATADIR") / "MeasurementLink" / "Discovery" / version
133122
else:
134123
raise NotImplementedError("Platform not supported")
135124

@@ -165,3 +154,20 @@ def _open_key_file(path: str) -> typing.TextIO:
165154
return os.fdopen(crt_file_descriptor, "r", encoding="utf-8-sig")
166155
else:
167156
return open(path, "r")
157+
158+
159+
def _get_nipath(name: str) -> pathlib.Path:
160+
if sys.platform == "win32":
161+
access: int = winreg.KEY_READ
162+
if "64" in name:
163+
access |= winreg.KEY_WOW64_64KEY
164+
with winreg.OpenKey(
165+
winreg.HKEY_LOCAL_MACHINE,
166+
r"SOFTWARE\National Instruments\Common\Installer",
167+
access=access,
168+
) as key:
169+
value, type = winreg.QueryValueEx(key, name)
170+
assert type == winreg.REG_SZ
171+
return pathlib.Path(value)
172+
else:
173+
raise NotImplementedError("Platform not supported")

tests/conftest.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,17 @@ def stub_v2(grpc_channel: grpc.Channel) -> v2_measurement_service_pb2_grpc.Measu
6161
def discovery_service_process() -> Generator[DiscoveryServiceProcess, None, None]:
6262
"""Test fixture that creates discovery service process."""
6363
if sys.platform != "win32":
64-
pytest.skip(
65-
f"Platform {sys.platform} is not supported for starting discovery service."
66-
"Could not proceed to run the tests."
67-
)
68-
elif not _get_registration_json_file_path().exists():
69-
pytest.skip(
70-
"MeasurementLink registration file not found or MeasurementLink not installed."
71-
"Could not proceed to run the tests. Install MeasurementLink to create the registration file."
72-
)
73-
else:
74-
with DiscoveryServiceProcess() as proc:
75-
yield proc
64+
pytest.skip(f"Platform {sys.platform} is not supported for discovery service tests.")
65+
66+
try:
67+
registration_json_file_exists = _get_registration_json_file_path().exists()
68+
except FileNotFoundError: # registry key not found
69+
registration_json_file_exists = False
70+
if not registration_json_file_exists:
71+
pytest.skip("Registration file not found. Ensure that MeasurementLink is installed.")
72+
73+
with DiscoveryServiceProcess() as proc:
74+
yield proc
7675

7776

7877
@pytest.fixture(scope="session")

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ envlist = clean, py{38,39,310,311,312}-all-extras
1010
[testenv]
1111
skip_install = true
1212
allowlist_externals = poetry
13-
passenv = RUNNER_NAME, ProgramW6432, ProgramData
13+
passenv = RUNNER_NAME
1414
commands =
1515
poetry run python --version
1616
poetry install -v --all-extras

0 commit comments

Comments
 (0)