import tarfile
import os
import cStringIO

def build_logs_archive(app):
  """ Builds a .tar.gz with the contents of the system logs found for the given app and returns
      the binary contents.
  """
  path = app.config['SYSTEM_LOGS_PATH']
  buf = cStringIO.StringIO()

  with tarfile.open(mode="w:gz", fileobj=buf) as tar:
    tar.add(path, arcname=os.path.basename(path))

  return buf.getvalue()