Skip to content

Commit 8b3b330

Browse files
committed
Improve permission denied error message
1 parent c6a73a9 commit 8b3b330

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

pslab/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ def install(args: argparse.Namespace):
490490
_install()
491491
return
492492

493-
print("User is in dialout group or udev rule is already installed.")
493+
print("User is in dialout/uucp group or udev rule is already installed.")
494494

495495

496496
def _install():

pslab/serial_handler.py

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,11 @@ def __init__(
8585
baudrate: int = 1000000,
8686
timeout: float = 1.0,
8787
):
88-
self.check_serial_access_permission()
8988
self.version = ""
9089
self._log = b""
9190
self._logging = False
9291
self.interface = serial.Serial()
92+
9393
self.send_byte = partial(self._send, size=1)
9494
update_wrapper(self.send_byte, self._send)
9595
self.send_int = partial(self._send, size=2)
@@ -100,15 +100,23 @@ def __init__(
100100
update_wrapper(self.get_int, self._receive)
101101
self.get_long = partial(self._receive, size=4)
102102
update_wrapper(self.get_long, self._receive)
103+
104+
self.check_serial_access_permission()
103105
self.connect(port=port, baudrate=baudrate, timeout=timeout)
104106
self.connected = self.interface.is_open
105107

106108
@staticmethod
107109
def check_serial_access_permission():
108110
"""Check that we have permission to use the tty on Linux."""
109111
if platform.system() == "Linux":
112+
if os.geteuid() == 0: # Running as root?
113+
return
114+
110115
for group in os.getgroups():
111-
if grp.getgrgid(group).gr_name == "dialout":
116+
if grp.getgrgid(group).gr_name in (
117+
"dialout",
118+
"uucp",
119+
):
112120
return
113121

114122
udev_paths = [
@@ -119,17 +127,22 @@ def check_serial_access_permission():
119127
for p in udev_paths:
120128
udev_rules = os.path.join(p, "99-pslab.rules")
121129
if os.path.isfile(udev_rules):
122-
break
130+
return
123131
else:
124-
raise OSError(
125-
"You are not a member of the dialout group and therefore "
126-
"do not have permission to talk to serial devices. Please "
127-
"add the current user to the dialout group. After logging "
128-
"out and then logging back in you will be able to access "
129-
"the PSLab.\n"
130-
"Alternativelly, a udev rule can be installed by running "
131-
"'pslab install' as root, or by copying "
132-
f"{pslab.__path__[0]}/99-pslab.rules to {udev_paths[1]}."
132+
raise PermissionError(
133+
"The current user does not have permission to access "
134+
"the PSLab device. To solve this, either:"
135+
"\n\n"
136+
"1. Add the user to the 'dialout' (on Debian-based "
137+
"systems) or 'uucp' (on Arch-based systems) group."
138+
"\n"
139+
"2. Install a udev rule to allow any user access to the "
140+
"device by running 'pslab install' as root, or by "
141+
"manually copying "
142+
f"{pslab.__path__[0]}/99-pslab.rules into {udev_paths[1]}."
143+
"\n\n"
144+
"You may also need to reboot the system for the "
145+
"permission changes to take effect."
133146
)
134147

135148
@staticmethod

0 commit comments

Comments
 (0)