本文整理汇总了Python中which.which函数的典型用法代码示例。如果您正苦于以下问题:Python which函数的具体用法?Python which怎么用?Python which使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了which函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _get_gotool
def _get_gotool(self, tool_name, env):
# First try the pref
# Then try which
# Then try the golang pref
# Finally try which golang
path = [d.strip()
for d in env.get_envvar("PATH", "").split(os.pathsep)
if d.strip()]
tool_path = env.get_pref(tool_name + "DefaultLocation", "")
if tool_path and os.path.exists(tool_path):
return tool_path
ext = sys.platform.startswith("win") and ".exe" or ""
golang_path = env.get_pref("golangDefaultLocation", "")
if golang_path:
tool_path = os.path.join(os.path.dirname(golang_dir), tool_name + ext)
if os.path.exists(tool_path):
return tool_path
try:
return which.which(tool_name, path=path)
except which.WhichError:
pass
try:
golang_path = which.which('golang', path=path)
except which.WhichError:
return None
tool_path = os.path.join(os.path.basename(golang_path, tool_name)) + ext
if os.path.exists(tool_path):
return tool_path
return None
示例2: test_recognize_nodejs_file_with_interpreter
def test_recognize_nodejs_file_with_interpreter(self):
# If we have a node interpreter on our path, then these will be seen as
# Node.js files, otherwise they are seen as JavaScript files.
manifest = [
(tempfile.mktemp(".js"), """\
require('console');
"""),
(tempfile.mktemp(".js"), """\
module.exports = {};
"""),
(tempfile.mktemp(".js"), """\
foo.on('something', function(event) {
console.log(event.name);
});
"""),
]
import which
try:
which.which("node")
lang = "Node.js"
except which.WhichError:
# Could not find node interpreter.
import logging
log = logging.getLogger("test")
log.warn("No node interpreter was found on the path")
lang = "JavaScript"
for name, content in manifest:
path = join(self.data_dir, name)
_writefile(path, content)
koDoc = self._koDocFromPath(path)
self.assertEqual(koDoc.language, lang,
"%r found, expected %r, content %r" % (koDoc.language, lang, content))
示例3: command
def command(cmd):
which(cmd)
def wrapper(argstr, **kwargs):
fullcmd = cmd + ' %s' % argstr
log.debug('Executing shell command: %s' % fullcmd)
return subprocess.Popen(fullcmd, shell=True, **kwargs)
return wrapper
示例4: debug
def debug(self, params, remote, background, debugger, debugparams, slowscript):
import which
use_lldb = False
use_gdb = False
if debugger:
try:
debugger = which.which(debugger)
except Exception as e:
print("You don't have %s in your PATH" % (debugger))
print(e)
return 1
else:
try:
debugger = which.which('gdb')
use_gdb = True
except Exception:
try:
debugger = which.which('lldb')
use_lldb = True
except Exception as e:
print("You don't have gdb or lldb in your PATH")
print(e)
return 1
args = [debugger]
extra_env = { 'MOZ_CRASHREPORTER_DISABLE' : '1' }
if debugparams:
import pymake.process
argv, badchar = pymake.process.clinetoargv(debugparams, os.getcwd())
if badchar:
print("The +debugparams you passed require a real shell to parse them.")
print("(We can't handle the %r character.)" % (badchar,))
return 1
args.extend(argv)
binpath = None
try:
binpath = self.get_binary_path('app')
except Exception as e:
print("It looks like your program isn't built.",
"You can run |mach build| to build it.")
print(e)
return 1
if use_gdb:
args.append('--args')
elif use_lldb:
args.append('--')
args.append(binpath)
if not remote:
args.append('-no-remote')
if not background and sys.platform == 'darwin':
args.append('-foreground')
if params:
args.extend(params)
if not slowscript:
extra_env['JS_DISABLE_SLOW_SCRIPT_SIGNALS'] = '1'
return self.run_process(args=args, append_env=extra_env,
ensure_exit_code=False, pass_thru=True)
示例5: getNodeOrNpmPath
def getNodeOrNpmPath(self, filename):
"""
Return the nodejs or npm path.
"""
if platform.system() == "Windows":
for ext in [".cmd", ".exe", ""]:
try:
nodeOrNpmPath = which.which(filename + ext,
path=self.getPossibleNodePathsWin())
if self.is_valid(nodeOrNpmPath):
return nodeOrNpmPath
except which.WhichError:
pass
else:
try:
return which.which(filename)
except which.WhichError:
pass
if filename == "node":
print(NODE_NOT_FOUND_MESSAGE)
elif filename == "npm":
print(NPM_NOT_FOUND_MESSAGE)
if platform.system() == "Windows":
appPaths = self.getPossibleNodePathsWin()
for p in appPaths:
print(" - %s" % p)
elif platform.system() == "Darwin":
print(" - /usr/local/bin/node")
elif platform.system() == "Linux":
print(" - /usr/bin/nodejs")
return None
示例6: eclipse
def eclipse(self, ide, args):
if ide == 'eclipse':
backend = 'CppEclipse'
elif ide == 'visualstudio':
backend = 'VisualStudio'
if ide == 'eclipse':
try:
which.which('eclipse')
except which.WhichError:
print('Eclipse CDT 8.4 or later must be installed in your PATH.')
print('Download: http://www.eclipse.org/cdt/downloads.php')
return 1
# Here we refresh the whole build. 'build export' is sufficient here and is probably more
# correct but it's also nice having a single target to get a fully built and indexed
# project (gives a easy target to use before go out to lunch).
res = self._mach_context.commands.dispatch('build', self._mach_context)
if res != 0:
return 1
# Generate or refresh the IDE backend.
python = self.virtualenv_manager.python_path
config_status = os.path.join(self.topobjdir, 'config.status')
args = [python, config_status, '--backend=%s' % backend]
res = self._run_command_in_objdir(args=args, pass_thru=True, ensure_exit_code=False)
if res != 0:
return 1
if ide == 'eclipse':
eclipse_workspace_dir = self.get_eclipse_workspace_path()
process = subprocess.check_call(['eclipse', '-data', eclipse_workspace_dir])
elif ide == 'visualstudio':
visual_studio_workspace_dir = self.get_visualstudio_workspace_path()
process = subprocess.check_call(['explorer.exe', visual_studio_workspace_dir])
示例7: push_to_try
def push_to_try(self, msg, verbose):
if not self._use_git:
try:
hg_args = ['hg', 'push-to-try', '-m', msg]
subprocess.check_call(hg_args, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
print('ERROR hg command %s returned %s' % (hg_args, e.returncode))
print('\nmach failed to push to try. There may be a problem '
'with your ssh key, or another issue with your mercurial '
'installation.')
# Check for the presence of the "push-to-try" extension, and
# provide instructions if it can't be found.
try:
subprocess.check_output(['hg', 'showconfig',
'extensions.push-to-try'])
except subprocess.CalledProcessError:
print('\nThe "push-to-try" hg extension is required. It '
'can be installed to Mercurial 3.3 or above by '
'running ./mach mercurial-setup')
sys.exit(1)
else:
try:
which.which('git-cinnabar')
self._git_push_to_try(msg)
except which.WhichError:
print('ERROR git-cinnabar is required to push from git to try with'
'the autotry command.\n\nMore information can by found at '
'https://github.com/glandium/git-cinnabar')
sys.exit(1)
示例8: has_command
def has_command(cmd):
from which import which, WhichError
try:
which(cmd)
return True
except WhichError as e:
return False
示例9: mercurial_setup
def mercurial_setup(self, update_only=False):
"""Ensure Mercurial is optimally configured.
This command will inspect your Mercurial configuration and
guide you through an interactive wizard helping you configure
Mercurial for optimal use on Mozilla projects.
User choice is respected: no changes are made without explicit
confirmation from you.
If "--update-only" is used, the interactive wizard is disabled
and this command only ensures that remote repositories providing
Mercurial extensions are up to date.
"""
import which
import mozboot.bootstrap as bootstrap
# "hg" is an executable script with a shebang, which will be found
# be which.which. We need to pass a win32 executable to the function
# because we spawn a process
# from it.
if sys.platform in ("win32", "msys"):
hg = which.which("hg.exe")
else:
hg = which.which("hg")
if update_only:
bootstrap.update_vct(hg, self._context.state_dir)
else:
bootstrap.configure_mercurial(hg, self._context.state_dir)
示例10: run_test
def run_test(
self,
test_paths,
b2g_home=None,
busybox=None,
device_name=None,
test_objects=None,
log=None,
# ignore parameters from other platforms' options
**kwargs
):
try:
import which
which.which("adb")
except which.WhichError:
# TODO Find adb automatically if it isn't on the path
print(ADB_NOT_FOUND % ("mochitest-remote", b2g_home))
sys.exit(1)
test_path = None
if test_objects:
if len(test_objects) > 1:
print("Warning: Only the first test will be used.")
test_path = self._wrap_path_argument(test_objects[0]["path"])
elif test_paths:
if len(test_paths) > 1:
print("Warning: Only the first test path will be used.")
test_path = self._wrap_path_argument(test_paths[0]).relpath()
import runtestsb2g
parser = runtestsb2g.B2GOptions()
options, args = parser.parse_args([])
options.b2g_path = b2g_home
options.busybox = busybox or os.environ.get("BUSYBOX")
options.localLib = self.bin_dir
options.localBin = self.bin_dir
options.logdir = self.xpcshell_dir
options.manifest = os.path.join(self.xpcshell_dir, "xpcshell.ini")
options.mozInfo = os.path.join(self.topobjdir, "mozinfo.json")
options.objdir = self.topobjdir
options.symbolsPath = (os.path.join(self.distdir, "crashreporter-symbols"),)
options.testingModulesDir = os.path.join(self.tests_dir, "modules")
options.testsRootDir = self.xpcshell_dir
options.testPath = test_path
options.use_device_libs = True
options.emulator = "arm"
if device_name.startswith("emulator"):
if "x86" in device_name:
options.emulator = "x86"
if not options.busybox:
options.busybox = self._download_busybox(b2g_home, options.emulator)
return runtestsb2g.run_remote_xpcshell(parser, options, args, log)
示例11: office_path
def office_path(self):
path = os.environ['PATH']
office = which('ooffice', path=path)
if not office:
office = which('soffice', path=path)
if not office:
raise KeyError("Cannot find OpenOffice on path")
return office[0]
示例12: notify
def notify(self, msg):
"""Show a desktop notification with the supplied message
On Linux and Mac, this will show a desktop notification with the message,
but on Windows we can only flash the screen.
"""
moz_nospam = os.environ.get('MOZ_NOSPAM')
if moz_nospam:
return
try:
if sys.platform.startswith('darwin'):
try:
notifier = which.which('terminal-notifier')
except which.WhichError:
raise Exception('Install terminal-notifier to get '
'a notification when the build finishes.')
self.run_process([notifier, '-title',
'Mozilla Build System', '-group', 'mozbuild',
'-message', msg], ensure_exit_code=False)
elif sys.platform.startswith('linux'):
try:
notifier = which.which('notify-send')
except which.WhichError:
raise Exception('Install notify-send (usually part of '
'the libnotify package) to get a notification when '
'the build finishes.')
self.run_process([notifier, '--app-name=Mozilla Build System',
'Mozilla Build System', msg], ensure_exit_code=False)
elif sys.platform.startswith('win'):
from ctypes import Structure, windll, POINTER, sizeof
from ctypes.wintypes import DWORD, HANDLE, WINFUNCTYPE, BOOL, UINT
class FLASHWINDOW(Structure):
_fields_ = [("cbSize", UINT),
("hwnd", HANDLE),
("dwFlags", DWORD),
("uCount", UINT),
("dwTimeout", DWORD)]
FlashWindowExProto = WINFUNCTYPE(BOOL, POINTER(FLASHWINDOW))
FlashWindowEx = FlashWindowExProto(("FlashWindowEx", windll.user32))
FLASHW_CAPTION = 0x01
FLASHW_TRAY = 0x02
FLASHW_TIMERNOFG = 0x0C
# GetConsoleWindows returns NULL if no console is attached. We
# can't flash nothing.
console = windll.kernel32.GetConsoleWindow()
if not console:
return
params = FLASHWINDOW(sizeof(FLASHWINDOW),
console,
FLASHW_CAPTION | FLASHW_TRAY | FLASHW_TIMERNOFG, 3, 0)
FlashWindowEx(params)
except Exception as e:
self.log(logging.WARNING, 'notifier-failed', {'error':
e.message}, 'Notification center failed: {error}')
示例13: test_info
def test_info(self, **params):
import which
from mozbuild.base import MozbuildObject
self.branches = params['branches']
self.start = params['start']
self.end = params['end']
self.show_info = params['show_info']
self.show_results = params['show_results']
self.show_durations = params['show_durations']
self.show_bugs = params['show_bugs']
self.verbose = params['verbose']
if (not self.show_info and
not self.show_results and
not self.show_durations and
not self.show_bugs):
# by default, show everything
self.show_info = True
self.show_results = True
self.show_durations = True
self.show_bugs = True
here = os.path.abspath(os.path.dirname(__file__))
build_obj = MozbuildObject.from_environment(cwd=here)
self._hg = None
if conditions.is_hg(build_obj):
if self._is_windows():
self._hg = which.which('hg.exe')
else:
self._hg = which.which('hg')
self._git = None
if conditions.is_git(build_obj):
if self._is_windows():
self._git = which.which('git.exe')
else:
self._git = which.which('git')
for test_name in params['test_names']:
print("===== %s =====" % test_name)
self.test_name = test_name
if len(self.test_name) < 6:
print("'%s' is too short for a test name!" % self.test_name)
continue
if self.show_info:
self.set_test_name()
if self.show_results:
self.report_test_results()
if self.show_durations:
self.report_test_durations()
if self.show_bugs:
self.report_bugs()
示例14: run_b2g_test
def run_b2g_test(self, context, tests=None, suite='mochitest', **kwargs):
"""Runs a b2g mochitest."""
if kwargs.get('desktop'):
kwargs['profile'] = kwargs.get('profile') or os.environ.get('GAIA_PROFILE')
if not kwargs['profile'] or not os.path.isdir(kwargs['profile']):
print(GAIA_PROFILE_NOT_FOUND)
sys.exit(1)
if os.path.isfile(os.path.join(kwargs['profile'], 'extensions',
'[email protected]')):
print(GAIA_PROFILE_IS_DEBUG.format(kwargs['profile']))
sys.exit(1)
elif context.target_out:
host_webapps_dir = os.path.join(context.target_out, 'data', 'local', 'webapps')
if not os.path.isdir(os.path.join(
host_webapps_dir, 'test-container.gaiamobile.org')):
print(ENG_BUILD_REQUIRED.format(host_webapps_dir))
sys.exit(1)
# TODO without os.chdir, chained imports fail below
os.chdir(self.mochitest_dir)
# The imp module can spew warnings if the modules below have
# already been imported, ignore them.
with warnings.catch_warnings():
warnings.simplefilter('ignore')
import imp
path = os.path.join(self.mochitest_dir, 'runtestsb2g.py')
with open(path, 'r') as fh:
imp.load_module('mochitest', fh, path,
('.py', 'r', imp.PY_SOURCE))
import mochitest
options = Namespace(**kwargs)
from manifestparser import TestManifest
if tests:
manifest = TestManifest()
manifest.tests.extend(tests)
options.manifestFile = manifest
if options.desktop:
return mochitest.run_desktop_mochitests(options)
try:
which.which('adb')
except which.WhichError:
# TODO Find adb automatically if it isn't on the path
print(ADB_NOT_FOUND.format(options.b2gPath))
return 1
return mochitest.run_remote_mochitests(options)
示例15: run_test
def run_test(self, **kwargs):
try:
import which
which.which('adb')
except which.WhichError:
# TODO Find adb automatically if it isn't on the path
print(ADB_NOT_FOUND % ('mochitest-remote', kwargs["b2g_home"]))
sys.exit(1)
import runtestsb2g
log = kwargs.pop("log")
self.log_manager.enable_unstructured()
if kwargs["xpcshell"] is None:
kwargs["xpcshell"] = "xpcshell"
if kwargs["b2g_path"] is None:
kwargs["b2g_path"] = kwargs["b2g_home"]
if kwargs["busybox"] is None:
kwargs["busybox"] = os.environ.get('BUSYBOX')
if kwargs["busybox"] is None:
kwargs["busybox"] = self._download_busybox(kwargs["b2g_home"], kwargs["emulator"])
if kwargs["localLib"] is None:
kwargs["localLib"] = self.bin_dir
if kwargs["localBin"] is None:
kwargs["localBin"] = self.bin_dir
if kwargs["logdir"] is None:
kwargs["logdir"] = self.xpcshell_dir
if kwargs["manifest"] is None:
kwargs["manifest"] = os.path.join(self.xpcshell_dir, 'xpcshell.ini')
if kwargs["mozInfo"] is None:
kwargs["mozInfo"] = os.path.join(self.topobjdir, 'mozinfo.json')
if kwargs["objdir"] is None:
kwargs["objdir"] = self.topobjdir
if kwargs["symbolsPath"] is None:
kwargs["symbolsPath"] = os.path.join(self.distdir, 'crashreporter-symbols')
if kwargs["testingModulesDir"] is None:
kwargs["testingModulesDir"] = os.path.join(self.tests_dir, 'modules')
if kwargs["use_device_libs"] is None:
kwargs["use_device_libs"] = True
if kwargs["device_name"].startswith('emulator') and 'x86' in kwargs["device_name"]:
kwargs["emulator"] = 'x86'
parser = parser_b2g()
options = argparse.Namespace(**kwargs)
rv = runtestsb2g.run_remote_xpcshell(parser, options, log)
self.log_manager.disable_unstructured()
return rv