def createNamespacePackage(module_name, is_top, module_relpath):
    source_ref = SourceCodeReference.fromFilenameAndLine(
        filename=module_relpath, line=1)
    source_ref = source_ref.atInternal()

    package = CompiledPythonPackage(
        module_name=module_name,
        is_top=is_top,
        mode="compiled",
        future_spec=FutureSpec(),
        source_ref=source_ref,
    )

    if python_version >= 300:
        statement = createPython3NamespacePath(package=package,
                                               module_relpath=module_relpath,
                                               source_ref=source_ref)
    else:
        statement = createPathAssignment(package, source_ref)

    package.setBody(makeStatementsSequenceFromStatement(statement=statement))

    completeVariableClosures(package)

    return source_ref, package
def createNamespacePackage(package_name, module_relpath):
    parts = package_name.split('.')

    source_ref = SourceCodeReference.fromFilenameAndLine(
        filename=module_relpath,
        line=1,
        future_spec=FutureSpec(),
    )
    source_ref = source_ref.atInternal()

    package_package_name = '.'.join(parts[:-1]) or None
    package = CompiledPythonPackage(
        name=parts[-1],
        mode="compiled",
        package_name=package_package_name,
        source_ref=source_ref,
    )

    if python_version >= 300:
        statement = createPython3NamespacePath(package_name=package_name,
                                               module_relpath=module_relpath,
                                               source_ref=source_ref)
    else:
        statement = createPathAssignment(source_ref)

    package.setBody(makeStatementsSequenceFromStatement(statement=statement))

    completeVariableClosures(package)

    return source_ref, package
Beispiel #3
0
def _detectedShlibFile(filename, module_name):
    if Utils.python_version >= 300:
        filename = filename.decode("utf-8")

    # That is not a shared library, but looks like one.
    if module_name == "__main__":
        return

    parts = module_name.split(".")
    if len(parts) == 1:
        package_name = None
        name = module_name
    else:
        package_name = ".".join(parts[:-1])
        name = parts[-1]

    from nuitka.nodes.FutureSpecs import FutureSpec
    from nuitka.nodes.ModuleNodes import PythonShlibModule
    from nuitka import SourceCodeReferences

    source_ref = SourceCodeReferences.fromFilename(filename=filename,
                                                   future_spec=FutureSpec())

    shlib_module = PythonShlibModule(name=name,
                                     package_name=package_name,
                                     source_ref=source_ref)

    from nuitka import ModuleRegistry
    ModuleRegistry.addRootModule(shlib_module)

    module_names.add(module_name)
Beispiel #4
0
def _makeModuleBodyFromSyntaxError(exc, module_name, module_filename):
    if module_filename not in Importing.warned_about:
        Importing.warned_about.add(module_filename)

        recursion_logger.warning("""\
Cannot follow import to module '%s' because of %r.""" %
                                 (module_name, exc.__class__.__name__))

    source_ref = SourceCodeReferences.fromFilename(filename=module_filename)

    module = CompiledPythonModule(
        module_name=module_name,
        is_top=False,
        mode="compiled",
        future_spec=FutureSpec(),
        source_ref=source_ref,
    )

    module_body = makeModuleFrame(
        module=module,
        statements=(makeRaiseExceptionStatementFromInstance(
            source_ref=source_ref, exception=exc), ),
        source_ref=source_ref,
    )

    module_body = makeStatementsSequenceFromStatement(statement=module_body)
    module.setChild("body", module_body)

    return module
Beispiel #5
0
def _detectedShlibFile(filename, module_name, result):
    if Utils.python_version >= 300:
        filename = filename.decode("utf-8")

    parts = module_name.split(".")
    if len(parts) == 1:
        package_name = None
        name = module_name
    else:
        package_name = ".".join(parts[:-1])
        name = parts[-1]

    from nuitka.nodes.FutureSpecs import FutureSpec
    from nuitka.nodes.ModuleNodes import PythonShlibModule
    from nuitka import SourceCodeReferences

    source_ref = SourceCodeReferences.fromFilename(
        filename    = filename,
        future_spec = FutureSpec()
    )

    shlib_module = PythonShlibModule(
        package_name = package_name,
        name         = name,
        source_ref   = source_ref
    )

    from nuitka import ModuleRegistry
    ModuleRegistry.addRootModule(shlib_module)

    module_names.add(module_name)
