Allow specifying contents of .py modules with file(text=...) in pdef

This commit is contained in:
rdb 2015-08-10 01:57:00 +02:00
parent 12af94707f
commit 2abc51d88f
2 changed files with 19 additions and 6 deletions

View File

@ -1841,7 +1841,10 @@ class Packager:
self.notify.warning(message) self.notify.warning(message)
return return
self.freezer.addModule(moduleName, filename = file.filename) if file.text:
self.freezer.addModule(moduleName, filename = file.filename, text = file.text)
else:
self.freezer.addModule(moduleName, filename = file.filename)
def addEggFile(self, file): def addEggFile(self, file):
# Precompile egg files to bam's. # Precompile egg files to bam's.

View File

@ -8,6 +8,7 @@ import marshal
import imp import imp
import platform import platform
import types import types
from StringIO import StringIO
from distutils.sysconfig import PREFIX, get_python_inc, get_python_version from distutils.sysconfig import PREFIX, get_python_inc, get_python_version
# Temporary (?) try..except to protect against unbuilt p3extend_frozen. # Temporary (?) try..except to protect against unbuilt p3extend_frozen.
@ -499,7 +500,8 @@ class Freezer:
def __init__(self, moduleName, filename = None, def __init__(self, moduleName, filename = None,
implicit = False, guess = False, implicit = False, guess = False,
exclude = False, forbid = False, exclude = False, forbid = False,
allowChildren = False, fromSource = None): allowChildren = False, fromSource = None,
text = None):
# The Python module name. # The Python module name.
self.moduleName = moduleName self.moduleName = moduleName
@ -534,6 +536,9 @@ class Freezer:
# record came from, supplied by the caller. # record came from, supplied by the caller.
self.fromSource = fromSource self.fromSource = fromSource
# If this is set, it contains Python code of the module.
self.text = text
# Some sanity checks. # Some sanity checks.
if not self.exclude: if not self.exclude:
self.allowChildren = True self.allowChildren = True
@ -750,7 +755,8 @@ class Freezer:
return modules return modules
def addModule(self, moduleName, implicit = False, newName = None, def addModule(self, moduleName, implicit = False, newName = None,
filename = None, guess = False, fromSource = None): filename = None, guess = False, fromSource = None,
text = None):
""" Adds a module to the list of modules to be exported by """ Adds a module to the list of modules to be exported by
this tool. If implicit is true, it is OK if the module does this tool. If implicit is true, it is OK if the module does
not actually exist. not actually exist.
@ -806,7 +812,7 @@ class Freezer:
# It's actually a regular module. # It's actually a regular module.
self.modules[newParentName] = self.ModuleDef( self.modules[newParentName] = self.ModuleDef(
parentName, implicit = implicit, guess = guess, parentName, implicit = implicit, guess = guess,
fromSource = fromSource) fromSource = fromSource, text = text)
else: else:
# Now get all the py files in the parent directory. # Now get all the py files in the parent directory.
@ -821,7 +827,7 @@ class Freezer:
# A normal, explicit module name. # A normal, explicit module name.
self.modules[newName] = self.ModuleDef( self.modules[newName] = self.ModuleDef(
moduleName, filename = filename, implicit = implicit, moduleName, filename = filename, implicit = implicit,
guess = guess, fromSource = fromSource) guess = guess, fromSource = fromSource, text = text)
def done(self, compileToExe = False): def done(self, compileToExe = False):
""" Call this method after you have added all modules with """ Call this method after you have added all modules with
@ -972,7 +978,11 @@ class Freezer:
stuff = ("", "rb", imp.PY_COMPILED) stuff = ("", "rb", imp.PY_COMPILED)
self.mf.load_module(mdef.moduleName, fp, pathname, stuff) self.mf.load_module(mdef.moduleName, fp, pathname, stuff)
else: else:
fp = open(pathname, modulefinder.READ_MODE) fp = open(pathname, 'U')
if mdef.text:
fp = StringIO(mdef.text)
else:
fp = open(pathname, modulefinder.READ_MODE)
stuff = ("", "r", imp.PY_SOURCE) stuff = ("", "r", imp.PY_SOURCE)
self.mf.load_module(mdef.moduleName, fp, pathname, stuff) self.mf.load_module(mdef.moduleName, fp, pathname, stuff)