Re: portable way of locating an executable (like which)

2012-09-22 Thread Ramchandra Apte
On Friday, 21 September 2012 02:37:01 UTC+5:30, gelonida  wrote:
 I'd like to implement the equivalent functionality of the unix command
 
 /usr/bin/which
 
 
 
 The function should work under Linux and under windows.
 
 
 
 Did anybody already implement such a function.
 
 If not, is there a portable way of splitting the environment variable PATH?
 
 
 
 Thanks for any sugestions

shutil.which does this in Python 3.3: 
http://docs.python.org/dev/library/shutil.html#shutil.which
You can copy the code to support older Python versions.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: portable way of locating an executable (like which)

2012-09-21 Thread Tarek Ziadé

On 9/21/12 1:59 AM, Nobody wrote:

On Thu, 20 Sep 2012 23:06:46 +0200, Gelonida N wrote:


I'd like to implement the equivalent functionality of the unix command
/usr/bin/which

The function should work under Linux and under windows.

Note that which attempts to emulate the behaviour of execvp() etc. The
exec(3) manpage will explain the precise algorithm used (e.g. they skip
files for which the process lacks execute permission).

Also, note that the shell has built-in commands, functions, and aliases in
addition to programs. The type built-in command performs a similar
function to which but using the shell's semantics. On some systems,
the default configuration may alias which to type.

On Windows, there's a host of different execute program interface, all
with subtly different semantics: which extensions they will run, which
extensions can be omitted, which paths are used (e.g. %PATH%, paths
from the registry, current directory).


You can also look at shutil.which

http://hg.python.org/cpython/file/aa153b827d17/Lib/shutil.py#l974


Mmmm I wonder why it's removed in the last revs..
--
http://mail.python.org/mailman/listinfo/python-list


Re: portable way of locating an executable (like which)

2012-09-21 Thread Hans Mulder
On 21/09/12 04:31:17, Dave Angel wrote:
 On 09/20/2012 06:04 PM, Jason Swails wrote:
 On Thu, Sep 20, 2012 at 5:06 PM, Gelonida N gelon...@gmail.com wrote:

 I'd like to implement the equivalent functionality of the unix command
 /usr/bin/which

 The function should work under Linux and under windows.

 Did anybody already implement such a function.
 If not, is there a portable way of splitting the environment variable PATH?

 I've used the following in programs I write:

 def which(program):
def is_exe(fpath):
   return os.path.exists(fpath) and os.access(fpath, os.X_OK)

fpath, fname = os.path.split(program)
if fpath:
   if is_exe(program):
  return program
else:
   for path in os.getenv(PATH).split(os.pathsep):

On Posix systems, you need to insert at this point:

if not path:
path = .

  exe_file = os.path.join(path, program)
  if is_exe(exe_file):
 return exe_file
return None

 IIRC, I adapted it from StackOverflow.  I know it works on Linux and Mac OS
 X, but not sure about windows (since I don't know if PATH works the same
 way there).
 
 I don't have a Windows machine set up right now, but I believe there are
 two more directories to search, besides the ones described in the PATH
 variable.
 
 One is the current directory, and the other is the Windows directory
 (maybe also the xxx/system32 or something).
 
 They don't have analogues in Linux or Mac, as far as I know.

On Posix system (inlcuding Linux and Mac OS X), the current
directory is not searched by default.  If there's an empty
string in os.getenv(PATH).split(os.pathsep), then the
current directory will be searched at that point in the part.


Hope this helps,

-- HansM



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: portable way of locating an executable (like which)

2012-09-20 Thread Mark Lawrence

On 20/09/2012 22:06, Gelonida N wrote:

I'd like to implement the equivalent functionality of the unix command
/usr/bin/which

The function should work under Linux and under windows.

Did anybody already implement such a function.


Searching found nothing obvious to me :(


If not, is there a portable way of splitting the environment variable PATH?


With os.sep ?



Thanks for any sugestions




--
Cheers.

Mark Lawrence.

--
http://mail.python.org/mailman/listinfo/python-list


Re: portable way of locating an executable (like which)

2012-09-20 Thread Jason Swails
On Thu, Sep 20, 2012 at 5:06 PM, Gelonida N gelon...@gmail.com wrote:

 I'd like to implement the equivalent functionality of the unix command
 /usr/bin/which

 The function should work under Linux and under windows.

 Did anybody already implement such a function.
 If not, is there a portable way of splitting the environment variable PATH?


I've used the following in programs I write:

def which(program):
   def is_exe(fpath):
  return os.path.exists(fpath) and os.access(fpath, os.X_OK)

   fpath, fname = os.path.split(program)
   if fpath:
  if is_exe(program):
 return program
   else:
  for path in os.getenv(PATH).split(os.pathsep):
 exe_file = os.path.join(path, program)
 if is_exe(exe_file):
return exe_file
   return None

IIRC, I adapted it from StackOverflow.  I know it works on Linux and Mac OS
X, but not sure about windows (since I don't know if PATH works the same
way there).

HTH,
Jason
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: portable way of locating an executable (like which)

2012-09-20 Thread Chris Angelico
On Fri, Sep 21, 2012 at 7:47 AM, Mark Lawrence breamore...@yahoo.co.uk wrote:
 On 20/09/2012 22:06, Gelonida N wrote:

 I'd like to implement the equivalent functionality of the unix command
 /usr/bin/which

 The function should work under Linux and under windows.

 Did anybody already implement such a function.

 Searching found nothing obvious to me :(

 If not, is there a portable way of splitting the environment variable
 PATH?
 With os.sep ?

os.sep is the directory separator, but os.pathsep may be what you
want. Between that and os.getenv('path') you can at least get the
directories. Then on Windows, you also need to check out
os.getenv('pathext') and split _that_ on the semicolon, and try each
of those as a file extension. I'm not sure whether or not Windows will
add extensions from pathext if one is given on the command line - for
instance, if typing foo.exe will search for foo.exe.bat - but the
basics are there.

Alternatively, there may be a Win32 API funct5ion that does this.
Would be worth a look.

ChrisA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: portable way of locating an executable (like which)

2012-09-20 Thread Ian Kelly
On Thu, Sep 20, 2012 at 4:21 PM, Chris Angelico ros...@gmail.com wrote:
 os.sep is the directory separator, but os.pathsep may be what you
 want. Between that and os.getenv('path') you can at least get the
 directories. Then on Windows, you also need to check out
 os.getenv('pathext') and split _that_ on the semicolon, and try each
 of those as a file extension. I'm not sure whether or not Windows will
 add extensions from pathext if one is given on the command line - for
 instance, if typing foo.exe will search for foo.exe.bat - but the
 basics are there.

Easy enough to test:

C:\echo echo hello!  foo.exe.bat

C:\foo.exe
hello!

Yup, it does.  It looks like it tries it without the extension first, though:

C:\copy c:\windows\notepad.exe foo.exe
1 file(s) copied.

C:\foo.exe
[starts notepad]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: portable way of locating an executable (like which)

2012-09-20 Thread Chris Angelico
On Fri, Sep 21, 2012 at 8:32 AM, Ian Kelly ian.g.ke...@gmail.com wrote:
 On Thu, Sep 20, 2012 at 4:21 PM, Chris Angelico ros...@gmail.com wrote:
 os.sep is the directory separator, but os.pathsep may be what you
 want. Between that and os.getenv('path') you can at least get the
 directories. Then on Windows, you also need to check out
 os.getenv('pathext') and split _that_ on the semicolon, and try each
 of those as a file extension. I'm not sure whether or not Windows will
 add extensions from pathext if one is given on the command line - for
 instance, if typing foo.exe will search for foo.exe.bat - but the
 basics are there.

 Easy enough to test:

 C:\echo echo hello!  foo.exe.bat

 C:\foo.exe
 hello!

 Yup, it does.  It looks like it tries it without the extension first, though:

 C:\copy c:\windows\notepad.exe foo.exe
 1 file(s) copied.

 C:\foo.exe
 [starts notepad]

Well, at least it's consistent. Makes your PATH extremely sensitive,
though, easy for anyone to inject executables into it. But then, you
can already do that by putting them in the current directory, so
that's not really any different.

Jason's solution looks fine apart from the PATHEXT requirement, so if
you know you have the full filename and you don't care if the actual
command interpreter will do exactly the same, that'll do you fine.

Is this something that might want to be a function in the os module?
Particularly so if, as I suspect there might be, there's a Win32 API
function that precisely replicates the behaviour of executable
invocation. A while since I've done much Windows programming but I
think there's a SearchPath function?

ChrisA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: portable way of locating an executable (like which)

2012-09-20 Thread Gelonida N

On 09/21/2012 12:21 AM, Chris Angelico wrote:

On Fri, Sep 21, 2012 at 7:47 AM, Mark Lawrence breamore...@yahoo.co.uk wrote:

On 20/09/2012 22:06, Gelonida N wrote:


I'd like to implement the equivalent functionality of the unix command
/usr/bin/which

The function should work under Linux and under windows.

Did anybody already implement such a function.


Searching found nothing obvious to me :(


I was afraid so, but wanted to be sure



If not, is there a portable way of splitting the environment variable
PATH?

With os.sep ?


os.sep is the directory separator, but os.pathsep may be what you
want.


Thanks,
os.pathsep was the missing piece for portably splitting the searchpath


 Between that and os.getenv('path') you can at least get the
directories. Then on Windows, you also need to check out
os.getenv('pathext') and split _that_ on the semicolon, and try each
of those as a file extension. I'm not sure whether or not Windows will
add extensions from pathext if one is given on the command line - for
instance, if typing foo.exe will search for foo.exe.bat - but the
basics are there.

For what I am doing I can even skip trying the pathexts, the ext is 
already given, but good to know :-)




Alternatively, there may be a Win32 API funct5ion that does this.
Would be worth a look.


Yeah true, but ideally I'd like to avoid platform detection and
just have a generic function.




--
http://mail.python.org/mailman/listinfo/python-list


Re: portable way of locating an executable (like which)

2012-09-20 Thread Gelonida N

On 09/21/2012 12:04 AM, Jason Swails wrote:



Thanks a lot Jason,



I've used the following in programs I write:

def which(program):
def is_exe(fpath):
   return os.path.exists(fpath) and os.access(fpath, os.X_OK)

fpath, fname = os.path.split(program)
if fpath:
   if is_exe(program):
  return program
else:
   for path in os.getenv(PATH).split(os.pathsep):
  exe_file = os.path.join(path, program)
  if is_exe(exe_file):
 return exe_file
return None

IIRC, I adapted it from StackOverflow.  I know it works on Linux and Mac
OS X, but not sure about windows (since I don't know if PATH works the
same way there).


I'll try it, the script looks reasonably portable (using os.pathsep)
to really replicate which I had probably to add os.getenv('pathext')
as Chris mentioned.
However for my current use case this is not necessarily required.


HTH,
Jason






--
http://mail.python.org/mailman/listinfo/python-list


Re: portable way of locating an executable (like which)

2012-09-20 Thread Mark Lawrence

On 21/09/2012 00:15, Gelonida N wrote:

On 09/21/2012 12:04 AM, Jason Swails wrote:



Thanks a lot Jason,



I've used the following in programs I write:

def which(program):
def is_exe(fpath):
   return os.path.exists(fpath) and os.access(fpath, os.X_OK)

fpath, fname = os.path.split(program)
if fpath:
   if is_exe(program):
  return program
else:
   for path in os.getenv(PATH).split(os.pathsep):
  exe_file = os.path.join(path, program)
  if is_exe(exe_file):
 return exe_file
return None

IIRC, I adapted it from StackOverflow.  I know it works on Linux and Mac
OS X, but not sure about windows (since I don't know if PATH works the
same way there).


I'll try it, the script looks reasonably portable (using os.pathsep)
to really replicate which I had probably to add os.getenv('pathext')
as Chris mentioned.
However for my current use case this is not necessarily required.


HTH,
Jason








http://nedbatchelder.com/code/utilities/wh_py.html

--
Cheers.

Mark Lawrence.

--
http://mail.python.org/mailman/listinfo/python-list


Re: portable way of locating an executable (like which)

2012-09-20 Thread Nobody
On Thu, 20 Sep 2012 23:06:46 +0200, Gelonida N wrote:

 I'd like to implement the equivalent functionality of the unix command
 /usr/bin/which
 
 The function should work under Linux and under windows.

Note that which attempts to emulate the behaviour of execvp() etc. The
exec(3) manpage will explain the precise algorithm used (e.g. they skip
files for which the process lacks execute permission).

Also, note that the shell has built-in commands, functions, and aliases in
addition to programs. The type built-in command performs a similar
function to which but using the shell's semantics. On some systems,
the default configuration may alias which to type.

On Windows, there's a host of different execute program interface, all
with subtly different semantics: which extensions they will run, which
extensions can be omitted, which paths are used (e.g. %PATH%, paths
from the registry, current directory).

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: portable way of locating an executable (like which)

2012-09-20 Thread Dave Angel
On 09/20/2012 06:04 PM, Jason Swails wrote:
 On Thu, Sep 20, 2012 at 5:06 PM, Gelonida N gelon...@gmail.com wrote:

 I'd like to implement the equivalent functionality of the unix command
 /usr/bin/which

 The function should work under Linux and under windows.

 Did anybody already implement such a function.
 If not, is there a portable way of splitting the environment variable PATH?

 I've used the following in programs I write:

 def which(program):
def is_exe(fpath):
   return os.path.exists(fpath) and os.access(fpath, os.X_OK)

fpath, fname = os.path.split(program)
if fpath:
   if is_exe(program):
  return program
else:
   for path in os.getenv(PATH).split(os.pathsep):
  exe_file = os.path.join(path, program)
  if is_exe(exe_file):
 return exe_file
return None

 IIRC, I adapted it from StackOverflow.  I know it works on Linux and Mac OS
 X, but not sure about windows (since I don't know if PATH works the same
 way there).


I don't have a Windows machine set up right now, but I believe there are
two more directories to search, besides the ones described in the PATH
variable.

One is the current directory, and the other is the Windows directory
(maybe also the xxx/system32 or something).

They don't have analogues in Linux or Mac, as far as I know.

-- 

DaveA

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: portable way of locating an executable (like which)

2012-09-20 Thread Andrew Berg
On 2012.09.20 21:31, Dave Angel wrote:
 I don't have a Windows machine set up right now, but I believe there are
 two more directories to search, besides the ones described in the PATH
 variable.
 
 One is the current directory, and the other is the Windows directory
 (maybe also the xxx/system32 or something).
Those system directories are in the path by default.

-- 
CPython 3.3.0rc2 | Windows NT 6.1.7601.17835
-- 
http://mail.python.org/mailman/listinfo/python-list