@@ -35,12 +35,6 @@ def __init__(self):
3535 self .ERRORLEVEL_FLAG = 0 # Used to return success code to environment
3636 self .retarget_data = {} # Used to retarget mbed-enabled platform properties
3737
38- # If there is a local mocking data use it and add / override manufacture_ids
39- mock_ids = self .mock_read ()
40- if mock_ids :
41- for mid in mock_ids :
42- self .manufacture_ids [mid ] = mock_ids [mid ]
43-
4438 # Create in HOME directory place for mbed-ls to store information
4539 self .mbedls_home_dir_init ()
4640
@@ -211,17 +205,35 @@ def __init__(self):
211205 MBED_HTM_NAME = 'mbed.htm'
212206
213207 def mbedls_home_dir_init (self ):
214- """ Initialize data in home directory for locking features
208+ """! Initialize data in home directory for locking features
209+ @details Create '.mbed-ls' sub-directory in current user $HOME directory
215210 """
216211 if not os .path .isdir (os .path .join (self .HOME_DIR , self .MBEDLS_HOME_DIR )):
217212 os .mkdir (os .path .join (self .HOME_DIR , self .MBEDLS_HOME_DIR ))
218213
214+ def mbedls_get_mocks (self ):
215+ """! Load existing mocking configuration from current user $HOME directory
216+ @details If there is a local mocking data use it and add / override manufacture_ids
217+ """
218+ mock_ids = self .mock_read ()
219+ if mock_ids :
220+ self .debug (self .mbedls_get_mocks .__name__ , "mock data found, %d entries" % len (mock_ids ))
221+ for mid in mock_ids :
222+ self .manufacture_ids [mid ] = mock_ids [mid ]
223+
219224 def mbedls_get_global_lock (self ):
225+ """! Create lock (file lock) object used to guard operations on
226+ mock configuration file in current user $HOME directory
227+ @return Global lock object instance
228+ """
220229 file_path = os .path .join (self .HOME_DIR , self .MBEDLS_HOME_DIR , self .MBEDLS_GLOBAL_LOCK )
221230 lock = lockfile .LockFile (file_path )
222231 return lock
223232
224233 def list_manufacture_ids (self ):
234+ """! Creates list of all available mappings for target_id -> Platform
235+ @return String with table formatted output
236+ """
225237 from prettytable import PrettyTable
226238
227239 columns = ['target_id_prefix' , 'platform_name' ]
@@ -237,11 +249,12 @@ def list_manufacture_ids(self):
237249
238250 def mock_read (self ):
239251 """! Load mocking data from local file
252+ @details Uses file locking for operation on Mock
253+ configuration file in current user $HOME directory
240254 @return Curent mocking configuration (dictionary)
241255 """
242256
243257 def read_mock_file (filename ):
244- self .debug (self .mock_read .__name__ , "reading mock file '%s'" % filename )
245258 try :
246259 with open (filename , "r" ) as f :
247260 return json .load (f )
@@ -253,31 +266,38 @@ def read_mock_file(filename):
253266 str (e )))
254267 return {}
255268
256- try :
257- lock = self .mbedls_get_global_lock ()
258- if lock .acquire (timeout = 0.5 ):
259- # This read is for backward compatibility
260- # When user already have on its system local mock-up it will work
261- # overwriting global one
262- if isfile (self .MOCK_FILE_NAME ):
263- ret = read_mock_file (self .MOCK_FILE_NAME )
264- lock .release ()
265- return ret
266-
267- if isfile (self .MOCK_HOME_FILE_NAME ):
268- ret = read_mock_file (self .MOCK_HOME_FILE_NAME )
269- lock .release ()
270- return ret
271- except (LockFailed , LockTimeout ) as e :
272- self .err (str (e ))
273- return {}
269+ lock = self .mbedls_get_global_lock ()
270+ while not lock .i_am_locking ():
271+ try :
272+ lock .acquire (timeout = 1 )
273+ except LockTimeout as e :
274+ lock .break_lock ()
275+ lock .acquire ()
276+ self .debug (self .mock_read .__name__ , str (e ))
277+ self .debug (self .mock_read .__name__ , "locked '%s'" % lock .path )
278+
279+ result = {}
280+
281+ # This read is for backward compatibility
282+ # When user already have on its system local mock-up it will work
283+ # overwriting global one
284+ if isfile (self .MOCK_FILE_NAME ):
285+ result = read_mock_file (self .MOCK_FILE_NAME )
286+ elif isfile (self .MOCK_HOME_FILE_NAME ):
287+ result = read_mock_file (self .MOCK_HOME_FILE_NAME )
288+
289+ lock .release ()
290+ self .debug (self .mock_read .__name__ , "released '%s'" % lock .path )
291+ return result
274292
275293 def mock_write (self , mock_ids ):
276294 """! Write current mocking structure
277295 @param mock_ids JSON mock data to dump to file
296+ @details Uses file locking for operation on Mock
297+ configuration file in current user $HOME directory
298+ @return True if configuration operation was success, else False
278299 """
279300 def write_mock_file (filename , mock_ids ):
280- self .debug (self .mock_write .__name__ , "writing mock file '%s'" % filename )
281301 try :
282302 with open (filename , "w" ) as f :
283303 f .write (json .dumps (mock_ids , indent = 4 ))
@@ -290,15 +310,21 @@ def write_mock_file(filename, mock_ids):
290310 str (e )))
291311 return False
292312
293- try :
294- lock = self .mbedls_get_global_lock ()
295- if lock .acquire (timeout = 0.5 ):
296- ret = write_mock_file (self .MOCK_HOME_FILE_NAME , mock_ids )
297- lock .release ()
298- return ret
299- except (LockFailed , LockTimeout ) as e :
300- self .err (str (e ))
301- return False
313+ lock = self .mbedls_get_global_lock ()
314+ while not lock .i_am_locking ():
315+ try :
316+ lock .acquire (timeout = 1 )
317+ except LockTimeout as e :
318+ lock .break_lock ()
319+ lock .acquire ()
320+ self .debug (self .mock_read .__name__ , str (e ))
321+ self .debug (self .mock_write .__name__ , "locked '%s'" % lock .path )
322+
323+ result = write_mock_file (self .MOCK_HOME_FILE_NAME , mock_ids )
324+
325+ lock .release ()
326+ self .debug (self .mock_read .__name__ , "released '%s'" % lock .path )
327+ return result
302328
303329 def retarget_read (self ):
304330 """! Load retarget data from local file
@@ -319,6 +345,8 @@ def retarget_read(self):
319345
320346 def retarget (self ):
321347 """! Enable retargeting
348+ @details Read data from local retarget configuration file
349+ @return Retarget data structure read from configuration file
322350 """
323351 self .retarget_data = self .retarget_read ()
324352 return self .retarget_data
0 commit comments