X-Git-Url: https://scripts.mit.edu/gitweb/wizard.git/blobdiff_plain/8264ae4501b4e3ca5e7fef548bebb33c16a62a65..e44406f8baa8d92acedfc69c528d6afe41af22e1:/wizard/util.py diff --git a/wizard/util.py b/wizard/util.py index 621dea1..2d9c72f 100644 --- a/wizard/util.py +++ b/wizard/util.py @@ -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? +""" +