2 # -*- coding: iso-8859-1 -*-
9 PKG_HEADER_FORMAT
= '3cB'
11 PKG_FILE_PATHLENGTH_FORMAT
= '!I'
12 PKG_FILE_DATALENGTH_FORMAT
= '!I'
14 def readHeader( file ):
15 'readHeader( file ) -> version'
17 header
= struct
.unpack(
19 file.read( struct
.calcsize( PKG_HEADER_FORMAT
) )
26 def writeHeader( file, version
):
27 'writeHeader( file, version ) -> None'
32 PKG_PREFIX
[0], PKG_PREFIX
[1], PKG_PREFIX
[2], PKG_VERSION
38 'readFile( file ) -> (path, data)'
40 pathLength
= struct
.unpack(
41 PKG_FILE_PATHLENGTH_FORMAT
,
42 file.read( struct
.calcsize( PKG_FILE_PATHLENGTH_FORMAT
) )
45 path
= file.read( pathLength
[0] )
48 dataLength
= struct
.unpack(
49 PKG_FILE_DATALENGTH_FORMAT
,
50 file.read( struct
.calcsize( PKG_FILE_DATALENGTH_FORMAT
) )
53 data
= file.read( dataLength
[0] )
58 def writeFile( file, path
, data
):
59 'writeFile( file, path, data ) -> None'
61 file.write( struct
.pack( PKG_FILE_PATHLENGTH_FORMAT
, len( path
) ) )
62 file.write( path
+ "\0")
63 file.write( struct
.pack( PKG_FILE_DATALENGTH_FORMAT
, len( data
) ) )
67 def listFiles( path
='.' ):
68 'listFiles( path ) -> []'
71 for name
in os
.listdir( path
):
72 name
= os
.path
.join( path
, name
)
74 if os
.path
.isdir( name
):
75 files
.extend( listFiles( name
) )
77 files
.append( os
.path
.normpath( name
) )
82 def unpack( source
, destination
):
83 'unpack( source, destination ) -> None'
85 if not os
.path
.exists( destination
):
86 os
.makedirs( destination
)
88 inputSize
= os
.path
.getsize( source
)
89 input = open( source
)
92 os
.chdir( destination
)
94 version
= readHeader( input )
96 while input.tell() < inputSize
:
97 (path
, data
) = readFile( input )
99 print '« %s (%d bytes)' % (path
, len( data
))
101 directory
= os
.path
.dirname( path
)
102 if directory
!= '' and not os
.path
.exists( directory
):
103 os
.makedirs( directory
)
105 output
= open( path
, 'w' )
112 def pack( source
, destination
):
113 'pack( source, destination ) -> None'
115 output
= open( destination
, 'w' )
122 writeHeader( output
, PKG_VERSION
)
125 print '» %s (%d bytes)' % (path
, os
.path
.getsize( path
))
128 writeFile( output
, path
, input.read() )
136 if __name__
== '__main__':
139 destination
= sys
.argv
[3]
142 pack( destination
, source
)
144 unpack( source
, destination
)
146 print 'Invalid mode.'