2 # Turn a pyc file into a resource file containing it in 'PYC ' resource form
4 addpack
.addpack('Tools')
5 addpack
.addpack('bgen')
9 from Resources
import *
18 error
= 'mkpycresourcefile.error'
22 raise ValueError, 'String too large'
23 return chr(len(str))+str
25 def createoutput(dst
):
26 """Create output file. Return handle and first id to use."""
29 FSpCreateResFile(dst
, 'PYTH', 'rsrc', smAllScripts
)
30 output
= FSpOpenResFile(dst
, WRITE
)
35 def writemodule(name
, id, data
):
36 """Write pyc code to a PYC resource with given name and id."""
37 # XXXX Check that it doesn't exist
39 res
.AddResource('PYC ', id, name
)
43 def mkpycresourcefile(src
, dst
):
44 """Copy pyc file/dir src to resource file dst."""
46 if not os
.path
.isdir(src
) and src
[-4:] <> '.pyc':
47 raise error
, 'I can only handle .pyc files or directories'
48 handle
, oid
= createoutput(dst
)
49 if os
.path
.isdir(src
):
50 id = handlesubdir(handle
, oid
, src
)
52 id = handleonepycfile(handle
, oid
, src
)
53 print 'Wrote',id-oid
,'PYC resources to', dst
56 def handleonepycfile(handle
, id, file):
57 """Copy one pyc file to the open resource file"""
58 d
, name
= os
.path
.split(file)
61 writemodule(name
, id, open(file, 'rb').read())
64 def handlesubdir(handle
, id, srcdir
):
65 """Recursively scan a directory for pyc files and copy to resources"""
66 print 'Directory', srcdir
67 src
= os
.listdir(srcdir
)
69 file = os
.path
.join(srcdir
, file)
70 if os
.path
.isdir(file):
71 id = handlesubdir(handle
, id, file)
72 elif file[-4:] == '.pyc':
73 id = handleonepycfile(handle
, id, file)
77 if __name__
== '__main__':
80 ifss
, ok
= macfs
.GetDirectory('Select root of tree to pack:')
83 args
= [ifss
.as_pathname()]
85 ofss
, ok
= macfs
.StandardPutFile('Output for '+os
.path
.split(ifn
)[1])
88 mkpycresourcefile(ifn
, ofss
.as_pathname())
89 sys
.exit(1) # So we can see something...