Ejemplo n.º 1
0
    def _addSlaveMainModule(root_module):
        from nuitka.tree.Building import CompiledPythonModule, readSourceCodeFromFilename, createModuleTree
        from nuitka.ModuleRegistry import addRootModule
        from nuitka.plugins.Plugins import Plugins
        # First, build the module node and then read again from the
        # source code.
        module_name = "__parents_main__"
        source_ref = root_module.getSourceReference()

        mode = Plugins.decideCompilation(module_name, source_ref)

        slave_main_module = CompiledPythonModule(
            name=module_name,
            package_name=None,
            mode=mode,
            source_ref=root_module.getSourceReference())

        source_code = readSourceCodeFromFilename("__parents_main__",
                                                 root_module.getFilename())

        # For the call stack, this may look bad or different to what
        # CPython does. Using the "__import__" built-in to not spoil
        # or use the module namespace.
        source_code += """
__import__("sys").modules["__main__"] = __import__("sys").modules[__name__]
__import__("multiprocessing.forking").forking.main()"""

        createModuleTree(module=slave_main_module,
                         source_ref=root_module.getSourceReference(),
                         source_code=source_code,
                         is_main=False)

        # This is an alternative entry point of course.
        addRootModule(slave_main_module)
Ejemplo n.º 2
0
    def _createTriggerLoadedModule(module, trigger_name, code):
        from nuitka.tree.Building import createModuleTree
        from nuitka.nodes.ModuleNodes import CompiledPythonModule
        from .Plugins import Plugins

        module_name = module.getName() + trigger_name
        source_ref = fromFilename(module.getCompileTimeFilename() +
                                  trigger_name)

        mode = Plugins.decideCompilation(module_name, source_ref)

        trigger_module = CompiledPythonModule(
            name=module_name,
            package_name=module.getPackage(),
            is_top=False,
            mode=mode,
            future_spec=None,
            source_ref=source_ref,
        )

        createModuleTree(
            module=trigger_module,
            source_ref=module.getSourceReference(),
            source_code=code,
            is_main=False,
        )

        return trigger_module
Ejemplo n.º 3
0
    def _createTriggerLoadedModule(module, trigger_name, code):
        from nuitka.tree.Building import createModuleTree
        from nuitka.nodes.ModuleNodes import CompiledPythonModule
        from nuitka.plugins.Plugins import Plugins

        module_name = module.getName() + trigger_name
        source_ref = fromFilename(module.getCompileTimeFilename() + trigger_name)

        mode = Plugins.decideCompilation(module_name, source_ref)

        trigger_module = CompiledPythonModule(
            name         = module_name,
            package_name = module.getPackage(),
            mode         = mode,
            source_ref   = source_ref
        )

        createModuleTree(
            module      = trigger_module,
            source_ref  = module.getSourceReference(),
            source_code = code,
            is_main     = False
        )

        return trigger_module
Ejemplo n.º 4
0
    def onModuleDiscovered(self, module):
        post_code, reason = self.createPostModuleLoadCode(module)

        if post_code:
            info(
                "Injecting plug-in based post load code for module '%s':" % \
                    module.getFullName()
            )
            for line in reason.split('\n'):
                info("    " + line)


            from nuitka.tree.Building import createModuleTree

            post_module = PythonModule(
                name         = module.getName() + "-onLoad",
                package_name = module.getPackage(),
                source_ref   = fromFilename(module.getCompileTimeFilename() + "-onLoad")
            )

            createModuleTree(
                module      = post_module,
                source_ref  = module.getSourceReference(),
                source_code = post_code,
                is_main     = False
            )

            post_modules[module.getFullName()] = post_module
