Ruby under cygwin & windows paths

Hello,

I’m using a script located under
c:\mypath\myscript.rb
which has
require ‘mylib’
(I have another file c:\mypath\mylib.rb.)

If I call ruby, the cygwin executable is called (I have no admin
rights on the computer, so I can’t afford to install another native
windows version of ruby.)

Now the problem:
If I go to c:\mypath and call myscript from there, everything works OK.
File.dirname(FILE) is set to “/cygdrive/c/mypath” and properly
included in $LOAD_PATH.

But if I execute the script from somewhere else, inclusion doesn’t
work any more:

C:/mypath/myscript.rb:10:in `require’: No such file to load – mylib
(LoadError)
from C:/mypath/myscript.rb:10

since File.dirname(FILE) is now set to “c:\mypath” and this one
also gets included in $LOAD_PATH. But cygwin cannot access that path
by default.

After adding
$LOAD_PATH << ‘/cygdrive/c/mydir’
to the script it works again, but I may not afford to modify the
script by hardcoding the path into it since it has to work on other
computers as well.

Is there a way to modify the script (in a compatible way with the rest
of the world) or (preferrably) to change the settings in cygwin
somehow, so that the inclusion will work again without hardcoding the
path?

Any help would be appreciated,
Mojca M.

On Fri, 13 Jan 2006 14:23:37 +0100, David V. [email protected]
wrote:

If you can use bash, I’d say switch to using the Cygwin shell on that
machine.

David V.

Also, you might try yet another ugly hack. Put a file called “ruby.bat”
somewhere that’s on your PATH before the Cygwin bin directory. Then have
that file call the Cygwin bash with a script that transforms the Windows
path into a POSIX one (I’m almost sure Cygwin comes with a tool for
that)
and calls ruby that way. The disadvantage to this is that it’ bound to
break command line switches sooner or later. I might get back to this a
while later today, these are replies in a hurry.

David V.

On Fri, 13 Jan 2006 13:12:02 +0100, Mojca M.
[email protected] wrote:

windows version of ruby.)
(LoadError)
computers as well.

Is there a way to modify the script (in a compatible way with the rest
of the world) or (preferrably) to change the settings in cygwin
somehow, so that the inclusion will work again without hardcoding the
path?

Any help would be appreciated,
Mojca M.

I’d guess thst’s because the Cygwin ruby expects a POSIX path, not the
Windows one. The wonderful tool Cygwin is, it doesn’t do miracles - you
should always use Cygwin tools from a Cygwin shell, not from CMD or by
putting them on the path by default. The first case works because for a
file in the current directory is the same as the Cygwin POSIX path.

If you can use bash, I’d say switch to using the Cygwin shell on that
machine.

David V.

Thanks. I didn’t think about creating another ruby.bat file. There’s a
“cygpath” utility in cygwin, but I didn’t figure out yet how to use it
to call another program an to tell him which paths to use.

I think you can run ruby using something like

bash -c “ruby ‘$(cygpath -u %1)’ %2 %3 %4 %5 %6 %7 %8 %9”

Maybe you could put this into some kind of ruby.bat. I don’t know if
cmd.exe provided this kind of substitution which would make things
easier.

I don’t have ConTeXt installed under cygwin

But which probably is the way to go here.

Even if you don’t have administration rights, you should be able to
compile it from source & install it in your user dir. (Maybe, somehow.)

Cheers,
Thomas.


Telefonate ohne weitere Kosten vom PC zum PC: http://messenger.yahoo.de

On Fri, 13 Jan 2006 18:50:59 +0100, Thomas [email protected]
wrote:

easier.

There might be issues if ConTeXt is hardcoded ro run “ruby.exe”, instead
of just “ruby” in a subshell. This is probably the first and last time I
see something that could be done with exec() instead of spawning a
subprocess…

The above command line should have “ruby.exe” instead of just ruby,
otherwise the BAT script would recurse forever. It seems you have the
Cygwin\bin on the path already, so that should be fine. Unfortunately,
despite the fact CMD came pretty close to a usable shell in XP, you
still
have to do the batch argument hack there - it seems you can’t specify
“all
arguments after and including the second”.

If by any chance the argument hack breaks ConTeXt, or that runs ruby
with
some switches before the script filename, you can always have ruby.bat
do
“ruby.js %*”, and have the JScript play around with the arguments, like
finding the one that ends with “.rb” and then have cygpath have a go at
it. And afterwards, give your local admin a printout of this thread, and
a
five dollar note saying “here, go buy a clue”.

I don’t have ConTeXt installed under cygwin

But which probably is the way to go here.

Even if you don’t have administration rights, you should be able to
compile it from source & install it in your user dir. (Maybe, somehow.)

It should be doable. Configure it with “./configure --prefix=‘~’” and it
should work. You might want to modify .profile to put ~/bin at the start
of PATH. And you also have to tell the linker where to find ~/lib, but
someone else will have to elucidate on that, I’m not very (at all)
experienced in ways *nixy that involve playing with the dynamic linking.

David V.

Interestingly enough my news posting didn’t make it to the mail side
http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/50b3c1b275cd0c6d

I still think modifying the environment is a better approach than
writing wrapper scripts.

cheers

robert

2006/1/13, Mojca M. [email protected]:

David V. wrote:

Also, you might try yet another ugly hack. Put a file called “ruby.bat”
somewhere that’s on your PATH before the Cygwin bin directory. Then have
that file call the Cygwin bash with a script that transforms the Windows
path into a POSIX one (I’m almost sure Cygwin comes with a tool for that)
and calls ruby that way. The disadvantage to this is that it’ bound to
break command line switches sooner or later. I might get back to this a
while later today, these are replies in a hurry.

Thanks. I didn’t think about creating another ruby.bat file. There’s a
“cygpath” utility in cygwin, but I didn’t figure out yet how to use it
to call another program an to tell him which paths to use.

I didn’t write the scripts by myself. ConTeXt (a kind of TeX
extension/macro package) uses perl and ruby for different tasks, so
that ruby is called somewhere inbetween. I can’t run the whole stuff
from bash since I don’t have ConTeXt installed under cygwin, but
ruby.bat might help perhaps. It’s pretty complicated situation (one
runs an exe which calls a ruby script which calls another perl script
which calls another ruby script, …) I’ll also try to find someone
who knows cygwin slightly better if everything else fails.

I didn’t even notice that cygwin is (mis)used for ruby (most scripts
work properly) until I struggled against some problems.

Thanks a lot for help.

Mojca

2006/1/14, David V. [email protected]:

If it does indeed work (Which I can’t, or rather won’t test), it
indubitably is. Wrapper scripts are just a hack that first came to mind
since I use them for a few other things - notably automagically running ri
in glorious ANSI technicolour and paging it via less, and basically
emulating alias on CMD.

The nice thing about modifying the env is that you can create a
default location for your self written lib scripts that and you can
always access them without additional hassle. Wrapper scripts are
just plan ugly IMHO.

The global environment change has a pitfall if RUBYLIB happens to be set
systemwide, but that shouldn’t be the case, and even if so, I think
user-set PATH-like environment variables always come first in the final
value.

I would only set it globally if the libs are intended to be used by
all users. About precedence: user settings always override global
settings but you can also use the global setting to define a user
variable, e.g. PATH=%PATH%;M:\y\local\path

Kind regards

robert

On Sat, 14 Jan 2006 00:23:51 +0100, Robert K.
[email protected] wrote:

If it does indeed work (Which I can’t, or rather won’t test), it
indubitably is. Wrapper scripts are just a hack that first came to mind
since I use them for a few other things - notably automagically running
ri
in glorious ANSI technicolour and paging it via less, and basically
emulating alias on CMD.

The global environment change has a pitfall if RUBYLIB happens to be set
systemwide, but that shouldn’t be the case, and even if so, I think
user-set PATH-like environment variables always come first in the final
value.

David V.

Hi,

At Fri, 13 Jan 2006 21:12:02 +0900,
Mojca M. wrote in [ruby-talk:175655]:

But if I execute the script from somewhere else, inclusion doesn’t
work any more:

C:/mypath/myscript.rb:10:in `require’: No such file to load – mylib (LoadError)
from C:/mypath/myscript.rb:10

