Hugh Sasse wrote:
Is IO.popen supposed to be working OK?
I'm getting errors with
PYTHON = "C:/cygwin/usr/bin/python"
PYTHON_PROG = "-../AnnaAIML-.7.0/anna_brain/skel.py"
MODE = "w+"
PAUSE = 30
def setup_program
if block_given?
Open3.popen3(PYTHON_PROG, MODE) do |io|
sleep PAUSE
ignore = io.read
yield io
end
else
@io = IO.popen(PYTHON_PROG, MODE) #<----- Errors here
# ignore = @io.read
end
end
with either No such file or directory" if the - is at the start,
or exec format error if it points to the python script with a
#!python
line. Yes, I'm trying to use ruby's interpretation of the #! line
to spin off a python program. So why it is treating this as a
file path spec rather than a command string? The ri docs talk
about it being a command string. If I put a path
to the Python in there using #{PYTHON} it can't find the file with a
space: "#{PYTHON} ../"...
So I've tried:
PYTHON_PROG = "-../AnnaAIML-.7.0/anna_brain/skel.py"
PYTHON_PROG = "../AnnaAIML-.7.0/anna_brain/skel.py"
PYTHON_PROG = "#{PYTHON} ../AnnaAIML-.7.0/anna_brain/skel.py"
all without success.
This is not mission critical for me, I'm just messing about with PyAIML
and Shoes, but I'd like to know why my assumptions about popen and popen3
are bonkers. I'd use expect.rb
http://www.ruby-doc.org/stdlib/libdoc/pty/rdoc/index.html
but I'm fairly sure that won't work on Windows.
Hugh
It looks like the directory you think you are in is not the directory
you actually are in. I'd suggest playing around with checking what
__FILE__ is in this program, etc., as Shoes does tricksy eval things
that can trip people up.
First though, I would suggest using absolute paths everywhere (even if
you have to hard code them in) in order to make sure your code works as
you expect. Since you're playing with new unfamiliar bits like Shoes, it
can be a bit difficult to hold all that in your head while also
maintaining the mental model of "where Shoes puts you" on the
filesystem. Absolute paths reduce that overhead and, since you seem to
be writing a one-off program, it won't cause too much technical debt.
Later, you can work out your own tricksy File.dirname(__FILE__) (or,
blech, FileUtils.chdir) things.
Also, w+ indicates that you are able to write to the pipe, not just read
it as you are (attempting) to do. I would suggest changing this to 'r'
for both clarity's sake and to prevent accidental write attempts. You
are only going to be reading from it (since it's the output of another
program), and it's best to treat it that way.
--
Jeff