Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Working directory for external commands

Reply
Thread Tools

Working directory for external commands

 
 
Duckz King_duckz
Guest
Posts: n/a
 
      02-09-2010
Hello everyone, I've been searching on google for a while now but I
can't find any solution to my question. Basically I want to be able to
specify the working directory for any external command I might launch,
without using ugly globals :S
What I'm trying to write is a wrapper class for git. As a client, I want
to be able to write, for example:

Code:
git = GitWrapper.new
puts git.status
and of course I don't want to know what's happening inside status().

In order to invoke git I'm doing `git status`, or `"c:/program
files/git/bin/sh.exe" -c "git status"`, depending on the OS and other
things. The problem is, backticks as well as %x, system and friends rely
on Dir.pwd, so in order to get a valid result from git I should do:
Code:
def status()
Dir.chdir("some/dir") do
return `git status`
end
end
which I'd rather avoid for its obvious fragility:
Code:
git = GitWrapper.new
a = Thread.new {10000.times do {Dir.chdir("some/other/dir");
do_some_work(); `rm -rf '*'`}}
puts git.status() # OMG!!!!!111
a.join
Any suggestions?
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Marnen Laibow-Koser
Guest
Posts: n/a
 
      02-09-2010
Duckz King_duckz wrote:
> Hello everyone, I've been searching on google for a while now but I
> can't find any solution to my question. Basically I want to be able to
> specify the working directory for any external command I might launch,
> without using ugly globals :S
> What I'm trying to write is a wrapper class for git.


This doesn't directly answer your question, but have you looked at Grit?

Best,
--Â*
Marnen Laibow-Koser
http://www.marnen.org

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      02-09-2010
2010/2/9 Duckz King_duckz <>:
> Hello everyone, I've been searching on google for a while now but I
> can't find any solution to my question. Basically I want to be able to
> specify the working directory for any external command I might launch,
> without using ugly globals :S
> What I'm trying to write is a wrapper class for git. As a client, I want
> to be able to write, for example:
>
>
Code:
> git =3D GitWrapper.new
> puts git.status
>
>
> and of course I don't want to know what's happening inside status().
>
> In order to invoke git I'm doing `git status`, or `"c:/program
> files/git/bin/sh.exe" -c "git status"`, depending on the OS and other
> things. The problem is, backticks as well as %x, system and friends rely
> on Dir.pwd, so in order to get a valid result from git I should do:
>
Code:
> def status()
> =A0 =A0Dir.chdir("some/dir") do
> =A0 =A0 =A0 =A0return `git status`
> =A0 =A0end
> end
>
> which I'd rather avoid for its obvious fragility:
>
Code:
> git =3D GitWrapper.new
> a =3D Thread.new {10000.times do {Dir.chdir("some/other/dir");
> do_some_work(); `rm -rf '*'`}}
> puts git.status() # OMG!!!!!111
> a.join
>
>
> Any suggestions?


The only safe way I can think of is to defer the chdir to a child
process. The shortest form would be

irb(main):006:0> s=3D`cd /tmp && exec pwd`
=3D> "/tmp\n"
irb(main):007:0>

(Using "git status" instead of "pwd" of course.)

A Ruby solution would probably include IO.popen or popen3 and fork.

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

 
Reply With Quote
 
Duckz King_duckz
Guest
Posts: n/a
 
      02-09-2010
@Marnen Laibow-Koser:
I didn't hear of it, but from a quick look it seems to allow a much
cleaner solution, I'll look into that! Anyways, the global Dir problem
has already occurred to me in other situations, and I'm still interested
in finding a safe solution! I wonder why popen or system don't allow an
optional parameter to specify the working directory!

@Robert Klemme:
Thanks for the hint, I guess I'll follow that path to write an intial
version, before looking in depth into Grit. Are there any issues with
fork implementation on Windows in Ruby 1.9.1? Still, I find it a shame
you can't specify the cwd as a parameter (as in CreateProcess() on
Windows for example, but I'm sure most OS have an equivalent).
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Albert Schlef
Guest
Posts: n/a
 
      02-09-2010
Duckz King_duckz wrote:
> I guess I'll follow that path to write an intial
> version, before looking in depth into Grit.


BTW, there are two gems: 'grit' and 'git'. I don't know what's the
difference between them.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Marnen Laibow-Koser
Guest
Posts: n/a
 
      02-09-2010
Albert Schlef wrote:
> Duckz King_duckz wrote:
>> I guess I'll follow that path to write an intial
>> version, before looking in depth into Grit.

>
> BTW, there are two gems: 'grit' and 'git'. I don't know what's the
> difference between them.


Grit is pure Ruby. I think the git gem is just a wrapper.

Best,
--Â*
Marnen Laibow-Koser
http://www.marnen.org

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
System.IO.Directory.GetDirectories() and System.IO.Directory.GetFiles() are not returning the specified directory Nathan Sokalski ASP .Net 2 09-06-2007 03:58 PM
Running external system commands in Java caleb Java 0 01-03-2006 02:17 AM
Need Help Differentiating Bad Commands From Incomplete Commands Tim Stanka Python 1 08-02-2004 02:08 AM
capturing the output of external commands Avi Kak Python 4 07-26-2004 03:16 AM
Re: man pages for C commands (GCC commands) Ben Pfaff C Programming 4 06-28-2003 06:21 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57