Ejemplo n.º 1
0
def generateModuleCode(module, data_filename):
    # As this not only creates all modules, but also functions, it deals
    # also with its functions.

    assert module.isCompiledPythonModule(), module

    context = Contexts.PythonModuleContext(
        module=module,
        data_filename=data_filename,
    )

    context.setExceptionEscape("module_exception_exit")

    function_decl_codes = []
    function_body_codes = []

    for function_body in module.getUsedFunctions():
        # Constant function returners get no code.
        (
            is_constant_returning,
            _constant_return_value,
        ) = function_body.getConstantReturnValue()
        if is_constant_returning:
            continue

        function_code, function_decl = generateFunctionBodyCode(
            function_body=function_body, context=context
        )

        function_body_codes.append(function_code)

        if function_decl is not None:
            function_decl_codes.append(function_decl)

    # These are for functions used from other modules. Due to cyclic
    # dependencies, we cannot rely on those to be already created.
    for function_body in module.getCrossUsedFunctions():
        assert function_body.isCrossModuleUsed()

        function_decl = getFunctionDirectDecl(
            function_identifier=function_body.getCodeName(),
            closure_variables=function_body.getClosureVariables(),
            file_scope=getExportScopeCode(
                cross_module=function_body.isCrossModuleUsed()
            ),
            context=Contexts.PythonFunctionDirectContext(
                parent=context, function=function_body
            ),
        )

        function_decl_codes.append(function_decl)

    return getModuleCode(
        module=module,
        function_decl_codes=function_decl_codes,
        function_body_codes=function_body_codes,
        module_const_blob_name=deriveModuleConstantsBlobName(data_filename),
        context=context,
    )
Ejemplo n.º 2
0
def main():
    datacomposer_logger.is_quiet = (
        os.environ.get("NUITKA_DATACOMPOSER_VERBOSE", "0") != "1"
    )

    # Internal tool, most simple command line handling. This is the build directory
    # where main Nuitka put the .const files.
    build_dir = sys.argv[1]
    output_filename = sys.argv[2]

    const_files = scanConstFiles(build_dir)

    total = 0

    desc = []

    names = set()

    for fullpath, filename in const_files:
        datacomposer_logger.info("Working on constant file %r." % filename)

        with open(fullpath, "rb") as const_file:
            constants_reader = ConstantStreamReader(const_file)
            count, part = _writeConstantStream(constants_reader)
        total += count

        name = deriveModuleConstantsBlobName(filename)

        # Make sure that is not repeated.
        assert name not in names, name
        names.add(name)

        datacomposer_logger.info(
            "Storing %r chunk with %s values size %r." % (name, count, len(part))
        )

        if str is not bytes:
            # Encoding needs to match generated source code output.
            name = name.encode("latin1")

        desc.append((name, part))

    datacomposer_logger.info("Total amount of constants is %d." % total)

    _writeConstantsBlob(output_filename=output_filename, desc=desc)

    sys.exit(0)
Ejemplo n.º 3
0
def main():
    global crc32  # singleton, pylint: disable=global-statement

    datacomposer_logger.is_quiet = (os.environ.get(
        "NUITKA_DATACOMPOSER_VERBOSE", "0") != "1")

    # Internal tool, most simple command line handling. This is the build directory
    # where main Nuitka put the .const files.
    build_dir = sys.argv[1]
    output_filename = sys.argv[2]

    const_files = scanConstFiles(build_dir)

    total = 0

    desc = []

    names = set()

    for fullpath, filename in const_files:
        datacomposer_logger.info("Working on constant file %r." % filename)

        constants_reader = ConstantStreamReader(fullpath)
        count, part = _writeConstantStream(constants_reader)
        total += count

        name = deriveModuleConstantsBlobName(filename)

        # Make sure that is not repeated.
        assert name not in names, name
        names.add(name)

        datacomposer_logger.info("Storing %r chunk with %s values size %r." %
                                 (name, count, len(part)))

        if str is not bytes:
            # Encoding needs to match generated source code output.
            name = name.encode("latin1")

        desc.append((name, part))

    datacomposer_logger.info("Total amount of constants is %d." % total)

    with open(output_filename, "w+b") as output:
        output.write(b"\0" * 8)

        def write(data):
            global crc32  # singleton, pylint: disable=global-statement

            output.write(data)
            crc32 = binascii.crc32(data, crc32)

        for name, part in desc:
            write(name + b"\0")
            write(struct.pack("I", len(part)))
            write(part)

        data_size = output.tell() - 8

        if str is bytes:
            # Python2 is doing signed CRC32, but we want unsigned.
            crc32 %= 1 << 32

        output.seek(0)
        output.write(struct.pack("II", crc32, data_size))

        assert output.tell() == 8

        datacomposer_logger.info(
            "Total constants blob size without header %d." % data_size)
        datacomposer_logger.info("Total constants blob CRC32 is %d." % crc32)

    sys.exit(0)
Ejemplo n.º 4
0
    def deriveModuleConstantsBlobName(cls, data_filename):
        result = deriveModuleConstantsBlobName(data_filename)

        return cls.encodeDataComposerName(result)