bored-art-40741
03/23/2016, 10:10 PM@contextmanager
def owner_printing_file_lock(path, blocking=True, timeout=None):
lock = InterProcessLock(path)
@contextmanager
def pid_message():
with open(lock.path, 'wb') as f:
current_process = psutil.Process()
message = '{} ({})'.format(current_process.pid, ' '.join(current_process.cmdline()))
f.write(message.encode('utf-8'))
yield
with open(lock.path, 'w'): pass
if not lock.acquire(blocking=False):
with open(lock.path, 'rb') as f:
lockfile_contents = f.read().decode('utf-8', 'replace')
print('Waiting on pants process to complete: {}'.format(lockfile_contents), file=sys.stderr)
with lock.acquire(blocking=blocking, timeout=timeout):
with pid_message():
yield lock
else:
with pid_message():
yield lock