|
6 | 6 | import os |
7 | 7 | import macfs |
8 | 8 | import sys |
9 | | - |
10 | | -READ = 1 |
11 | | -WRITE = 2 |
12 | | -smAllScripts = -3 |
| 9 | +import py_resource |
13 | 10 |
|
14 | 11 | error = 'mkpycresourcefile.error' |
15 | 12 |
|
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 | | - |
25 | | - FSpCreateResFile(dst, 'Pyth', 'rsrc', smAllScripts) |
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 | 13 | def mkpycresourcefile(src, dst): |
40 | 14 | """Copy pyc file/dir src to resource file dst.""" |
41 | 15 |
|
42 | 16 | if not os.path.isdir(src) and src[-4:] <> '.pyc': |
43 | 17 | raise error, 'I can only handle .pyc files or directories' |
44 | | - handle, oid = createoutput(dst) |
| 18 | + fsid = py_resource.create(dst) |
45 | 19 | if os.path.isdir(src): |
46 | | - id = handlesubdir(handle, oid, src) |
| 20 | + handlesubdir(src) |
47 | 21 | else: |
48 | | - id = handleonepycfile(handle, oid, src) |
49 | | - print 'Wrote',id-oid,'PYC resources to', dst |
50 | | - CloseResFile(handle) |
| 22 | + id, name = py_resource.frompycfile(src) |
| 23 | + print 'Wrote %d: %s %s'%(id, name, src) |
| 24 | + CloseResFile(fsid) |
51 | 25 |
|
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): |
| 26 | +def handlesubdir(srcdir): |
61 | 27 | """Recursively scan a directory for pyc files and copy to resources""" |
62 | | - print 'Directory', srcdir |
63 | 28 | src = os.listdir(srcdir) |
64 | 29 | for file in src: |
65 | 30 | file = os.path.join(srcdir, file) |
66 | 31 | if os.path.isdir(file): |
67 | | - id = handlesubdir(handle, id, file) |
| 32 | + handlesubdir(file) |
68 | 33 | elif file[-4:] == '.pyc': |
69 | | - id = handleonepycfile(handle, id, file) |
70 | | - return id |
71 | | - |
| 34 | + id, name = py_resource.frompycfile(file) |
| 35 | + print 'Wrote %d: %s %s'%(id, name, file) |
72 | 36 |
|
73 | 37 | if __name__ == '__main__': |
74 | 38 | args = sys.argv[1:] |
|
0 commit comments