Revision 7831fc5f lib/utils/__init__.py

b/lib/utils/__init__.py
59 59
from ganeti.utils.mlock import * # pylint: disable-msg=W0401
60 60
from ganeti.utils.log import * # pylint: disable-msg=W0401
61 61
from ganeti.utils.hash import * # pylint: disable-msg=W0401
62
from ganeti.utils.wrapper import * # pylint: disable-msg=W0401
62 63

  
63 64

  
64 65
#: when set to True, L{RunCmd} is disabled
......
662 663
  return status
663 664

  
664 665

  
665
def SetCloseOnExecFlag(fd, enable):
666
  """Sets or unsets the close-on-exec flag on a file descriptor.
667

  
668
  @type fd: int
669
  @param fd: File descriptor
670
  @type enable: bool
671
  @param enable: Whether to set or unset it.
672

  
673
  """
674
  flags = fcntl.fcntl(fd, fcntl.F_GETFD)
675

  
676
  if enable:
677
    flags |= fcntl.FD_CLOEXEC
678
  else:
679
    flags &= ~fcntl.FD_CLOEXEC
680

  
681
  fcntl.fcntl(fd, fcntl.F_SETFD, flags)
682

  
683

  
684
def SetNonblockFlag(fd, enable):
685
  """Sets or unsets the O_NONBLOCK flag on on a file descriptor.
686

  
687
  @type fd: int
688
  @param fd: File descriptor
689
  @type enable: bool
690
  @param enable: Whether to set or unset it
691

  
692
  """
693
  flags = fcntl.fcntl(fd, fcntl.F_GETFL)
694

  
695
  if enable:
696
    flags |= os.O_NONBLOCK
697
  else:
698
    flags &= ~os.O_NONBLOCK
699

  
700
  fcntl.fcntl(fd, fcntl.F_SETFL, flags)
701

  
702

  
703
def RetryOnSignal(fn, *args, **kwargs):
704
  """Calls a function again if it failed due to EINTR.
705

  
706
  """
707
  while True:
708
    try:
709
      return fn(*args, **kwargs)
710
    except EnvironmentError, err:
711
      if err.errno != errno.EINTR:
712
        raise
713
    except (socket.error, select.error), err:
714
      # In python 2.6 and above select.error is an IOError, so it's handled
715
      # above, in 2.5 and below it's not, and it's handled here.
716
      if not (err.args and err.args[0] == errno.EINTR):
717
        raise
718

  
719

  
720 666
def RunParts(dir_name, env=None, reset_env=False):
721 667
  """Run Scripts or programs in a directory
722 668

  
......
1876 1822
  return result
1877 1823

  
1878 1824

  
1879
def TestDelay(duration):
1880
  """Sleep for a fixed amount of time.
1881

  
1882
  @type duration: float
1883
  @param duration: the sleep duration
1884
  @rtype: boolean
1885
  @return: False for negative value, True otherwise
1886

  
1887
  """
1888
  if duration < 0:
1889
    return False, "Invalid sleep duration"
1890
  time.sleep(duration)
1891
  return True, None
1892

  
1893

  
1894
def CloseFdNoError(fd, retries=5):
1895
  """Close a file descriptor ignoring errors.
1896

  
1897
  @type fd: int
1898
  @param fd: the file descriptor
1899
  @type retries: int
1900
  @param retries: how many retries to make, in case we get any
1901
      other error than EBADF
1902

  
1903
  """
1904
  try:
1905
    os.close(fd)
1906
  except OSError, err:
1907
    if err.errno != errno.EBADF:
1908
      if retries > 0:
1909
        CloseFdNoError(fd, retries - 1)
1910
    # else either it's closed already or we're out of retries, so we
1911
    # ignore this and go on
1912

  
1913

  
1914 1825
def CloseFDs(noclose_fds=None):
1915 1826
  """Close file descriptors.
1916 1827

  
......
2638 2549
  return bool(exitcode)
2639 2550

  
2640 2551

  
2641
def IgnoreProcessNotFound(fn, *args, **kwargs):
2642
  """Ignores ESRCH when calling a process-related function.
2643

  
2644
  ESRCH is raised when a process is not found.
2645

  
2646
  @rtype: bool
2647
  @return: Whether process was found
2648

  
2649
  """
2650
  try:
2651
    fn(*args, **kwargs)
2652
  except EnvironmentError, err:
2653
    # Ignore ESRCH
2654
    if err.errno == errno.ESRCH:
2655
      return False
2656
    raise
2657

  
2658
  return True
2659

  
2660

  
2661
def IgnoreSignals(fn, *args, **kwargs):
2662
  """Tries to call a function ignoring failures due to EINTR.
2663

  
2664
  """
2665
  try:
2666
    return fn(*args, **kwargs)
2667
  except EnvironmentError, err:
2668
    if err.errno == errno.EINTR:
2669
      return None
2670
    else:
2671
      raise
2672
  except (select.error, socket.error), err:
2673
    # In python 2.6 and above select.error is an IOError, so it's handled
2674
    # above, in 2.5 and below it's not, and it's handled here.
2675
    if err.args and err.args[0] == errno.EINTR:
2676
      return None
2677
    else:
2678
      raise
2679

  
2680

  
2681 2552
def LockFile(fd):
2682 2553
  """Locks a file using POSIX locks.
2683 2554

  
......
2736 2607
  return value
2737 2608

  
2738 2609

  
2739
def GetClosedTempfile(*args, **kwargs):
2740
  """Creates a temporary file and returns its path.
2741

  
2742
  """
2743
  (fd, path) = tempfile.mkstemp(*args, **kwargs)
2744
  CloseFdNoError(fd)
2745
  return path
2746

  
2747

  
2748 2610
def GenerateSelfSignedX509Cert(common_name, validity):
2749 2611
  """Generates a self-signed X509 certificate.
2750 2612

  

Also available in: Unified diff