Beispiel #6
0
def createNamespacePackage(package_name, module_relpath):
    parts = package_name.split('.')

    source_ref = SourceCodeReference.fromFilenameAndLine(
        filename    = module_relpath,
        line        = 1,
        future_spec = FutureSpec(),
    )
    source_ref = source_ref.atInternal()

    package_package_name = '.'.join(parts[:-1]) or None
    package = PythonPackage(
        name         = parts[-1],
        package_name = package_package_name,
        source_ref   = source_ref,
    )

    package.setBody(
        makeStatementsSequenceFromStatement(
            statement = (
                StatementAssignmentVariable(
                    variable_ref = ExpressionTargetVariableRef(
                        variable_name = "__path__",
                        source_ref    = source_ref
                    ),
                    source       = ExpressionCallNoKeywords(
                        called     = ExpressionImportName(
                            module      = ExpressionImportModule(
                                module_name = "_frozen_importlib",
                                import_list = (),
                                level       = 0,
                                source_ref  = source_ref
                            ),
                            import_name = "_NamespacePath",
                            source_ref  = source_ref
                        ),
                        args       = ExpressionConstantRef(
                            constant   = (
                                package_name,
                                [module_relpath],
                                None
                            ),
                            source_ref =  source_ref
                        ),
                        source_ref =  source_ref
                    ),
                    source_ref   = source_ref
                )
            )
        )
    )

    completeVariableClosures(package)

    return source_ref, package
Beispiel #7
0
    def __init__(self):
        PythonModule.__init__(
            self,
            name="__internal__",
            package_name=None,
            source_ref=SourceCodeReference.fromFilenameAndLine(
                filename="internal",
                line=0,
                future_spec=FutureSpec(),
                inside_exec=False))

        SingleCreationMixin.__init__(self)
def createNamespacePackage(module_name, is_top, source_ref):
    package = CompiledPythonPackage(
        module_name=module_name,
        is_top=is_top,
        mode="compiled",
        future_spec=FutureSpec(),
        source_ref=source_ref,
    )

    if python_version >= 0x300:
        statement = createPython3NamespacePath(package=package, source_ref=source_ref)
    else:
        statement = createPathAssignment(package, source_ref)

    package.setChild("body", makeStatementsSequenceFromStatement(statement=statement))

    completeVariableClosures(package)

    return package
def fromFilename(filename):
    return SourceCodeReference.fromFilenameAndLine(
        filename    = filename,
        line        = 1,
        future_spec = FutureSpec(),
    )
def pushFutureSpec():
    _future_specs.append(FutureSpec())
