Completed
Push — master ( c2b843...91b860 )
by Felipe A.
49s
created

browsepy.which()   B

Complexity

Conditions 5

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 5
dl 0
loc 8
rs 8.5455
1
#!/usr/bin/env python
2
# -*- coding: UTF-8 -*-
3
4
import os
5
import os.path
6
import sys
7
import itertools
8
9
PY_LEGACY = sys.version_info[0] < 3
10
if PY_LEGACY:
11
    FileNotFoundError = type('FileNotFoundError', (OSError,), {})
12
    range = xrange
13
    filter = itertools.ifilter
14
    str_base = basestring
15
else:
16
    FileNotFoundError = FileNotFoundError
17
    range = range
18
    filter = filter
19
    str_base = str
20
21
def isnonstriterable(iterable):
22
    return hasattr(iterable, '__iter__') and not isinstance(iterable, str_base)
23
24
def which(name,
25
          env_path=[path.strip('"') for path in os.environ['PATH'].split(os.pathsep)],
26
          is_executable_fnc=lambda path: (os.path.isfile(path) and os.access(path, os.X_OK))):
27
    for path in env_path:
28
        exe_file = os.path.join(path, name)
29
        if is_executable_fnc(exe_file):
30
            return exe_file
31
    return None
32