removed obsolete file
[tangerine.git] / tools / package / pkg
blob7a99d4e1a800b8d5e72819ccd4e07dfe73a572eb
1 #!/usr/bin/env python
2 # -*- coding: iso-8859-1 -*-
4 import os, sys, struct
5 from stat import ST_SIZE
7 PKG_PREFIX = 'PKG'
8 PKG_VERSION = 1
10 PKG_HEADER_FORMAT = '3cB'
12 PKG_FILE_PATHLENGTH_FORMAT = '!I'
13 PKG_FILE_DATALENGTH_FORMAT = '!I'
15 def readHeader( file ):
16 'readHeader( file ) -> version'
18 header = struct.unpack(
19 PKG_HEADER_FORMAT,
20 file.read( struct.calcsize( PKG_HEADER_FORMAT ) )
22 version = header[3]
24 size = struct.unpack(
25 PKG_FILE_DATALENGTH_FORMAT,
26 file.read( struct.calcsize( PKG_FILE_DATALENGTH_FORMAT ) )
29 return version
32 def writeHeader( file, version ):
33 'writeHeader( file, version ) -> None'
35 file.write(
36 struct.pack(
37 PKG_HEADER_FORMAT,
38 PKG_PREFIX[0], PKG_PREFIX[1], PKG_PREFIX[2], PKG_VERSION
42 file.write(
43 struct.pack(
44 PKG_FILE_DATALENGTH_FORMAT,
50 def readFile( file ):
51 'readFile( file ) -> (path, data)'
53 pathLength = struct.unpack(
54 PKG_FILE_PATHLENGTH_FORMAT,
55 file.read( struct.calcsize( PKG_FILE_PATHLENGTH_FORMAT ) )
58 path = file.read( pathLength[0] )
59 file.read(1)
61 dataLength = struct.unpack(
62 PKG_FILE_DATALENGTH_FORMAT,
63 file.read( struct.calcsize( PKG_FILE_DATALENGTH_FORMAT ) )
66 data = file.read( dataLength[0] )
68 return (path, data)
71 def writeFile( file, path, data ):
72 'writeFile( file, path, data ) -> None'
74 file.write( struct.pack( PKG_FILE_PATHLENGTH_FORMAT, len( path ) ) )
75 file.write( path + "\0")
76 file.write( struct.pack( PKG_FILE_DATALENGTH_FORMAT, len( data ) ) )
77 file.write( data )
80 def listFiles( path='.' ):
81 'listFiles( path ) -> []'
83 files = []
84 for name in os.listdir( path ):
85 name = os.path.join( path, name )
87 if os.path.isdir( name ):
88 files.extend( listFiles( name ) )
89 else:
90 files.append( os.path.normpath( name ) )
92 return files
95 def unpack( source, destination ):
96 'unpack( source, destination ) -> None'
98 if not os.path.exists( destination ):
99 os.makedirs( destination )
101 inputSize = os.path.getsize( source )
102 input = open( source )
104 oldwd = os.getcwd()
105 os.chdir( destination )
107 version = readHeader( input )
109 while input.tell() < inputSize:
110 (path, data) = readFile( input )
112 print %s (%d bytes)' % (path, len( data ))
114 directory = os.path.dirname( path )
115 if directory != '' and not os.path.exists( directory ):
116 os.makedirs( directory )
118 output = open( path, 'w' )
119 output.write( data )
120 output.close()
122 os.chdir( oldwd )
125 def pack( source, destination ):
126 'pack( source, destination ) -> None'
128 output = open( destination, 'w' )
130 oldwd = os.getcwd()
131 os.chdir( source )
133 files = listFiles()
135 writeHeader( output, PKG_VERSION )
137 for path in files:
138 print %s (%d bytes)' % (path, os.path.getsize( path ))
140 input = open( path )
141 writeFile( output, path, input.read() )
142 input.close()
144 os.chdir( oldwd )
146 output.seek( struct.calcsize( PKG_HEADER_FORMAT ) )
147 st = os.fstat( output.fileno() )
149 output.write(
150 struct.pack(
151 PKG_FILE_DATALENGTH_FORMAT,
152 st[ST_SIZE]
157 output.close()
160 if __name__ == '__main__':
161 mode = sys.argv[1]
162 source = sys.argv[2]
163 destination = sys.argv[3]
165 if mode == 'c':
166 pack( destination, source )
167 elif mode == 'x':
168 unpack( source, destination )
169 else:
170 print 'Invalid mode.'