Browse Source

Added pfile: store STDIN in a temporary file and pass it to `file`

Alois Mahdal 11 years ago
parent
commit
e06f8326bf
1 changed files with 34 additions and 0 deletions
  1. 34
    0
      bin/pfile

+ 34
- 0
bin/pfile View File

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