123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131(**************************************************************************)(* *)(* This file is part of Frama-C. *)(* *)(* Copyright (C) 2007-2023 *)(* CEA (Commissariat à l'énergie atomique et aux énergies *)(* alternatives) *)(* *)(* you can redistribute it and/or modify it under the terms of the GNU *)(* Lesser General Public License as published by the Free Software *)(* Foundation, version 2.1. *)(* *)(* It is distributed in the hope that it will be useful, *)(* but WITHOUT ANY WARRANTY; without even the implied warranty of *)(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *)(* GNU Lesser General Public License for more details. *)(* *)(* See the GNU Lesser General Public License version 2.1 *)(* for more details (enclosed in the file licenses/LGPLv2.1). *)(* *)(**************************************************************************)(* Only Compiled when package Zmq is installed *)(* No interface, registered via side-effects *)(* -------------------------------------------------------------------------- *)(* --- ZeroMQ Server Options --- *)(* -------------------------------------------------------------------------- *)moduleSenv=Server_parametersletbatch_group=Senv.add_group"Protocol BATCH"let()=Parameter_customize.set_groupbatch_groupmoduleBatch=Senv.String_list(structletoption_name="-server-batch"letarg_name="file.json,..."lethelp="Executes all requests in each <file.json>, and save the \
associated results in <file.out.json>."end)let()=Parameter_customize.set_groupbatch_grouplet()=Parameter_customize.do_not_save()moduleBatchOutputDir=Senv.Empty_string(structletoption_name="-server-batch-output-dir"letarg_name="path"lethelp="Outputs the results of -server-batch in <path> instead of the input \
directory."end)let()=Server_doc.protocol~title:"Batch Protocol"~readme:"server_batch.md"(* -------------------------------------------------------------------------- *)(* --- Execute JSON --- *)(* -------------------------------------------------------------------------- *)moduleJs=Yojson.BasicmoduleJu=Yojson.Basic.Utilletpretty=Js.pretty_print~std:falseletexecute_commandjs=letrequest=Ju.member"request"js|>Ju.to_stringinletid=Ju.member"id"jsinletdata=Ju.member"data"jsinmatchMain.findrequestwith|None->Senv.error"[batch] %a: request %S not found"prettyidrequest;`Assoc["id",id;"error",`String"request not found"]|Some(kind,handler)->trySenv.feedback"[%a] %s"Main.pp_kindkindrequest;`Assoc["id",id;"data",handlerdata]withData.InputError(msg)->Senv.error"[%s] %s@."requestmsg;`Assoc["id",id;"error",`Stringmsg;"at",js]letrecexecute_batchjs=matchjswith|`Null->`Null|`Listjs->`List(List.mapexecute_batchjs)|js->tryexecute_commandjswithJu.Type_error(msg,js)->Senv.error"[batch] incorrect encoding:@\n%s@\n@[<hov 2>At: %a@]@."msgprettyjs;`Null(* -------------------------------------------------------------------------- *)(* --- Execute the Scripts --- *)(* -------------------------------------------------------------------------- *)letexecute()=beginletfiles=Batch.get()inBatch.clear();(* clear in any case *)List.iterbeginfunfile->Senv.feedback"Script %S"file;letresponse=tryexecute_batch(Js.from_filefile)withYojson.Json_errormsg->Senv.error"[batch] error in JSON file:@\n%s@."msg;`Nullinletoutput=Filename.remove_extensionfile^".out.json"inletoutput=matchBatchOutputDir.get()with|""->output|dir->Filename.(dir^dir_sep^basenameoutput)inSenv.feedback"Output %S"output;letout=open_outoutputinJs.pretty_to_channeloutresponse;output_charout'\n';close_outoutendfilesend(* -------------------------------------------------------------------------- *)(* --- Run the Server from the Command line --- *)(* -------------------------------------------------------------------------- *)let()=Db.Main.extendexecute(* -------------------------------------------------------------------------- *)