utils: fix calls to close() in the lock/unlock functions

(Bitbake rev: 4262c26d36d1c1b6801ac422716a227c1f6b9589)

Signed-off-by: Chris Larson <chris_larson@mentor.com>
Signed-off-by: Richard Purdie <rpurdie@linux.intel.com>
This commit is contained in:
Chris Larson
2010-12-09 21:50:23 -05:00
committed by Richard Purdie
parent e2363f3cdd
commit 25e519a80f

View File

@@ -401,6 +401,7 @@ def better_eval(source, locals):
@contextmanager @contextmanager
def fileslocked(files): def fileslocked(files):
"""Context manager for locking and unlocking file locks."""
locks = [] locks = []
if files: if files:
for lockfile in files: for lockfile in files:
@@ -418,7 +419,7 @@ def lockfile(name):
""" """
path = os.path.dirname(name) path = os.path.dirname(name)
if not os.path.isdir(path): if not os.path.isdir(path):
logger.error("Lockfile path '%s' does not exist", path) logger.error("Lockfile destination directory '%s' does not exist", path)
sys.exit(1) sys.exit(1)
if not os.access(path, os.W_OK): if not os.access(path, os.W_OK):
@@ -437,16 +438,16 @@ def lockfile(name):
# lock is the most likely to win it. # lock is the most likely to win it.
try: try:
lf = open(name, "a + ") lf = open(name, 'a+')
fcntl.flock(lf.fileno(), fcntl.LOCK_EX) fileno = lf.fileno()
statinfo = os.fstat(lf.fileno()) fcntl.flock(fileno, fcntl.LOCK_EX)
statinfo = os.fstat(fileno)
if os.path.exists(lf.name): if os.path.exists(lf.name):
statinfo2 = os.stat(lf.name) statinfo2 = os.stat(lf.name)
if statinfo.st_ino == statinfo2.st_ino: if statinfo.st_ino == statinfo2.st_ino:
return lf return lf
# File no longer exists or changed, retry lf.close()
lf.close except Exception:
except Exception as e:
continue continue
def unlockfile(lf): def unlockfile(lf):
@@ -455,7 +456,7 @@ def unlockfile(lf):
""" """
os.unlink(lf.name) os.unlink(lf.name)
fcntl.flock(lf.fileno(), fcntl.LOCK_UN) fcntl.flock(lf.fileno(), fcntl.LOCK_UN)
lf.close lf.close()
def md5_file(filename): def md5_file(filename):
""" """