Ejemplo n.º 1
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)
Ejemplo n.º 2
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)
Ejemplo n.º 3
0
def _detectedShlibFile(filename, module_name):
    # That is not a shared library, but looks like one.
    if module_name == "__main__":
        return

    from nuitka import ModuleRegistry
    if ModuleRegistry.hasRootModule(module_name):
        return

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

    source_ref = SourceCodeReferences.fromFilename(
        filename = filename
    )

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

    ModuleRegistry.addRootModule(shlib_module)
    ImportCache.addImportedModule(shlib_module)

    module_names.add(module_name)
Ejemplo n.º 4
0
    def loadSource(self, filename):
        self.moving = True
        self.source_code = open(filename).read()
        self.textedit_source.setPlainText(self.source_code)
        self.moving = False

        self.displayed = SourceCodeReferences.fromFilename(filename=filename)
Ejemplo n.º 5
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
Ejemplo n.º 6
0
def _detectedShlibFile(filename, module_name):
    # That is not a shared library, but looks like one.
    if module_name == "__main__":
        return

    from nuitka import ModuleRegistry
    if ModuleRegistry.hasRootModule(module_name):
        return

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

    source_ref = SourceCodeReferences.fromFilename(
        filename = filename
    )

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

    ModuleRegistry.addRootModule(shlib_module)
    ImportCache.addImportedModule(shlib_module)

    module_names.add(module_name)
Ejemplo n.º 7
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]

    source_ref = SourceCodeReferences.fromFilename(
        filename = filename
    )

    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)
Ejemplo n.º 8
0
    def loadSource(self, filename):
        self.moving = True
        self.source_code = open(filename).read()
        self.textedit_source.setPlainText(self.source_code)
        self.moving = False

        self.displayed = SourceCodeReferences.fromFilename(
            filename = filename
        )
Ejemplo n.º 9
0
def _readSourceCodeFromFilename3(source_filename):
    with open(source_filename, "rb") as source_file:
        source_code = source_file.read()

    if source_code.startswith(b'\xef\xbb\xbf'):
        source_code = source_code[3:]

    new_line = source_code.find(b"\n")

    if new_line is not -1:
        line = source_code[: new_line]

        line_match = re.search(b"coding[:=]\\s*([-\\w.]+)", line)

        if line_match:
            encoding = line_match.group(1).decode("ascii")

            # Detect encoding problem, as decode won't raise the compatible
            # thing.
            try:
                import codecs
                codecs.lookup(encoding)
            except LookupError:
                if Utils.python_version >= 341 or \
                   (Utils.python_version >= 335 and \
                    Utils.python_version < 340) or \
                   (Utils.python_version >= 323 and \
                    Utils.python_version < 330):
                    reason = "encoding problem: %s" % encoding
                else:
                    reason = "unknown encoding: %s" % encoding

                SyntaxErrors.raiseSyntaxError(
                    reason       = reason,
                    source_ref   = SourceCodeReferences.fromFilename(
                        source_filename,
                        None
                    ),
                    display_line = False
                )

            return source_code[ new_line : ].decode(encoding)

        new_line = source_code.find(b"\n", new_line+1)

        if new_line is not -1:
            line = source_code[ : new_line ]

            line_match = re.search(b"coding[:=]\\s*([-\\w.]+)", line)

            if line_match:
                encoding = line_match.group(1).decode("ascii")

                return "\n" + source_code[ new_line : ].decode(encoding)

    return source_code.decode("utf-8")
