def start_mxbuild_server(local_path, runtime_version, java_version):
    cache = "/tmp/downloads"  # disable caching here, not in compile step
    mono_location = mono.ensure_and_get_mono(runtime_version, cache)
    mono_env = mono.get_env_with_monolib(mono_location)
    path = os.path.join(os.getcwd(), "runtimes", str(runtime_version))
    if not os.path.isdir(os.path.join(path, "modeler")):
        ensure_mxbuild_in_directory(os.path.join(local_path, "mxbuild"),
                                    runtime_version, cache)
        path = os.path.join(local_path, "mxbuild")

    jvm_location = java.ensure_and_get_jvm(java_version,
                                           cache,
                                           local_path,
                                           package="jdk")
    subprocess.Popen(
        [
            os.path.join(mono_location, "bin/mono"),
            "--config",
            os.path.join(mono_location, "etc/mono/config"),
            os.path.join(path, "modeler", "mxbuild.exe"),
            "--serve",
            "--port=6666",
            "--java-home=%s" % jvm_location,
            "--java-exe-path=%s/bin/java" % jvm_location,
        ],
        env=mono_env,
    )
def stage(build_path, cache_path, local_path, runtime_version, java_version):
    mono_location = mono.ensure_and_get_mono(runtime_version, cache_path)
    logging.info("Mono available: %s", mono_location)
    mono_env = mono.get_env_with_monolib(mono_location)

    mxbuild_location = os.path.join(local_path, "mxbuild")

    ensure_mxbuild_in_directory(mxbuild_location, runtime_version, cache_path)

    jvm_location = java.ensure_and_get_jvm(java_version, cache_path,
                                           local_path)

    util.lazy_remove_file(BUILD_ERRORS_JSON)

    args = [
        os.path.join(mono_location, "bin/mono"),
        "--config",
        os.path.join(mono_location, "etc/mono/config"),
        os.path.join(mxbuild_location, "modeler/mxbuild.exe"),
        "--target=package",
        "--output=/tmp/model.mda",
        "--java-home=%s" % jvm_location,
        "--java-exe-path=%s" % os.path.join(jvm_location, "bin/java"),
    ]

    if runtime_version >= 6.4 or os.environ.get("FORCE_WRITE_BUILD_ERRORS"):
        args.append("--write-errors=%s" % BUILD_ERRORS_JSON)
        logging.debug("Will write build errors to %s", BUILD_ERRORS_JSON)

    if os.environ.get("FORCED_MXBUILD_URL"):
        args.append("--loose-version-check")
        logging.warning(
            "Using forced mxbuild version, the model will be converted")

    args.append(util.get_mpr_file_from_dir(build_path))

    try:
        logging.debug("subprocess call %s", args)
        subprocess.check_call(args, env=mono_env)
    except subprocess.CalledProcessError as ex:
        buildstatus_callback(BUILD_ERRORS_JSON)
        raise RuntimeError(ex)

    for file_name in os.listdir(build_path):
        filepath = os.path.join(build_path, file_name)
        if file_name != ".local":
            if os.path.isdir(filepath):
                shutil.rmtree(filepath)
            else:
                os.unlink(filepath)

    zf = zipfile.ZipFile("/tmp/model.mda")
    try:
        zf.extractall(build_path)
    finally:
        zf.close()

    try:
        with open(os.path.join(build_path, ".sourcepush"), "w") as dsp:
            dsp.write("sourcepush")
    except OSError as ex:
        logging.warning("Could not write source push indicator: %s", str(ex))