makefh and which functions in utils.py

This commit is contained in:
Martin A. Brown 2016-02-12 10:31:50 -08:00
parent fc9da80f6f
commit a74bcb645e
2 changed files with 31 additions and 14 deletions

View File

@ -5,19 +5,8 @@ from __future__ import absolute_import, division, print_function
import os
import inspect
from .utils import logger, makefh
from . import doctypes
from .utils import logger
def makefh(thing):
if isinstance(thing, file):
f = thing
elif isinstance(thing, str) and os.path.isfile(thing):
f = open(thing)
else:
raise TypeError("Cannot make file from type %s of %r" %
(type(thing), thing,))
return f
def listDoctypes():

View File

@ -2,6 +2,7 @@
from __future__ import absolute_import, division, print_function
import os
import sys
import logging
@ -15,8 +16,35 @@ def getLogger(**opts):
logger = getLogger()
def runner(cmd, env, stdin, stdout, stderr):
pass
def is_executable(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
def which(program):
'''return None or the full path to an executable (respecting $PATH)
http://stackoverflow.com/questions/377017/test-if-executable-exists-in-python/377028#377028
'''
fpath, fname = os.path.split(program)
if fpath and is_executable(program):
return program
else:
for path in os.environ["PATH"].split(os.pathsep):
path = path.strip('"')
sut = os.path.join(path, program)
if is_executable(sut):
return sut
return None
def makefh(thing):
if isinstance(thing, file):
f = thing
elif isinstance(thing, str) and os.path.isfile(thing):
f = open(thing)
else:
raise TypeError("Cannot make file from type %s of %r" %
(type(thing), thing,))
return f
#
# -- end of file