https://pantsbuild.org/ logo
#general
Title
# general
b

bored-art-40741

03/23/2016, 9:38 PM
Copy code
@contextmanager
def owner_printing_file_lock(path, blocking=True, timeout=None):
  lock = InterProcessLock(path)
  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)
  else:
    lock.release()

  with lock.acquire(blocking=blocking, timeout=timeout):
    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 lock
    with open(lock.path, 'w'):
      pass