Ejemplo n.º 5
0
    def _createTriggerLoadedModule(module, trigger_name, code, flags):
        """Create a "trigger" for a module to be imported.

        Notes:
            The trigger will incorpaorate the code to be prepended / appended.
            Called by @onModuleDiscovered.

        Args:
            module: the module object (serves as dict key)
            trigger_name: string ("-preload"/"-postload")
            code: the code string

        Returns
            trigger_module
        """
        from nuitka.nodes.ModuleNodes import CompiledPythonModule
        from nuitka.tree.Building import createModuleTree

        from .Plugins import Plugins

        module_name = ModuleName(module.getFullName() + trigger_name)
        source_ref = fromFilename(module.getCompileTimeFilename() +
                                  trigger_name)

        mode = Plugins.decideCompilation(module_name, source_ref)

        trigger_module = CompiledPythonModule(
            module_name=module_name,
            is_top=False,
            mode=mode,
            future_spec=None,
            source_ref=source_ref,
        )

        createModuleTree(
            module=trigger_module,
            source_ref=module.getSourceReference(),
            source_code=code,
            is_main=False,
        )

        if mode == "bytecode":
            trigger_module.setSourceCode(code)

        # In debug mode, put the files in the build folder, so they can be looked up easily.
        if Options.is_debug and "HIDE_SOURCE" not in flags:
            source_path = os.path.join(
                OutputDirectories.getSourceDirectoryPath(),
                module_name + ".py")

            putTextFileContents(filename=source_path, contents=code)

        return trigger_module
    def onModuleInitialSet(self):
        from nuitka.importing.ImportCache import addImportedModule
        from nuitka.ModuleRegistry import getRootTopModule
        from nuitka.plugins.Plugins import Plugins
        from nuitka.tree.Building import (
            CompiledPythonModule,
            createModuleTree,
            readSourceCodeFromFilename,
        )

        # First, build the module node and then read again from the
        # source code.
        root_module = getRootTopModule()

        module_name = ModuleName("__parents_main__")
        source_ref = root_module.getSourceReference()

        mode = Plugins.decideCompilation(module_name, source_ref)

        slave_main_module = CompiledPythonModule(
            module_name=module_name,
            is_top=False,
            mode=mode,
            future_spec=None,
            source_ref=root_module.getSourceReference(),
        )

        source_code = readSourceCodeFromFilename(module_name,
                                                 root_module.getFilename())

        # For the call stack, this may look bad or different to what
        # CPython does. Using the "__import__" built-in to not spoil
        # or use the module namespace. The forking module was split up
        # into multiple modules in Python 3.4.
        if python_version >= 0x340:
            source_code += """
__import__("sys").modules["__main__"] = __import__("sys").modules[__name__]
__import__("multiprocessing.spawn").spawn.freeze_support()"""
        else:
            source_code += """
__import__("sys").modules["__main__"] = __import__("sys").modules[__name__]
__import__("multiprocessing.forking").forking.freeze_support()"""

        createModuleTree(
            module=slave_main_module,
            source_ref=root_module.getSourceReference(),
            source_code=source_code,
            is_main=False,
        )

        addImportedModule(imported_module=slave_main_module)

        yield slave_main_module
Ejemplo n.º 7
0
    def _createTriggerLoadedModule(module, trigger_name, code):
        """Create a "trigger" for a module to be imported.

        Notes:
            The trigger will incorpaorate the code to be prepended / appended.
            Called by @onModuleDiscovered.

        Args:
            module: the module object (serves as dict key)
            trigger_name: string ("-preload"/"-postload")
            code: the code string

        Returns
            trigger_module
        """
        from nuitka.nodes.ModuleNodes import CompiledPythonModule
        from nuitka.tree.Building import createModuleTree

        from .Plugins import Plugins

        module_name = ModuleName(module.getFullName() + trigger_name)
        source_ref = fromFilename(module.getCompileTimeFilename() +
                                  trigger_name)

        mode = Plugins.decideCompilation(module_name, source_ref)

        trigger_module = CompiledPythonModule(
            module_name=module_name,
            is_top=False,
            mode=mode,
            future_spec=None,
            source_ref=source_ref,
        )

        createModuleTree(
            module=trigger_module,
            source_ref=module.getSourceReference(),
            source_code=code,
            is_main=False,
        )

        if mode == "bytecode":
            trigger_module.setSourceCode(code)

        if Options.isDebug():
            source_path = os.path.join(
                OutputDirectories.getSourceDirectoryPath(),
                module_name + ".py")

            with open(source_path, "w") as output:
                output.write(code)

        return trigger_module
