annotate which.py @ 1:f4b604d44ff5 default tip

rename console script to avoid import madness
author Jeff Hammel <k0scist@gmail.com>
date Tue, 04 Jul 2017 20:56:43 -0700
parents bcd62e6d822c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
bcd62e6d822c initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env python
bcd62e6d822c initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
2
bcd62e6d822c initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
3 import os
bcd62e6d822c initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
4 import sys
bcd62e6d822c initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
5
bcd62e6d822c initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
6 def which(fileName, path=os.environ['PATH']):
bcd62e6d822c initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
7 """python equivalent of which; should really be in the stdlib"""
bcd62e6d822c initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
8 dirs = path.split(os.pathsep)
bcd62e6d822c initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
9 for dir in dirs:
bcd62e6d822c initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
10 if os.path.isfile(os.path.join(dir, fileName)):
bcd62e6d822c initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
11 return os.path.join(dir, fileName)
bcd62e6d822c initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
12 if os.path.isfile(os.path.join(dir, fileName + ".exe")):
bcd62e6d822c initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
13 return os.path.join(dir, fileName + ".exe")
bcd62e6d822c initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
14
bcd62e6d822c initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
15 def main(args=sys.argv[1:]):
bcd62e6d822c initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
16 """CLI"""
bcd62e6d822c initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
17 for i in sys.argv[1:]:
bcd62e6d822c initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
18 print which(i)
bcd62e6d822c initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
19
bcd62e6d822c initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
20 if __name__ == '__main__':
bcd62e6d822c initial commit
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
21 main()