since File.dirname(FILE) is now set to “c:\mypath” and this one
also gets included in $LOAD_PATH. But cygwin cannot access that path
by default.

Sounds strange. File.dirname(“C:/mypath/myscript.rb”) is
“c:\mypath” but not “c:/mypath”? How do you add it to
$LOAD_PATH?

On 1/13/06, David V. wrote:

Maybe you could put this into some kind of ruby.bat. I don’t know if
cmd.exe provided this kind of substitution which would make things
easier.

This command looks promising. I can’t test it before monday, but I
hope that it will solve the problem. Thanks a lot.

There might be issues if ConTeXt is hardcoded ro run “ruby.exe”, instead
of just “ruby” in a subshell. This is probably the first and last time I
see something that could be done with exec() instead of spawning a
subprocess…

It’s not “ruby.exe”, only “ruby”. (If the problem was in ConTeXt, the
developer would fix it within a couple of minutes/hours. If some
software has to be installed by the admin on Windows on our computers,
there’s very little chance that it would be done before the summer.)

finding the one that ends with “.rb” and then have cygpath have a go at
it. And afterwards, give your local admin a printout of this thread, and a
five dollar note saying “here, go buy a clue”.

Thanks for additional notes. I hope that I won’t need such deeps hacks
as writing js code to parse the command line, but let’s try the above
first and think further if it fails.

