text stringlengths 12 786k |
|---|
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 = ... |
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_o... |
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 d... |
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 too... |
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 " , @ " ) p... |
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 . ... |
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... |
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_stri... |
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 , _ , ... |
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 rse... |
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 | ... |
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 | O... |
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 . en... |
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 ... |
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 ,... |
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 ->... |
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_f... |
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... |
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... |
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_... |
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_... |
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 " ca... |
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 direc... |
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 |
type tmp_name_pat = ( string -> string , Format . formatter , unit , string ) format4 |
let delete_tmp dir = ignore ( delete ~ recurse : true dir ) |
let tmps = ref Fpath . Set . empty |
let tmps_add file = tmps := Fpath . Set . add file ! tmps |
let tmps_rem file = delete_tmp file ; tmps := Fpath . Set . remove file ! tmps |
let delete_tmps ( ) = Fpath . Set . iter delete_tmp ! tmps |
let ( ) = at_exit delete_tmps |
let tmp ( ? mode = default_tmp_mode ) ? dir pat = let dir = match dir with None -> Bos_os_tmp . default_dir ( ) | Some d -> d in let err ( ) = Fmt . error_msg " create temporary directory % s in % a : \ too many failing attempts " ( strf pat " XXXXXX " ) Fpath . pp dir in let ... |
let with_tmp ? mode ? dir pat f v = Result . bind ( tmp ? mode ? dir pat ) @@ fun dir -> try let ret = f dir v in tmps_rem dir ; Ok ret with e -> tmps_rem dir ; raise e |
type t = string String . map |
let current ( ) = try let env = Unix . environment ( ) in let add acc assign = match acc with | Error _ as e -> e | Ok m -> match String . cut ~ sep " " := assign with | Some ( var , value ) -> Ok ( String . Map . add var value m ) | None -> Fmt . error_msg " could not pa... |
let to_array env = let add_var name value acc = String . concat [ name ; " " ; = value ] :: acc in Array . of_list ( String . Map . fold add_var env [ ] ) |
let var name = try Some ( Unix . getenv name ) with Not_found -> None |
let set_var name v = let v = match v with None -> " " | Some v -> v in try Ok ( Unix . putenv name v ) with | Unix . Unix_error ( e , _ , _ ) -> Fmt . error_msg " set environment variable % s : % s " name ( Unix . error_message e ) |
let opt_var name ~ absent = try Unix . getenv name with Not_found -> absent |
let req_var name = try Ok ( Unix . getenv name ) with |
type ' a parser = string -> ( ' a , [ ` Msg of string ] ) result |
let parser kind k_of_string = fun s -> match k_of_string s with | None -> Fmt . error_msg " could not parse % s value from % a " kind String . dump s | Some v -> Ok v |
let bool = let of_string s = match String . Ascii . lowercase s with | " " | " false " | " no " | " n " | " 0 " -> Some false | " true " | " yes " | " y " | " 1 " -> Some true | _ -> None in parser " bool " of_string |
let string = fun s -> Ok s |
let cmd = fun s -> match Bos_cmd . of_string s with |
let some p = fun s -> match p s with Ok v -> Ok ( Some v ) | Error _ as e -> e |
let parse name p ~ absent = match var name with match p s with | Ok _ as r -> r | Error ( ` Msg e ) -> Fmt . error_msg " environment variable % s : % s " name e |
let value ( ? log = Logs . Error ) name p ~ absent = Bos_log . on_error_msg ~ level : log ~ use ( : fun ( ) -> absent ) ( parse name p ~ absent ) |
let err_empty_buf = " buffer size can ' t be 0 " |
let err_invalid_input = " input no longer valid , did it escape its scope " ? |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.