]> scripts.mit.edu Git - wizard.git/blobdiff - wizard/util.py
Implement locks for upgrade, add d.upgrade()
[wizard.git] / wizard / util.py
index 621dea16d61a9d4b36292562c04c1b32d726c1d4..2d9c72f118467f9cbc14525acd237927b3a87b08 100644 (file)
@@ -83,6 +83,27 @@ class IgnoreKeyboardInterrupts(object):
     def __exit__(self, *args):
         signal.signal(signal.SIGINT, signal.default_int_handler)
 
+class LockDirectory(object):
+    """
+    Context for locking a directory.
+    """
+    def __init__(self, lockfile):
+        self.lockfile = lockfile
+    def __enter__(self):
+        try:
+            os.open(self.lockfile, os.O_CREAT | os.O_EXCL)
+        except OSError as e:
+            if e.errno == errno.EEXIST:
+                raise DirectoryLockedError(os.getcwd())
+            elif e.errno == errno.EACCES:
+                raise command.PermissionsError(os.getcwd())
+            raise
+    def __exit__(self, *args):
+        try:
+            os.unlink(self.lockfile)
+        except OSError:
+            pass
+
 def chdir(dir):
     """
     Changes a directory, but has special exceptions for certain
@@ -293,3 +314,14 @@ class PermissionsError(IOError):
 
 class NoSuchDirectoryError(IOError):
     errno = errno.ENOENT
+
+class DirectoryLockedError(wizard.Error):
+    def __init__(self, dir):
+        self.dir = dir
+    def __str__(self):
+        return """
+
+ERROR: Could not acquire lock on directory.  Maybe there is
+another migration process running?
+"""
+