Example #1
0
    def __init__(self, matplotlib, scipy):
        self.matplotlib = matplotlib
        self.scipy = scipy

        self.enabled_plugins = None  # list of active standard plugins
        self.numpy_copied = False  # indicator: numpy files copied
        self.scipy_copied = True  # indicator: scipy files copied
        if self.scipy:
            self.scipy_copied = False

        self.mpl_data_copied = True  # indicator: matplotlib data copied
        if self.matplotlib:
            self.mpl_data_copied = False
            for p in getActivePlugins():
                if p.plugin_name.endswith("hinted-mods.py"):
                    break
            else:
                self.warning(
                    "matplotlib may need hinted compilation for non-standard backends"
                )
    def onModuleEncounter(self, module_filename, module_name, module_kind):
        """ Help decide whether to include a module.

        Notes:
            Performance considerations: the calls array is rather long
            (may be thousands of items). So we store ignored modules
            separately and check that array first.
            We also maintain an array for known implicit imports and early
            check against them, too.

        Args:
            module_filename: path of the module
            module_name: module name
            module_kind: one of "py" or "shlib" (not used here)

        Returns:
            None, (True, 'text') or (False, 'text').
            Example: (False, "because it is not called").
        """
        full_name = module_name
        elements = full_name.split(".")
        package = module_name.getPackageName()
        package_dir = remove_suffix(module_filename, elements[0])

        # fall through for easy cases
        if elements[0] == "pkg_resources":
            return None

        if (full_name in self.ignored_modules
                or elements[0] in self.ignored_modules):  # known to be ignored
            return False, "module is not used"

        if self.accept_test is False and elements[0] in (
                "pytest",
                "_pytest",
                "unittest",
        ):
            self.info(drop_msg(full_name, package))
            self.ignored_modules.add(full_name)
            return False, "suppress testing components"

        if full_name in self.implicit_imports:  # known implicit import
            return True, "module is an implicit import"  # ok

        # check if other plugins would accept this
        for plugin in getActivePlugins():
            if plugin.plugin_name == self.plugin_name:
                continue  # skip myself of course
            rc = plugin.onModuleEncounter(module_filename, module_name,
                                          module_kind)
            if rc is not None:
                if rc[0] is True:  # plugin wants to keep this
                    self.implicit_imports.add(full_name)
                    keep_msg = "keep %s (plugin '%s')" % (full_name,
                                                          plugin.plugin_name)
                    count = self.msg_count.get(plugin.plugin_name, 0)
                    if count < self.msg_limit:
                        self.info(keep_msg)
                    self.msg_count[plugin.plugin_name] = count + 1
                    if count == self.msg_limit:
                        self.info("... 'keep' msg limit exceeded for '%s'." %
                                  plugin.plugin_name)
                    return True, "module is imported"  # ok
                # plugin wants to drop this
                self.ignored_modules.add(full_name)
                ignore_msg = "drop %s (plugin '%s')" % (full_name,
                                                        plugin.plugin_name)
                self.info(ignore_msg)
                return False, "dropped by plugin " + plugin.plugin_name

        if full_name == "cv2":
            return True, "needed by OpenCV"

        if full_name.startswith("pywin"):
            return True, "needed by pywin32"

        checklist = get_checklist(full_name)
        for m in self.import_calls:  # loop thru the called items
            if m in checklist:
                return True, "module is hinted to"  # ok

        if check_dependents(full_name, self.import_files) is True:
            return True, "parent of recursed-to module"

        # next we ask if implicit imports knows our candidate
        if self.implicit_imports_plugin is None:  # the plugin is not yet loaded
            for plugin in getActivePlugins():
                if plugin.plugin_name == "implicit-imports":
                    self.implicit_imports_plugin = plugin
                    break
            if self.implicit_imports_plugin is None:
                sys.exit("could not find 'implicit-imports' plugin")

        # ask the 'implicit-imports' plugin whether it knows this guy
        if package is not None:
            try:
                import_set = self.implicit_imports_plugin.getImportsByFullname(
                    package, package_dir)
            except TypeError:
                sys.exit(
                    "versions of hinted-mods.py and ImplicitImports.py are incompatible"
                )

            import_list0 = [item[0] for item in import_set]  # only the names
            if full_name in import_list0:  # found!
                for item in import_list0:  # store everything in that list
                    self.implicit_imports.add(item)
                return True, "module is an implicit import"  # ok

        # not known by anyone: kick it out!
        self.info(drop_msg(full_name, package))  # issue ignore message
        # faster decision next time
        self.ignored_modules.add(full_name)
        return False, "module is not used"
from logging import info
from nuitka import Options
from nuitka.containers.oset import OrderedSet
from nuitka.containers.odict import OrderedDict
from nuitka.plugins.PluginBase import NuitkaPluginBase
from nuitka.utils.FileOperations import getFileContents
from nuitka.utils.Timing import StopWatch
from nuitka.utils.Utils import getOS
from nuitka.Version import getNuitkaVersion

nuitka_version = getNuitkaVersion()
if nuitka_version <= "0.6.7":
    from nuitka.plugins.Plugins import active_plugin_list as active_plugins
else:
    from nuitka.plugins.Plugins import getActivePlugins
    active_plugins = getActivePlugins()


def remove_suffix(mod_dir, mod_name):
    if mod_name not in mod_dir:
        return mod_dir
    l = len(mod_name)
    p = mod_dir.find(mod_name) + l
    return mod_dir[:p]


def check_dependents(full_name, import_list):
    """ Check if we are parent of a loaded / recursed-to module file.

    Notes:
        Accept full_name if full_name.something is a recursed-to module