Ejemplo n.º 8
0
    def _addSlaveMainModule(root_module):
        from nuitka.tree.Building import (
            CompiledPythonModule,
            readSourceCodeFromFilename,
            createModuleTree,
        )
        from nuitka.ModuleRegistry import addRootModule
        from nuitka.plugins.Plugins import Plugins
        from sys import hexversion

        # First, build the module node and then read again from the
        # source code.
        module_name = "__parents_main__"
        source_ref = root_module.getSourceReference()

        mode = Plugins.decideCompilation(module_name, source_ref)

        slave_main_module = CompiledPythonModule(
            name=module_name,
            package_name=None,
            is_top=False,
            mode=mode,
            future_spec=None,
            source_ref=root_module.getSourceReference(),
        )

        source_code = readSourceCodeFromFilename(
            "__parents_main__", root_module.getFilename()
        )

        # For the call stack, this may look bad or different to what
        # CPython does. Using the "__import__" built-in to not spoil
        # or use the module namespace. The forking module was split up
        # into multiple modules in Python 3.4.0.a2
        if hexversion >= 0x030400A2:
            source_code += """
__import__("sys").modules["__main__"] = __import__("sys").modules[__name__]
__import__("multiprocessing.spawn").spawn.freeze_support()"""
        else:
            source_code += """
__import__("sys").modules["__main__"] = __import__("sys").modules[__name__]
__import__("multiprocessing.forking").forking.freeze_support()"""

        createModuleTree(
            module=slave_main_module,
            source_ref=root_module.getSourceReference(),
            source_code=source_code,
            is_main=False,
        )

        # This is an alternative entry point of course.
        addRootModule(slave_main_module)
Ejemplo n.º 9
0
    def _addSlaveMainModule(root_module):
        from nuitka.tree.Building import (
            CompiledPythonModule,
            readSourceCodeFromFilename,
            createModuleTree,
        )
        from nuitka.ModuleRegistry import addRootModule
        from nuitka.plugins.Plugins import Plugins
        from sys import hexversion

        # First, build the module node and then read again from the
        # source code.
        module_name = "__parents_main__"
        source_ref = root_module.getSourceReference()

        mode = Plugins.decideCompilation(module_name, source_ref)

        slave_main_module = CompiledPythonModule(
            name=module_name,
            package_name=None,
            is_top=False,
            mode=mode,
            future_spec=None,
            source_ref=root_module.getSourceReference(),
        )

        source_code = readSourceCodeFromFilename("__parents_main__",
                                                 root_module.getFilename())

        # For the call stack, this may look bad or different to what
        # CPython does. Using the "__import__" built-in to not spoil
        # or use the module namespace. The forking module was split up
        # into multiple modules in Python 3.4.0.a2
        if hexversion >= 0x030400A2:
            source_code += """
__import__("sys").modules["__main__"] = __import__("sys").modules[__name__]
__import__("multiprocessing.spawn").spawn.freeze_support()"""
        else:
            source_code += """
__import__("sys").modules["__main__"] = __import__("sys").modules[__name__]
__import__("multiprocessing.forking").forking.freeze_support()"""

        createModuleTree(
            module=slave_main_module,
            source_ref=root_module.getSourceReference(),
            source_code=source_code,
            is_main=False,
        )

        # This is an alternative entry point of course.
        addRootModule(slave_main_module)
Ejemplo n.º 10
0
    def _createTriggerLoadedModule(module, trigger_name, code):
        from nuitka.tree.Building import createModuleTree
        from nuitka.nodes.ModuleNodes import CompiledPythonModule

        trigger_module = CompiledPythonModule(
            name=module.getName() + trigger_name,
            package_name=module.getPackage(),
            source_ref=fromFilename(module.getCompileTimeFilename() +
                                    trigger_name))

        createModuleTree(module=trigger_module,
                         source_ref=module.getSourceReference(),
                         source_code=code,
                         is_main=False)

        return trigger_module
Ejemplo n.º 11
0
    def onModuleEncounter(self, module_filename, module_name, module_package,
                          module_kind):
        if module_name == "multiprocessing" and \
           module_package is None \
           and not self.multiprocessing_added:
            self.multiprocessing_added = True

            from nuitka.ModuleRegistry import getRootModules, addRootModule
            from nuitka.tree.Building import CompiledPythonModule, readSourceCodeFromFilename, createModuleTree

            for root_module in getRootModules():
                if root_module.isMainModule():
                    # First, build the module node and then read again from the
                    # source code.

                    slave_main_module = CompiledPythonModule(
                        name         = "__parents_main__",
                        package_name = None,
                        source_ref   = root_module.getSourceReference()
                    )

                    source_code = readSourceCodeFromFilename(
                        "__parents_main__",
                        root_module.getFilename()
                    )

                    # For the call stack, this may look bad or different to what
                    # CPython does. Using the "__import__" built-in to not spoil
                    # or use the module namespace.
                    source_code += """
__import__("sys").modules["__main__"] = __import__("sys").modules[__name__]
__import__("multiprocessing.forking").forking.main()"""

                    createModuleTree(
                        module      = slave_main_module,
                        source_ref  = root_module.getSourceReference(),
                        source_code = source_code,
                        is_main     = False
                    )

                    # This is an alternative entry point of course.
                    addRootModule(slave_main_module)

                    break
            else:
                assert False
Ejemplo n.º 12
0
    def onModuleDiscovered(self, module):
        post_code = self.createPostModuleLoadCode(module)

        if post_code:
            from nuitka.tree.Building import createModuleTree

            post_module = PythonModule(
                name=module.getName() + "-onLoad",
                package_name=module.getPackage(),
                source_ref=fromFilename(module.getName() + "-onLoad"))

            createModuleTree(module=post_module,
                             source_ref=module.getSourceReference(),
                             source_code=post_code,
                             is_main=False)

            post_modules[module.getFullName()] = post_module
Ejemplo n.º 13
0
    def _createTriggerLoadedModule(module, trigger_name, code):
        """ Create a "trigger" for a module to be imported.

        Notes:
            The trigger will incorpaorate the code to be prepended / appended.
            Called by @onModuleDiscovered.

        Args:
            module: the module object (serves as dict key)
            trigger_name: string ("-preload"/"-postload")
            code: the code string
        Returns
            trigger_module
        """
        from nuitka.tree.Building import createModuleTree
        from nuitka.nodes.ModuleNodes import CompiledPythonModule
        from .Plugins import Plugins

        module_name = module.getName() + trigger_name
        source_ref = fromFilename(module.getCompileTimeFilename() +
                                  trigger_name)

        mode = Plugins.decideCompilation(module_name, source_ref)

        trigger_module = CompiledPythonModule(
            name=module_name,
            package_name=module.getPackage(),
            is_top=False,
            mode=mode,
            future_spec=None,
            source_ref=source_ref,
        )

        createModuleTree(
            module=trigger_module,
            source_ref=module.getSourceReference(),
            source_code=code,
            is_main=False,
        )

        if mode == "bytecode":
            trigger_module.setSourceCode(code)

        return trigger_module
Ejemplo n.º 14
0
    def onModuleDiscovered(self, module):
        post_code = self.createPostModuleLoadCode(module)

        if post_code:
            from nuitka.tree.Building import createModuleTree

            post_module = PythonModule(
                name         = module.getName() + "-onLoad",
                package_name = module.getPackage(),
                source_ref   = fromFilename(module.getName() + "-onLoad")
            )

            createModuleTree(
                module      = post_module,
                source_ref  = module.getSourceReference(),
                source_code = post_code,
                is_main     = False
            )

            post_modules[module.getFullName()] = post_module
Ejemplo n.º 15
0
    def _addSlaveMainModule(root_module):
        from nuitka.tree.Building import CompiledPythonModule, readSourceCodeFromFilename, createModuleTree
        from nuitka.ModuleRegistry import addRootModule
        from nuitka.plugins.Plugins import Plugins
        # First, build the module node and then read again from the
        # source code.
        module_name = "__parents_main__"
        source_ref = root_module.getSourceReference()

        mode = Plugins.decideCompilation(module_name, source_ref)

        slave_main_module = CompiledPythonModule(
            name         = module_name,
            package_name = None,
            mode         = mode,
            source_ref   = root_module.getSourceReference()
        )

        source_code = readSourceCodeFromFilename(
            "__parents_main__",
            root_module.getFilename()
        )

        # For the call stack, this may look bad or different to what
        # CPython does. Using the "__import__" built-in to not spoil
        # or use the module namespace.
        source_code += """
__import__("sys").modules["__main__"] = __import__("sys").modules[__name__]
__import__("multiprocessing.forking").forking.main()"""

        createModuleTree(
            module      = slave_main_module,
            source_ref  = root_module.getSourceReference(),
            source_code = source_code,
            is_main     = False
        )

        # This is an alternative entry point of course.
        addRootModule(slave_main_module)