ruby under cygwin & windows paths

M

Mojca Miklavec

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 (LoadE=
rror)
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 Miklavec
 
D

David Vallner

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 =20
(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 Miklavec


I'd guess thst's because the Cygwin ruby expects a POSIX path, not the =20
Windows one. The wonderful tool Cygwin is, it doesn't do miracles - you =20
should always use Cygwin tools from a Cygwin shell, not from CMD or by =20
putting them on the path by default. The first case works because for a =20
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 =20
machine.

David Vallner
 
R

Robert Klemme

Mojca said:
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?

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.

Kind regards

robert
 
D

David Vallner

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

David Vallner


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

David Vallner
 
M

Mojca Miklavec

David said:
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
 
T

Thomas

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
 
D

David Vallner

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 =20
cmd.exe provided this kind of substitution which would make things =20
easier.

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

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

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

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

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

David Vallner
 
D

David Vallner

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

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

cheers

robert

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

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

David Vallner
 
R

Robert Klemme

2006/1/14 said:
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 r= i
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=3D%PATH%;M:\y\local\path

Kind regards

robert
 
N

nobu

Hi,

At Fri, 13 Jan 2006 21:12:02 +0900,
Mojca Miklavec 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?
 
M

Mojca Miklavec

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.)
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 "al= l
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".

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.
It should be doable. Configure it with "./configure --prefix=3D'~'" and i= t
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.

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.

Hi,

Mojca Miklavec 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 (L= oadError)
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?

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

unless defined? ownpath
ownpath =3D $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
 
M

Mojca Miklavec

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=
 
D

David Vallner

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 =20
suppose.)

Mojca


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

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

David Vallner
 
T

Thomas

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.
 
M

Mojca Miklavec

Robert Klemme said:
You just need to set the environment variable RUBYLIB appropriately.
Either Windows or Unix path convention will do. You can do that either a= d
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=3D/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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top