Revision f65f63ef lib/utils.py

b/lib/utils.py
1713 1713
      raise
1714 1714

  
1715 1715

  
1716
def TailFile(fname, lines=20):
1717
  """Return the last lines from a file.
1718

  
1719
  @note: this function will only read and parse the last 4KB of
1720
      the file; if the lines are very long, it could be that less
1721
      than the requested number of lines are returned
1722

  
1723
  @param fname: the file name
1724
  @type lines: int
1725
  @param lines: the (maximum) number of lines to return
1726

  
1727
  """
1728
  fd = open(fname, "r")
1729
  try:
1730
    fd.seek(0, 2)
1731
    pos = fd.tell()
1732
    pos = max(0, pos-4096)
1733
    fd.seek(pos, 0)
1734
    raw_data = fd.read()
1735
  finally:
1736
    fd.close()
1737

  
1738
  rows = raw_data.splitlines()
1739
  return rows[-lines:]
1740

  
1741

  
1716 1742
def LockedMethod(fn):
1717 1743
  """Synchronized object access decorator.
1718 1744

  

Also available in: Unified diff