@@ -33,6 +33,9 @@ def __init__(self):
3333 self .name_link_pattern = "(usb-[0-9a-zA-Z_-]*_[0-9a-zA-Z]*-.*$)"
3434 self .mount_media_pattern = "^/[a-zA-Z0-9/]* on (/[a-zA-Z0-9/]*) "
3535
36+ self .nlp = re .compile (self .name_link_pattern )
37+ self .hup = re .compile (self .hex_uuid_pattern )
38+
3639 def list_mbeds (self ):
3740 """! Returns detailed list of connected mbeds
3841 @return Returns list of structures with detailed info about each mbed
@@ -117,25 +120,37 @@ def list_mbeds(self):
117120
118121 # Private methods
119122
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
123125 @details Uses Linux shell command: 'ls -oA /dev/disk/by-id/'
126+ @return tuple(stdout lines, retcode)
124127 """
125- result = []
126128 cmd = 'ls -oA /dev/' + subdir + '/by-id/'
127129 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 )
130131 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 )
137145 return result
138146
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+
139154 def get_mounts (self ):
140155 """! Lists mounted devices with vfat file system (potential mbeds)
141156 @result Returns list of all mounted vfat devices
@@ -147,12 +162,13 @@ def get_mounts(self):
147162 self .debug (self .get_mounts .__name__ , cmd )
148163
149164 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 )
155165 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 )
156172 return result
157173
158174 def get_disk_hex_ids (self , disk_list ):
@@ -161,14 +177,12 @@ def get_disk_hex_ids(self, disk_list):
161177 @return Returns map of disks and corresponding disks' Hex ids
162178 @details Uses regular expressions to get Hex strings (TargeTIDs) from list of disks
163179 """
164- nlp = re .compile (self .name_link_pattern )
165- hup = re .compile (self .hex_uuid_pattern )
166180 disk_hex_ids = {}
167181 for dl in disk_list :
168- m = nlp .search (dl )
182+ m = self . nlp .search (dl )
169183 if m and len (m .groups ()):
170184 disk_link = m .group (1 )
171- m = hup .search (disk_link )
185+ m = self . hup .search (disk_link )
172186 if m and len (m .groups ()):
173187 disk_hex_ids [m .group (1 )] = disk_link
174188 return disk_hex_ids
@@ -180,10 +194,9 @@ def get_mbed_serial(self, serial_list, dhi):
180194 @return Returns None if corresponding serial device is not found, else returns serial device path
181195 @details Devices are located in Linux '/dev/' directory structure
182196 """
183- nlp = re .compile (self .name_link_pattern )
184197 for sl in serial_list :
185198 if dhi in sl :
186- m = nlp .search (sl )
199+ m = self . nlp .search (sl )
187200 if m and len (m .groups ()):
188201 serial_link = m .group (1 )
189202 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):
232245 @return list of lists [mbed_name, mbed_dev_disk, mbed_mount_point, mbed_dev_serial, disk_hex_id]
233246 @details Find for all disk connected all MBED ones we know about from TID list
234247 """
248+ disk_hex_ids = self .get_disk_hex_ids (disk_list )
249+
235250 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 ():
239254 orphan_found = True
240255 for tid in map_tid_to_mbed .keys ():
241- if tid in disk :
256+ if disk . startswith ( tid ) :
242257 orphan_found = False
243258 break
244259 if orphan_found :
245- orphan_mbeds . append ( disk )
260+ orphan_mbeds [ disk ] = disk_hex_ids [ disk ]
246261
247262 # Search for corresponding MBED serial
248- disk_hex_ids = self .get_disk_hex_ids (orphan_mbeds )
249-
250263 result = []
251- # FInd orphan serial name
252- for dhi in disk_hex_ids :
264+ # Find orphan serial name
265+ for dhi in orphan_mbeds :
253266 orphan_serial = self .get_mbed_serial (serial_list , dhi )
254267 if orphan_serial :
255268 orphan_dev_disk = self .get_dev_name (disk_hex_ids [dhi ])
0 commit comments