Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Retrieving the full path of Unix apps

Reply
Thread Tools

Retrieving the full path of Unix apps

 
 
Lorin Hochstein
Guest
Posts: n/a
 
      10-05-2004
Hello all,

I'd like to retrieve the full path of an arbitrary program on a Unix
system (e.g. gcc). What's the nicest way to do this? Currently I'm
invoking the "which" program and parsing what it outputs to determine if
the output looks like a path.

Here's what I'm currently doing. Is there a more elegant way to do this?


def fullpath(prog):
"""Compute the full path of a program"""
s = os.popen('which ' + prog).readline()[:-1]
# Confirm that the return value looks like a path
r = re.compile('/(\w+/)*' + prog)
if r.match(s) is not None:
return s
else:
raise ValueError,s



Thanks,

Lorin
 
Reply With Quote
 
 
 
 
Grant Edwards
Guest
Posts: n/a
 
      10-05-2004
On 2004-10-05, Lorin Hochstein <(E-Mail Removed)> wrote:

> I'd like to retrieve the full path of an arbitrary program on
> a Unix system (e.g. gcc).


If you really have to worry about any arbitrary program, then
it's not really a solvable problem. There may be any
non-negative number of paths for a file with a particular name.
(Including zero.) Finding an exhastive list requires you to
walk the entire directory tree from the root down.

> What's the nicest way to do this?


Do you care _which_ path you end up with in the case where
there are multiple ones that end in the filename of interest?

Are you assuming that the program is in one of the directories
listed in the PATH environment variable?

> Currently I'm invoking the "which" program and parsing what it
> outputs to determine if the output looks like a path.
>
> Here's what I'm currently doing. Is there a more elegant way
> to do this?


You could grab the value of the environment variable PATH, and
search those directories for the file in question. That's
pretty much what the 'which' command does.

--
Grant Edwards grante Yow! Is it FUN to be
at a MIDGET?
visi.com
 
Reply With Quote
 
 
 
 
Lorin Hochstein
Guest
Posts: n/a
 
      10-05-2004
Grant Edwards wrote:
> On 2004-10-05, Lorin Hochstein <(E-Mail Removed)> wrote:
>
>
> Do you care _which_ path you end up with in the case where
> there are multiple ones that end in the filename of interest?
>
> Are you assuming that the program is in one of the directories
> listed in the PATH environment variable?
>


Sorry, I should've been more clear. I am assuming the program is in one
of the directories listed in the PATH environment variable. I want the
same one that 'which' will return: the one that would get invoked if
someone just typed in the name of the program at the command-line.

>
> You could grab the value of the environment variable PATH, and
> search those directories for the file in question. That's
> pretty much what the 'which' command does.
>


That does seem more elegant than relying on 'which'.

Thanks,


Lorin

 
Reply With Quote
 
Andrew Dalke
Guest
Posts: n/a
 
      10-05-2004
Lorin Hochstein wrote:
> I'd like to retrieve the full path of an arbitrary program on a Unix
> system (e.g. gcc). What's the nicest way to do this? Currently I'm
> invoking the "which" program and parsing what it outputs to determine if
> the output looks like a path.


Here's an incomplete implementation of 'which'

import os

def is_executable(filename):
# Placeholder: not sure how to do this
return 1

def which(app):
dirnames = os.environ["PATH"].split(os.path.pathsep)
for dirname in dirnames:
filename = os.path.join(dirname, app)
if (os.path.exists(filename) and
os.path.isfile(filename) and
is_executable(filename)):
return filename
return None

>>> print which("ls")

/bin/ls
>>> print which("qwerty")

None
>>> print which("python")

/usr/local/bin/python
>>>


Andrew
http://www.velocityreviews.com/forums/(E-Mail Removed)


 
Reply With Quote
 
Michael Hoffman
Guest
Posts: n/a
 
      10-05-2004
Lorin Hochstein wrote:

> That does seem more elegant than relying on 'which'.


I dunno. I imagine that after a certain point, relying on 'which' will
be faster since it is written in C. You're going to have to do process a
lot of stats to find out which files you have execute permission for.
And they have already dealt with all the caveats that you haven't even
thought of yet
--
Michael Hoffman
 
Reply With Quote
 
Andrew Dalke
Guest
Posts: n/a
 
      10-05-2004
Michael Hoffman wrote:
> You're going to have to do process a
> lot of stats to find out which files you have execute permission for.
> And they have already dealt with all the caveats that you haven't even
> thought of yet


OTOH, if the path or filename contains a "\n" then the
OP's code won't work. If the filesystem uses Unicode then
there will also be problems.

Andrew
(E-Mail Removed)
 
Reply With Quote
 
Nick Craig-Wood
Guest
Posts: n/a
 
      10-06-2004
Andrew Dalke <(E-Mail Removed)> wrote:
> Here's an incomplete implementation of 'which'
>
> import os
>
> def is_executable(filename):
> # Placeholder: not sure how to do this
> return 1


def is_executable(filename):
return os.access(filename, os.X_OK)

Is probably close enough unless you are running setuid (and even then
it probably does the right thing!)

> def which(app):
> dirnames = os.environ["PATH"].split(os.path.pathsep)
> for dirname in dirnames:
> filename = os.path.join(dirname, app)
> if (os.path.exists(filename) and
> os.path.isfile(filename) and
> is_executable(filename)):
> return filename
> return None


--
Nick Craig-Wood <(E-Mail Removed)> -- http://www.craig-wood.com/nick
 
Reply With Quote
 
Andrew Dalke
Guest
Posts: n/a
 
      10-06-2004
Nick Craig-Wood wrote:
> def is_executable(filename):
> return os.access(filename, os.X_OK)


Ahh. Didn't know about 'access'. Looks like I
could change

>> if (os.path.exists(filename) and
>> os.path.isfile(filename) and
>> is_executable(filename)):


into

if (os.access(filename, os.X_OK) and
os.path.isfile(filename)):

because access returns false if the file/directory
doesn't exist.

Andrew
(E-Mail Removed)
 
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
os.path.walk() to get full path of all files dude Python 5 03-17-2011 09:09 AM
.NET 2.0 Web Apps / .NET 3.5 ASP.NET Apps - Differences/Similariti london1919 MCTS 1 12-08-2008 03:16 PM
Which do you prefer? MCTS: Web Apps or MCTS: Windows Apps? Willy David Jr MCTS 3 01-09-2007 05:49 AM
Top 3 firewalls, anti-virus apps, anti-spyware apps The Master of The Known Universe Computer Support 10 05-12-2006 11:00 PM
Running python apps from within python apps aph Python 4 01-14-2006 04:06 PM



Advertisments