text
stringlengths
12
786k
let unwrap_assoc = function | ` Assoc lst -> lst | _ -> raise ( WrongFormat " conf must be a valid JSON object " )
let parse_cmds cmds args = let mapper = function | ` String cmd -> cmd | _ -> raise ( WrongFormat " invalid cmd arg " ) in if List . length cmds > 0 then raise ( WrongFormat " duplicated cmds " ) else List . map mapper args
let get_cmds cmds = if List . length cmds = 0 then raise ( WrongFormat " cmds not given " ) else cmds
let cmdopt name = function | None -> raise ( WrongFormat ( name " ^ not given " ) ) | Some arg -> arg
let construct_conf id cmds filearg mratio_s mratio_e seed_file = let cmds = get_cmds cmds |> to_abs_cmds in let prog = List . hd cmds in let filearg = cmdopt " filearg " filearg in let mratio_s = cmdopt " mratiostart " mratio_s in let mratio_e = try cmdopt " mratioend " mratio_e with _ -> mratio_...
let parse_conf json id = let assoc = unwrap_assoc json in let rec parse_loop cmds filearg ratio_s ratio_e seedfile = function | ( " cmds " , ` List args ) :: tl -> parse_loop ( parse_cmds cmds args ) filearg ratio_s ratio_e seedfile tl | ( " filearg " , ` Int idx ) :: tl -> parse_lo...
let parse_json json = let per_conf acc obj = let conf = parse_conf obj ( List . length acc ) in conf :: acc in let lst = get_conflist json in List . fold_left per_conf [ ] lst
let parse file = if not ( Sys . file_exists file ) then raise Not_found else ( ) ; let ch = open_in file in let json = from_channel ch in close_in ch ; parse_json json
type function_settings = { function_name : string ; memory_size : int ; version : string ; log_stream : string ; log_group : string }
module Env_vars = struct let runtime_endpoint_var = " AWS_LAMBDA_RUNTIME_API " let lambda_function_name = " AWS_LAMBDA_FUNCTION_NAME " let lambda_function_version = " AWS_LAMBDA_FUNCTION_VERSION " let lambda_function_memory_size = " AWS_LAMBDA_FUNCTION_MEMORY_SIZE " let lambda_log_stream_name = " ...
let env = let env_lst = Array . to_list ( Unix . environment ( ) ) in List . fold_left ( fun m var -> match String . split_on_char ' ' = var with | k :: v :: _ -> StringMap . add k v m | _ -> m ) StringMap . empty env_lst
let get_runtime_api_endpoint ( ) = let var = Env_vars . runtime_endpoint_var in match StringMap . find_opt var env with | Some v -> Ok v | None -> Error ( Printf . sprintf " Could not find runtime API env var : % s " var )
let get_function_settings ( ? env = env ) ( ) = let get_env_vars ( ) = let function_name = StringMap . find_opt Env_vars . lambda_function_name env in let version = StringMap . find_opt Env_vars . lambda_function_version env in let memory_str = StringMap . find_opt Env_vars . lambda_funct...
let acknowledge_config config = List . iter ( fun ( _ , config ) -> List . iter Param_tags . acknowledge config . plus_tags ) config
let cache = Hashtbl . create 107
let ( configs , add_config ) = let configs = ref [ ] in ( fun ( ) -> ! configs ) , ( fun config -> acknowledge_config config ; configs := config :: ! configs ; Hashtbl . clear cache )
let parse_string s = let conf = Lexers . conf_lines None 1 ( Printf . sprintf " string : % S " s ) ( Lexing . from_string s ) in add_config conf
let parse_file ? dir file = try with_input_file file begin fun ic -> let conf = Lexers . conf_lines dir 1 ( Printf . sprintf " file : % S " file ) ( Lexing . from_channel ic ) in add_config conf end with Lexers . Error msg -> raise ( Lexers . Error ( file ^ " : " ^ msg ) )
let apply_config s ( config : t ) init = List . fold_left begin fun tags ( key , v ) -> if key_match key s then List . fold_right Tags . add v . plus_tags ( List . fold_right Tags . remove v . minus_tags tags ) else tags end init config
let apply_configs s = List . fold_right ( apply_config s ) ( configs ( ) ) Tags . empty
let tags_of_filename s = try Hashtbl . find cache s with Not_found -> let res = apply_configs s in let ( ) = Hashtbl . replace cache s res in res
let has_tag tag = Tags . mem tag ( tags_of_filename " " )
let tag_file file tags = if tags <> [ ] then parse_string ( Printf . sprintf " % S : % s " file ( String . concat " , " tags ) ) ; ;
let tag_any tags = if tags <> [ ] then parse_string ( Printf . sprintf " true : % s " ( String . concat " , " tags ) ) ; ;
type flag_action = SetFlag of string | ClearFlag of string
let getDigestKV ( ) = let digest = Project . digest ( ) in [ ( " obuild - digest " , digest ) ]
let generateMlFile project file flags = Utils . generateFile file ( fun add -> add " \ n " ; add ( sprintf " let project_name = " \% s " \\ n " project . Analyze . project_file . Project . name ) ; add ( sprintf " let project_version = " \% s " \\ n " project . Analy...
let generateCFile project file flags = Utils . generateFile file ( fun add -> add " /* autogenerated file by obuild . do not modify */\ n " ; add ( sprintf " # define PROJECT_NAME " \% s " \\ n " project . Analyze . project_file . Project . name ) ; add ( sprintf " # define...
let makeSetup digestKV project flags = hashtbl_fromList ( digestKV @ hashtbl_toList project . Analyze . project_ocamlcfg @ List . map ( fun ( opt , v ) -> ( opt , string_of_bool v ) ) ( Gconf . get_target_options ( ) ) @ List . map ( fun ( flagname , flagval ) -> ( " ...
let sanityCheck ( ) = let ( _ : string ) = Prog . getOcamlOpt ( ) in let ( _ : string ) = Prog . getOcamlC ( ) in let ( _ : string ) = Prog . getOcamlDep ( ) in ( )
let comparekvs reason setup l = List . iter ( fun ( k , v ) -> try let v ' = Hashtbl . find setup k in if v ' <> v then raise ( ConfigChanged reason ) with Not_found -> raise ( ConfigChanged reason ) ) l
let comparekvs_hashtbl reason setup l = Hashtbl . iter ( fun k v -> try let v ' = Hashtbl . find setup k in if v ' <> v then raise ( ConfigChanged reason ) with Not_found -> raise ( ConfigChanged reason ) ) l
let execute_configure_script proj_file = match proj_file . Project . configure_script with | None -> ( ) | Some script -> let args = [ ( Prog . getOcaml ( ) ) ; ( fp_to_string script ) ] in ( match Process . run args with | Process . Success ( _ , warnings , _ ) -> pr...
let create_dist project flags = verbose Verbose " configuration changed , deleting dist \ n " ; %! Filesystem . removeDirContent ( Dist . build_path ) ; Dist . remove_dead_links ( ) ; verbose Verbose " auto - generating configuration files \ n " ; %! let autogenDir = Dist . crea...
let get_assoc name assoc = try let v = List . assoc name assoc in Some v with Not_found -> None
let get_flags_value proj_file setup_flags user_flags = List . map ( fun flag -> let name = flag . Project . Flag . name in let def = flag . Project . Flag . default in let override = ref ( get_assoc name setup_flags ) in List . iter ( fun tw -> match tw with | ClearFlag s -> if s = name...
let check_extra_tools proj_file = let syspath = Utils . get_system_paths ( ) in List . iter ( fun tool -> try let _ = Utils . find_in_paths syspath tool in ( ) with Utils . FileNotFoundInPaths _ -> raise ( ToolNotFound tool ) ) proj_file . Project . extra_tools
let get_flags hash = Hashtbl . fold ( fun k v acc -> if string_startswith " flag " - k then ( string_drop 5 k , bool_of_string v ) :: acc else acc ) hash [ ]
let bool_of_opt hashtable k = let get_opt k = try Hashtbl . find hashtable k with Not_found -> raise ( ConfigurationMissingKey k ) in let v = get_opt k in try bool_of_string v with Failure _ -> raise ( ConfigurationTypeMismatch ( k , " bool " , v ) )
let set_opts hashtable = let opts = Gconf . get_target_options_keys ( ) in List . iter ( fun k -> Gconf . set_target_options k ( bool_of_opt hashtable k ) ) opts
let check_ocaml ( ) = let ocamlCfg = Prog . getOcamlConfig ( ) in let ocaml_ver = Hashtbl . find ocamlCfg " version " in let ver = string_split ' . ' ocaml_ver in ( match ver with | major :: minor :: _ -> ( if int_of_string major < 4 then gconf . bin_annot <- false ; if int_of_...
let run proj_file user_flags user_opts = Dist . create_maybe ( ) ; let _ = check_ocaml ( ) in let digestKV = getDigestKV ( ) in execute_configure_script proj_file ; let configure = try Some ( Dist . read_configure ( ) ) with _ -> None in let configure_flags = match configure with | ...
let check proj_file reconf setup = let ocamlCfg = check_ocaml ( ) in let digestKV = getDigestKV ( ) in comparekvs_hashtbl " ocaml config " setup ocamlCfg ; let reconfigure = try comparekvs " digest " setup digestKV ; false with e -> if reconf then true else raise e in let setup_flags = get_fla...
let set_endpoint_env_var ( ) = let open Config in Unix . putenv Env_vars . runtime_endpoint_var " localhost : 8080 " ; ( )
let set_lambda_env_vars ( ) = let open Config in Unix . putenv Env_vars . lambda_function_name " test_func " ; Unix . putenv Env_vars . lambda_function_version " $ LATEST " ; Unix . putenv Env_vars . lambda_function_memory_size " 128 " ; Unix . putenv Env_vars . lambda_log_strea...
let unset_env_vars ( ) = let open Config in Unix . putenv Env_vars . runtime_endpoint_var " " ; Unix . putenv Env_vars . lambda_function_name " " ; Unix . putenv Env_vars . lambda_function_version " " ; Unix . putenv Env_vars . lambda_function_memory_size " " ; Unix . pute...
let get_env ( ) = let env_lst = Array . to_list ( Unix . environment ( ) ) in List . fold_left ( fun m var -> match String . split_on_char ' ' = var with | k :: v :: _ -> StringMap . add k v m | _ -> m ) StringMap . empty env_lst
let setup_and_run f ( ) = unset_env_vars ( ) ; set_endpoint_env_var ( ) ; set_lambda_env_vars ( ) ; f ( )
let suite = [ ( " config from env vars " , ` Quick , setup_and_run @@ fun ( ) -> match Config . get_function_settings ~ env ( : get_env ( ) ) ( ) with | Ok env_settings -> Alcotest . ( check int " memory size read from env " 128 env_settings . Config . memory_size ) ...
type t = ( string , Digest . t * string ) Hashtbl . t
let create ( ) = Hashtbl . create 13
let check tbl name crc source = try let ( old_crc , old_source ) = Hashtbl . find tbl name in if crc <> old_crc then raise ( Inconsistency ( name , source , old_source ) ) with Not_found -> Hashtbl . add tbl name ( crc , source )
let check_noadd tbl name crc source = try let ( old_crc , old_source ) = Hashtbl . find tbl name in if crc <> old_crc then raise ( Inconsistency ( name , source , old_source ) ) with Not_found -> raise ( Not_available name )
let set tbl name crc source = Hashtbl . add tbl name ( crc , source )
let source tbl name = snd ( Hashtbl . find tbl name )
let extract tbl = Hashtbl . fold ( fun name ( crc , auth ) accu -> ( name , crc ) :: accu ) tbl [ ]
let filter p tbl = let to_remove = ref [ ] in Hashtbl . iter ( fun name ( crc , auth ) -> if not ( p name ) then to_remove := name :: ! to_remove ) tbl ; List . iter ( fun name -> while Hashtbl . mem tbl name do Hashtbl . remove tbl name done ) ! to_remove
module Id = struct let name = " Camlp4Parsers . LoadCamlp4Ast " let version = Sys . ocaml_version end
module Make ( Ast : Camlp4 . Sig . Camlp4Ast . S ) = struct module Ast = Ast open Ast let _loc = Loc . ghost let parse_implem ? directive_handler ( : _ ) _ _ = let e = Ast . ExApp ( _loc , Ast . ExApp ( _loc , Ast . ExId ( _loc , Ast . IdAcc ( _loc , Ast . IdUid...
let module M = Camlp4 . Register . OCamlParser ( Id ) ( Make ) in ( )
let char_zero = Char . to_int ' 0 '
let char_a = Char . to_int ' a '
let char_A = Char . to_int ' A '
module Signedness = struct type t = | Signed | Unsigned [ @@ deriving sexp_of ] end
module Raw = struct let of_bytes bytes ~ width = init_byte ~ width ~ f ( : fun i -> try Bytes . get bytes i with | _ -> ' \ 000 ' ) ; ; let of_string string ~ width = init_byte ~ width ~ f ( : fun i -> try string . [ i ] with | _ -> ' \ 000 ' ) ; ; let to_bytes ( ...
let rec rsplit_into_n ~ n string = let len = String . length string in if len <= n then [ string ] else String . drop_prefix string ( len - n ) :: rsplit_into_n ~ n ( String . drop_suffix string n ) ; ;
let int_of_hex_char c = match c with | ' 0 ' . . ' 9 ' -> Char . to_int c - char_zero | ' a ' . . ' f ' -> Char . to_int c - char_a + 10 | ' A ' . . ' F ' -> Char . to_int c - char_A + 10 | _ -> raise_s [ % message " Invalid hex char " ~ _ ( : c ...
let hex_char_of_int i = if i >= 0 && i <= 9 then Char . of_int_exn ( i + char_zero ) else if i >= 10 && i <= 15 then Char . of_int_exn ( i - 10 + char_a ) else raise_s [ % message " Cannot convert int to hex char " ~ int_value ( : i : int ) ] ; ;
let to_hum x = rsplit_into_n ~ n : 4 x |> List . rev |> String . concat ~ sep " : _ "
let to_binary_string t = let width = width t in String . init width ~ f ( : fun j -> let j = width - j - 1 in let byte = j lsr 3 in let bit = j land 7 in let char = unsafe_get_byte t byte |> Char . to_int in let bit = ( char land ( 1 lsl bit ) ) lsr bit in Char . of_int_exn ( bit + c...
let to_binary_string_hum t = to_binary_string t |> to_hum
let to_int64 t = unsafe_get_int64 t 0
let to_int t = Int64 . to_int_trunc ( to_int64 t )
let to_int32 t = Int64 . to_int32_trunc ( to_int64 t )
let to_int64_array t = Array . init ( words t ) ~ f ( : fun i -> unsafe_get_int64 t i )
let to_z ~ signedness t = let module Z = Zarith . Z in let words = words t in let rec f word z = if word < 0 then z else ( let w = unsafe_get_int64 t word in let a = Int64 . ( w lsr 32 ) in let z = Z . ( logor ( shift_left z 32 ) ( of_int64 a ) ) in let a = Int64 . ( w land 0xFF...
let to_hex_string ~ signedness t = let rec of_binary ( ~ signedness : Signedness . t ) s = let hex_of_bin s = match s with | " 0000 " -> " 0 " | " 0001 " -> " 1 " | " 0010 " -> " 2 " | " 0011 " -> " 3 " | " 0100 " -> " 4 " | " 0101 " -> " ...
let of_hum x = String . filter x ~ f ( : fun c -> not Char . ( c = ' _ ' ) )
let of_binary_string b = if String . length b = 0 then raise_s [ % message " [ Constant . of_binary_string ] input string is empty " ] else ( let width = String . length b in let rec to_int8 b acc offset count = if count = 0 then Char . of_int_exn acc else ( let char = b . [ offset ...
let of_binary_string_hum b = of_binary_string ( of_hum b )
let of_int64 ~ width i = let sign_extend_value = if Int64 . ( i < 0L ) then - 1L else 0L in init_int64 ~ width ~ f ( : function | 0 -> i | _ -> sign_extend_value ) ; ;
let of_int ~ width i = of_int64 ~ width ( Int64 . of_int i )
let of_int32 ~ width i = of_int64 ~ width ( Int64 . of_int32 i )
let of_int64_array ~ width a = let array_width = Array . length a lsl log_bits_per_word in if array_width > Int . round_up ~ to_multiple_of : bits_per_word width then raise_s [ % message " [ of_int64_array ] array to large for given [ width ] " ( array_width : int ) ( width : int ) ] ...
let z32mask = Zarith . Z . of_int64 0xFFFF_FFFFL
let of_z ~ width z = let module Z = Zarith . Z in let t = create width in let rec f i z width = if width <= 0 || Z . ( equal z zero ) then ( ) else ( let a = Z . ( to_int64 ( logand z z32mask ) ) in let z = Z . shift_right z 32 in let b = Z . ( to_int64 ( logand z z32mask ) ...
let of_pow2_string ( ~ signedness : Signedness . t ) ~ width ~ pow2 h = let to_binary t = let len = String . length t in let len_pow = len * pow2 in let rec make_string = function | 0 -> " " | i -> let c = t . [ i - 1 ] in let digit = match c with | ' 0 ' . . ' 9 ' -...
let of_hex_string ~ signedness ~ width h = of_pow2_string ~ signedness ~ width ~ pow2 : 4 h
let of_octal_string ~ signedness ~ width h = of_pow2_string ~ signedness ~ width ~ pow2 : 3 h
module type Bit = sig type t val vdd : t val gnd : t val equal : t -> t -> bool end
module Make_bit_list ( Bit : Bit ) = struct let to_constant bits = List . map bits ~ f ( : fun bit -> if Bit . equal bit Bit . vdd then ' 1 ' else ' 0 ' ) |> String . of_char_list |> of_binary_string ; ; let of_constant t = to_binary_string t |> String . to_list |> List . ma...
module Bits = Make_bit_list ( struct type t = int let vdd = 1 let gnd = 0 let equal = Int . equal end )
let pp fmt t = Caml . Format . fprintf fmt " % s " ( to_binary_string_hum t )
module _ = Pretty_printer . Register ( struct type nonrec t = t let module_name = " Hardcaml . Constant " let to_string t = to_binary_string_hum t end )
let options = [ ( " auto . offset . reset " , " earliest " ) ]
let main ( brokers , ( topic , topics ) , group_id ) = let open Deferred . Result . Let_syntax in Log . Global . debug " Starting " ; let options = ( " metadata . broker . list " , brokers ) :: ( " group . id " , group_id ) :: options in let % bind consumer = ...
let main_or_error opts = match % bind main opts with | Ok _ as v -> return v | Error ( _ , msg ) -> return @@ Error ( Error . of_string msg )
let ( ) = let open Command . Let_syntax in Command . async_or_error ~ summary " : Consume messages on Kafka topic " [ % map_open let _ = Log . Global . set_level_via_param ( ) and topics = flag " topic " ( one_or_more string ) ~ doc " : NAME Which topics to consume from " and ...
module Pair ( A : Thing ) ( B : Thing ) : Thing with type t = A . t * B . t = struct type t = A . t * B . t let compare ( a1 , b1 ) ( a2 , b2 ) = let c = A . compare a1 a2 in if c <> 0 then c else B . compare b1 b2 let hash ( a , b ) = Hashtbl . hash ( A . hash a...
module Make_map ( T : Thing ) ( Set : Set_plus_stdlib with type elt = T . t ) = struct include Map . Make [ @ inlined hint ] ( T ) module Set = Set let of_list l = List . fold_left ( fun map ( id , v ) -> add id v map ) empty l let disjoint_union ? eq ? print m1 m2 = ignore pr...