annotate python/lsex.py @ 402:c00b70509fff

flesh out
author Jeff Hammel <jhammel@mozilla.com>
date Tue, 30 Jul 2013 03:32:31 -0700
parents 77e7cab3e9f2
children 20aa4a6ef719
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env python
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
2 import os
69
71576cdc28ab add option to pritn set of names
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
3 import sys
71576cdc28ab add option to pritn set of names
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
4 from optparse import OptionParser
0
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
5
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
6 # make sure duplicate path elements aren't printed twice
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
7 def ordered_set(alist):
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
8 seen = set()
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
9 new = []
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
10 for item in alist:
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
11 if item in seen:
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
12 continue
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
13 seen.add(item)
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
14 new.append(item)
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
15 return new
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
16
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
17 def lsex(path=None):
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
18 """
367
77e7cab3e9f2 modularize
Jeff Hammel <jhammel@mozilla.com>
parents: 69
diff changeset
19 list of executable files on the path
77e7cab3e9f2 modularize
Jeff Hammel <jhammel@mozilla.com>
parents: 69
diff changeset
20
77e7cab3e9f2 modularize
Jeff Hammel <jhammel@mozilla.com>
parents: 69
diff changeset
21 - path: list or PATH-style string of directories to search.
77e7cab3e9f2 modularize
Jeff Hammel <jhammel@mozilla.com>
parents: 69
diff changeset
22 if not specified, use system PATH
0
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
23 """
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
24
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
25 if path is None:
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
26 # use system path
367
77e7cab3e9f2 modularize
Jeff Hammel <jhammel@mozilla.com>
parents: 69
diff changeset
27 path = os.environ['PATH']
77e7cab3e9f2 modularize
Jeff Hammel <jhammel@mozilla.com>
parents: 69
diff changeset
28 if isinstance(path, basestring):
77e7cab3e9f2 modularize
Jeff Hammel <jhammel@mozilla.com>
parents: 69
diff changeset
29 path = ordered_set(path.split(os.pathsep))
0
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
30
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
31 executables = []
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
32
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
33 # add the executable files to the list
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
34 for i in path:
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
35 if not os.path.isdir(i):
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
36 continue
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
37 files = [ os.path.join(i,j) for j in os.listdir(i) ]
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
38 files = filter(lambda x: os.access(x, os.X_OK), files)
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
39 files.sort() # just to make the output pretty
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
40 executables.extend(files)
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
41 return executables
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
42
69
71576cdc28ab add option to pritn set of names
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
43 def executable_names(path=None):
71576cdc28ab add option to pritn set of names
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
44 executables = lsex(path)
71576cdc28ab add option to pritn set of names
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
45 executables = set([os.path.basename(i) for i in executables])
71576cdc28ab add option to pritn set of names
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
46 return executables
71576cdc28ab add option to pritn set of names
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
47
0
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
48 if __name__ == '__main__':
69
71576cdc28ab add option to pritn set of names
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
49 parser = OptionParser()
71576cdc28ab add option to pritn set of names
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
50 parser.add_option('--names', action='store_true', default=False,
71576cdc28ab add option to pritn set of names
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
51 help="list only the set of names")
71576cdc28ab add option to pritn set of names
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
52
71576cdc28ab add option to pritn set of names
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
53 options, args = parser.parse_args()
71576cdc28ab add option to pritn set of names
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
54 if options.names:
71576cdc28ab add option to pritn set of names
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
55 for i in sorted(executable_names()):
71576cdc28ab add option to pritn set of names
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
56 print i
71576cdc28ab add option to pritn set of names
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
57 sys.exit(0)
71576cdc28ab add option to pritn set of names
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
58
0
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
59 for i in lsex():
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
60 print i