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 |
Jack Jansen | eaa1959 | 1996-05-31 13:02:52 +0000 | [diff] [blame] | 9 | import py_resource |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 10 | |
| 11 | error = 'mkpycresourcefile.error' |
| 12 | |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 13 | def mkpycresourcefile(src, dst): |
| 14 | """Copy pyc file/dir src to resource file dst.""" |
| 15 | |
| 16 | if not os.path.isdir(src) and src[-4:] <> '.pyc': |
| 17 | raise error, 'I can only handle .pyc files or directories' |
Jack Jansen | eaa1959 | 1996-05-31 13:02:52 +0000 | [diff] [blame] | 18 | fsid = py_resource.create(dst) |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 19 | if os.path.isdir(src): |
Jack Jansen | eaa1959 | 1996-05-31 13:02:52 +0000 | [diff] [blame] | 20 | handlesubdir(src) |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 21 | else: |
Jack Jansen | eaa1959 | 1996-05-31 13:02:52 +0000 | [diff] [blame] | 22 | id, name = py_resource.frompycfile(src) |
| 23 | print 'Wrote %d: %s %s'%(id, name, src) |
| 24 | CloseResFile(fsid) |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 25 | |
Jack Jansen | eaa1959 | 1996-05-31 13:02:52 +0000 | [diff] [blame] | 26 | def handlesubdir(srcdir): |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 27 | """Recursively scan a directory for pyc files and copy to resources""" |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 28 | src = os.listdir(srcdir) |
| 29 | for file in src: |
| 30 | file = os.path.join(srcdir, file) |
| 31 | if os.path.isdir(file): |
Jack Jansen | eaa1959 | 1996-05-31 13:02:52 +0000 | [diff] [blame] | 32 | handlesubdir(file) |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 33 | elif file[-4:] == '.pyc': |
Jack Jansen | eaa1959 | 1996-05-31 13:02:52 +0000 | [diff] [blame] | 34 | id, name = py_resource.frompycfile(file) |
| 35 | print 'Wrote %d: %s %s'%(id, name, file) |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 36 | |
| 37 | if __name__ == '__main__': |
| 38 | args = sys.argv[1:] |
| 39 | if not args: |
Jack Jansen | 9062fa2 | 1995-08-14 12:21:12 +0000 | [diff] [blame] | 40 | ifss, ok = macfs.GetDirectory('Select root of tree to pack:') |
| 41 | if not ok: |
| 42 | sys.exit(0) |
| 43 | args = [ifss.as_pathname()] |
Jack Jansen | 7571f30 | 1995-07-29 13:48:41 +0000 | [diff] [blame] | 44 | for ifn in args: |
| 45 | ofss, ok = macfs.StandardPutFile('Output for '+os.path.split(ifn)[1]) |
| 46 | if not ok: |
| 47 | sys.exit(0) |
| 48 | mkpycresourcefile(ifn, ofss.as_pathname()) |
| 49 | sys.exit(1) # So we can see something... |