12345678910111213141516171819202122232425262728293031323334
  1. #!/usr/bin/python
  2. import os
  3. import subprocess
  4. import sys
  5. import tempfile
  6. def stdin_chunk(chunksize):
  7. while True:
  8. chunk = sys.stdin.read(chunksize)
  9. if chunk:
  10. yield chunk
  11. else:
  12. break
  13. if __name__ == '__main__':
  14. CHUNKSIZE = 8192
  15. tfile, tname = tempfile.mkstemp()
  16. for chunk in stdin_chunk(CHUNKSIZE):
  17. os.write(tfile, chunk)
  18. os.close(tfile)
  19. p = subprocess.Popen(["file", tname], stdout=subprocess.PIPE)
  20. stdout, stderr = p.communicate()
  21. os.unlink(tname)
  22. if stderr:
  23. sys.stderr.write(stderr)
  24. sys.exit(1)
  25. else:
  26. sys.stdout.write(stdout)