1
1
2
- from gzip import GzipFile
3
- from StringIO import StringIO
2
+ # from gzip import GzipFile
3
+ # from StringIO import StringIO
4
4
import struct
5
5
6
+ from subprocess import Popen , PIPE
7
+ from fcntl import fcntl , F_SETFL , F_GETFL
8
+ from select import select
9
+ from os import O_NONBLOCK , write
10
+
11
+ def cmd (args , text ):
12
+ proc = Popen (args , stdin = PIPE , stdout = PIPE )
13
+ output = ''
14
+ set_non_blocking = lambda fd : fcntl (fd , F_SETFL , fcntl (fd , F_GETFL ) | O_NONBLOCK )
15
+ set_non_blocking (proc .stdin )
16
+ set_non_blocking (proc .stdout )
17
+ while proc .poll () is None :
18
+ r ,w ,x = select ([proc .stdout ], [proc .stdin ] if text else [], [], 0.1 )
19
+ if r :
20
+ output += proc .stdout .read ()
21
+ if w :
22
+ n = write (proc .stdin .fileno (), text )
23
+ text = text [n :]
24
+ if not text :
25
+ proc .stdin .close ()
26
+ if proc .returncode :
27
+ raise OSError (args , proc .returncode )
28
+ r ,w ,x = select ([proc .stdout ], [], [], 0.1 )
29
+ if r :
30
+ output += proc .stdout .read ()
31
+ return output
32
+
6
33
class BadNBTData (Exception ):
7
34
pass
8
35
@@ -28,16 +55,18 @@ def read(f, n):
28
55
29
56
def gunzip (s ):
30
57
"""Unzips a string compressed with gzip compression (level 9)"""
31
- return GzipFile (fileobj = StringIO (s ), mode = 'r' ).read ()
58
+ #return GzipFile(fileobj=StringIO(s), mode='r').read()
59
+ return cmd (['gunzip' ], s )
32
60
33
61
def gzip (s ):
34
62
"""Zips a string with gzip compression (level 9)"""
35
- sio = StringIO ()
36
- gz = GzipFile (fileobj = sio , mode = 'w' )
37
- gz .write (s )
38
- gz .close ()
39
- sio .seek (0 )
40
- return sio .read ()
63
+ # sio = StringIO()
64
+ # gz = GzipFile(fileobj=sio, mode='w')
65
+ # gz.write(s)
66
+ # gz.close()
67
+ # sio.seek(0)
68
+ # return sio.read()
69
+ return cmd (['gzip' ], s )
41
70
42
71
43
72
class NBTCompound (dict ):
0 commit comments