def load_Crypto_Util__file_system(finder, module): """pycryptodome package""" # WARNING: do not touch this code string PYCRYPTODOME_CODE_STR = """ import os def pycryptodome_filename(dir_comps, filename): import sys if dir_comps[0] != "Crypto": raise ValueError("Only available for modules under 'Crypto'") dir_comps = list(dir_comps) + [filename] root_lib = os.path.join(os.path.dirname(sys.executable), "lib") return os.path.join(root_lib, ".".join(dir_comps)) """ if not module.WillBeStoredInFileSystem() and module.code is not None: new_code = compile(PYCRYPTODOME_CODE_STR, module.file, "exec") co_func = new_code.co_consts[2] co = module.code constants = list(co.co_consts) for i, value in enumerate(constants): if isinstance(value, type(co)) and value.co_name == co_func.co_name: constants[i] = rebuild_code_object(co_func) break module.code = rebuild_code_object(co, constants=constants)
def load_matplotlib(finder, module): """the matplotlib package requires mpl-data in a subdirectory of the package.""" MATPLOTLIB_CODE_STR = """ def _get_data_path(): return os.path.join(os.path.dirname(sys.executable), '{}') """ import matplotlib data_path = matplotlib.get_data_path() target_path = os.path.join("lib", module.name, "mpl-data") finder.IncludeFiles(data_path, target_path, copyDependentFiles=False) if module.code is not None: code_str = MATPLOTLIB_CODE_STR.format(target_path) new_code = compile(code_str, module.file, "exec") co_func = new_code.co_consts[0] co = module.code constants = list(co.co_consts) for i, value in enumerate(constants): if (isinstance(value, type(co)) and value.co_name == co_func.co_name): constants[i] = rebuild_code_object(co_func) break module.code = rebuild_code_object(co, constants=constants) finder.ExcludeModule("matplotlib.tests") finder.IncludePackage("matplotlib")
def _ReplacePathsInCode(self, topLevelModule, co): """Replace paths in the code as directed, returning a new code object with the modified paths in place.""" # Prepare the new filename. origFileName = newFileName = os.path.normpath(co.co_filename) for searchValue, replaceValue in self.replace_paths: if searchValue == "*": searchValue = os.path.dirname(topLevelModule.file) if topLevelModule.path: searchValue = os.path.dirname(searchValue) if searchValue: searchValue = searchValue + os.path.sep if not origFileName.startswith(searchValue): continue newFileName = replaceValue + origFileName[len(searchValue):] break # Run on subordinate code objects from function & class definitions. constants = list(co.co_consts) for i, value in enumerate(constants): if isinstance(value, type(co)): constants[i] = self._ReplacePathsInCode(topLevelModule, value) return rebuild_code_object(co, constants=constants, filename=newFileName)
def _ReplacePackageInCode(self, module): """Replace the value of __package__ directly in the code, only in zipped modules.""" co = module.code if co is None or module.parent is None or \ module.WillBeStoredInFileSystem() or \ "__package__" in module.global_names: # In some modules, like 'six' the variable is defined, so... return # Only if the code references it. if "__package__" in co.co_names: # Insert a bytecode to represent the code: # __package__ = module.parent.name constants = list(co.co_consts) pkg_const_index = len(constants) pkg_name_index = co.co_names.index("__package__") if pkg_const_index > 255 or pkg_name_index > 255: # Don't touch modules with many constants or names; # This is good for now. return # The bytecode/wordcode codes = [LOAD_CONST, pkg_const_index, STORE_NAME, pkg_name_index] asm_code = bytes(codes) new_code = asm_code + co.co_code constants.append(module.parent.name) code = rebuild_code_object(co, code=new_code, constants=constants) module.code = code