comparison python/lsex.py @ 69:71576cdc28ab

add option to pritn set of names
author Jeff Hammel <jhammel@mozilla.com>
date Wed, 12 May 2010 13:23:08 -0700
parents f3ab51c79813
children 77e7cab3e9f2
comparison
equal deleted inserted replaced
68:3c1f9b412675 69:71576cdc28ab
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 import os 2 import os
3 import sys
4 from optparse import OptionParser
3 5
4 # make sure duplicate path elements aren't printed twice 6 # make sure duplicate path elements aren't printed twice
5 def ordered_set(alist): 7 def ordered_set(alist):
6 seen = set() 8 seen = set()
7 new = [] 9 new = []
13 return new 15 return new
14 16
15 def lsex(path=None): 17 def lsex(path=None):
16 """ 18 """
17 list executable files on the path 19 list executable files on the path
18 o path: list of directories to search. if not specified, use system path 20 - path: list of directories to search. if not specified, use system path
19 """ 21 """
20 22
21 if path is None: 23 if path is None:
22 # use system path 24 # use system path
23 path = ordered_set(os.environ['PATH'].split(':')) 25 path = ordered_set(os.environ['PATH'].split(':'))
32 files = filter(lambda x: os.access(x, os.X_OK), files) 34 files = filter(lambda x: os.access(x, os.X_OK), files)
33 files.sort() # just to make the output pretty 35 files.sort() # just to make the output pretty
34 executables.extend(files) 36 executables.extend(files)
35 return executables 37 return executables
36 38
39 def executable_names(path=None):
40 executables = lsex(path)
41 executables = set([os.path.basename(i) for i in executables])
42 return executables
43
37 if __name__ == '__main__': 44 if __name__ == '__main__':
45 parser = OptionParser()
46 parser.add_option('--names', action='store_true', default=False,
47 help="list only the set of names")
48
49 options, args = parser.parse_args()
50 if options.names:
51 for i in sorted(executable_names()):
52 print i
53 sys.exit(0)
54
38 for i in lsex(): 55 for i in lsex():
39 print i 56 print i