Mercurial > hg > config
annotate python/lsex.py @ 107:1da1baa03ac1
add a cli example program
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Mon, 29 Nov 2010 07:21:30 -0800 |
parents | 71576cdc28ab |
children | 77e7cab3e9f2 |
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 """ |
f3ab51c79813
adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
19 list executable files on the path |
69
71576cdc28ab
add option to pritn set of names
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
20 - path: list of directories to search. 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
|
21 """ |
f3ab51c79813
adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
22 |
f3ab51c79813
adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
23 if path is None: |
f3ab51c79813
adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
24 # use system path |
f3ab51c79813
adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
25 path = ordered_set(os.environ['PATH'].split(':')) |
f3ab51c79813
adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
26 |
f3ab51c79813
adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
27 executables = [] |
f3ab51c79813
adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
28 |
f3ab51c79813
adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
29 # 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
|
30 for i in path: |
f3ab51c79813
adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
31 if not os.path.isdir(i): |
f3ab51c79813
adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
32 continue |
f3ab51c79813
adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
33 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
|
34 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
|
35 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
|
36 executables.extend(files) |
f3ab51c79813
adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
37 return executables |
f3ab51c79813
adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
38 |
69
71576cdc28ab
add option to pritn set of names
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
39 def executable_names(path=None): |
71576cdc28ab
add option to pritn set of names
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
40 executables = lsex(path) |
71576cdc28ab
add option to pritn set of names
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
41 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
|
42 return executables |
71576cdc28ab
add option to pritn set of names
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
43 |
0
f3ab51c79813
adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
44 if __name__ == '__main__': |
69
71576cdc28ab
add option to pritn set of names
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
45 parser = OptionParser() |
71576cdc28ab
add option to pritn set of names
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
46 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
|
47 help="list only the set of names") |
71576cdc28ab
add option to pritn set of names
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
48 |
71576cdc28ab
add option to pritn set of names
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
49 options, args = parser.parse_args() |
71576cdc28ab
add option to pritn set of names
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
50 if options.names: |
71576cdc28ab
add option to pritn set of names
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
51 for i in sorted(executable_names()): |
71576cdc28ab
add option to pritn set of names
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
52 print i |
71576cdc28ab
add option to pritn set of names
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
53 sys.exit(0) |
71576cdc28ab
add option to pritn set of names
Jeff Hammel <jhammel@mozilla.com>
parents:
0
diff
changeset
|
54 |
0
f3ab51c79813
adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
55 for i in lsex(): |
f3ab51c79813
adding configuration from https://svn.openplans.org/svn/config_jhammel/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
56 print i |