Skip to content

Commit d0c1dd6

Browse files
committed
Use a context manager for locking the cache
Should help reduce the amount of programmer errors
1 parent 8a6c2e0 commit d0c1dd6

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

mbed/mbed.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import zipfile
4848
import argparse
4949
from random import randint
50+
from contextlib import contextmanager
5051

5152

5253
# Application version
@@ -1228,9 +1229,8 @@ def clone(self, url, path, rev=None, depth=None, protocol=None, offline=False, *
12281229
os.makedirs(os.path.split(path)[0])
12291230

12301231
info("Carbon copy from \"%s\" to \"%s\"" % (cache, path))
1231-
self.cache_lock(url)
1232-
shutil.copytree(cache, path)
1233-
self.cache_unlock(url)
1232+
with self.cache_lock_held(url):
1233+
shutil.copytree(cache, path)
12341234

12351235
with cd(path):
12361236
scm.seturl(formaturl(url, protocol))
@@ -1260,9 +1260,8 @@ def clone(self, url, path, rev=None, depth=None, protocol=None, offline=False, *
12601260
self.url = url
12611261
self.path = os.path.abspath(path)
12621262
self.ignores()
1263-
self.cache_lock(url)
1264-
self.set_cache(url)
1265-
self.cache_unlock(url)
1263+
with self.cache_lock_held(url):
1264+
self.set_cache(url)
12661265
return True
12671266

12681267
if offline:
@@ -1431,6 +1430,14 @@ def cache_unlock(self, url):
14311430
pass
14321431
return True
14331432

1433+
@contextmanager
1434+
def cache_lock_held(self, url):
1435+
self.cache_lock(url)
1436+
try:
1437+
yield
1438+
finally:
1439+
self.cache_unlock(url)
1440+
14341441
def pid_exists(self, pid):
14351442
try:
14361443
os.kill(int(pid), 0)

0 commit comments

Comments
 (0)