home *** CD-ROM | disk | FTP | other *** search
/ PC World 2001 April / PCWorld_2001-04_cd.bin / Software / TemaCD / webclean / !!!python!!! / BeOpen-Python-2.0.exe / WHICH.PY < prev    next >
Encoding:
Python Source  |  2000-09-28  |  1.2 KB  |  57 lines

  1. #! /usr/bin/env python
  2.  
  3. # Variant of "which".
  4. # On stderr, near and total misses are reported.
  5. # '-l<flags>' argument adds ls -l<flags> of each file found.
  6.  
  7. import sys
  8. if sys.path[0] in (".", ""): del sys.path[0]
  9.  
  10. import sys, os, string
  11. from stat import *
  12.  
  13. def msg(str):
  14.     sys.stderr.write(str + '\n')
  15.  
  16. pathlist = string.splitfields(os.environ['PATH'], ':')
  17.  
  18. sts = 0
  19. longlist = ''
  20.  
  21. if sys.argv[1:] and sys.argv[1][:2] == '-l':
  22.     longlist = sys.argv[1]
  23.     del sys.argv[1]
  24.  
  25. for prog in sys.argv[1:]:
  26.     ident = ()
  27.     for dir in pathlist:
  28.         file = os.path.join(dir, prog)
  29.         try:
  30.             st = os.stat(file)
  31.         except os.error:
  32.             continue
  33.         if not S_ISREG(st[ST_MODE]):
  34.             msg(file + ': not a disk file')
  35.         else:
  36.             mode = S_IMODE(st[ST_MODE])
  37.             if mode & 0111:
  38.                 if not ident:
  39.                     print file
  40.                     ident = st[:3]
  41.                 else:
  42.                     if st[:3] == ident:
  43.                         s = 'same as: '
  44.                     else:
  45.                         s = 'also: '
  46.                     msg(s + file)
  47.             else:
  48.                 msg(file + ': not executable')
  49.         if longlist:
  50.             sts = os.system('ls ' + longlist + ' ' + file)
  51.             if sts: msg('"ls -l" exit status: ' + `sts`)
  52.     if not ident:
  53.         msg(prog + ': not found')
  54.         sts = 1
  55.  
  56. sys.exit(sts)
  57.