Ejemplo n.º 10
0
def _readSourceCodeFromFilename3(source_filename):
    with open(source_filename, "rb") as source_file:
        source_code = source_file.read()

    if source_code.startswith(b'\xef\xbb\xbf'):
        source_code = source_code[3:]

    new_line = source_code.find(b"\n")

    if new_line is not -1:
        line = source_code[:new_line]

        line_match = re.search(b"coding[:=]\\s*([-\\w.]+)", line)

        if line_match:
            encoding = line_match.group(1).decode("ascii")

            # Detect encoding problem, as decode won't raise the compatible
            # thing.
            try:
                import codecs
                codecs.lookup(encoding)
            except LookupError:
                if Utils.python_version >= 341 or \
                   (Utils.python_version >= 335 and \
                    Utils.python_version < 340) or \
                   (Utils.python_version >= 323 and \
                    Utils.python_version < 330):
                    reason = "encoding problem: %s" % encoding
                else:
                    reason = "unknown encoding: %s" % encoding

                SyntaxErrors.raiseSyntaxError(
                    reason=reason,
                    source_ref=SourceCodeReferences.fromFilename(
                        source_filename, None),
                    display_line=False)

            return source_code[new_line:].decode(encoding)

        new_line = source_code.find(b"\n", new_line + 1)

        if new_line is not -1:
            line = source_code[:new_line]

            line_match = re.search(b"coding[:=]\\s*([-\\w.]+)", line)

            if line_match:
                encoding = line_match.group(1).decode("ascii")

                return "\n" + source_code[new_line:].decode(encoding)

    return source_code.decode("utf-8")
Ejemplo n.º 11
0
def _readSourceCodeFromFilename2(source_filename):
    # Detect the encoding.
    with open(source_filename, "rU") as source_file:
        encoding = _detectEncoding2(source_file)

        source_code = source_file.read()

        # Try and detect SyntaxError from missing or wrong encodings.
        if type(source_code) is not unicode and encoding == "ascii":
            try:
                _source_code = source_code.decode(encoding)
            except UnicodeDecodeError as e:
                lines = source_code.split('\n')
                so_far = 0

                for count, line in enumerate(lines):
                    so_far += len(line) + 1

                    if so_far > e.args[2]:
                        break
                else:
                    # Cannot happen, decode error implies non-empty.
                    count = -1

                wrong_byte = re.search(
                    "byte 0x([a-f0-9]{2}) in position",
                    str(e)
                ).group(1)

                SyntaxErrors.raiseSyntaxError(
                    reason       = """\
Non-ASCII character '\\x%s' in file %s on line %d, but no encoding declared; \
see http://python.org/dev/peps/pep-0263/ for details""" % (
                        wrong_byte,
                        source_filename,
                        count+1,
                    ),
                    source_ref   = SourceCodeReferences.fromFilename(
                        source_filename,
                        None
                    ).atLineNumber(count+1),
                    display_line = False
                )

    return source_code
Ejemplo n.º 12
0
def _readSourceCodeFromFilename2(source_filename):
    # Detect the encoding.
    encoding = _detectEncoding2(source_filename)

    with open(source_filename, "rU") as source_file:
        source_code = source_file.read()

        # Try and detect SyntaxError from missing or wrong encodings.
        if type(source_code) is not unicode and encoding == "ascii":
            try:
                _source_code = source_code.decode(encoding)
            except UnicodeDecodeError as e:
                lines = source_code.split("\n")
                so_far = 0

                for count, line in enumerate(lines):
                    so_far += len(line) + 1

                    if so_far > e.args[2]:
                        break
                else:
                    # Cannot happen, decode error implies non-empty.
                    count = -1

                wrong_byte = re.search(
                    "byte 0x([a-f0-9]{2}) in position",
                    str(e)
                ).group(1)

                SyntaxErrors.raiseSyntaxError(
                    reason       = """\
Non-ASCII character '\\x%s' in file %s on line %d, but no encoding declared; \
see http://python.org/dev/peps/pep-0263/ for details""" % (
                        wrong_byte,
                        source_filename,
                        count+1,
                    ),
                    source_ref   = SourceCodeReferences.fromFilename(
                        source_filename,
                        None
                    ).atLineNumber(count+1),
                    display_line = False
                )

    return source_code
