]> git.apps.os.sepia.ceph.com Git - teuthology.git/commitdiff
Reimplement the file locker as a context manager
authorZack Cerza <zack.cerza@inktank.com>
Wed, 6 Aug 2014 21:38:59 +0000 (15:38 -0600)
committerZack Cerza <zack.cerza@inktank.com>
Thu, 7 Aug 2014 17:13:42 +0000 (11:13 -0600)
Also give it a 'noop' flag to enable its callers to optionally not use
locking.

Signed-off-by: Zack Cerza <zack.cerza@inktank.com>
teuthology/repo_utils.py

index 40d45530d1f130ba5658aa988a032f1ce96a5782..5c3ee93c67c562fe78d24f102d75c1a5b4f08d74 100644 (file)
@@ -142,7 +142,7 @@ def validate_branch(branch):
         raise ValueError("Illegal branch name: '%s'" % branch)
 
 
-def fetch_qa_suite(branch):
+def fetch_qa_suite(branch, lock=True):
     """
     Make sure ceph-qa-suite is checked out.
 
@@ -153,16 +153,13 @@ def fetch_qa_suite(branch):
     dest_path = os.path.join(src_base_path, 'ceph-qa-suite_' + branch)
     qa_suite_url = os.path.join(config.ceph_git_base_url, 'ceph-qa-suite')
     # only let one worker create/update the checkout at a time
-    lock = filelock(dest_path.rstrip('/') + '.lock')
-    lock.acquire()
-    try:
+    lock_path = dest_path.rstrip('/') + '.lock'
+    with FileLock(lock_path, noop=not lock):
         enforce_repo_state(qa_suite_url, dest_path, branch)
-    finally:
-        lock.release()
     return dest_path
 
 
-def fetch_teuthology_branch(branch):
+def fetch_teuthology_branch(branch, lock=True):
     """
     Make sure we have the correct teuthology branch checked out and up-to-date
 
@@ -172,9 +169,8 @@ def fetch_teuthology_branch(branch):
     src_base_path = config.src_base_path
     dest_path = os.path.join(src_base_path, 'teuthology_' + branch)
     # only let one worker create/update the checkout at a time
-    lock = filelock(dest_path.rstrip('/') + '.lock')
-    lock.acquire()
-    try:
+    lock_path = dest_path.rstrip('/') + '.lock'
+    with FileLock(lock_path, noop=not lock):
         teuthology_git_upstream = config.ceph_git_base_url + \
             'teuthology.git'
         enforce_repo_state(teuthology_git_upstream, dest_path, branch)
@@ -195,24 +191,27 @@ def fetch_teuthology_branch(branch):
                 log.warn(line.strip())
         log.info("Bootstrap exited with status %s", returncode)
 
-    finally:
-        lock.release()
-
     return dest_path
 
 
-class filelock(object):
-    # simple flock class
-    def __init__(self, fn):
-        self.fn = fn
-        self.fd = None
-
-    def acquire(self):
-        assert not self.fd
-        self.fd = file(self.fn, 'w')
-        fcntl.lockf(self.fd, fcntl.LOCK_EX)
-
-    def release(self):
-        assert self.fd
-        fcntl.lockf(self.fd, fcntl.LOCK_UN)
-        self.fd = None
+class FileLock(object):
+    def __init__(self, filename, noop=False):
+        self.filename = filename
+        self.file = None
+        self.noop = noop
+
+    def __enter__(self):
+        if not self.noop:
+            assert self.file is None
+            self.file = file(self.filename, 'w')
+            fcntl.lockf(self.file, fcntl.LOCK_EX)
+        return self
+
+    def __exit__(self, exc_type, exc_val, exc_tb):
+        if not self.noop:
+            assert self.file is not None
+            fcntl.lockf(self.file, fcntl.LOCK_UN)
+            self.file.close()
+            self.file = None
+            if os.path.exists(self.filename):
+                os.remove(self.filename)