-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RFC: deprecate readsfrom/writesto in favor of simply using open on AbstractCmd #6948
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -418,30 +418,42 @@ function eachline(cmd::AbstractCmd,stdin) | |
end | ||
eachline(cmd::AbstractCmd) = eachline(cmd,DevNull) | ||
|
||
#returns a pipe to read from the last command in the pipelines | ||
readsfrom(cmds::AbstractCmd) = readsfrom(cmds, DevNull) | ||
function readsfrom(cmds::AbstractCmd, stdin::AsyncStream) | ||
processes = @tmp_rpipe out tmp spawn(false, cmds, (stdin,tmp,STDERR)) | ||
start_reading(out) | ||
(out, processes) | ||
# return a (Pipe,Process) pair to write/read to/from the pipeline | ||
function open(cmds::AbstractCmd, mode::String="r", stdio::AsyncStream=DevNull) | ||
if mode == "r" | ||
processes = @tmp_rpipe out tmp spawn(false, cmds, (stdio,tmp,STDERR)) | ||
start_reading(out) | ||
(out, processes) | ||
elseif mode == "w" | ||
processes = @tmp_wpipe tmp inpipe spawn(false,cmds, (tmp,stdio,STDERR)) | ||
(inpipe, processes) | ||
else | ||
throw(ArgumentError("mode must be \"r\" or \"w\", not \"$mode\"")) | ||
end | ||
end | ||
|
||
function writesto(cmds::AbstractCmd, stdout::UVStream) | ||
processes = @tmp_wpipe tmp inpipe spawn(false, cmds, (tmp,stdout,STDERR)) | ||
(inpipe, processes) | ||
function open(f::Function, cmds::AbstractCmd, args...) | ||
try | ||
io, P = open(cmds, args...) | ||
ret = f(io) | ||
close(io) | ||
!success(P) && pipeline_error(P) | ||
return ret | ||
catch | ||
kill(P) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this need close(io) here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was thinking that killing |
||
rethrow() | ||
end | ||
end | ||
writesto(cmds::AbstractCmd) = writesto(cmds, DevNull) | ||
|
||
# TODO: convert this to use open(cmd, "r+"), with a single read/write pipe | ||
function readandwrite(cmds::AbstractCmd) | ||
(out, processes) = @tmp_wpipe tmp inpipe readsfrom(cmds, tmp) | ||
(out, processes) = @tmp_wpipe tmp inpipe open(cmds, "r", tmp) | ||
(out, inpipe, processes) | ||
end | ||
|
||
function readbytes(cmd::AbstractCmd, stdin::AsyncStream=DevNull) | ||
(out,pc) = readsfrom(cmd, stdin) | ||
if !success(pc) | ||
pipeline_error(pc) | ||
end | ||
(out,pc) = open(cmd, "r", stdin) | ||
!success(pc) && pipeline_error(P) | ||
wait_close(out) | ||
return takebuf_array(out.buffer) | ||
end | ||
|
@@ -450,15 +462,10 @@ function readall(cmd::AbstractCmd, stdin::AsyncStream=DevNull) | |
return bytestring(readbytes(cmd, stdin)) | ||
end | ||
|
||
writeall(cmd::AbstractCmd, stdin::String) = writeall(cmd, stdin, DevNull) | ||
function writeall(cmd::AbstractCmd, stdin::String, stdout::AsyncStream) | ||
(in,pc) = writesto(cmd, stdout) | ||
write(in, stdin) | ||
close(in) | ||
if !success(pc) | ||
pipeline_error(pc) | ||
function writeall(cmd::AbstractCmd, stdin::String, stdout::AsyncStream=DevNull) | ||
open(cmd, "w", stdout) do io | ||
write(io, stdin) | ||
end | ||
return true | ||
end | ||
|
||
function run(cmds::AbstractCmd,args...) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4949,9 +4949,22 @@ System | |
|
||
Send a signal to a process. The default is to terminate the process. | ||
|
||
.. function:: readsfrom(command) | ||
|
||
Starts running a command asynchronously, and returns a tuple (stream,process). The first value is a stream reading from the process' standard output. | ||
.. function:: open(command, mode::String="r", stdio=DevNull) | ||
|
||
Start running ``command`` asynchronously, and return a tuple | ||
``(stream,process)``. If ``mode`` is ``"r"``, then ``stream`` | ||
reads from the process's standard output and ``stdio`` optionally | ||
specifies the process's standard input stream. If ``mode`` is | ||
``"w"``, then ``stream`` writes to the process's standard input | ||
and ``stdio`` optionally specifies the process's standard output | ||
stream. | ||
|
||
.. function:: open(f::Function, command, mode::String="r", stdio=DevNull) | ||
|
||
Similar to ``open(command, mode, stdio)``, but calls ``f(stream)`` | ||
on the resulting read or write stream, then closes the stream | ||
and waits for the process to complete. Returns the value returned | ||
by ``f``. | ||
|
||
.. function:: writesto(command) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also needs to be removed. |
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think
success || die
might read better than the negative form
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay.