Retrieving the full path of Unix apps

Discussion in 'Python' started by Lorin Hochstein, Oct 5, 2004.

  1. 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
     
    Lorin Hochstein, Oct 5, 2004
    #1
    1. Advertising

  2. On 2004-10-05, Lorin Hochstein <> 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
     
    Grant Edwards, Oct 5, 2004
    #2
    1. Advertising

  3. Grant Edwards wrote:
    > On 2004-10-05, Lorin Hochstein <> 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
     
    Lorin Hochstein, Oct 5, 2004
    #3
  4. Lorin Hochstein

    Andrew Dalke Guest

    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
     
    Andrew Dalke, Oct 5, 2004
    #4
  5. 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
     
    Michael Hoffman, Oct 5, 2004
    #5
  6. Lorin Hochstein

    Andrew Dalke Guest

    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
     
    Andrew Dalke, Oct 5, 2004
    #6
  7. Andrew Dalke <> 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 <> -- http://www.craig-wood.com/nick
     
    Nick Craig-Wood, Oct 6, 2004
    #7
  8. Lorin Hochstein

    Andrew Dalke Guest

    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
     
    Andrew Dalke, Oct 6, 2004
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Samuel R. Neff
    Replies:
    2
    Views:
    609
    bradley
    Jun 10, 2005
  2. marathoner
    Replies:
    1
    Views:
    420
    Alexey Smirnov
    Mar 14, 2007
  3. dude
    Replies:
    5
    Views:
    1,763
    Jussi Piitulainen
    Mar 17, 2011
  4. marathoner

    FileUpLoad Control - Retrieving full pathname

    marathoner, Mar 14, 2007, in forum: ASP .Net Web Controls
    Replies:
    3
    Views:
    122
    Steve C. Orr [MCSD, MVP, CSM, ASP Insider]
    Mar 22, 2007
  5. Replies:
    1
    Views:
    190
    Ken Bloom
    May 28, 2007
Loading...

Share This Page