45
45
from itertools import chain , repeat
46
46
import time
47
47
import zipfile
48
- import argparse
48
+ from random import randint
49
49
50
50
51
51
# Application version
@@ -156,7 +156,7 @@ def log(msg, is_error=False):
156
156
sys .stderr .write (msg ) if is_error else sys .stdout .write (msg )
157
157
158
158
def message (msg ):
159
- return "[mbed] %s\n " % msg
159
+ return "[mbed-%s ] %s\n " % ( os . getpid (), msg )
160
160
161
161
def info (msg , level = 1 ):
162
162
if level <= 0 or verbose :
@@ -1345,6 +1345,7 @@ def cache_lock(self, url):
1345
1345
cpath = self .url2cachedir (url )
1346
1346
if cpath :
1347
1347
lock_file = os .path .join (cpath , '.lock' )
1348
+
1348
1349
if not os .path .isdir (cpath ):
1349
1350
os .makedirs (cpath )
1350
1351
@@ -1353,35 +1354,29 @@ def cache_lock(self, url):
1353
1354
for i in range (300 ):
1354
1355
if i :
1355
1356
time .sleep (1 )
1356
- if os .path .isfile (lock_file ):
1357
- try :
1358
- # lock file exists, but we need to check pid as well in case the process died
1357
+
1358
+ try :
1359
+ # lock file exists, but we need to check pid as well in case the process died
1360
+ if os .path .isfile (lock_file ):
1359
1361
with open (lock_file , 'r' , 0 ) as f :
1360
- pid = f .read (8 )
1361
- if pid and int (pid ) != os .getpid ():
1362
- if self .pid_exists (pid ):
1363
- info ("Cache lock file exists and process %s is alive." % pid )
1364
- continue
1362
+ pid = int (f .read (8 ))
1363
+ if pid != os .getpid () and self .pid_exists (pid ):
1364
+ info ("Cache lock file exists and process %s is alive." % pid )
1365
1365
else :
1366
- info ("Cache lock file exists, but process is dead. Cleaning up" )
1366
+ info ("Cache lock file exists, but %s is dead. Cleaning up" % pid )
1367
1367
os .remove (lock_file )
1368
- continue
1369
- except (IOError , OSError ):
1370
1368
continue
1371
- pass
1372
-
1373
- can_lock = True
1374
- break
1375
1369
1376
- if can_lock :
1377
- try :
1378
1370
with open (lock_file , 'wb' , 0 ) as f :
1379
- info ("Writing cache lock file for pid %s" % os .getpid ())
1371
+ info ("Writing cache lock file %s for pid %s" % ( lock_file , os .getpid () ))
1380
1372
f .write (str (os .getpid ()))
1381
1373
f .flush ()
1382
1374
os .fsync (f )
1375
+ break
1383
1376
except (IOError , OSError ):
1384
- error ("Unable to lock cache dir \" %s\" " % (cpath ))
1377
+ error ("OS error occurred" )
1378
+ except Exception as e :
1379
+ error ("Weird error occurred" )
1385
1380
1386
1381
else :
1387
1382
error ("Exceeded 5 minutes limit while waiting for other process to finish caching" )
0 commit comments