Example #1
0
def _reportCandidates(package_name, module_name, candidate, candidates):
    module_name = (
        package_name.getChildNamed(module_name)
        if package_name is not None
        else module_name
    )

    if (
        candidate.priority == 1
        and Options.shallPreferSourcecodeOverExtensionModules() is None
    ):
        for c in candidates:
            # Don't compare to itself and don't consider unused bytecode a problem.
            if c is candidate or c.priority == 3:
                continue

            if c.search_order == candidate.search_order:
                if not module_name.hasOneOfNamespaces(unworthy_namespaces):

                    recursion_logger.info(
                        """\
Should decide '--prefer-source-code' vs. '--no-prefer-source-code', using \
existing '%s' extension module by default. Candidates were: %s <-> %s."""
                        % (module_name, candidate, c)
                    )
Example #2
0
def checkPluginPath(plugin_filename, module_package):
    plugin_filename = os.path.normpath(plugin_filename)

    if Options.isShowInclusion():
        recursion_logger.info("Checking top level plug-in path %s %s" %
                              (plugin_filename, module_package))

    plugin_info = considerFilename(module_filename=plugin_filename)

    if plugin_info is not None:
        # File or package makes a difference, handle that
        if os.path.isfile(plugin_info[0]) or Importing.isPackageDir(
                plugin_info[0]):
            checkPluginSinglePath(plugin_filename,
                                  module_package=module_package)
        elif os.path.isdir(plugin_info[0]):
            for sub_path, sub_filename in listDir(plugin_info[0]):
                assert sub_filename != "__init__.py"

                if Importing.isPackageDir(sub_path) or sub_path.endswith(
                        ".py"):
                    checkPluginSinglePath(sub_path, module_package=None)
        else:
            recursion_logger.warning("Failed to include module from %r." %
                                     plugin_info[0])
    else:
        recursion_logger.warning("Failed to recurse to directory %r." %
                                 plugin_filename)
Example #3
0
def _attemptRecursion(module):
    new_modules = module.attemptRecursion()

    if Options.isShowInclusion():
        for new_module in new_modules:
            recursion_logger.info("{source_ref} : {tags} : {message}".format(
                source_ref=new_module.getSourceReference().getAsString(),
                tags="new_code",
                message="Recursed to module package.",
            ))
Example #4
0
def _reportCandidates(module_name, candidate, candidates):
    if (candidate.priority == 1
            and Options.shallPreferSourcecodeOverExtensionModules() is None):
        for c in candidates:
            # Don't compare to itself and don't consider unused bytecode a problem.
            if c is candidate or c.priority == 3:
                continue

            if c.search_order == candidate.search_order:
                recursion_logger.info("""\
Should decide '--prefer-source-code' vs. '--no-prefer-source-code', using \
existing '%s' extension module by default. Candidates were: %s <-> %s.""" %
                                      (module_name, candidate, c))
Example #5
0
def checkPluginFilenamePattern(pattern):
    if Options.isShowInclusion():
        recursion_logger.info("Checking plug-in pattern '%s':" % pattern)

    assert not os.path.isdir(pattern), pattern

    found = False

    for filename in glob.iglob(pattern):
        if filename.endswith(".pyc"):
            continue

        if not os.path.isfile(filename):
            continue

        found = True
        checkPluginSinglePath(filename, module_package=None)

    if not found:
        recursion_logger.warning("Didn't match any files against pattern %r." % pattern)
Example #6
0
def checkPluginFilenamePattern(pattern):
    if Options.isShowInclusion():
        recursion_logger.info("Checking plug-in pattern '%s':" % pattern)

    if os.path.isdir(pattern):
        sys.exit("Error, pattern cannot be a directory name.")

    found = False

    for filename in glob.iglob(pattern):
        if filename.endswith(".pyc"):
            continue

        if not os.path.isfile(filename):
            continue

        found = True
        checkPluginSinglePath(filename, None)

    if not found:
        warning("Didn't match any files against pattern '%s'." % pattern)
