Skip to content

Commit

Permalink
usb: check permissions only on Linux and use appropriate logger method
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthieuDartiailh committed Oct 28, 2024
1 parent 5266825 commit da964f3
Showing 1 changed file with 21 additions and 24 deletions.
45 changes: 21 additions & 24 deletions pyvisa_py/usb.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import errno
import logging
import os
import sys
import traceback
from typing import Any, List, Tuple, Type, Union

Expand Down Expand Up @@ -292,37 +293,33 @@ def list_resources() -> List[str]:
)
logging_level = logger.getEffectiveLevel()

Check warning on line 294 in pyvisa_py/usb.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/usb.py#L294

Added line #L294 was not covered by tests
if logging_level <= logging.DEBUG:
if exc_strs := traceback.format_exception(err):
msg = "Traceback:"
logger.debug(msg)
logger.debug("-" * len(msg))
for exc_str in exc_strs:
for line in exc_str.split("\n"):
logger.debug(line)
logger.debug("-" * len(msg))
logger.debug("Error while reading serial number", exc_info=err)

Check warning on line 296 in pyvisa_py/usb.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/usb.py#L296

Added line #L296 was not covered by tests
elif logging_level <= logging.INFO:
if exc_strs := traceback.format_exception_only(err):
logger.info(

Check warning on line 299 in pyvisa_py/usb.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/usb.py#L299

Added line #L299 was not covered by tests
"Error raised from underlying module (pyusb): %s",
exc_strs[0].strip(),
)

dev_path = f"/dev/bus/usb/{dev.bus:03d}/{dev.address:03d}"
if os.path.exists(dev_path) and not os.access(dev_path, os.O_RDWR):
missing_perms = []
if not os.access(dev_path, os.O_RDONLY):
missing_perms.append("read from")
if not os.access(dev_path, os.O_WRONLY):
missing_perms.append("write to")
missing_perms_str = " or ".join(missing_perms)
logger.warning(
"User does not have permission to %s %s, so the above USB INSTR"
" device cannot be used by pyvisa; see"
" https://pyvisa.readthedocs.io/projects/pyvisa-py/en/latest/faq.html"
" for more info.",
missing_perms_str,
dev_path,
)
# Check permissions on Linux
if sys.platform.startswith("linux"):
dev_path = f"/dev/bus/usb/{dev.bus:03d}/{dev.address:03d}"

Check warning on line 306 in pyvisa_py/usb.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/usb.py#L306

Added line #L306 was not covered by tests
if os.path.exists(dev_path) and not os.access(dev_path, os.O_RDWR):
missing_perms = []

Check warning on line 308 in pyvisa_py/usb.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/usb.py#L308

Added line #L308 was not covered by tests
if not os.access(dev_path, os.O_RDONLY):
missing_perms.append("read from")

Check warning on line 310 in pyvisa_py/usb.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/usb.py#L310

Added line #L310 was not covered by tests
if not os.access(dev_path, os.O_WRONLY):
missing_perms.append("write to")
missing_perms_str = " or ".join(missing_perms)
logger.warning(

Check warning on line 314 in pyvisa_py/usb.py

View check run for this annotation

Codecov / codecov/patch

pyvisa_py/usb.py#L312-L314

Added lines #L312 - L314 were not covered by tests
"User does not have permission to %s %s, so the above "
"USB INSTR device cannot be used by pyvisa; see"
" https://pyvisa.readthedocs.io/projects/pyvisa-py/en/latest/faq.html"
" for more info.",
missing_perms_str,
dev_path,
)

continue

out.append(
Expand Down

0 comments on commit da964f3

Please sign in to comment.