of PATH. And you also have to tell the linker where to find ~/lib, but
someone else will have to elucidate on that, I’m not very (at all)
experienced in ways *nixy that involve playing with the dynamic linking.

I don’t doubt that it’s doable, but I would prefer trying just
anything else before this. Besides I see no reason why to do it if the
current installation is generally working (except for some modules
which require additional libraries). I have enough problems with
MikTeX installation (which is tested pretty well) - I don’t dare even
think about all the possible problems and bugs on cygwin (two were
already fixed during my last failures to make ConTeXt work on those
computers). Installing it on each single computer without an automated
update mechanism (and updates come out approximately every 10 days)?
If I ask the admin to install tetex under cygwin, it will interfere
with the existing MikTeX and so on … It sounds more like a nightmare
than a serious alternative.

[email protected] wrote:

also gets included in $LOAD_PATH. But cygwin cannot access that path
by default.

Sounds strange. File.dirname(“C:/mypath/myscript.rb”) is
“c:\mypath” but not “c:/mypath”? How do you add it to
$LOAD_PATH?

I guess these lines do it
(http://source.contextgarden.net/newpstopdf.rb):

unless defined? ownpath
ownpath = $0.sub(/[\/]\w*?.rb/i,‘’)
$: << ownpath
end

But it works ok on Windows (if there’s no cygwin interference).

Thanks to everyone for suggestions, I’ll report about success/failure
on Monday once I come back to the university,
Mojca

On Mon, 16 Jan 2006 17:37:03 +0100, Mojca M.
[email protected] wrote:

cmd.exe provided this kind of substitution which would make things

Mojca

The $() seems to try and execute the path… A slip of the mind? My
first
guess would be using backticks instead of the ‘$()’, but that has issues
with the backslashes being processed in the path I give it when trying
out, and I smell rocky waters ahead. And by rocky, I mean some
sed-massaging of %1 (it doesn’t get much rockier), and I’ll honestly
admit
my sed knowledge is very bad.

Did you check out that hint from that guy that posted on google groups
that didn’t get replicated to the list? It just might end up a lot less
painful.

David V.

bash -c “ruby ‘$(cygpath -u %1)’ %2 %3 %4 %5 %6 %7 %8 %9”

Thanks again for all the proposals, but the part with “$(cygpath -u
%1)” doesn’t work:
ruby: No such file or derectory – $(cygpath -u ) (Load Error)

Okay, this is getting somewhat OT and should probably rather go to the
cygwin list. Anyway,

ruby.bat:
bash -c “ruby.exe $(cygpath -u ‘%1’) %2 %3 %4 %5 %6 %7 %8 %9”

test.rb:
p $0, “Succeeded!”

At the command line (cmd.exe):
G:>ruby.bat g:\tml\src\test.rb
“/mnt/g/tml/src/test.rb”
“Succeeded!”

$(foo) is equivalent to foo. A matter of taste.

Anyway, the solution with modifying RUBYLIB or some other env variable
should work too – maybe in a wrapper of the script you’re calling. In
general, I think mixing cygwin & native win apps isn’t a good idea which
is why I would suggest to install either ruby4win or tetex4cygwin.

Cheers,
Thomas.

Robert K. [email protected] wrote:

You just need to set the environment variable RUBYLIB appropriately.
Either Windows or Unix path convention will do. You can do that either ad
hoc in the shell from where you execute your script, put it in .bashrc in
your home (“cd” in bash) or modify your complete environment by going to
“My Computer” and then setting your env var there.

I’m sorry. I don’t know how I missed this message despite a special
notice that it was lost.

Thanks a lot.
set RUBYLIB=/cygdrive/c/programs/texmf/scripts/context/ruby
was the keyword which finally solved the problem.

(And the bitter experience afterwards: ruby doesn’t need to be
installed at all, only extracted to some folder and the path has to be
set properly. I feel like a prisoner who doesn’t even notice if the
door is unlocked.)

Mojca

On 1/13/06, Thomas [email protected] wrote:

Thanks. I didn’t think about creating another ruby.bat file. There’s a
“cygpath” utility in cygwin, but I didn’t figure out yet how to use it
to call another program an to tell him which paths to use.

I think you can run ruby using something like

bash -c “ruby ‘$(cygpath -u %1)’ %2 %3 %4 %5 %6 %7 %8 %9”

Maybe you could put this into some kind of ruby.bat. I don’t know if
cmd.exe provided this kind of substitution which would make things easier.

Thanks again for all the proposals, but the part with “$(cygpath -u
%1)” doesn’t work:
ruby: No such file or derectory – $(cygpath -u ) (Load Error)

No luck this time.

(The problem on the “bash” side, not on the “command-line” side I
suppose.)

Mojca