Support for freezing packages (Just).

This commit is contained in:
Jack Jansen 1998-08-18 12:23:11 +00:00
parent 201f46de2c
commit b93f52158b
4 changed files with 58 additions and 12 deletions

View File

@ -28,9 +28,18 @@ def open(dst):
Res.UseResFile(output)
return output
def writemodule(name, id, data, type='PYC ', preload=0):
def writemodule(name, id, data, type='PYC ', preload=0, ispackage=0):
"""Write pyc code to a PYC resource with given name and id."""
# XXXX Check that it doesn't exist
# Normally, byte 4-7 are the time stamp, but that is not used
# for 'PYC ' resources. We abuse byte 4 as a flag to indicate
# that it is a package rather than an ordinary module.
# See also macimport.c. (jvr)
if ispackage:
data = data[:4] + '\377\0\0\0' + data[8:] # flag resource as package
else:
data = data[:4] + '\0\0\0\0' + data[8:] # clear mod date field, used as package flag
res = Res.Resource(data)
res.AddResource(type, id, name)
if preload:
@ -40,22 +49,23 @@ def writemodule(name, id, data, type='PYC ', preload=0):
res.WriteResource()
res.ReleaseResource()
def frompycfile(file, name=None, preload=0):
def frompycfile(file, name=None, preload=0, ispackage=0):
"""Copy one pyc file to the open resource file"""
if name == None:
d, name = os.path.split(file)
name = name[:-4]
id = findfreeid()
writemodule(name, id, __builtin__.open(file, 'rb').read(), preload=preload)
data = __builtin__.open(file, 'rb').read()
writemodule(name, id, data, preload=preload, ispackage=ispackage)
return id, name
def frompyfile(file, name=None, preload=0):
def frompyfile(file, name=None, preload=0, ispackage=0):
"""Compile python source file to pyc file and add to resource file"""
import py_compile
py_compile.compile(file)
file = file +'c'
return frompycfile(file, name, preload=preload)
return frompycfile(file, name, preload=preload, ispackage=ispackage)
# XXXX Note this is incorrect, it only handles one type and one file....

View File

@ -332,14 +332,45 @@ char *filename;
co = NULL;
} else {
co = PyMarshal_ReadObjectFromString((*h)+8, size-8);
/*
** Normally, byte 4-7 are the time stamp, but that is not used
** for 'PYC ' resources. We abuse byte 4 as a flag to indicate
** that it is a package rather than an ordinary module.
** See also py_resource.py. (jvr)
*/
if ((*h)[4] & 0xff) {
/* it's a package */
/* Set __path__ to the package name */
PyObject *d, *s;
int err;
m = PyImport_AddModule(module);
if (m == NULL) {
co = NULL;
goto packageerror;
}
d = PyModule_GetDict(m);
s = PyString_InternFromString(module);
if (s == NULL) {
co = NULL;
goto packageerror;
}
err = PyDict_SetItemString(d, "__path__", s);
Py_DECREF(s);
if (err != 0) {
co = NULL;
goto packageerror;
}
}
}
}
packageerror:
HUnlock(h);
if ( filerh != -1 )
CloseResFile(filerh);
UseResFile(oldrh);
if ( co ) {
m = PyImport_ExecCodeModule(module, co);
m = PyImport_ExecCodeModuleEx(module, co, "<pyc resource>");
Py_DECREF(co);
} else {
m = NULL;

View File

@ -128,10 +128,12 @@ def getfragname(path, dynamicfiles):
def addpythonmodules(module_dict):
# XXX should really use macgen_rsrc.generate(), this does the same, but skips __main__
items = module_dict.items()
items.sort()
for name, module in items:
if module.gettype() != 'module' or name == "__main__":
mtype = module.gettype()
if mtype not in ['module', 'package'] or name == "__main__":
continue
location = module.__file__
@ -143,7 +145,8 @@ def addpythonmodules(module_dict):
continue
print 'Adding module ³%s²' % name
id, name = py_resource.frompyfile(location, name, preload=0)
id, name = py_resource.frompyfile(location, name, preload=0,
ispackage=mtype=='package')
def Pstring(str):
if len(str) > 255:

View File

@ -8,7 +8,8 @@ def generate(output, module_dict, debug=0, preload=1):
fsid = py_resource.create(output)
for name, module in module_dict.items():
if module.gettype() != 'module':
mtype = module.gettype()
if mtype not in ['module', 'package']:
continue
location = module.__file__
@ -19,7 +20,8 @@ def generate(output, module_dict, debug=0, preload=1):
print '*** skipping', location
continue
id, name = py_resource.frompyfile(location, name, preload=preload)
id, name = py_resource.frompyfile(location, name, preload=preload,
ispackage=mtype=='package')
if debug > 0:
print 'PYC resource %5d\t%s\t%s'%(id, name, location)