blob: 2afb26f5853e1127f3c8825e8a1c8ba7d693303f [file] [log] [blame]
Jack Jansen7571f301995-07-29 13:48:41 +00001#
2# Turn a pyc file into a resource file containing it in 'PYC ' resource form
Jack Jansen7571f301995-07-29 13:48:41 +00003from Res import *
4import Res
5from Resources import *
6import os
7import macfs
8import sys
9
10READ = 1
11WRITE = 2
12smAllScripts = -3
13
14error = 'mkpycresourcefile.error'
15
16def Pstring(str):
17 if len(str) > 255:
18 raise ValueError, 'String too large'
19 return chr(len(str))+str
20
21def createoutput(dst):
22 """Create output file. Return handle and first id to use."""
23
24
Jack Jansen532e3c21996-02-21 15:36:26 +000025 FSpCreateResFile(dst, 'Pyth', 'rsrc', smAllScripts)
Jack Jansen7571f301995-07-29 13:48:41 +000026 output = FSpOpenResFile(dst, WRITE)
27 UseResFile(output)
28 num = 128
29 return output, num
30
31def writemodule(name, id, data):
32 """Write pyc code to a PYC resource with given name and id."""
33 # XXXX Check that it doesn't exist
34 res = Resource(data)
35 res.AddResource('PYC ', id, name)
36 res.WriteResource()
37 res.ReleaseResource()
38
39def mkpycresourcefile(src, dst):
40 """Copy pyc file/dir src to resource file dst."""
41
42 if not os.path.isdir(src) and src[-4:] <> '.pyc':
43 raise error, 'I can only handle .pyc files or directories'
44 handle, oid = createoutput(dst)
45 if os.path.isdir(src):
46 id = handlesubdir(handle, oid, src)
47 else:
48 id = handleonepycfile(handle, oid, src)
49 print 'Wrote',id-oid,'PYC resources to', dst
50 CloseResFile(handle)
51
52def handleonepycfile(handle, id, file):
53 """Copy one pyc file to the open resource file"""
54 d, name = os.path.split(file)
55 name = name[:-4]
56 print ' module', name
57 writemodule(name, id, open(file, 'rb').read())
58 return id+1
59
60def handlesubdir(handle, id, srcdir):
61 """Recursively scan a directory for pyc files and copy to resources"""
62 print 'Directory', srcdir
63 src = os.listdir(srcdir)
64 for file in src:
65 file = os.path.join(srcdir, file)
66 if os.path.isdir(file):
67 id = handlesubdir(handle, id, file)
68 elif file[-4:] == '.pyc':
69 id = handleonepycfile(handle, id, file)
70 return id
71
72
73if __name__ == '__main__':
74 args = sys.argv[1:]
75 if not args:
Jack Jansen9062fa21995-08-14 12:21:12 +000076 ifss, ok = macfs.GetDirectory('Select root of tree to pack:')
77 if not ok:
78 sys.exit(0)
79 args = [ifss.as_pathname()]
Jack Jansen7571f301995-07-29 13:48:41 +000080 for ifn in args:
81 ofss, ok = macfs.StandardPutFile('Output for '+os.path.split(ifn)[1])
82 if not ok:
83 sys.exit(0)
84 mkpycresourcefile(ifn, ofss.as_pathname())
85 sys.exit(1) # So we can see something...