Пример #1
0
    def conf_download():

        # Conf file name
        of = 'conf.yaml'

        # Input parameters
        inputs = {'output_file': of}

        # Dispatcher
        d = dsp.register()
        ret = d.dispatch(inputs, ['conf', 'done'])

        # Read from file
        data = None
        with open(of, "rb") as conf_yaml:
            data = conf_yaml.read()

        # Output xls file
        iofile = io.BytesIO(data)
        iofile.seek(0)
        return send_file(
            iofile,
            attachment_filename="conf.yaml",
            as_attachment=True,
        )
Пример #2
0
    def download_template():

        # Temp file name
        of = next(tempfile._get_candidate_names())

        # Input parameters
        inputs = {"output_file": of, "template_type": "input"}

        # Dispatcher
        d = dsp.register()
        ret = d.dispatch(inputs, ["template", "done"])

        # Read from file
        data = None
        with open(of, "rb") as xlsx:
            data = xlsx.read()

        # Delete files
        os.remove(of)

        # Output xls file
        iofile = io.BytesIO(data)
        iofile.seek(0)
        return send_file(
            iofile,
            attachment_filename="co2mpas-input-template.xlsx",
            as_attachment=True,
        )
Пример #3
0
    def conf_template():

        # Temp file name
        of = next(tempfile._get_candidate_names())

        # Input parameters
        inputs = {"output_file": of}

        # Dispatcher
        d = dsp.register()
        ret = d.dispatch(inputs, ["conf", "done"])

        # Read from file
        data = None
        with open(of, "rb") as conf:
            data = conf.read()

        # Delete files
        os.remove(of)

        # Output xls file
        iofile = io.BytesIO(data)
        iofile.seek(0)
        return send_file(
            iofile,
            attachment_filename="conf.yaml",
            as_attachment=True,
        )
Пример #4
0
    def run_process(args):

        thread = threading.current_thread()
        files = [
            "input/" + f for f in listdir_inputs("input") if isfile(join("input", f))
        ]

        # Create output directory for this execution
        output_folder = "output/" + str(thread.ident)
        os.makedirs(output_folder or ".", exist_ok=True)

        # Dedicated logging for this run
        fileh = logging.FileHandler(
            "output/" + str(thread.ident) + "/" + "logfile.txt", "a"
        )
        formatter = logging.Formatter(
            "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
        )
        fileh.setFormatter(formatter)
        log = logging.getLogger()
        log.setLevel(logging.DEBUG)
        for hdlr in log.handlers[:]:
            log.removeHandler(hdlr)
        log.addHandler(fileh)

        # Input parameters
        kwargs = {
            "output_folder": output_folder,
            "only_summary": bool(args.get("only_summary")),
            "hard_validation": bool(args.get("hard_validation")),
            "declaration_mode": bool(args.get("declaration_mode")),
            "encryption_keys": "",
            "sign_key": "",
            "encryption_keys_passwords": "",
            "enable_selector": False,
            "type_approval_mode": bool(args.get("tamode")),
        }

        inputs = dict(
            plot_workflow=False,
            host="127.0.0.1",
            port=4999,
            cmd_flags=kwargs,
            input_files=files,
        )

        # Dispatcher
        d = dsp.register()
        ret = d.dispatch(inputs, ["done", "run"])
        f = open("output/" + str(thread.ident) + "/result.dat", "w+")
        f.write(str(ret))
        return ""
Пример #5
0
    def conf_generate():

        # Conf file name
        of = "conf.yaml"

        # Input parameters
        inputs = {"output_file": of}

        # Dispatcher
        d = dsp.register()
        ret = d.dispatch(inputs, ["conf", "done"])

        return redirect("/conf/configuration-form", code=302)
Пример #6
0
    def demo_download():

        # Temporary output folder
        of = next(tempfile._get_candidate_names())

        # Input parameters
        inputs = {'output_folder': of}

        # Dispatcher
        d = dsp.register()
        ret = d.dispatch(inputs, ['demo', 'done'])

        # List of demo files created
        demofiles = [f for f in listdir(of) if isfile(join(of, f))]

        # Create zip archive on the fly
        zip_subdir = of
        iofile = io.BytesIO()
        zf = zipfile.ZipFile(iofile,
                             mode='w',
                             compression=zipfile.ZIP_DEFLATED)

        # Adds demo files to archive
        for f in demofiles:
            # Add file, at correct path
            zf.write(os.path.abspath(os.path.join(of, f)), f)

        # Close archive
        zf.close()

        # Remove temporary files
        shutil.rmtree(of)

        # Output zip file
        iofile.seek(0)
        return send_file(iofile,
                         attachment_filename='co2mpas-demo.zip',
                         as_attachment=True)
Пример #7
0
def run_process(args, sid):
    """Run the simulation process in a thread"""

    # Pick current thread
    process = multiprocessing.current_process()
    run_id = '-'.join([sid, str(process.pid)])

    # Create output directory for this execution
    output_folder = co2wui_fpath("output", run_id)
    os.makedirs(output_folder or ".", exist_ok=True)

    # File list
    files = listdir_inputs("input")

    # Remove excluded files
    exclude_list = args.get("exclude_list").split("|")
    if exclude_list != ['']:
      excluded = list(map(int, exclude_list))
      for f in reversed(sorted(excluded)):
        del files[int(f) - 1]

    # Dump to file
    with open(
        co2wui_fpath("output", run_id, "files.dat"), "wb"
    ) as files_list:
        dill.dump(files, files_list)

    # Dedicated logging for this run
    fileh = logging.FileHandler(
        co2wui_fpath("output", run_id, "logfile.txt"), "a"
    )
    logger = logging.getLogger()
    logger.setLevel(logging.INFO)
    frmt = "%(asctime)-15s:%(levelname)5.5s:%(name)s:%(message)s"
    logging.basicConfig(level=logging.INFO, format=frmt)
    logger.addHandler(fileh)

    # Input parameters
    kwargs = {
        "output_folder": output_folder,
        "only_summary": bool(args.get("only_summary")),
        "hard_validation": bool(args.get("hard_validation")),
        "declaration_mode": bool(args.get("declaration_mode")),
        "encryption_keys": str(enc_keys_fpath()) if enc_keys_fpath().exists() else "",
        "sign_key": str(key_sign_fpath()) if bool(args.get("tamode")) else "",
        "enable_selector": False,
        "type_approval_mode": bool(args.get("tamode")),
    }

    if (conf_fpath().exists() and bool(args.get("custom_conf"))):
      kwargs["model_conf"] = conf_fpath()

    with open(
        co2wui_fpath("output", run_id, "header.dat"), "wb"
    ) as header_file:
        dill.dump(kwargs, header_file)

    inputs = dict(
        logger=logger,
        plot_workflow=False,
        host="127.0.0.1",
        port=4999,
        cmd_flags=kwargs,
        input_files=[str(f) for f in files],
        **{sh.START: kwargs},
    )

    # Dispatcher
    d = dsp.register()

    d.add_function("pass_logger", sh.bypass, inputs=["logger"], outputs=["core_model"])
    d.add_data("core_model", function=register_logger, wait_inputs=True)

    n = d.get_node("register_core", node_attr=None)[0]
    n["filters"] = n.get("filters", [])
    n["filters"].append(log_phases)

    ret = d.dispatch(inputs, ["done", "run", "core_model"])
    with open(
        co2wui_fpath("output", run_id, "result.dat"), "wb"
    ) as summary_file:
        dill.dump(ret["summary"], summary_file)
    return ""
Пример #8
0
    def run_process(args):

        thread = threading.current_thread()
        files = [
            osp.join("input", f) for f in listdir_inputs("input")
            if osp.isfile(osp.join("input", f))
        ]

        # Create output directory for this execution
        output_folder = osp.join("output", str(thread.ident))
        os.makedirs(output_folder or ".", exist_ok=True)

        # Dedicated logging for this run
        fileh = logging.FileHandler(
            osp.join("output", str(thread.ident), "logfile.txt"), "a")
        logger = logging.getLogger()
        logger.setLevel(logging.INFO)
        frmt = "%(asctime)-15s:%(levelname)5.5s:%(name)s:%(message)s"
        logging.basicConfig(level=logging.INFO, format=frmt)
        logger.addHandler(fileh)

        # Input parameters
        kwargs = {
            "output_folder":
            output_folder,
            "only_summary":
            bool(args.get("only_summary")),
            "hard_validation":
            bool(args.get("hard_validation")),
            "declaration_mode":
            bool(args.get("declaration_mode")),
            "encryption_keys":
            "keys/dice.co2mpas.keys"
            if os.path.exists("keys/dice.co2mpas.keys") else "",
            "sign_key":
            "keys/sign.co2mpas.key"
            if os.path.exists("keys/sign.co2mpas.key") else "",
            "encryption_keys_passwords":
            "",
            "enable_selector":
            False,
            "type_approval_mode":
            bool(args.get("tamode")),
        }

        with open(osp.join("output", str(thread.ident), "header.dat"),
                  "wb") as header_file:
            pickle.dump(kwargs, header_file)

        inputs = dict(
            plot_workflow=False,
            host="127.0.0.1",
            port=4999,
            cmd_flags=kwargs,
            input_files=files,
        )

        # Dispatcher
        d = dsp.register()
        ret = d.dispatch(inputs, ["done", "run"])
        with open(osp.join("output", str(thread.ident), "result.dat"),
                  "wb") as summary_file:
            pickle.dump(ret["summary"], summary_file)
        return ""