Example #7
0
def _recurseTo(module_package, module_filename, module_relpath, module_kind,
               reason):
    from nuitka.nodes.ModuleNodes import makeUncompiledPythonModule
    from nuitka.tree import Building

    module, source_ref, source_code = Building.decideModuleTree(
        filename=module_filename,
        package=module_package,
        is_top=False,
        is_main=False,
        is_shlib=module_kind == "shlib",
    )

    if Options.isShowInclusion():
        recursion_logger.info("Recurse to import '%s' from '%s'. (%s)" %
                              (module.getFullName(), module_relpath, reason))

    if source_code is not None:
        try:
            Building.createModuleTree(
                module=module,
                source_ref=source_ref,
                source_code=source_code,
                is_main=False,
            )
        except (SyntaxError, IndentationError) as e:
            if module_filename not in Importing.warned_about:
                Importing.warned_about.add(module_filename)

                recursion_logger.warning(
                    """\
Cannot follow import to module %r (%r) because of %r""" %
                    (module_relpath, module_filename, e.__class__.__name__))

            return None, False
        except Building.CodeTooComplexCode:
            if module_filename not in Importing.warned_about:
                Importing.warned_about.add(module_filename)

                recursion_logger.warning("""\
Cannot recurse to import module %r (%r) because code is too complex.""" % (
                    module_relpath,
                    module_filename,
                ))

                if Options.isStandaloneMode():
                    module = makeUncompiledPythonModule(
                        module_name=module.getFullName(),
                        filename=module_filename,
                        bytecode=marshal.dumps(
                            compile(source_code,
                                    module_filename,
                                    "exec",
                                    dont_inherit=True)),
                        is_package=module.isCompiledPythonPackage(),
                        user_provided=True,
                        technical=False,
                    )

                    ModuleRegistry.addUncompiledModule(module)

            return None, False

    ImportCache.addImportedModule(module)

    return module, True
Example #8
0
def checkPluginSinglePath(plugin_filename, module_package):
    # Many branches, for the decision is very complex, pylint: disable=too-many-branches

    if Options.isShowInclusion():
        recursion_logger.info("Checking detail plug-in path '%s' '%s':" %
                              (plugin_filename, module_package))

    module_name, module_kind = Importing.getModuleNameAndKindFromFilename(
        plugin_filename)

    module_name = ModuleName.makeModuleNameInPackage(module_name,
                                                     module_package)

    if module_kind is not None:
        decision, reason = decideRecursion(
            module_filename=plugin_filename,
            module_name=module_name,
            module_kind=module_kind,
            extra_recursion=True,
        )

        if decision:
            module_relpath = relpath(plugin_filename)

            module, is_added = recurseTo(
                module_filename=plugin_filename,
                module_relpath=module_relpath,
                module_package=module_package,
                module_kind=module_kind,
                reason=reason,
            )

            if module:
                if not is_added:
                    recursion_logger.warning(
                        "Recursed to %s '%s' at '%s' twice." % (
                            "package"
                            if module.isCompiledPythonPackage() else "module",
                            module.getName(),
                            plugin_filename,
                        ))

                    if not isSameModulePath(module.getFilename(),
                                            plugin_filename):
                        recursion_logger.warning(
                            "Duplicate '%s' of '%s' ignored ." % (
                                plugin_filename,
                                module.getFilename(),
                            ))

                        return

                if Options.isShowInclusion():
                    recursion_logger.info("Recursed to '%s' %s" % (
                        module.getFullName(),
                        module,
                    ))

                ImportCache.addImportedModule(module)

                if module.isCompiledPythonPackage():
                    package_filename = module.getFilename()

                    if os.path.isdir(package_filename):
                        # Must be a namespace package.
                        assert python_version >= 0x300

                        package_dir = package_filename

                        # Only include it, if it contains actual modules, which will
                        # recurse to this one and find it again.
                    else:
                        package_dir = os.path.dirname(package_filename)

                        # Real packages will always be included.
                        ModuleRegistry.addRootModule(module)

                    if Options.isShowInclusion():
                        recursion_logger.info("Package directory '%s'." %
                                              package_dir)

                    for sub_path, sub_filename in listDir(package_dir):
                        if sub_filename in ("__init__.py", "__pycache__"):
                            continue

                        assert sub_path != plugin_filename

                        if Importing.isPackageDir(
                                sub_path) and not os.path.exists(sub_path +
                                                                 ".py"):
                            checkPluginSinglePath(
                                sub_path, module_package=module.getFullName())
                        elif sub_path.endswith(".py"):
                            checkPluginSinglePath(
                                sub_path, module_package=module.getFullName())

                elif module.isCompiledPythonModule():
                    ModuleRegistry.addRootModule(module)
                elif module.isPythonShlibModule():
                    if Options.isStandaloneMode():
                        ModuleRegistry.addRootModule(module)

            else:
                recursion_logger.warning(
                    "Failed to include module from '%s'." % plugin_filename)
