本文整理汇总了Python中os.defpath方法的典型用法代码示例。如果您正苦于以下问题:Python os.defpath方法的具体用法?Python os.defpath怎么用?Python os.defpath使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类os
的用法示例。
在下文中一共展示了os.defpath方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: find_executable
# 需要导入模块: import os [as 别名]
# 或者: from os import defpath [as 别名]
def find_executable(executable, path=None):
"""Tries to find 'executable' in the directories listed in 'path'.
A string listing directories separated by 'os.pathsep'; defaults to
os.environ['PATH']. Returns the complete filename or None if not found.
"""
if path is None:
path = os.environ.get('PATH', os.defpath)
paths = path.split(os.pathsep)
base, ext = os.path.splitext(executable)
if (sys.platform == 'win32' or os.name == 'os2') and (ext != '.exe'):
executable = executable + '.exe'
if not os.path.isfile(executable):
for p in paths:
f = os.path.join(p, executable)
if os.path.isfile(f):
# the file exists, we have a shot at spawn working
return f
return None
else:
return executable
示例2: _popen
# 需要导入模块: import os [as 别名]
# 或者: from os import defpath [as 别名]
def _popen(command, args):
import os
path = os.environ.get("PATH", os.defpath).split(os.pathsep)
path.extend(('/sbin', '/usr/sbin'))
for dir in path:
executable = os.path.join(dir, command)
if (os.path.exists(executable) and
os.access(executable, os.F_OK | os.X_OK) and
not os.path.isdir(executable)):
break
else:
return None
# LC_ALL to ensure English output, 2>/dev/null to prevent output on
# stderr (Note: we don't have an example where the words we search for
# are actually localized, but in theory some system could do so.)
cmd = 'LC_ALL=C %s %s 2>/dev/null' % (executable, args)
return os.popen(cmd)
示例3: which
# 需要导入模块: import os [as 别名]
# 或者: from os import defpath [as 别名]
def which(filename):
"""
Return complete path to executable given by filename.
"""
if not ('PATH' in os.environ) or os.environ['PATH'] == '':
p = os.defpath
else:
p = os.environ['PATH']
pathlist = p.split (os.pathsep)
pathlist.insert(0,".")
pathlist.insert(0,"/bin")
pathlist.insert(0,"/usr/bin")
pathlist.insert(0,"/opt/local/bin")
pathlist.insert(0,"/usr/local/bin")
pathlist.insert(0,"/Applications/Gmsh.app/Contents/MacOS")
for path in pathlist:
f = os.path.join(path, filename)
if os.access(f, os.X_OK):
return f
return None
示例4: which
# 需要导入模块: import os [as 别名]
# 或者: from os import defpath [as 别名]
def which(filename):
"""
Return complete path to executable given by filename.
"""
if not ('PATH' in os.environ) or os.environ['PATH'] == '':
p = os.defpath
else:
p = os.environ['PATH']
pathlist = p.split (os.pathsep)
pathlist.append(".")
for path in pathlist:
f = os.path.join(path, filename)
if os.access(f, os.X_OK):
return f
return None
示例5: locate_oc_binary
# 需要导入模块: import os [as 别名]
# 或者: from os import defpath [as 别名]
def locate_oc_binary():
''' Find and return oc binary file '''
# https://github.com/openshift/openshift-ansible/issues/3410
# oc can be in /usr/local/bin in some cases, but that may not
# be in $PATH due to ansible/sudo
paths = os.environ.get("PATH", os.defpath).split(os.pathsep) + ADDITIONAL_PATH_LOOKUPS
oc_binary = 'oc'
# Use shutil.which if it is available, otherwise fallback to a naive path search
try:
which_result = shutil.which(oc_binary, path=os.pathsep.join(paths))
if which_result is not None:
oc_binary = which_result
except AttributeError:
for path in paths:
if os.path.exists(os.path.join(path, oc_binary)):
oc_binary = os.path.join(path, oc_binary)
break
return oc_binary
# pylint: disable=too-few-public-methods
示例6: which
# 需要导入模块: import os [as 别名]
# 或者: from os import defpath [as 别名]
def which (filename):
"""This takes a given filename; tries to find it in the environment path;
then checks if it is executable. This returns the full path to the filename
if found and executable. Otherwise this returns None."""
# Special case where filename already contains a path.
if os.path.dirname(filename) != '':
if os.access (filename, os.X_OK):
return filename
if 'PATH' not in os.environ or os.environ['PATH'] == '':
p = os.defpath
else:
p = os.environ['PATH']
pathlist = p.split(os.pathsep)
for path in pathlist:
f = os.path.join(path, filename)
if os.access(f, os.X_OK):
return f
return None
示例7: which
# 需要导入模块: import os [as 别名]
# 或者: from os import defpath [as 别名]
def which(filename, env=None):
'''This takes a given filename; tries to find it in the environment path;
then checks if it is executable. This returns the full path to the filename
if found and executable. Otherwise this returns None.'''
# Special case where filename contains an explicit path.
if os.path.dirname(filename) != '' and is_executable_file(filename):
return filename
if env is None:
env = os.environ
p = env.get('PATH')
if not p:
p = os.defpath
pathlist = p.split(os.pathsep)
for path in pathlist:
ff = os.path.join(path, filename)
if is_executable_file(ff):
return ff
return None
示例8: activate
# 需要导入模块: import os [as 别名]
# 或者: from os import defpath [as 别名]
def activate(virtualenv):
"""Return a dict with the changes caused by the virtualenv.
The changes are:
- Prepend the path of the virtualenv binary directory to $PATH.
- Set a $VIRTUAL_ENV variable with the path to the activated virtualenv.
"""
system_path = os.environ.get('PATH', os.defpath)
virtualenv_path = os.path.join(virtualenv, BINDIR) # /path/to/venv/bin
path = os.pathsep.join((virtualenv_path, system_path)) # PATH=/path/to/venv/bin:$PATH
env = {'VIRTUAL_ENV': virtualenv}
return {'path': path, 'env': env}
示例9: __enter__
# 需要导入模块: import os [as 别名]
# 或者: from os import defpath [as 别名]
def __enter__(self):
self.path = mkdtemp(prefix='pep517-build-env-')
log.info('Temporary build environment: %s', self.path)
self.save_path = os.environ.get('PATH', None)
self.save_pythonpath = os.environ.get('PYTHONPATH', None)
install_scheme = 'nt' if (os.name == 'nt') else 'posix_prefix'
install_dirs = get_paths(install_scheme, vars={
'base': self.path,
'platbase': self.path,
})
scripts = install_dirs['scripts']
if self.save_path:
os.environ['PATH'] = scripts + os.pathsep + self.save_path
else:
os.environ['PATH'] = scripts + os.pathsep + os.defpath
if install_dirs['purelib'] == install_dirs['platlib']:
lib_dirs = install_dirs['purelib']
else:
lib_dirs = install_dirs['purelib'] + os.pathsep + \
install_dirs['platlib']
if self.save_pythonpath:
os.environ['PYTHONPATH'] = lib_dirs + os.pathsep + \
self.save_pythonpath
else:
os.environ['PYTHONPATH'] = lib_dirs
return self
示例10: _execvpe
# 需要导入模块: import os [as 别名]
# 或者: from os import defpath [as 别名]
def _execvpe(file, args, env=None):
if env is not None:
func = execve
argrest = (args, env)
else:
func = execv
argrest = (args,)
env = environ
head, tail = path.split(file)
if head:
func(file, *argrest)
return
if 'PATH' in env:
envpath = env['PATH']
else:
envpath = defpath
PATH = envpath.split(pathsep)
saved_exc = None
saved_tb = None
for dir in PATH:
fullname = path.join(dir, file)
try:
func(fullname, *argrest)
except error, e:
tb = sys.exc_info()[2]
if (e.errno != errno.ENOENT and e.errno != errno.ENOTDIR
and saved_exc is None):
saved_exc = e
saved_tb = tb
示例11: program_in_path
# 需要导入模块: import os [as 别名]
# 或者: from os import defpath [as 别名]
def program_in_path(program):
path = os.environ.get("PATH", os.defpath).split(os.pathsep)
path = [os.path.join(dir, program) for dir in path]
path = [True for file in path
if os.path.isfile(file) or os.path.isfile(file + '.exe')]
return bool(path)
示例12: test_find_executable
# 需要导入模块: import os [as 别名]
# 或者: from os import defpath [as 别名]
def test_find_executable(self):
with test_support.temp_dir() as tmp_dir:
# use TESTFN to get a pseudo-unique filename
program_noeext = test_support.TESTFN
# Give the temporary program an ".exe" suffix for all.
# It's needed on Windows and not harmful on other platforms.
program = program_noeext + ".exe"
filename = os.path.join(tmp_dir, program)
with open(filename, "wb"):
pass
os.chmod(filename, stat.S_IXUSR)
# test path parameter
rv = find_executable(program, path=tmp_dir)
self.assertEqual(rv, filename)
if sys.platform == 'win32':
# test without ".exe" extension
rv = find_executable(program_noeext, path=tmp_dir)
self.assertEqual(rv, filename)
# test find in the current directory
with test_support.change_cwd(tmp_dir):
rv = find_executable(program)
self.assertEqual(rv, program)
# test non-existent program
dont_exist_program = "dontexist_" + program
rv = find_executable(dont_exist_program , path=tmp_dir)
self.assertIsNone(rv)
# test os.defpath: missing PATH environment variable
with test_support.EnvironmentVarGuard() as env:
from distutils import spawn
with test_support.swap_attr(spawn.os, 'defpath', tmp_dir):
env.pop('PATH')
rv = find_executable(program)
self.assertEqual(rv, filename)
示例13: test_find_mac
# 需要导入模块: import os [as 别名]
# 或者: from os import defpath [as 别名]
def test_find_mac(self):
data = '''\
fake hwaddr
cscotun0 Link encap:UNSPEC HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00
eth0 Link encap:Ethernet HWaddr 12:34:56:78:90:ab
'''
def mock_popen(cmd):
if sys.platform == 'cli':
return io.StringIO(data)
return io.BytesIO(data)
path = os.environ.get("PATH", os.defpath).split(os.pathsep)
path.extend(('/sbin', '/usr/sbin'))
for dir in path:
executable = os.path.join(dir, 'ifconfig')
if (os.path.exists(executable) and
os.access(executable, os.F_OK | os.X_OK) and
not os.path.isdir(executable)):
break
else:
self.skipTest('requires ifconfig')
with test_support.swap_attr(os, 'popen', mock_popen):
mac = uuid._find_mac(
command='ifconfig',
args='',
hw_identifiers=['hwaddr'],
get_index=lambda x: x + 1,
)
self.assertEqual(mac, 0x1234567890ab)
示例14: __enter__
# 需要导入模块: import os [as 别名]
# 或者: from os import defpath [as 别名]
def __enter__(self):
self._temp_dir.create()
self.save_path = os.environ.get('PATH', None)
self.save_pythonpath = os.environ.get('PYTHONPATH', None)
self.save_nousersite = os.environ.get('PYTHONNOUSERSITE', None)
install_scheme = 'nt' if (os.name == 'nt') else 'posix_prefix'
install_dirs = get_paths(install_scheme, vars={
'base': self.path,
'platbase': self.path,
})
scripts = install_dirs['scripts']
if self.save_path:
os.environ['PATH'] = scripts + os.pathsep + self.save_path
else:
os.environ['PATH'] = scripts + os.pathsep + os.defpath
# Note: prefer distutils' sysconfig to get the
# library paths so PyPy is correctly supported.
purelib = get_python_lib(plat_specific=0, prefix=self.path)
platlib = get_python_lib(plat_specific=1, prefix=self.path)
if purelib == platlib:
lib_dirs = purelib
else:
lib_dirs = purelib + os.pathsep + platlib
if self.save_pythonpath:
os.environ['PYTHONPATH'] = lib_dirs + os.pathsep + \
self.save_pythonpath
else:
os.environ['PYTHONPATH'] = lib_dirs
os.environ['PYTHONNOUSERSITE'] = '1'
return self.path
示例15: _find_in_path
# 需要导入模块: import os [as 别名]
# 或者: from os import defpath [as 别名]
def _find_in_path(self, cmd):
PATH = os.environ.get("PATH", os.defpath).split(os.pathsep)
for x in PATH:
possible = os.path.join(x, cmd)
if os.path.exists(possible):
return possible
return None