1 """Creation of PYC resources"""
11 """Return a pascal-style string from a python-style string"""
13 raise ValueError, 'String too large'
14 return chr(len(str))+str
16 def create(dst
, creator
='Pyth'):
17 """Create output file. Return handle and first id to use."""
23 Res
.FSpCreateResFile(dst
, creator
, 'rsrc', smAllScripts
)
27 output
= Res
.FSpOpenResFile(dst
, WRITE
)
28 Res
.UseResFile(output
)
31 def writemodule(name
, id, data
, type='PYC ', preload
=0, ispackage
=0):
32 """Write pyc code to a PYC resource with given name and id."""
33 # XXXX Check that it doesn't exist
35 # Normally, byte 4-7 are the time stamp, but that is not used
36 # for 'PYC ' resources. We abuse byte 4 as a flag to indicate
37 # that it is a package rather than an ordinary module.
38 # See also macimport.c. (jvr)
40 data
= data
[:4] + '\377\0\0\0' + data
[8:] # flag resource as package
42 data
= data
[:4] + '\0\0\0\0' + data
[8:] # clear mod date field, used as package flag
43 res
= Res
.Resource(data
)
44 res
.AddResource(type, id, name
)
46 attrs
= res
.GetResAttrs()
48 res
.SetResAttrs(attrs
)
52 def frompycfile(file, name
=None, preload
=0, ispackage
=0):
53 """Copy one pyc file to the open resource file"""
55 d
, name
= os
.path
.split(file)
58 data
= __builtin__
.open(file, 'rb').read()
59 writemodule(name
, id, data
, preload
=preload
, ispackage
=ispackage
)
62 def frompyfile(file, name
=None, preload
=0, ispackage
=0):
63 """Compile python source file to pyc file and add to resource file"""
66 py_compile
.compile(file)
68 return frompycfile(file, name
, preload
=preload
, ispackage
=ispackage
)
70 # XXXX Note this is incorrect, it only handles one type and one file....
74 def findfreeid(type='PYC '):
75 """Find next free id-number for given resource type"""
78 if _firstfreeid
== None:
81 num
= Res
.Count1Resources(type)
82 for i
in range(1, num
+1):
83 r
= Res
.Get1IndResource(type, i
)
84 id, d1
, d2
= r
.GetResInfo()
85 highest
= max(highest
, id)
87 _firstfreeid
= highest
+1
89 _firstfreeid
= _firstfreeid
+1