text
stringlengths
0
601k
let char = kconv ~ docv " : CHAR " ~ kind " : a character " String . to_char Fmt . char
let bool = kconv ~ docv " : BOOL " ~ kind " ` : true ' or ` false ' " String . to_bool Fmt . bool
let int = kconv ~ docv " : INT " ~ kind " : an integer " String . to_int Fmt . int
let nativeint = kconv ~ docv " : INT " ~ kind " : a native integer " String . to_nativeint Fmt . nativeint
let int32 = kconv ~ docv " : INT32 " ~ kind " : a 32 - bit integer " String . to_int32 Fmt . int32
let int64 = kconv ~ docv " : INT64 " ~ kind " : a 64 - bit integer " String . to_int64 Fmt . int64
let float = kconv ~ docv " : FLOAT " ~ kind " : a float " String . to_float Fmt . float
let enum enum = if enum = [ ] then invalid_arg " empty enumeration " else let parse s = try Ok ( List . assoc s enum ) with | Not_found -> let alts = List . map ( fun ( a , _ ) -> strf " % a " ( quote Fmt . string ) a ) enum in Error ( err_invalid s ( strf " one of % s " ( String . concat ~ sep " , : " alts ) ) ) in le...
let parse_split ( ? sep = " , " ) s parse = let rec loop acc = function | s :: ss -> Result . bind ( parse s ) @@ fun v -> loop ( v :: acc ) ss | [ ] -> Ok ( List . rev acc ) in loop [ ] ( String . cuts ~ sep " , " : s )
let list ? sep c = let parse s = parse_split ? sep s c . parse in let print = Fmt . list ~ sep ( : Fmt . any " , " ) c . print in conv ~ docv ( : strf " LIST % s " c . docv ) parse print
let array ? sep c = let parse s = match parse_split ? sep s c . parse with | Error _ as e -> e | Ok l -> Ok ( Array . of_list l ) in let print = Fmt . array ~ sep ( : Fmt . any " , " ) c . print in conv ~ docv ( : strf " ARRAY % s " c . docv ) parse print
let pair ( ? sep = " , " ) l r = let parse s = match String . cut ~ sep s with | None -> Error ( err_invalid s ( strf " a separator ` % s ' in the string " sep ) ) | Some ( ls , rs ) -> Result . bind ( l . parse ls ) @@ fun l -> Result . bind ( r . parse rs ) @@ fun r -> Ok ( l , r ) in let print = Fmt . pair ~ sep : F...
let pp_unix_error ppf e = Fmt . string ppf ( Unix . error_message e )
let pp_process_status ppf = function
let err_empty_line = " no command , empty command line "
let err_file f e = Fmt . error_msg " % a : % a " Fpath . pp f pp_unix_error e
let err_run cmd pp e = Fmt . error_msg " run % a : % a " Bos_cmd . dump cmd pp e
let rec waitpid flags pid = try Unix . waitpid flags pid with
let rec create_process prog args stdin stdout stderr = try Unix . create_process prog args stdin stdout stderr with | Unix . Unix_error ( Unix . EINTR , _ , _ ) -> create_process prog args stdin stdout stderr
let rec create_process_env prog args env stdin stdout stderr = try Unix . create_process_env prog args env stdin stdout stderr with | Unix . Unix_error ( Unix . EINTR , _ , _ ) -> create_process_env prog args env stdin stdout stderr
let rec pipe ( ) = try Unix . pipe ( ) with
let rec set_close_on_exec fd = try Unix . set_close_on_exec fd with
let rec clear_close_on_exec fd = try Unix . clear_close_on_exec fd with
let rec openfile fn mode perm = try Unix . openfile fn mode perm with
let rec close fd = try Unix . close fd with
let close_no_err fd = try close fd with e -> ( )
let rec select r w e t = try Unix . select r w e t with
let create_process cmd env ~ stdin ~ stdout ~ stderr = let log_header pid = " EXEC " : ^ String . of_int pid in let line = Bos_cmd . to_list cmd in let prog = try List . hd line with Failure _ -> failwith err_empty_line in let line = Array . of_list line in match env with | None -> let pid = create_process prog line st...
let default_path_sep = if Sys . win32 then " ; " else " " :
let exe_is_path t = String . exists ( Char . equal dir_sep ) t
let tool_file ~ dir tool = match dir . [ String . length dir - 1 ] with
let search_in_path tool = let rec loop tool = function | " " -> None | p -> let dir , p = match String . cut ~ sep : default_path_sep p with | None -> p , " " | Some ( dir , p ) -> dir , p in if dir = " " then loop tool p else let tool_file = tool_file ~ dir tool in match Bos_os_file . _is_executable tool_file with | f...
let search_in_dirs ~ dirs tool = let rec loop tool = function | [ ] -> None | d :: dirs -> let tool_file = tool_file ~ dir ( : Fpath . to_string d ) tool in match Bos_os_file . _is_executable tool_file with | false -> loop tool dirs | true -> Some ( Fpath . v tool_file ) in loop tool dirs
let ensure_exe_suffix_if_win32 = match Sys . win32 with fun t -> match String . is_suffix ~ affix " . : exe " t with | true -> t | false -> t ^ " . exe "
let _find_tool ? search tool = match tool with let tool = ensure_exe_suffix_if_win32 tool in match exe_is_path tool with | true -> begin match Fpath . of_string tool with | Ok t -> Ok ( Some t ) | Error ( ` Msg _ ) as e -> e end | false -> match search with | None -> Ok ( search_in_path tool ) | Some dirs -> Ok ( searc...
let find_tool ? search cmd = match Bos_cmd . to_list cmd with
let err_not_found ? search cmd = match Bos_cmd . is_empty cmd with let pp_search ppf = function | None -> Fmt . string ppf " PATH " | Some dirs -> let pp_dir ppf d = Fmt . string ppf ( Filename . quote @@ Fpath . to_string d ) in Fmt . ( list ~ sep ( : Fmt . any " , @ " ) pp_dir ) ppf dirs in let tool = List . hd @@ Bo...
let get_tool ? search cmd = match find_tool ? search cmd with
let exists ? search cmd = match find_tool ? search cmd with
let must_exist ? search cmd = match find_tool ? search cmd with
let resolve ? search cmd = match find_tool ? search cmd with let t = Fpath . to_string t in Ok ( Bos_cmd . of_list ( t :: List . tl ( Bos_cmd . to_list cmd ) ) )
let search_path_dirs ( ? sep = default_path_sep ) path = let rec loop acc = function | " " -> Ok ( List . rev acc ) | p -> let dir , p = match String . cut ~ sep p with | None -> p , " " | Some ( dir , p ) -> dir , p in if dir = " " then loop acc p else match Fpath . of_string dir with | Error ( ` Msg m ) -> Fmt . erro...
module Fds = struct module Fd = struct type t = Unix . file_descr let compare : t -> t -> int = compare end module S = Set . Make ( Fd ) type t = S . t ref let empty ( ) = ref S . empty let rem fd s = s := S . remove fd ! s let add fd s = if fd = Unix . stdin || fd = Unix . stdout || fd = Unix . stderr then ( ) else ( ...
let write_fd_for_file ~ append f = try let flags = Unix . ( [ O_WRONLY ; O_CREAT ] ) in let flags = ( if append then Unix . O_APPEND else Unix . O_TRUNC ) :: flags in Ok ( openfile ( Fpath . to_string f ) flags 0o644 ) with Unix . Unix_error ( e , _ , _ ) -> err_file f e
let read_fd_for_file f = try Ok ( openfile ( Fpath . to_string f ) [ Unix . O_RDONLY ] 0o644 ) with Unix . Unix_error ( e , _ , _ ) -> err_file f e
let string_of_fd_async fd = let len = unix_buffer_size in let buf = Buffer . create len in let b = Bytes . create len in let rec step fd store b ( ) = try match Unix . read fd b 0 len with | 0 -> ` Ok ( Buffer . contents buf ) | n -> Buffer . add_substring buf ( Bytes . unsafe_to_string b ) 0 n ; step fd store b ( ) wi...
let string_of_fd fd = let rec loop = function ` Ok s -> s | ` Await step -> loop ( step ( ) ) in loop ( string_of_fd_async fd ( ) )
let string_to_fd_async s fd = let rec step fd s first len ( ) = let b = Bytes . unsafe_of_string s in try match Unix . single_write fd b first len with | c when c = len -> ` Ok ( ) | c -> step fd s ( first + c ) ( len - c ) ( ) with | Unix . Unix_error ( Unix . EINTR , _ , _ ) -> step fd s first len ( ) | Unix . Unix_e...
let string_to_fd s fd = let rec loop = function ` Ok ( ) -> ( ) | ` Await step -> loop ( step ( ) ) in loop ( string_to_fd_async s fd ( ) )
let string_to_of_fd s ~ to_fd ~ of_fd = let never ( ) = assert false in let wset , write = [ to_fd ] , string_to_fd_async s to_fd in let rset , read = [ of_fd ] , string_of_fd_async of_fd in let ret = ref " " in let rec loop rset read wset write = let rable , wable , _ = select rset wset [ ] ( - 1 . ) in let rset , rea...
type status = [ ` Exited of int | ` Signaled of int ]
let run_info_cmd ri = ri
let pp_status ppf = function
type run_status = run_info * status
let err_file ( ? append = false ) f = Err_file ( f , append )
let err_null = err_file Bos_os_file . null
let fd_for_run_err out_fd = function
type pipeline = { write : ( string * Unix . file_descr ) option ; read : Unix . file_descr ; pids : ( Bos_cmd . t * int ) list }
let in_string s = In_string s
let in_file f = In_file f
let in_null = in_file Bos_os_file . null
let in_stdin = In_fd Unix . stdin
type run_out = { env : Bos_os_env . t option ; cmd : Bos_cmd . t ; run_err : run_err ; run_in : run_in ; }
let rec wait_pids rev_pids = let rec loop ret = function | ( cmd , pid ) :: pids -> let s = snd ( waitpid [ ] pid ) in if ret <> None then loop ret pids else begin match s with | Unix . WEXITED 0 -> loop ret pids | Unix . WEXITED c -> loop ( Some ( cmd , ` Exited c ) ) pids | Unix . WSIGNALED s -> loop ( Some ( cmd , `...
let do_in_fd_read_stdout stdin o pids do_read = let fds = Fds . empty ( ) in try Fds . add stdin fds ; let read_stdout , stdout = pipe ( ) in Fds . add read_stdout fds ; Fds . add stdout fds ; match fd_for_run_err stdout o . run_err with | Error _ as e -> Fds . close_all fds ; e | Ok stderr -> Fds . add stderr fds ; se...
let do_in_fd_out_string stdin o pids = do_in_fd_read_stdout stdin o pids begin fun fds read_stdout pids -> let res = string_of_fd read_stdout in let ret = wait_pids pids in Fds . close_all fds ; Ok ( res , ret ) end
let do_in_fd_out_run_in stdin o pids = do_in_fd_read_stdout stdin o pids begin fun fds read_stdout pids -> Fds . rem read_stdout fds ; Fds . close_all fds ; Ok ( In_run_out { write = None ; read = read_stdout ; pids } ) end
let do_in_fd_out_fd stdin stdout o pids = let fds = Fds . empty ( ) in try Fds . add stdin fds ; Fds . add stdout fds ; match fd_for_run_err stdout o . run_err with | Error _ as e -> Fds . close_all fds ; e | Ok stderr -> Fds . add stderr fds ; let pid = create_process o . cmd o . env ~ stdin ~ stdout ~ stderr in let r...
let do_in_run_out_string p o = do_in_fd_out_string p . read o p . pids
let do_in_run_out_run_in p o = do_in_fd_out_run_in p . read o p . pids
let do_in_run_out_fd p out_fd o = do_in_fd_out_fd p . read out_fd o p . pids
let do_in_string_read_stdout s o do_read = let fds = Fds . empty ( ) in try let stdin , write_stdin = pipe ( ) in Fds . add stdin fds ; Fds . add write_stdin fds ; let read_stdout , stdout = pipe ( ) in Fds . add read_stdout fds ; Fds . add stdout fds ; match fd_for_run_err stdout o . run_err with | Error _ as e -> Fds...
let do_in_string_out_string s o = do_in_string_read_stdout s o begin fun fds write_stdin read_stdout pid -> let res = string_to_of_fd s ~ to_fd : write_stdin ~ of_fd : read_stdout in Fds . close write_stdin fds ; let ret = wait_pids [ ( o . cmd , pid ) ] in Fds . close_all fds ; Ok ( res , ret ) end
let do_in_string_out_run_in s o = do_in_string_read_stdout s o begin fun fds write_stdin read_stdout pid -> Fds . rem read_stdout fds ; Fds . close_all fds ; Ok ( In_run_out { write = Some ( s , write_stdin ) ; read = read_stdout ; pids = [ o . cmd , pid ] } ) end
let do_in_string_out_fd s stdout o = let fds = Fds . empty ( ) in try Fds . add stdout fds ; let stdin , write_stdin = pipe ( ) in Fds . add stdin fds ; Fds . add write_stdin fds ; match fd_for_run_err stdout o . run_err with | Error _ as e -> Fds . close_all fds ; e | Ok stderr -> Fds . add stderr fds ; set_close_on_e...
let do_in_fd : type a . Unix . file_descr -> run_out -> a _run_out -> ( a , [ > ` Msg of string ] ) result = fun in_fd o ret -> match ret with | To_string -> do_in_fd_out_string in_fd o [ ] | To_run_in -> do_in_fd_out_run_in in_fd o [ ] | To_fd out_fd -> do_in_fd_out_fd in_fd out_fd o [ ] | To_file ( f , append ) -> Re...
let run_cmd : type a . run_out -> a _run_out -> ( a , [ > ` Msg of string ] ) result = begin match ret with | To_string -> do_in_string_out_string s o | To_run_in -> do_in_string_out_run_in s o | To_fd out_fd -> do_in_string_out_fd s out_fd o | To_file ( f , append ) -> Result . bind ( write_fd_for_file ~ append f ) @@...
let out_string ( ? trim = true ) o = match run_cmd o To_string with
let out_lines ? trim o = Result . bind ( out_string ? trim o ) @@ fun ( s , st ) -> Ok ( ( if s = " " then [ ] else String . cuts ~ sep " :\ n " s ) , st )
let out_file ( ? append = false ) f o = run_cmd o ( To_file ( f , append ) )
let out_run_in o = run_cmd o To_run_in
let out_null o = out_file Bos_os_file . null o
let out_stdout o = run_cmd o ( To_fd Unix . stdout )
let to_string ? trim o = out_string ? trim o |> success
let to_lines ? trim o = out_lines ? trim o |> success
let to_file ? append f o = out_file ? append f o |> success
let to_null o = out_null o |> success
let to_stdout o = out_stdout o |> success
let run_io ? env ? err ( : run_err = Err_stderr ) cmd run_in = { env ; cmd ; run_err ; run_in }
let run_out ? env ? err cmd = run_io ? env ? err cmd in_stdin
let run_in ? env ? err cmd i = run_io ? env ? err cmd i |> to_stdout
let run ? env ? err cmd = run_io ? env ? err cmd in_stdin |> to_stdout
let run_status ? env ? err ( ? quiet = false ) cmd = let err = match err with | None -> if quiet then err_null else err_stderr | Some err -> err in let ret = match quiet with | true -> in_null |> run_io ? env ~ err cmd |> out_null | false -> in_stdin |> run_io ? env ~ err cmd |> out_stdout in match ret with | Ok ( ( ) ...
let create ( ? path = true ) ( ? mode = 0o755 ) dir = let rec mkdir d mode = try Ok ( Unix . mkdir ( Fpath . to_string d ) mode ) with | Unix . Unix_error ( Unix . EEXIST , _ , _ ) -> Ok ( ) | Unix . Unix_error ( e , _ , _ ) -> if d = dir then Fmt . error_msg " create directory % a : % s " Fpath . pp d ( uerror e ) els...
let rec contents ( ? dotfiles = false ) ( ? rel = false ) dir = let rec readdir dh acc = match ( try Some ( Unix . readdir dh ) with End_of_file -> None ) with | None -> Ok acc | Some ( " . . " | " . " ) -> readdir dh acc | Some f when dotfiles || not ( String . is_prefix ~ affix " . " : f ) -> begin match Fpath . of_s...
let fold_contents ? err ? dotfiles ? elements ? traverse f acc d = Result . bind ( contents d ) @@ Bos_os_path . fold ? err ? dotfiles ? elements ? traverse f acc
let user ( ) = let debug err = Bos_log . debug ( fun m -> m " OS . Dir . user : % s " err ) in let env_var_fallback ( ) = Result . bind ( Bos_os_env . ( parse " HOME " ( some path ) ~ absent : None ) ) @@ function | Some p -> Ok p | None -> Fmt . error_msg " cannot determine user home directory : \ HOME environment var...
let rec current ( ) = try let p = Unix . getcwd ( ) in match Fpath . of_string p with | Ok dir -> if Fpath . is_abs dir then Ok dir else Fmt . error_msg " getcwd ( 3 ) returned a relative path : ( % a ) " Fpath . pp dir | Error _ -> Fmt . error_msg " get current working directory : cannot parse it to a path ( % a ) " S...
let rec set_current dir = try Ok ( Unix . chdir ( Fpath . to_string dir ) ) with Fmt . error_msg " set current working directory to % a : % s " Fpath . pp dir ( uerror e )
let with_current dir f v = Result . bind ( current ( ) ) @@ fun old -> try Result . bind ( set_current dir ) @@ fun ( ) -> let ret = f v in Result . bind ( set_current old ) @@ fun ( ) -> Ok ret with | exn -> ignore ( set_current old ) ; raise exn