Mercurial > hg > config
comparison python/lsex.py @ 865:20aa4a6ef719
python3
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Mon, 18 Jun 2018 14:48:41 -0700 |
parents | 77e7cab3e9f2 |
children |
comparison
equal
deleted
inserted
replaced
864:7191914724f0 | 865:20aa4a6ef719 |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 | |
2 import os | 3 import os |
3 import sys | 4 import sys |
4 from optparse import OptionParser | 5 from optparse import OptionParser |
5 | 6 |
6 # make sure duplicate path elements aren't printed twice | 7 try: |
8 # python 2 | |
9 string = (str, unicode) | |
10 except NameError: | |
11 # python 3 | |
12 string = (str,) | |
13 | |
7 def ordered_set(alist): | 14 def ordered_set(alist): |
15 """make sure duplicate path elements aren't printed twice""" | |
16 | |
8 seen = set() | 17 seen = set() |
9 new = [] | 18 new = [] |
10 for item in alist: | 19 for item in alist: |
11 if item in seen: | 20 if item in seen: |
12 continue | 21 continue |
23 """ | 32 """ |
24 | 33 |
25 if path is None: | 34 if path is None: |
26 # use system path | 35 # use system path |
27 path = os.environ['PATH'] | 36 path = os.environ['PATH'] |
28 if isinstance(path, basestring): | 37 if isinstance(path, string): |
29 path = ordered_set(path.split(os.pathsep)) | 38 path = ordered_set(path.split(os.pathsep)) |
30 | 39 |
31 executables = [] | 40 executables = [] |
32 | 41 |
33 # add the executable files to the list | 42 # add the executable files to the list |
34 for i in path: | 43 for i in path: |
35 if not os.path.isdir(i): | 44 if not os.path.isdir(i): |
36 continue | 45 continue |
37 files = [ os.path.join(i,j) for j in os.listdir(i) ] | 46 files = [ os.path.join(i,j) for j in os.listdir(i) ] |
38 files = filter(lambda x: os.access(x, os.X_OK), files) | 47 files = list(filter(lambda x: os.access(x, os.X_OK), files)) |
39 files.sort() # just to make the output pretty | 48 files.sort() # just to make the output pretty |
40 executables.extend(files) | 49 executables.extend(files) |
41 return executables | 50 return executables |
42 | 51 |
43 def executable_names(path=None): | 52 def executable_names(path=None): |
50 parser.add_option('--names', action='store_true', default=False, | 59 parser.add_option('--names', action='store_true', default=False, |
51 help="list only the set of names") | 60 help="list only the set of names") |
52 | 61 |
53 options, args = parser.parse_args() | 62 options, args = parser.parse_args() |
54 if options.names: | 63 if options.names: |
55 for i in sorted(executable_names()): | 64 print ('\n'.join(sorted(executable_names()))) |
56 print i | |
57 sys.exit(0) | 65 sys.exit(0) |
58 | 66 |
59 for i in lsex(): | 67 for i in lsex(): |
60 print i | 68 print (i) |