def RunConstrainedCmd(cmd,
                      lock_file,
                      path,
                      inp=None,
                      _lock_timeout=_RCMD_LOCK_TIMEOUT,
                      _sleep_fn=time.sleep,
                      _prepare_fn=_PrepareRestrictedCmd,
                      _runcmd_fn=utils.RunCmd,
                      _enabled=constants.ENABLE_RESTRICTED_COMMANDS):
  logging.info("Preparing to run restricted command '%s'", cmd)

  if not _enabled:
    _Fail("Restricted commands disabled at configure time")

  lock = None
  try:
    cmdresult = None
    try:
      lock = utils.FileLock.Open(lock_file)
      lock.Exclusive(blocking=True, timeout=_lock_timeout)

      (status, value) = _prepare_fn(path, cmd)

      if status:
        if inp:
          input_fd = tempfile.TemporaryFile()
          input_fd.write(inp)
          input_fd.flush()
          input_fd.seek(0)
        else:
          input_fd = None
        cmdresult = _runcmd_fn([value], env={}, reset_env=True,
                               postfork_fn=lambda _: lock.Unlock(),
                               input_fd=input_fd)
        if input_fd:
          input_fd.close()
      else:
        logging.error(value)
    except Exception: # pylint: disable=W0703
      # Keep original error in log
      logging.exception("Caught exception")

    if cmdresult is None:
      logging.info("Sleeping for %0.1f seconds before returning",
                   _RCMD_INVALID_DELAY)
      _sleep_fn(_RCMD_INVALID_DELAY)

      # Do not include original error message in returned error
      _Fail("Executing command '%s' failed" % cmd)
    elif cmdresult.failed or cmdresult.fail_reason:
      _Fail("Restricted command '%s' failed: %s; output: %s",
            cmd, cmdresult.fail_reason, cmdresult.output)
    else:
      return cmdresult.output
  finally:
    if lock is not None:
      # Release lock at last
      lock.Close()
      lock = None
Read a Agilent/Varian binary (fid) file.
dataset/ETHPy150Open ganeti/ganeti/lib/backend.py/RunConstrainedCmd--crossover with--dataset/ETHPy150Open jjhelmus/nmrglue/nmrglue/fileio/varian.py/read_fid