Ejemplo n.º 13
0
def _readSourceCodeFromFilename3( source_filename ):
    source_code = open( source_filename, "rb" ).read()

    if source_code.startswith( b'\xef\xbb\xbf' ):
        return source_code[3:]

    new_line = source_code.find( b"\n" )

    if new_line is not -1:
        line = source_code[ : new_line ]

        line_match = re.search( b"coding[:=]\s*([-\w.]+)", line )

        if line_match:
            encoding = line_match.group(1).decode( "ascii" )

            # Detect encoding problem, as decode won't raise the compatible thing.
            try:
                import codecs
                codecs.lookup( encoding )
            except LookupError:
                SyntaxErrors.raiseSyntaxError(
                    reason       = "unknown encoding: %s" % encoding,
                    source_ref   = SourceCodeReferences.fromFilename( source_filename, None ),
                    display_line = False
                )

            return source_code[ new_line + 1 : ].decode( encoding )

        new_line = source_code.find( b"\n", new_line+1 )

        if new_line is not -1:
            line = source_code[ : new_line ]

            line_match = re.search( b"coding[:=]\s*([-\w.]+)", line )

            if line_match:
                encoding = line_match.group(1).decode( "ascii" )

                return source_code[ new_line + 1 : ].decode( encoding )


    return source_code.decode( "utf-8" )
Ejemplo n.º 14
0
def _detectedShlibFile(filename, module_name):
    # That is not a shared library, but looks like one.
    if module_name == "__main__":
        return

    # Cyclic dependency
    from nuitka import ModuleRegistry

    if ModuleRegistry.hasRootModule(module_name):
        return

    source_ref = SourceCodeReferences.fromFilename(filename=filename)

    shlib_module = PythonShlibModule(module_name=module_name, source_ref=source_ref)

    ModuleRegistry.addRootModule(shlib_module)
    ImportCache.addImportedModule(shlib_module)

    module_names.add(module_name)
Ejemplo n.º 15
0
def _readSourceCodeFromFilename3(source_filename):
    source_code = open(source_filename, "rb").read()

    if source_code.startswith(b'\xef\xbb\xbf'):
        return source_code[3:]

    new_line = source_code.find(b"\n")

    if new_line is not -1:
        line = source_code[:new_line]

        line_match = re.search(b"coding[:=]\s*([-\w.]+)", line)

        if line_match:
            encoding = line_match.group(1).decode("ascii")

            # Detect encoding problem, as decode won't raise the compatible thing.
            try:
                import codecs
                codecs.lookup(encoding)
            except LookupError:
                SyntaxErrors.raiseSyntaxError(
                    reason="unknown encoding: %s" % encoding,
                    source_ref=SourceCodeReferences.fromFilename(
                        source_filename, None),
                    display_line=False)

            return source_code[new_line + 1:].decode(encoding)

        new_line = source_code.find(b"\n", new_line + 1)

        if new_line is not -1:
            line = source_code[:new_line]

            line_match = re.search(b"coding[:=]\s*([-\w.]+)", line)

            if line_match:
                encoding = line_match.group(1).decode("ascii")

                return source_code[new_line + 1:].decode(encoding)

    return source_code.decode("utf-8")
Ejemplo n.º 16
0
def _detectedExtensionModule(filename, module_name, result, technical):
    # That is not a shared library, but looks like one.
    if module_name == "__main__":
        return

    # Extension modules are not tracked outside of standalone
    # mode.
    if not Options.isStandaloneMode():
        return

    # Avoid duplicates
    if module_name in module_names:
        return

    source_ref = SourceCodeReferences.fromFilename(filename=filename)

    extension_module = PythonExtensionModule(module_name=module_name,
                                             technical=technical,
                                             source_ref=source_ref)

    ImportCache.addImportedModule(extension_module)

    module_names.add(module_name)
    result.append(extension_module)