Example #9
0
def checkPluginSinglePath(plugin_filename, module_package):
    # Many branches, for the decision is very complex, pylint: disable=too-many-branches

    # The importing wants these to be unique.
    plugin_filename = os.path.abspath(plugin_filename)

    if Options.isShowInclusion():
        recursion_logger.info("Checking detail plug-in path '%s' '%s':" %
                              (plugin_filename, module_package))

    module_name, module_kind = Importing.getModuleNameAndKindFromFilename(
        plugin_filename)

    module_name = ModuleName.makeModuleNameInPackage(module_name,
                                                     module_package)

    if module_kind == "extension" and not Options.isStandaloneMode():
        recursion_logger.warning(
            "Cannot include '%s' unless using at least standalone mode." %
            module_name.asString())

    if module_kind is not None:
        decision, reason = decideRecursion(
            module_filename=plugin_filename,
            module_name=module_name,
            module_kind=module_kind,
            extra_recursion=True,
        )

        if decision:
            module = recurseTo(
                signal_change=None,
                module_filename=plugin_filename,
                module_name=module_name,
                module_kind=module_kind,
                reason=reason,
            )

            if module:
                if Options.isShowInclusion():
                    recursion_logger.info("Included '%s' as '%s'." % (
                        module.getFullName(),
                        module,
                    ))

                ImportCache.addImportedModule(module)

                if module.isCompiledPythonPackage():
                    package_filename = module.getFilename()

                    if os.path.isdir(package_filename):
                        # Must be a namespace package.
                        assert python_version >= 0x300

                        package_dir = package_filename

                        # Only include it, if it contains actual modules, which will
                        # recurse to this one and find it again.
                    else:
                        package_dir = os.path.dirname(package_filename)

                        # Real packages will always be included.
                        ModuleRegistry.addRootModule(module)

                    if Options.isShowInclusion():
                        recursion_logger.info("Package directory '%s'." %
                                              package_dir)

                    for sub_path, sub_filename in listDir(package_dir):
                        if sub_filename in ("__init__.py", "__pycache__"):
                            continue

                        assert sub_path != plugin_filename

                        if Importing.isPackageDir(
                                sub_path) and not os.path.exists(sub_path +
                                                                 ".py"):
                            checkPluginSinglePath(
                                sub_path, module_package=module.getFullName())
                        elif sub_path.endswith(".py"):
                            checkPluginSinglePath(
                                sub_path, module_package=module.getFullName())

                elif module.isCompiledPythonModule():
                    ModuleRegistry.addRootModule(module)
                elif module.isPythonExtensionModule():
                    if Options.isStandaloneMode():
                        ModuleRegistry.addRootModule(module)

            else:
                recursion_logger.warning(
                    "Failed to include module from '%s'." % plugin_filename)