Last active
October 10, 2016 07:33
-
-
Save milesrout/6ca29400f5cc2055e02f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class _open_grailfile: | |
def __init__(self, path): | |
self.path = path | |
def __enter__(self): | |
"""Lock and open the Grailfile at the given path.""" | |
# if the Grailfile is foobar/Grailfile, store a lock at foobar/.grail/LOCK | |
dotdir_path = _get_dotgrail_dir(path) | |
lock_path = dotdir_path / 'LOCK' | |
# Don't sit there waiting for the Grailfile to be unlocked | |
lock = fasteners.InterProcessLock(str(lock_path)) | |
with fasteners.try_lock(lock) as got: | |
if not got: | |
raise utils.GrailError("Grailfile is locked") | |
with path.open('r') as f: | |
lines = list(f.readlines()) | |
return Grailfile(lines) | |
def __exit__(self): | |
# When the context manager is exiting, write out the contents of the manifest to disk. | |
with path.open('w') as f: | |
grailfile.write(f) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment