1 | """ |
---|
2 | A simple wrapper for writing tarballs as a stream. |
---|
3 | """ |
---|
4 | |
---|
5 | import logging, tarfile |
---|
6 | |
---|
7 | log = logging.getLogger( __name__ ) |
---|
8 | |
---|
9 | class StreamBall( object ): |
---|
10 | def __init__( self, mode, members=None ): |
---|
11 | self.members = members |
---|
12 | if members is None: |
---|
13 | self.members = {} |
---|
14 | self.mode = mode |
---|
15 | self.wsgi_status = None |
---|
16 | self.wsgi_headeritems = None |
---|
17 | def add( self, file, relpath ): |
---|
18 | self.members[file] = relpath |
---|
19 | def stream( self, environ, start_response ): |
---|
20 | response_write = start_response( self.wsgi_status, self.wsgi_headeritems ) |
---|
21 | class tarfileobj: |
---|
22 | def write( self, *args, **kwargs ): |
---|
23 | response_write( *args, **kwargs ) |
---|
24 | tf = tarfile.open( mode=self.mode, fileobj=tarfileobj() ) |
---|
25 | for file, rel in self.members.items(): |
---|
26 | tf.add( file, arcname=rel ) |
---|
27 | tf.close() |
---|
28 | return [] |
---|