Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def it_releases_lock_on_exit(self, tmpfile):
with flock(tmpfile):
print('oh, hi!')
with flock(tmpfile):
print('oh, hi!')
def it_disallows_subsequent_callers(self, tmpfile):
with flock(tmpfile):
print('oh, hi!')
assert_locked(tmpfile)
def it_works_fine_with_a_directory(self, tmpfile):
import os.path
assert not os.path.isdir(tmpfile)
os.mkdir(tmpfile)
with flock(tmpfile):
print('oh, hi!')
assert os.path.isdir(tmpfile)
def assert_locked(tmpfile):
with ShouldRaise(flock.Locked(tmpfile)):
with flock(tmpfile):
raise AssertionError('this should not work')
def it_creates_a_file_if_it_didnt_exist(self, tmpfile):
from os.path import exists
assert not exists(tmpfile)
with flock(tmpfile):
print('oh, hi!')
assert exists(tmpfile)
def flock(path):
"""attempt to show the user a better message on failure, and handle the race condition"""
def handle_race(path):
show_runaway_processes(path)
if handle_race.limit > 0:
handle_race.limit -= 1
else:
reraise(Impossible('lock is held, but not by any process, ten times'))
handle_race.limit = 10
from .flock import flock
return flock(path, on_fail=handle_race)
def playground_locked(self):
"""Lock the entire playground."""
def on_lock_held(path):
reraise(LockHeld(
'another pgctl command is currently managing this service: (%s)\n%s' %
(bestrelpath(path), ps(fuser(path)))
))
with contextlib2.ExitStack() as context:
for service in self.services:
service.ensure_exists()
# This lock represents a pgctl cli interacting with the service.
from .flock import flock
lock = context.enter_context(flock(
service.path.join('.pgctl.lock').strpath,
on_fail=on_lock_held,
))
from .flock import set_fd_inheritable
set_fd_inheritable(lock, False)
yield