forked from akkana/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpathmatch
executable file
·53 lines (44 loc) · 1.52 KB
/
pathmatch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env python
# pathmatch: Search for a string in $PATH
# usage pathmatch pattern [pattern ...]
# e.g. if you're looking for volume control, pathmatch mix vol
# will show you all programs in your path that incorporate "mix" or "vol"
# anywhere in their names.
#
# Copyright 2003,2011 by Akkana Peck.
# Share and enjoy under the terms of the GPL v2 or later.
import sys, string, os, types
def getMatchingFiles(pat, files) :
matches = []
lc = string.lower(pat)
for fil in range(0, len(files)) :
#print "Looking for", lc, "in", files[fil]
callfile = string.lower(files[fil])
if (string.find(callfile, lc) >= 0) :
matches.append(files[fil])
return matches
def findPatInPath(pat, pathdirs) :
"""Return a set of files in the path matching the pattern"""
matchprogs = set([])
for dir in pathdirs :
try :
progs = os.listdir(dir)
except OSError as e:
continue
matches = getMatchingFiles(pat, progs)
for prog in matches :
# Only add it if it's an executable file:
fullpath = os.path.join(dir, prog)
if not os.access(fullpath, os.X_OK) :
continue
if os.path.isdir(fullpath) :
continue
matchprogs.add(prog)
return matchprogs
# main()
pathdirs = string.split(os.environ["PATH"], ":")
for argp in range(1, len(sys.argv)) :
progs = list(findPatInPath(sys.argv[argp], pathdirs))
progs.sort()
for p in progs :
print(p)