added concrete implementations of putc(), getc(), getchar() and gets()
[tangerine.git] / tools / package / pkg
blob84c6c78634e68586da2ae5c614b5db66d48aa30b
1 #!/usr/bin/env python
2 # -*- coding: iso-8859-1 -*-
4 import os, sys, struct
6 PKG_PREFIX = 'PKG'
7 PKG_VERSION = 0
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(
18 PKG_HEADER_FORMAT,
19 file.read( struct.calcsize( PKG_HEADER_FORMAT ) )
21 version = header[3]
23 return version
26 def writeHeader( file, version ):
27 'writeHeader( file, version ) -> None'
29 file.write(
30 struct.pack(
31 PKG_HEADER_FORMAT,
32 PKG_PREFIX[0], PKG_PREFIX[1], PKG_PREFIX[2], PKG_VERSION
37 def readFile( file ):
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] )
46 file.read(1)
48 dataLength = struct.unpack(
49 PKG_FILE_DATALENGTH_FORMAT,
50 file.read( struct.calcsize( PKG_FILE_DATALENGTH_FORMAT ) )
53 data = file.read( dataLength[0] )
55 return (path, data)
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 ) ) )
64 file.write( data )
67 def listFiles( path='.' ):
68 'listFiles( path ) -> []'
70 files = []
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 ) )
76 else:
77 files.append( os.path.normpath( name ) )
79 return files
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 )
91 oldwd = os.getcwd()
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' )
106 output.write( data )
107 output.close()
109 os.chdir( oldwd )
112 def pack( source, destination ):
113 'pack( source, destination ) -> None'
115 output = open( destination, 'w' )
117 oldwd = os.getcwd()
118 os.chdir( source )
120 files = listFiles()
122 writeHeader( output, PKG_VERSION )
124 for path in files:
125 print %s (%d bytes)' % (path, os.path.getsize( path ))
127 input = open( path )
128 writeFile( output, path, input.read() )
129 input.close()
131 os.chdir( oldwd )
133 output.close()
136 if __name__ == '__main__':
137 mode = sys.argv[1]
138 source = sys.argv[2]
139 destination = sys.argv[3]
141 if mode == 'c':
142 pack( destination, source )
143 elif mode == 'x':
144 unpack( source, destination )
145 else:
146 print 'Invalid mode.'