Module Shexp_process__.Process
type 'a t
An
'a t
value represent a process pipeline description, that can be evaluated usingeval
into a value of type'a
. Note that creating an'a t
value has no effect. Effects are only performed when callingeval
.
module Context : sig ... end
Execution contexts
val eval : ?context:Context.t -> 'a t -> 'a
Evaluate the given process in the given environment.
If
context
is not specified, a temporary one is created. If you are callingeval
many times, then creating a context and reusing it is more efficient.
module Logged : sig ... end
module Traced : sig ... end
module Prim : sig ... end
module type Debugger = Shexp_process__.Debugger_intf.S
shexp_process allows one to plug a debugger in the evaluator.
Logged
andTraced
are essentially two non-interactive debuggers.
module With_debug : functor (Dbg : Debugger) -> sig ... end
Basic processes
val return : 'a -> 'a t
Classic monad operations. Note that because shexp_process uses threads under the hood, you should be careful about using global mutable data structures. To communicate values between concurrent processes, use the
sync
function.
val bind : 'a t -> f:('a -> 'b t) -> 'b t
val map : 'a t -> f:('a -> 'b) -> 'b t
val fail : exn -> _ t
Create a process that fails with the given exception. Evaluation of this process will raise this exception.
val fork : 'a t -> 'b t -> ('a * 'b) t
fork a b
represents two processes that are executed concurrently. The resulting process will block until botha
andb
have finished and will return both of their results.This is essentially the same as forking a system process: both process can independently change the current working directory, change their standard out, etc...
Regarding errors, if the evaluation of both processes fail, then only one the exceptions will be kept. It is not specified which one. You should use
Traced
to get a full trace and see what exceptions are raised and where.
val fork_all : 'a t list -> 'a list t
fork_all ps
representsList.length ps
processes executed concurrently.fork_all
generalizesfork
from pairs of elements to lists of elements.
val fork_all_unit : unit t list -> unit t
A slightly more efficient version of
map (fork_all ps) ~f:(fun _ -> ())
val protect : finally:unit t -> 'a t -> 'a t
protect ~finally t
protects against execution errors.finally
is always executed, even if the evaluation oft
raises.
val reify_exn : ('a -> 'b t) -> 'a -> 'b t
Capture exceptions into the process monad. You can use this if the argument of
protect
is the result of a function call.For instance in the following code
finally
wouldn't be executed:let f () = raise Not_found protect ~finally (f ())
You can write instead:
protect ~finally (reify_exn f ())
Running commands
val run : string -> string list -> unit t
Run an external program. This will fail if the external program does not exists, is signaled or exit with a non-zero exit code.
val run_exit_code : string -> string list -> int t
Run an external program and return its exit code. This will fail if the external program is signaled.
module Exit_status = Shexp_process.Exit_status
val run_exit_status : string -> string list -> Exit_status.t t
Run an external program and return its exit status.
val run_bool : ?true_v:int list -> ?false_v:int list -> string -> string list -> bool t
Run an external program, returns
true
if its exit code is part oftrue_v
andfalse
if it is part offalse_v
.
val call : string list -> unit t
Same functions as the 'run' ones above, but take a string list instead. This way, the first element and the others are treated in a homogeneous way. It can ease prepending commands in specific circumstances, e.g.
if profile then call ("time" :: command) else call command
E.g.
call ["grep"; "-i"; pattern; filename]
is equivalent torun "grep" ["-i"; pattern; filename]
val call_exit_code : string list -> int t
val call_exit_status : string list -> Exit_status.t t
val call_bool : ?true_v:int list -> ?false_v:int list -> string list -> bool t
module Background_command : sig ... end
val spawn : string -> string list -> Background_command.t t
Start an external program but do not wait for its termination. If you never call
wait
on the result, the process will become a zombie after it terminates.
val wait : Background_command.t -> Exit_status.t t
Wait for a background command to terminate and return its exit status.
Unix environment
val find_executable : string -> string option t
Return the absolute path to the given command.
val find_executable_exn : string -> string t
val get_env : string -> string option t
Return the value associated to the given environment variable.
Current working directory
val cwd_logical : string t
Return the current working directory. Note that there is no guarantee that this directory exists. For instance if a component in this path has is renamed during the evaluation of the process, then this will be a dangling directory.
IO
module Std_io = Shexp_process.Std_io
val echo : ?where:Std_io.t -> ?n:unit -> string -> unit t
Output a string on one of the standard io.
~n:()
suppresses the newline output at the end.
val print : string -> unit t
echo ~where:Stdout
val eprint : string -> unit t
echo ~where:Stderr
val printf : ('a, unit, string, unit t) Stdlib.format4 -> 'a
val eprintf : ('a, unit, string, unit t) Stdlib.format4 -> 'a
val read_all : string t
Consume all standard input
val fold_lines : init:'a -> f:('a -> string -> 'a t) -> 'a t
Fold over lines in the input.
f
is given the line with the end of line. Both"\r\n"
and"\n"
are treated as end of lines.
Pipes
val pipe : ?connect:(Std_io.t list * Std_io.t) -> unit t -> 'a t -> 'a t
pipe ~connect:(aios, bio) a b
is a process obtain by connecting theaios
ofa
to thebio
ofb
.a
andb
are evaluated in parallel (as withfork
).(aio, bio)
defaults to([Stdout], Stdin)
.
val pipe_both : ?connect:(Std_io.t list * Std_io.t) -> 'a t -> 'b t -> ('a * 'b) t
pipe_pair a b
is a the same aspipe
but returns the results of botha
andb
.
Redirections
val redirect : Std_io.t list -> ?perm:int -> flags:Unix.open_flag list -> string -> 'a t -> 'a t
redirect ios ?perm ~flags filename t
redirects the following ios to a file int
.perm
andflags
are passed the same as forUnix.openfile
.
val stderr_to : ?append:unit -> string -> 'a t -> 'a t
val outputs_to : ?append:unit -> string -> 'a t -> 'a t
val stdin_from : string -> 'a t -> 'a t
val replace_io : ?stdin:Std_io.t -> ?stdout:Std_io.t -> ?stderr:Std_io.t -> 'a t -> 'a t
Replace the given standard io by the given one. For instance to redirect stdout to stderr:
replace_io ~stdout:Stderr t
Temporary files & directory
val temp_dir : string t
Return the current temporary directory
val set_temp_dir : string -> 'a t -> 'a t
set_temp_dir dir k
represents the processk
evaluated in a context where the current temporary directory is set todir
.
FS operations
val chmod : string -> perm:Unix.file_perm -> unit t
val chown : string -> uid:int -> gid:int -> unit t
val mkdir : ?perm:Unix.file_perm -> ?p:unit -> string -> unit t
val rm : string -> unit t
val rmdir : string -> unit t
val mkfifo : ?perm:Unix.file_perm -> string -> unit t
val link : string -> string -> unit t
val rename : string -> string -> unit t
val symlink : string -> string -> unit t
val stat : string -> Unix.LargeFile.stats t
val lstat : string -> Unix.LargeFile.stats t
val readlink : string -> string t
val readdir : string -> string list t
val file_exists : string -> bool t
val rm_rf : string -> unit t
Recursively remove a tree
Misc
val sleep : float -> unit t
Synchronisation
Misc
module Infix : sig ... end
module Let_syntax : sig ... end
Open this module when using
ppx_let
module List : sig ... end