Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file owl_zoo_ver.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173(*
* OWL - OCaml Scientific and Engineering Computing
* Copyright (c) 2016-2019 Liang Wang <liang.wang@cl.cam.ac.uk>
*)lethtb=Owl_zoo_path.htb(** run system command and return the result as string *)letsyscallcmd=letic,oc=Unix.open_processcmdinletbuf=Buffer.create50in(trywhiletruedoBuffer.add_channelbufic1donewithEnd_of_file->());Unix.close_process(ic,oc)|>ignore;Buffer.contentsbuf(**
Version information of gists is saved as key-value pairs in Hash table:
key: gid; value: (version list, timestamp).
*)letcreate_htb()=lettb=Hashtbl.create128inHashtbl.addtb""([|""|],0.);Owl_io.marshal_to_filetbhtb(**
Try to get the value of key `gid`; if not found, return default values with
a `true` flag.
*)letget_value(gid:string)=ifnot(Sys.file_existshtb)thencreate_htb();lettb=Owl_io.marshal_from_filehtbinif(gid="")then([|""|],0.,tb,true)elsetryletv,ts=Hashtbl.findtbgidinv,ts,tb,falsewithNot_found->[|""|],0.,tb,true(** Get the timestamp of the latest version of a gist; return 0. if the gist
does not exist. *)letget_timestamp(gid:string)=let_,ts,_,miss_flag=get_valuegidinifmiss_flag=falsethentselse0.(**
Get the most up-to-date gist version from Gist server; return "" if the gid
is not found or network error happens.
*)letget_remote_vid(gid:string)=letcmd="curl https://api.github.com/gists/"^gidinlets=syscallcmdinletr=Str.regexp"\"version\":[ \n\r\t]+\"\\([0-9a-z]+\\)\""intry(Str.search_forwardrs0|>ignore;Str.matched_group1s)withNot_found->""(** Get the latest version downloaded on local machine; if the local version is
not found on record, get the newest vid from Gist server. *)letget_latest_vid(gid:string)(tol:float)=letv,ts,tb,miss_flag=get_valuegidinlett=Unix.time()inifmiss_flag=false&&(t-.ts)<tolthen(assert(Array.lengthv>0);Array.getv(Array.lengthv-1))else(Owl_log.debug"owl-zoo: Gist %s exceeds time tolerence %f; fetching newest vid from server"gidtol;Hashtbl.replacetbgid(v,t);Owl_io.marshal_to_filetbhtb;get_remote_vidgid)(** Check if a specific version of a gist exists on the record. *)letexist(gid:string)(vid:string)=letv,_,_,miss_flag=get_valuegidinifmiss_flag=falsethenArray.memvidvelsefalse(**
Add a specific version of a gist to the record; if this version already
exists, do nothing; if this gist does not exist, create a new item.
*)letupdate(gid:string)(vid:string)=letv,_,tb,miss_flag=get_valuegidinif(miss_flag=false)then(ifnot(Array.memvidv)then(letv'=Array.appendv[|vid|]inletts=Unix.time()inHashtbl.replacetbgid(v',ts);Owl_io.marshal_to_filetbhtb;)else(Owl_log.debug"owl-zoo: Gist %s/%s already exists in the record"gidvid))else(letv=[|vid|]inletts=Unix.time()inHashtbl.addtbgid(v,ts);Owl_io.marshal_to_filetbhtb;)(** Remove a gist's record. *)letremove(gid:string)=let_,_,tb,miss_flag=get_valuegidinifmiss_flag=falsethen(Hashtbl.removetbgid;Owl_io.marshal_to_filetbhtb;)else(Owl_log.debug"owl-zoo: Gist %s not found in the record"gid)(* TODO: richer time format *)letto_timestamptime_str=tryfloat_of_stringtime_strwithFailure_->raiseOwl_exception.ZOO_ILLEGAL_GIST_NAME(**
Parse a full gist name scheme string and return a gist id, a version id, a
float value indicating the time tolerance for this gist, and a bool
flag indicating if `pin` is set to true in the gist name.
*)letparse_gist_stringgist=letvalidate_lenslen=if((String.lengths)=len)thenselseraiseOwl_exception.ZOO_ILLEGAL_GIST_NAMEinletsplit?(delim=" ")str=letregex=Str.regexpdeliminStr.split_delimregexstrinletlst=split~delim:"?"("gid="^gist)inletparams=Hashtbl.create5inList.iter(funp->letkv=split~delim:"="p|>Array.of_listinHashtbl.addparamskv.(0)kv.(1))lst;letgid=Hashtbl.findparams"gid"inlettol=tryHashtbl.findparams"tol"|>to_timestampwithNot_found->infinityinletvid=tryHashtbl.findparams"vid"withNot_found->get_latest_vidgidtolinletpin=tryHashtbl.findparams"pin"|>bool_of_stringwith|Not_found->false|Invalid_argument_->raiseOwl_exception.ZOO_ILLEGAL_GIST_NAMEin(validate_lengid32),(validate_lenvid40),tol,pin