@@ -33,6 +33,9 @@ def __init__(self):
33
33
self .name_link_pattern = "(usb-[0-9a-zA-Z_-]*_[0-9a-zA-Z]*-.*$)"
34
34
self .mount_media_pattern = "^/[a-zA-Z0-9/]* on (/[a-zA-Z0-9/]*) "
35
35
36
+ self .nlp = re .compile (self .name_link_pattern )
37
+ self .hup = re .compile (self .hex_uuid_pattern )
38
+
36
39
def list_mbeds (self ):
37
40
"""! Returns detailed list of connected mbeds
38
41
@return Returns list of structures with detailed info about each mbed
@@ -117,25 +120,37 @@ def list_mbeds(self):
117
120
118
121
# Private methods
119
122
120
- def get_dev_by_id (self , subdir ):
121
- """! Lists disk devices by id
122
- @return List of strings from 'ls' command executed in shell
123
+ def get_dev_by_id_cmd (self , subdir ):
124
+ """! Calls command line 'ls' to get devices by their ids
123
125
@details Uses Linux shell command: 'ls -oA /dev/disk/by-id/'
126
+ @return tuple(stdout lines, retcode)
124
127
"""
125
- result = []
126
128
cmd = 'ls -oA /dev/' + subdir + '/by-id/'
127
129
if self .DEBUG_FLAG :
128
- self .debug (self .get_dev_by_id .__name__ , cmd )
129
-
130
+ self .debug (self .get_dev_by_id_cmd .__name__ , cmd )
130
131
p = subprocess .Popen (cmd , shell = True , stdout = subprocess .PIPE , stderr = subprocess .STDOUT )
131
- for line in p .stdout .readlines ():
132
- line = line .rstrip ()
133
- result .append (line )
134
- if self .DEBUG_FLAG :
135
- self .debug (self .get_dev_by_id .__name__ , line )
136
- retval = p .wait ()
132
+ return (p .stdout .readlines (), p .wait ())
133
+
134
+ def get_dev_by_id_process (self , lines , retval ):
135
+ """! Remove unnecessary lines from command line output
136
+ """
137
+ result = []
138
+ if not retval :
139
+ for line in lines :
140
+ line = line .rstrip ()
141
+ if not line .lower ().startswith ('total ' ): # total 0
142
+ result .append (line )
143
+ if self .DEBUG_FLAG :
144
+ self .debug (self .get_dev_by_id_process .__name__ , line )
137
145
return result
138
146
147
+ def get_dev_by_id (self , subdir ):
148
+ """! Lists disk devices by id
149
+ @return List of strings from 'ls' command executed in shell
150
+ """
151
+ lines , retval = self .get_dev_by_id_cmd (subdir )
152
+ return self .get_dev_by_id_process (lines , retval )
153
+
139
154
def get_mounts (self ):
140
155
"""! Lists mounted devices with vfat file system (potential mbeds)
141
156
@result Returns list of all mounted vfat devices
@@ -147,12 +162,13 @@ def get_mounts(self):
147
162
self .debug (self .get_mounts .__name__ , cmd )
148
163
149
164
p = subprocess .Popen (cmd , shell = True , stdout = subprocess .PIPE , stderr = subprocess .STDOUT )
150
- for line in p .stdout .readlines ():
151
- line = line .rstrip ()
152
- result .append (line )
153
- if self .DEBUG_FLAG :
154
- self .debug (self .get_mounts .__name__ , line )
155
165
retval = p .wait ()
166
+ if not retval :
167
+ for line in p .stdout .readlines ():
168
+ line = line .rstrip ()
169
+ result .append (line )
170
+ if self .DEBUG_FLAG :
171
+ self .debug (self .get_mounts .__name__ , line )
156
172
return result
157
173
158
174
def get_disk_hex_ids (self , disk_list ):
@@ -161,14 +177,12 @@ def get_disk_hex_ids(self, disk_list):
161
177
@return Returns map of disks and corresponding disks' Hex ids
162
178
@details Uses regular expressions to get Hex strings (TargeTIDs) from list of disks
163
179
"""
164
- nlp = re .compile (self .name_link_pattern )
165
- hup = re .compile (self .hex_uuid_pattern )
166
180
disk_hex_ids = {}
167
181
for dl in disk_list :
168
- m = nlp .search (dl )
182
+ m = self . nlp .search (dl )
169
183
if m and len (m .groups ()):
170
184
disk_link = m .group (1 )
171
- m = hup .search (disk_link )
185
+ m = self . hup .search (disk_link )
172
186
if m and len (m .groups ()):
173
187
disk_hex_ids [m .group (1 )] = disk_link
174
188
return disk_hex_ids
@@ -180,10 +194,9 @@ def get_mbed_serial(self, serial_list, dhi):
180
194
@return Returns None if corresponding serial device is not found, else returns serial device path
181
195
@details Devices are located in Linux '/dev/' directory structure
182
196
"""
183
- nlp = re .compile (self .name_link_pattern )
184
197
for sl in serial_list :
185
198
if dhi in sl :
186
- m = nlp .search (sl )
199
+ m = self . nlp .search (sl )
187
200
if m and len (m .groups ()):
188
201
serial_link = m .group (1 )
189
202
mbed_dev_serial = "/dev/" + self .get_dev_name (serial_link )
@@ -232,24 +245,24 @@ def get_not_detected(self, tids, disk_list, serial_list, mount_list):
232
245
@return list of lists [mbed_name, mbed_dev_disk, mbed_mount_point, mbed_dev_serial, disk_hex_id]
233
246
@details Find for all disk connected all MBED ones we know about from TID list
234
247
"""
248
+ disk_hex_ids = self .get_disk_hex_ids (disk_list )
249
+
235
250
map_tid_to_mbed = self .get_tid_mbed_name_remap (tids )
236
- orphan_mbeds = []
237
- for disk in disk_list :
238
- if "mbed" in disk .lower ():
251
+ orphan_mbeds = {}
252
+ for disk in disk_hex_ids :
253
+ if "mbed" in disk_hex_ids [ disk ] .lower ():
239
254
orphan_found = True
240
255
for tid in map_tid_to_mbed .keys ():
241
- if tid in disk :
256
+ if disk . startswith ( tid ) :
242
257
orphan_found = False
243
258
break
244
259
if orphan_found :
245
- orphan_mbeds . append ( disk )
260
+ orphan_mbeds [ disk ] = disk_hex_ids [ disk ]
246
261
247
262
# Search for corresponding MBED serial
248
- disk_hex_ids = self .get_disk_hex_ids (orphan_mbeds )
249
-
250
263
result = []
251
- # FInd orphan serial name
252
- for dhi in disk_hex_ids :
264
+ # Find orphan serial name
265
+ for dhi in orphan_mbeds :
253
266
orphan_serial = self .get_mbed_serial (serial_list , dhi )
254
267
if orphan_serial :
255
268
orphan_dev_disk = self .get_dev_name (disk_hex_ids [dhi ])
0 commit comments