Ejemplo n.º 17
0
def decideModuleTree(filename, package, is_shlib, is_top, is_main):
    # Many variables, branches, due to the many cases
    # pylint: disable=too-many-branches,too-many-locals,too-many-statements

    assert package is None or type(package) is ModuleName
    assert filename is not None

    if is_main and os.path.isdir(filename):
        source_filename = os.path.join(filename, "__main__.py")

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

        filename = source_filename

        main_added = True
    else:
        main_added = False

    if os.path.isfile(filename):
        source_filename = filename

        source_ref = SourceCodeReferences.fromFilename(filename=filename)

        if is_main:
            module_name = ModuleName("__main__")
        else:
            # Derive module name from filename.
            module_name = os.path.basename(filename)
            if is_shlib:
                module_name = module_name.split(".")[0]
            elif 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)

            module_name = ModuleName.makeModuleNameInPackage(
                module_name, package)

        if is_shlib:
            result = PythonShlibModule(module_name=module_name,
                                       source_ref=source_ref)
            source_code = None
        else:
            source_code = readSourceCodeFromFilename(
                module_name=module_name, source_filename=source_filename)

            if is_main:
                result = PythonMainModule(
                    main_added=main_added,
                    mode=decideCompilationMode(False, module_name, source_ref),
                    future_spec=None,
                    source_ref=source_ref,
                )

                checkPythonVersionFromCode(source_code)
            else:
                mode = decideCompilationMode(is_top, module_name, source_ref)

                if (mode == "bytecode" and not is_top
                        and hasCachedImportedModulesNames(
                            module_name, source_code)):

                    optimization_logger.info("%r is included as bytecode." %
                                             (module_name.asString()))
                    result = UncompiledPythonModule(
                        module_name=module_name,
                        filename=filename,
                        bytecode=demoteSourceCodeToBytecode(
                            module_name=module_name,
                            source_code=source_code,
                            filename=filename,
                        ),
                        source_ref=source_ref,
                        user_provided=False,
                        technical=False,
                    )

                    used_modules = OrderedSet()

                    for used_module_name in getCachedImportedModulesNames(
                            module_name=module_name, source_code=source_code):
                        (
                            _module_package,
                            module_filename,
                            _finding,
                        ) = Importing.findModule(
                            importing=result,
                            module_name=used_module_name,
                            parent_package=None,
                            level=-1,
                            warn=False,
                        )

                        used_modules.add((used_module_name,
                                          os.path.relpath(module_filename)))

                    result.setUsedModules(used_modules)

                    # Not used anymore
                    source_code = None
                else:
                    result = CompiledPythonModule(
                        module_name=module_name,
                        is_top=is_top,
                        mode=mode,
                        future_spec=None,
                        source_ref=source_ref,
                    )

    elif Importing.isPackageDir(filename):
        if is_top:
            module_name = splitPath(filename)[-1]
        else:
            module_name = os.path.basename(filename)

        module_name = ModuleName.makeModuleNameInPackage(module_name, package)

        source_filename = os.path.join(filename, "__init__.py")

        if not os.path.isfile(source_filename):
            source_ref, result = createNamespacePackage(
                module_name=module_name,
                is_top=is_top,
                module_relpath=filename)
            source_filename = None
            source_code = None
        else:
            source_ref = SourceCodeReferences.fromFilename(
                filename=os.path.abspath(source_filename))

            result = CompiledPythonPackage(
                module_name=module_name,
                is_top=is_top,
                mode=decideCompilationMode(is_top, module_name, source_ref),
                future_spec=None,
                source_ref=source_ref,
            )

            source_code = readSourceCodeFromFilename(
                module_name=module_name, source_filename=source_filename)
    else:
        sys.stderr.write("%s: can't open file '%s'.\n" %
                         (os.path.basename(sys.argv[0]), filename))
        sys.exit(2)

    return result, source_ref, source_code
Ejemplo n.º 18
0
def decideModuleTree(filename, package, is_shlib, is_top, is_main):
    # Many variables, branches, due to the many cases, pylint: disable=too-many-branches,too-many-statements

    assert package is None or type(package) is str
    assert filename is not None

    if is_main and os.path.isdir(filename):
        source_filename = os.path.join(filename, "__main__.py")

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

        filename = source_filename

        main_added = True
    else:
        main_added = False

    if os.path.isfile(filename):
        source_filename = filename

        source_ref = SourceCodeReferences.fromFilename(filename=filename, )

        if is_main:
            module_name = "__main__"
        else:
            module_name = os.path.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,
                                       package_name=package,
                                       source_ref=source_ref)
        elif is_main:
            result = PythonMainModule(main_added=main_added,
                                      mode=Plugins.decideCompilation(
                                          "__main__", source_ref),
                                      future_spec=None,
                                      source_ref=source_ref)
        else:
            if package is not None:
                full_name = package + '.' + module_name
            else:
                full_name = module_name

            result = CompiledPythonModule(name=module_name,
                                          package_name=package,
                                          mode=Plugins.decideCompilation(
                                              full_name, source_ref),
                                          future_spec=None,
                                          source_ref=source_ref)

    elif Importing.isPackageDir(filename):
        if is_top:
            module_name = splitPath(filename)[-1]
        else:
            module_name = os.path.basename(filename)

        source_filename = os.path.join(filename, "__init__.py")

        if not os.path.isfile(source_filename):
            source_ref, result = createNamespacePackage(
                module_name=module_name,
                package_name=package,
                module_relpath=filename)
            source_filename = None
        else:
            source_ref = SourceCodeReferences.fromFilename(
                filename=os.path.abspath(source_filename), )

            if package is not None:
                full_name = package + '.' + module_name
            else:
                full_name = module_name

            result = CompiledPythonPackage(name=module_name,
                                           package_name=package,
                                           mode=Plugins.decideCompilation(
                                               full_name, source_ref),
                                           future_spec=None,
                                           source_ref=source_ref)

            assert result.getFullName() == full_name, result
    else:
        sys.stderr.write("%s: can't open file '%s'.\n" %
                         (os.path.basename(sys.argv[0]), filename))
        sys.exit(2)

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

    return result, source_ref, source_filename
Ejemplo n.º 19
0
def decideModuleTree(filename, package, is_shlib, is_top, is_main):
    # Many variables, branches, due to the many cases, pylint: disable=too-many-branches

    assert package is None or type(package) is ModuleName
    assert filename is not None

    if is_main and os.path.isdir(filename):
        source_filename = os.path.join(filename, "__main__.py")

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

        filename = source_filename

        main_added = True
    else:
        main_added = False

    if os.path.isfile(filename):
        source_filename = filename

        source_ref = SourceCodeReferences.fromFilename(filename=filename)

        if is_main:
            module_name = ModuleName("__main__")
        else:
            # Derive module name from filename.
            module_name = os.path.basename(filename)
            if is_shlib:
                module_name = module_name.split(".")[0]
            elif 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)

            module_name = ModuleName.makeModuleNameInPackage(module_name, package)

        if is_shlib:
            result = PythonShlibModule(module_name=module_name, source_ref=source_ref)
        elif is_main:
            result = PythonMainModule(
                main_added=main_added,
                mode=decideCompilationMode(False, module_name, source_ref),
                future_spec=None,
                source_ref=source_ref,
            )
        else:
            result = CompiledPythonModule(
                module_name=module_name,
                is_top=is_top,
                mode=decideCompilationMode(is_top, module_name, source_ref),
                future_spec=None,
                source_ref=source_ref,
            )

    elif Importing.isPackageDir(filename):
        if is_top:
            module_name = splitPath(filename)[-1]
        else:
            module_name = os.path.basename(filename)

        module_name = ModuleName.makeModuleNameInPackage(module_name, package)

        source_filename = os.path.join(filename, "__init__.py")

        if not os.path.isfile(source_filename):
            source_ref, result = createNamespacePackage(
                module_name=module_name, is_top=is_top, module_relpath=filename
            )
            source_filename = None
        else:
            source_ref = SourceCodeReferences.fromFilename(
                filename=os.path.abspath(source_filename)
            )

            result = CompiledPythonPackage(
                module_name=module_name,
                is_top=is_top,
                mode=decideCompilationMode(is_top, module_name, source_ref),
                future_spec=None,
                source_ref=source_ref,
            )
    else:
        sys.stderr.write(
            "%s: can't open file '%s'.\n" % (os.path.basename(sys.argv[0]), filename)
        )
        sys.exit(2)

    return result, source_ref, source_filename
Ejemplo n.º 20
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
    assert filename is not None

    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,
        )

        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,
                package_name = package,
                source_ref   = source_ref
            )
        elif is_main:
            result = PythonMainModule(
                main_added = main_added,
                source_ref = source_ref
            )
        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):
            source_ref, result = createNamespacePackage(
                package_name   = package_name,
                module_relpath = filename
            )
            source_filename = None
        else:
            source_ref = SourceCodeReferences.fromFilename(
                filename = Utils.abspath(source_filename),
            )

            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
Ejemplo n.º 21
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 )

    module_body = buildParseTree(
        provider    = result,
        source_code = source_code,
        source_ref  = source_ref,
        is_module   = True
    )

    result.setBody( module_body )

    addImportedModule( Utils.relpath( filename ), result )

    completeVariableClosures( result )

    return result
Ejemplo n.º 22
0
def decideModuleSourceRef(filename, module_name, is_main, is_fake, logger):
    # Many branches due to the many cases

    assert type(module_name) is ModuleName
    assert filename is not None

    is_namespace = False
    is_package = False

    if is_main and os.path.isdir(filename):
        source_filename = os.path.join(filename, "__main__.py")

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

        filename = source_filename

        main_added = True
    else:
        main_added = False

    if is_fake:
        source_filename = filename

        source_ref = SourceCodeReferences.fromFilename(filename=filename)

        module_name = is_fake

    elif os.path.isfile(filename):
        source_filename = filename

        source_ref = SourceCodeReferences.fromFilename(filename=filename)

    elif isPackageDir(filename):
        is_package = True

        source_filename = os.path.join(filename, "__init__.py")

        if not os.path.isfile(source_filename):
            source_ref = SourceCodeReferences.fromFilename(
                filename=filename
            ).atInternal()
            is_namespace = True
        else:
            source_ref = SourceCodeReferences.fromFilename(
                filename=os.path.abspath(source_filename)
            )

    else:
        logger.sysexit(
            "%s: can't open file '%s'." % (os.path.basename(sys.argv[0]), filename),
            exit_code=2,
        )

    return (
        main_added,
        is_package,
        is_namespace,
        source_ref,
        source_filename,
    )
Ejemplo n.º 23
0
def decideModuleTree(filename, package, is_shlib, is_top, is_main):
    # Many variables, branches, due to the many cases, pylint: disable=R0912,R0915

    assert package is None or type(package) is str
    assert filename is not None

    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,
        )

        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,
                package_name = package,
                source_ref   = source_ref
            )
        elif is_main:
            result = PythonMainModule(
                main_added = main_added,
                source_ref = source_ref
            )
        else:
            if package is not None:
                full_name = package + "." + module_name
            else:
                full_name = module_name

            decision = Plugins.decideCompilation(full_name, source_ref)

            if decision == "compiled":
                result = CompiledPythonModule(
                    name         = module_name,
                    package_name = package,
                    source_ref   = source_ref
                )
            else:
                source_code = readSourceCodeFromFilename(module_name, filename)

                source_code = Plugins.onFrozenModuleSourceCode(
                    module_name = full_name,
                    is_package  = False,
                    source_code = source_code
                )

                bytecode = compile(source_code, filename, "exec")

                bytecode = Plugins.onFrozenModuleBytecode(
                    module_name = module_name,
                    is_package  = False,
                    bytecode    = bytecode
                )

                result = UncompiledPythonModule(
                    name         = module_name,
                    package_name = package,
                    bytecode     = bytecode,
                    filename     = filename,
                    user_provided = True,
                    technical    = False,
                    source_ref   = source_ref
                )

                # Don't read it anymore.
                source_filename = None

    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):
            source_ref, result = createNamespacePackage(
                package_name   = package_name,
                module_relpath = filename
            )
            source_filename = None
        else:
            source_ref = SourceCodeReferences.fromFilename(
                filename = Utils.abspath(source_filename),
            )

            if package is not None:
                full_name = package + "." + package_name
            else:
                full_name = package_name

            decision = Plugins.decideCompilation(full_name, source_ref)

            if decision == "compiled":
                result = CompiledPythonPackage(
                    name         = package_name,
                    package_name = package,
                    source_ref   = source_ref
                )
            else:
                bytecode = compile(source_code, filename, "exec")

                bytecode = Plugins.onFrozenModuleBytecode(
                    module_name = module_name,
                    is_package  = False,
                    bytecode    = bytecode
                )

                result = UncompiledPythonPackage(
                    name         = module_name,
                    package_name = package,
                    bytecode     = bytecode,
                    filename     = filename,
                    user_provided = True,
                    technical    = False,
                    source_ref   = source_ref
                )

                # Don't read it anymore.
                source_filename = None
    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
Ejemplo n.º 24
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
Ejemplo n.º 25
0
def decideModuleTree(filename, package, is_shlib, is_top, is_main):
    # Many variables, branches, due to the many cases, pylint: disable=R0912,R0915

    assert package is None or type(package) is str
    assert filename is not None

    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, )

        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,
                                       package_name=package,
                                       source_ref=source_ref)
        elif is_main:
            result = PythonMainModule(main_added=main_added,
                                      source_ref=source_ref)
        else:
            if package is not None:
                full_name = package + "." + module_name
            else:
                full_name = module_name

            decision = Plugins.decideCompilation(full_name, source_ref)

            if decision == "compiled":
                result = CompiledPythonModule(name=module_name,
                                              package_name=package,
                                              source_ref=source_ref)
            else:
                source_code = readSourceCodeFromFilename(module_name, filename)

                source_code = Plugins.onFrozenModuleSourceCode(
                    module_name=full_name,
                    is_package=False,
                    source_code=source_code)

                bytecode = compile(source_code, filename, "exec")

                bytecode = Plugins.onFrozenModuleBytecode(
                    module_name=module_name,
                    is_package=False,
                    bytecode=bytecode)

                result = UncompiledPythonModule(name=module_name,
                                                package_name=package,
                                                bytecode=bytecode,
                                                filename=filename,
                                                user_provided=True,
                                                technical=False,
                                                source_ref=source_ref)

                # Don't read it anymore.
                source_filename = None

    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):
            source_ref, result = createNamespacePackage(
                package_name=package_name, module_relpath=filename)
            source_filename = None
        else:
            source_ref = SourceCodeReferences.fromFilename(
                filename=Utils.abspath(source_filename), )

            if package is not None:
                full_name = package + "." + package_name
            else:
                full_name = package_name

            decision = Plugins.decideCompilation(full_name, source_ref)

            if decision == "compiled":
                result = CompiledPythonPackage(name=package_name,
                                               package_name=package,
                                               source_ref=source_ref)
            else:
                bytecode = compile(source_code, filename, "exec")

                bytecode = Plugins.onFrozenModuleBytecode(
                    module_name=module_name,
                    is_package=False,
                    bytecode=bytecode)

                result = UncompiledPythonPackage(name=module_name,
                                                 package_name=package,
                                                 bytecode=bytecode,
                                                 filename=filename,
                                                 user_provided=True,
                                                 technical=False,
                                                 source_ref=source_ref)

                # Don't read it anymore.
                source_filename = None
    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