@@ -85,11 +85,11 @@ def __init__(
85
85
baudrate : int = 1000000 ,
86
86
timeout : float = 1.0 ,
87
87
):
88
- self .check_serial_access_permission ()
89
88
self .version = ""
90
89
self ._log = b""
91
90
self ._logging = False
92
91
self .interface = serial .Serial ()
92
+
93
93
self .send_byte = partial (self ._send , size = 1 )
94
94
update_wrapper (self .send_byte , self ._send )
95
95
self .send_int = partial (self ._send , size = 2 )
@@ -100,15 +100,23 @@ def __init__(
100
100
update_wrapper (self .get_int , self ._receive )
101
101
self .get_long = partial (self ._receive , size = 4 )
102
102
update_wrapper (self .get_long , self ._receive )
103
+
104
+ self .check_serial_access_permission ()
103
105
self .connect (port = port , baudrate = baudrate , timeout = timeout )
104
106
self .connected = self .interface .is_open
105
107
106
108
@staticmethod
107
109
def check_serial_access_permission ():
108
110
"""Check that we have permission to use the tty on Linux."""
109
111
if platform .system () == "Linux" :
112
+ if os .geteuid () == 0 : # Running as root?
113
+ return
114
+
110
115
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
+ ):
112
120
return
113
121
114
122
udev_paths = [
@@ -119,17 +127,22 @@ def check_serial_access_permission():
119
127
for p in udev_paths :
120
128
udev_rules = os .path .join (p , "99-pslab.rules" )
121
129
if os .path .isfile (udev_rules ):
122
- break
130
+ return
123
131
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."
133
146
)
134
147
135
148
@staticmethod
0 commit comments