mirror of
https://github.com/python/cpython.git
synced 2025-08-02 16:13:13 +00:00
Support for freezing packages (Just).
This commit is contained in:
parent
201f46de2c
commit
b93f52158b
4 changed files with 58 additions and 12 deletions
|
@ -28,9 +28,18 @@ def open(dst):
|
||||||
Res.UseResFile(output)
|
Res.UseResFile(output)
|
||||||
return 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."""
|
"""Write pyc code to a PYC resource with given name and id."""
|
||||||
# XXXX Check that it doesn't exist
|
# 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 = Res.Resource(data)
|
||||||
res.AddResource(type, id, name)
|
res.AddResource(type, id, name)
|
||||||
if preload:
|
if preload:
|
||||||
|
@ -40,22 +49,23 @@ def writemodule(name, id, data, type='PYC ', preload=0):
|
||||||
res.WriteResource()
|
res.WriteResource()
|
||||||
res.ReleaseResource()
|
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"""
|
"""Copy one pyc file to the open resource file"""
|
||||||
if name == None:
|
if name == None:
|
||||||
d, name = os.path.split(file)
|
d, name = os.path.split(file)
|
||||||
name = name[:-4]
|
name = name[:-4]
|
||||||
id = findfreeid()
|
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
|
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"""
|
"""Compile python source file to pyc file and add to resource file"""
|
||||||
import py_compile
|
import py_compile
|
||||||
|
|
||||||
py_compile.compile(file)
|
py_compile.compile(file)
|
||||||
file = file +'c'
|
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....
|
# XXXX Note this is incorrect, it only handles one type and one file....
|
||||||
|
|
||||||
|
|
|
@ -332,14 +332,45 @@ char *filename;
|
||||||
co = NULL;
|
co = NULL;
|
||||||
} else {
|
} else {
|
||||||
co = PyMarshal_ReadObjectFromString((*h)+8, size-8);
|
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);
|
HUnlock(h);
|
||||||
if ( filerh != -1 )
|
if ( filerh != -1 )
|
||||||
CloseResFile(filerh);
|
CloseResFile(filerh);
|
||||||
UseResFile(oldrh);
|
UseResFile(oldrh);
|
||||||
if ( co ) {
|
if ( co ) {
|
||||||
m = PyImport_ExecCodeModule(module, co);
|
m = PyImport_ExecCodeModuleEx(module, co, "<pyc resource>");
|
||||||
Py_DECREF(co);
|
Py_DECREF(co);
|
||||||
} else {
|
} else {
|
||||||
m = NULL;
|
m = NULL;
|
||||||
|
|
|
@ -128,10 +128,12 @@ def getfragname(path, dynamicfiles):
|
||||||
|
|
||||||
|
|
||||||
def addpythonmodules(module_dict):
|
def addpythonmodules(module_dict):
|
||||||
|
# XXX should really use macgen_rsrc.generate(), this does the same, but skips __main__
|
||||||
items = module_dict.items()
|
items = module_dict.items()
|
||||||
items.sort()
|
items.sort()
|
||||||
for name, module in items:
|
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
|
continue
|
||||||
location = module.__file__
|
location = module.__file__
|
||||||
|
|
||||||
|
@ -143,7 +145,8 @@ def addpythonmodules(module_dict):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
print 'Adding module ³%s²' % name
|
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):
|
def Pstring(str):
|
||||||
if len(str) > 255:
|
if len(str) > 255:
|
||||||
|
|
|
@ -6,9 +6,10 @@ import sys
|
||||||
|
|
||||||
def generate(output, module_dict, debug=0, preload=1):
|
def generate(output, module_dict, debug=0, preload=1):
|
||||||
fsid = py_resource.create(output)
|
fsid = py_resource.create(output)
|
||||||
|
|
||||||
for name, module in module_dict.items():
|
for name, module in module_dict.items():
|
||||||
if module.gettype() != 'module':
|
mtype = module.gettype()
|
||||||
|
if mtype not in ['module', 'package']:
|
||||||
continue
|
continue
|
||||||
location = module.__file__
|
location = module.__file__
|
||||||
|
|
||||||
|
@ -19,10 +20,11 @@ def generate(output, module_dict, debug=0, preload=1):
|
||||||
print '*** skipping', location
|
print '*** skipping', location
|
||||||
continue
|
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:
|
if debug > 0:
|
||||||
print 'PYC resource %5d\t%s\t%s'%(id, name, location)
|
print 'PYC resource %5d\t%s\t%s'%(id, name, location)
|
||||||
|
|
||||||
Res.CloseResFile(fsid)
|
Res.CloseResFile(fsid)
|
||||||
|
|
||||||
def warnings(module_dict):
|
def warnings(module_dict):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue