Created
July 1, 2016 08:12
-
-
Save limboinf/7424dedfb1da39310b9e0ce4ae1e8587 to your computer and use it in GitHub Desktop.
在pyprof2calltree这个项目中学到的在系统中查看命令是否存在
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding=utf-8 | |
""" | |
在pyprof2calltree这个项目中学到的在系统中查看命令是否存在 | |
https://github.com/pwaller/pyprof2calltree/blob/master/pyprof2calltree.py | |
:copyright: (c) 2016 by fangpeng(@beginman.cn). | |
:license: MIT, see LICENSE for more details. | |
""" | |
import os | |
import sys | |
import subprocess | |
KCACHEGRIND_EXECUTABLES = ["kcachegrind", "qcachegrind"] | |
def is_basestring(s): | |
try: | |
u = unicode | |
# Python 2.x | |
return isinstance(s, basestring) | |
except NameError: | |
# Python 3.x | |
return isinstance(s, (str, bytes)) | |
def is_installed(prog): | |
"""Return whether or not a given executable is installed on the machine.""" | |
devnull = open(os.devnull, 'w') | |
retcode = subprocess.call(['which', prog], stdout=devnull) | |
devnull.close() | |
return retcode == 0 | |
def check(): | |
available_cmd = None | |
for cmd in KCACHEGRIND_EXECUTABLES: | |
if is_installed(cmd): | |
available_cmd = cmd | |
break | |
if available_cmd is None: | |
sys.stderr.write("Could not find %s\n" | |
% ", ".join(KCACHEGRIND_EXECUTABLES)) | |
return | |
if __name__ == '__main__': | |
check() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment