<commit_before>#!/usr/bin/env python
"""
Compiler tool.
"""

from optparse import OptionParser
import sys
import os
import fnmatch

# Parse command line arguments
usage = "usage: %prog [options] MODEL_FOLDER MODEL_NAME"
parser = OptionParser(usage)
parser.add_option("-c", "--casadi", dest="casadi_folder",
                  help="CasADi installation folder")
parser.add_option("-f", "--flatten_only",
                  action="store_true", dest="flatten_only")
(options, args) = parser.parse_args()
if len(args) != 2:
    parser.error("incorrect number of arguments")

model_folder = args[0]
model_name = args[1]

# Set CasADi installation folder
if options.casadi_folder is not None:
    sys.path.append(options.casadi_folder)

# Import rest of pymola
from . import parser, tree
if not options.flatten_only:
    from . import gen_casadi

# Load folder
ast = None
for root, dir, files in os.walk(model_folder):
    for items in fnmatch.filter(files, "*.mo"):
        with open(os.path.join(root, items), 'r') as f:
            if ast is None:
                ast = parser.parse(f.read())
            else:
                ast.extend(parser.parse(f.read()))

# Compile
if options.flatten_only:
    ast = tree.flatten(ast, model_name)
    print(ast)
else:
    gen_casadi.generate(ast, model_name)<commit_msg>Do code generation using casadi<commit_after>#!/usr/bin/env python
"""
Compiler tool.
"""

from optparse import OptionParser
import sys
import os
import fnmatch

# Parse command line arguments
usage = "usage: %prog [options] MODEL_FOLDER MODEL_NAME"
parser = OptionParser(usage)
parser.add_option("-c", "--casadi", dest="casadi_folder",
                  help="CasADi installation folder")
parser.add_option("-f", "--flatten_only",
                  action="store_true", dest="flatten_only")
(options, args) = parser.parse_args()
if len(args) != 2:
    parser.error("incorrect number of arguments")

model_folder = args[0]
model_name = args[1]

# Set CasADi installation folder
if options.casadi_folder is not None:
    sys.path.append(options.casadi_folder)

# Import rest of pymola
from . import parser, tree
if not options.flatten_only:
    from . import gen_casadi
    import casadi as ca

# Load folder
ast = None
for root, dir, files in os.walk(model_folder):
    for items in fnmatch.filter(files, "*.mo"):
        with open(os.path.join(root, items), 'r') as f:
            if ast is None:
                ast = parser.parse(f.read())
            else:
                ast.extend(parser.parse(f.read()))

# Compile
if options.flatten_only:
    ast = tree.flatten(ast, model_name)
    print(ast)
else:
    model = gen_casadi.generate(ast, model_name)
    f = model.get_function()

    # Generate C code
    cg = ca.CodeGenerator(model_name)
    cg.add(f)
    cg.add(f.forward(1))
    cg.add(f.reverse(1))
    cg.add(f.reverse(1).forward(1))
    cg.generate()

    file_name = model_name + '.c'

    # Compile shared library
    if os.name == 'posix':
        ext = 'so'
    else:
        ext = 'dll'
    try:
        os.system("clang -shared {} -o lib{}.{}".format(file_name, model_name, ext))
    except:
        raise
    finally:
        os.remove(file_name)
{'fraction_non_alphanumeric': 0.12755905511811025, 'fraction_numerical': 0.0031496062992125984, 'mean_word_length': 3.061381074168798, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5902440', 'n_tokens_mistral': 1064, 'n_tokens_neox': 1039, 'n_words': 271}