annotate python/lsex.py @ 897:ba2630ce851b

update default branch for git
author Jeff Hammel <k0scist@gmail.com>
date Mon, 01 Aug 2022 16:40:53 -0700
parents 20aa4a6ef719
children
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
865
20aa4a6ef719 python3
Jeff Hammel <k0scist@gmail.com>
parents: 367
diff changeset
2
0
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
3 import os
69
71576cdc28ab add option to pritn set of names
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
4 import sys
71576cdc28ab add option to pritn set of names
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
5 from optparse import OptionParser
0
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
6
865
20aa4a6ef719 python3
Jeff Hammel <k0scist@gmail.com>
parents: 367
diff changeset
7 try:
20aa4a6ef719 python3
Jeff Hammel <k0scist@gmail.com>
parents: 367
diff changeset
8 # python 2
20aa4a6ef719 python3
Jeff Hammel <k0scist@gmail.com>
parents: 367
diff changeset
9 string = (str, unicode)
20aa4a6ef719 python3
Jeff Hammel <k0scist@gmail.com>
parents: 367
diff changeset
10 except NameError:
20aa4a6ef719 python3
Jeff Hammel <k0scist@gmail.com>
parents: 367
diff changeset
11 # python 3
20aa4a6ef719 python3
Jeff Hammel <k0scist@gmail.com>
parents: 367
diff changeset
12 string = (str,)
20aa4a6ef719 python3
Jeff Hammel <k0scist@gmail.com>
parents: 367
diff changeset
13
0
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
14 def ordered_set(alist):
865
20aa4a6ef719 python3
Jeff Hammel <k0scist@gmail.com>
parents: 367
diff changeset
15 """make sure duplicate path elements aren't printed twice"""
20aa4a6ef719 python3
Jeff Hammel <k0scist@gmail.com>
parents: 367
diff changeset
16
0
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
17 seen = set()
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
18 new = []
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
19 for item in alist:
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
20 if item in seen:
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
21 continue
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
22 seen.add(item)
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
23 new.append(item)
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
24 return new
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
25
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
26 def lsex(path=None):
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
27 """
367
77e7cab3e9f2 modularize
Jeff Hammel <jhammel@mozilla.com>
parents: 69
diff changeset
28 list of executable files on the path
77e7cab3e9f2 modularize
Jeff Hammel <jhammel@mozilla.com>
parents: 69
diff changeset
29
77e7cab3e9f2 modularize
Jeff Hammel <jhammel@mozilla.com>
parents: 69
diff changeset
30 - path: list or PATH-style string of directories to search.
77e7cab3e9f2 modularize
Jeff Hammel <jhammel@mozilla.com>
parents: 69
diff changeset
31 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
32 """
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
33
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
34 if path is None:
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
35 # use system path
367
77e7cab3e9f2 modularize
Jeff Hammel <jhammel@mozilla.com>
parents: 69
diff changeset
36 path = os.environ['PATH']
865
20aa4a6ef719 python3
Jeff Hammel <k0scist@gmail.com>
parents: 367
diff changeset
37 if isinstance(path, string):
367
77e7cab3e9f2 modularize
Jeff Hammel <jhammel@mozilla.com>
parents: 69
diff changeset
38 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
39
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
40 executables = []
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
41
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
42 # 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
43 for i in path:
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
44 if not os.path.isdir(i):
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
45 continue
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
46 files = [ os.path.join(i,j) for j in os.listdir(i) ]
865
20aa4a6ef719 python3
Jeff Hammel <k0scist@gmail.com>
parents: 367
diff changeset
47 files = list(filter(lambda x: os.access(x, os.X_OK), files))
0
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
48 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
49 executables.extend(files)
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
50 return executables
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
51
69
71576cdc28ab add option to pritn set of names
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
52 def executable_names(path=None):
71576cdc28ab add option to pritn set of names
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
53 executables = lsex(path)
71576cdc28ab add option to pritn set of names
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
54 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
55 return executables
71576cdc28ab add option to pritn set of names
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
56
0
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
57 if __name__ == '__main__':
69
71576cdc28ab add option to pritn set of names
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
58 parser = OptionParser()
71576cdc28ab add option to pritn set of names
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
59 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
60 help="list only the set of names")
71576cdc28ab add option to pritn set of names
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
61
71576cdc28ab add option to pritn set of names
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
62 options, args = parser.parse_args()
71576cdc28ab add option to pritn set of names
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
63 if options.names:
865
20aa4a6ef719 python3
Jeff Hammel <k0scist@gmail.com>
parents: 367
diff changeset
64 print ('\n'.join(sorted(executable_names())))
69
71576cdc28ab add option to pritn set of names
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
65 sys.exit(0)
865
20aa4a6ef719 python3
Jeff Hammel <k0scist@gmail.com>
parents: 367
diff changeset
66
0
f3ab51c79813 adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff changeset
67 for i in lsex():
865
20aa4a6ef719 python3
Jeff Hammel <k0scist@gmail.com>
parents: 367
diff changeset
68 print (i)