Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 1 | # |
| 2 | # Turn a pyc file into a resource file containing it in 'PYC ' resource form |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 3 | from Res import * |
| 4 | import Res |
| 5 | from Resources import * |
| 6 | import os |
| 7 | import macfs |
| 8 | import sys |
| 9 | |
| 10 | READ = 1 |
| 11 | WRITE = 2 |
| 12 | smAllScripts = -3 |
| 13 | |
| 14 | error = 'mkpycresourcefile.error' |
| 15 | |
| 16 | def Pstring(str): |
| 17 | if len(str) > 255: |
| 18 | raise ValueError, 'String too large' |
| 19 | return chr(len(str))+str |
| 20 | |
| 21 | def createoutput(dst): |
| 22 | """Create output file. Return handle and first id to use.""" |
| 23 | |
| 24 | |
Jack Jansen | 532e3c2 | 1996-02-21 15:36:26 +0000 | [diff] [blame] | 25 | FSpCreateResFile(dst, 'Pyth', 'rsrc', smAllScripts) |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 26 | output = FSpOpenResFile(dst, WRITE) |
| 27 | UseResFile(output) |
| 28 | num = 128 |
| 29 | return output, num |
| 30 | |
| 31 | def 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 | |
| 39 | def 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 | |
| 52 | def 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 | |
| 60 | def 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 | |
| 73 | if __name__ == '__main__': |
| 74 | args = sys.argv[1:] |
| 75 | if not args: |
Jack Jansen | 9062fa2 | 1995-08-14 12:21:12 +0000 | [diff] [blame] | 76 | ifss, ok = macfs.GetDirectory('Select root of tree to pack:') |
| 77 | if not ok: |
| 78 | sys.exit(0) |
| 79 | args = [ifss.as_pathname()] |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 80 | 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... |