config

view python/lsex.py @ 224:57677a2bf772

remove umask
author Jeff Hammel <jhammel@mozilla.com>
date Wed May 16 08:58:47 2012 -0700 (2 days ago)
parents f3ab51c79813
children
line source
1 #!/usr/bin/env python
2 import os
3 import sys
4 from optparse import OptionParser
6 # make sure duplicate path elements aren't printed twice
7 def ordered_set(alist):
8 seen = set()
9 new = []
10 for item in alist:
11 if item in seen:
12 continue
13 seen.add(item)
14 new.append(item)
15 return new
17 def lsex(path=None):
18 """
19 list executable files on the path
20 - path: list of directories to search. if not specified, use system path
21 """
23 if path is None:
24 # use system path
25 path = ordered_set(os.environ['PATH'].split(':'))
27 executables = []
29 # add the executable files to the list
30 for i in path:
31 if not os.path.isdir(i):
32 continue
33 files = [ os.path.join(i,j) for j in os.listdir(i) ]
34 files = filter(lambda x: os.access(x, os.X_OK), files)
35 files.sort() # just to make the output pretty
36 executables.extend(files)
37 return executables
39 def executable_names(path=None):
40 executables = lsex(path)
41 executables = set([os.path.basename(i) for i in executables])
42 return executables
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")
49 options, args = parser.parse_args()
50 if options.names:
51 for i in sorted(executable_names()):
52 print i
53 sys.exit(0)
55 for i in lsex():
56 print i