Beispiel #11
0
def buildModuleTree(filename, package, is_top, is_main):
    # Many variables, branches, due to the many cases, pylint: disable=R0912

    assert package is None or type(package) is str

    if is_main and Utils.isDir(filename):
        source_filename = Utils.joinpath(filename, "__main__.py")

        if not Utils.isFile(source_filename):
            sys.stderr.write("%s: can't find '__main__' module in '%s'\n" %
                             (Utils.basename(sys.argv[0]), filename))
            sys.exit(2)

        filename = source_filename

        main_added = True
    else:
        main_added = False

    if Utils.isFile(filename):
        source_filename = filename

        source_ref = SourceCodeReferences.fromFilename(
            filename=filename, future_spec=FutureSpec())

        if is_main:
            module_name = "__main__"
        else:
            module_name = Utils.basename(filename)

            if module_name.endswith(".py"):
                module_name = module_name[:-3]

            if "." in module_name:
                sys.stderr.write(
                    "Error, '%s' is not a proper python module name.\n" %
                    (module_name))

                sys.exit(2)

        if is_main:
            result = PythonMainModule(source_ref=source_ref,
                                      main_added=main_added)
        else:
            result = PythonModule(name=module_name,
                                  package=package,
                                  source_ref=source_ref)
    elif Utils.isDir(filename) and Utils.isFile(
            Utils.joinpath(filename, "__init__.py")):
        source_filename = Utils.joinpath(filename, "__init__.py")

        if is_top:
            source_ref = SourceCodeReferences.fromFilename(
                filename=Utils.abspath(source_filename),
                future_spec=FutureSpec())

            package_name = Utils.splitpath(filename)[-1]
        else:
            source_ref = SourceCodeReferences.fromFilename(
                filename=Utils.abspath(source_filename),
                future_spec=FutureSpec())

            package_name = Utils.basename(filename)

        result = PythonPackage(name=package_name,
                               package=package,
                               source_ref=source_ref)
    else:
        sys.stderr.write("%s: can't open file '%s'.\n" %
                         (Utils.basename(sys.argv[0]), filename))
        sys.exit(2)

    if not Options.shallHaveStatementLines():
        source_ref = source_ref.atInternal()

    source_code = readSourceCodeFromFilename(source_filename)

    buildParseTree(provider=result,
                   source_code=source_code,
                   source_ref=source_ref)

    addImportedModule(Utils.relpath(filename), result)

    completeVariableClosures(result)

    return result
Beispiel #12
0
def decideModuleTree(filename, package, is_shlib, is_top, is_main):
    # Many variables, branches, due to the many cases, pylint: disable=R0912

    assert package is None or type(package) is str

    if is_main and Utils.isDir(filename):
        source_filename = Utils.joinpath(filename, "__main__.py")

        if not Utils.isFile(source_filename):
            sys.stderr.write(
                "%s: can't find '__main__' module in '%s'\n" % (
                    Utils.basename(sys.argv[0]),
                    filename
                )
            )
            sys.exit(2)

        filename = source_filename

        main_added = True
    else:
        main_added = False

    if Utils.isFile(filename):
        source_filename = filename

        source_ref = SourceCodeReferences.fromFilename(
            filename    = filename,
            future_spec = FutureSpec()
        )

        if is_main:
            module_name = "__main__"
        else:
            module_name = Utils.basename(filename)

            if module_name.endswith(".py"):
                module_name = module_name[:-3]

            if is_shlib:
                module_name = module_name.split('.')[0]

            if '.' in module_name:
                sys.stderr.write(
                    "Error, '%s' is not a proper python module name.\n" % (
                        module_name
                    )
                )

                sys.exit(2)

        if is_shlib:
            result = PythonShlibModule(
                name         = module_name,
                source_ref   = source_ref,
                package_name = package,
            )
        elif is_main:
            result = PythonMainModule(
                source_ref = source_ref,
                main_added = main_added
            )
        else:
            result = PythonModule(
                name         = module_name,
                package_name = package,
                source_ref   = source_ref
            )
    elif Importing.isPackageDir(filename):
        if is_top:
            package_name = Utils.splitpath(filename)[-1]
        else:
            package_name = Utils.basename(filename)

        source_filename = Utils.joinpath(filename, "__init__.py")

        if not Utils.isFile(source_filename):
            assert Utils.python_version >= 330, source_filename

            source_ref, result = createNamespacePackage(
                package_name   = package_name,
                module_relpath = filename
            )
            source_filename = None
        else:
            source_ref = SourceCodeReferences.fromFilename(
                filename    = Utils.abspath(source_filename),
                future_spec = FutureSpec()
            )

            result = PythonPackage(
                name         = package_name,
                package_name = package,
                source_ref   = source_ref
            )
    else:
        sys.stderr.write(
            "%s: can't open file '%s'.\n" % (
                Utils.basename(sys.argv[0]),
                filename
            )
        )
        sys.exit(2)

    if not Options.shallHaveStatementLines():
        source_ref = source_ref.atInternal()

    return result, source_ref, source_filename