2013-07-03 04:33:48 +04:00
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
2015-06-22 03:39:09 +03:00
|
|
|
from __future__ import absolute_import, print_function, unicode_literals
|
2013-07-03 04:33:48 +04:00
|
|
|
|
2016-05-02 02:22:31 +03:00
|
|
|
import __main__
|
2013-07-03 04:33:48 +04:00
|
|
|
import argparse
|
2016-05-13 16:06:08 +03:00
|
|
|
import json
|
2013-07-03 04:33:48 +04:00
|
|
|
import logging
|
2015-03-25 09:05:28 +03:00
|
|
|
import mozpack.path as mozpath
|
2013-07-03 04:33:48 +04:00
|
|
|
import os
|
2015-09-30 16:44:48 +03:00
|
|
|
import platform
|
|
|
|
import subprocess
|
|
|
|
import sys
|
2015-07-02 22:18:52 +03:00
|
|
|
import which
|
2016-02-10 02:34:37 +03:00
|
|
|
from distutils.version import LooseVersion
|
2013-07-03 04:33:48 +04:00
|
|
|
|
|
|
|
from mozbuild.base import (
|
|
|
|
MachCommandBase,
|
|
|
|
)
|
|
|
|
|
|
|
|
from mach.decorators import (
|
|
|
|
CommandArgument,
|
|
|
|
CommandProvider,
|
|
|
|
Command,
|
|
|
|
)
|
|
|
|
|
2016-05-13 16:06:08 +03:00
|
|
|
ESLINT_PACKAGES = [
|
|
|
|
"eslint@2.9.0",
|
|
|
|
"eslint-plugin-html@1.4.0",
|
|
|
|
"eslint-plugin-mozilla@0.0.3",
|
|
|
|
"eslint-plugin-react@4.2.3"
|
|
|
|
]
|
2016-05-11 17:16:33 +03:00
|
|
|
|
2015-07-02 22:18:52 +03:00
|
|
|
ESLINT_NOT_FOUND_MESSAGE = '''
|
|
|
|
Could not find eslint! We looked at the --binary option, at the ESLINT
|
2016-05-13 16:06:08 +03:00
|
|
|
environment variable, and then at your local node_modules path. Please Install
|
|
|
|
eslint and needed plugins with:
|
2016-05-11 17:16:33 +03:00
|
|
|
|
|
|
|
mach eslint --setup
|
|
|
|
|
|
|
|
and try again.
|
|
|
|
'''.strip()
|
|
|
|
|
2015-09-30 16:44:48 +03:00
|
|
|
NODE_NOT_FOUND_MESSAGE = '''
|
2016-02-10 02:34:37 +03:00
|
|
|
nodejs v4.2.3 is either not installed or is installed to a non-standard path.
|
2015-09-30 16:44:48 +03:00
|
|
|
Please install nodejs from https://nodejs.org and try again.
|
|
|
|
|
|
|
|
Valid installation paths:
|
|
|
|
'''.strip()
|
|
|
|
|
|
|
|
NPM_NOT_FOUND_MESSAGE = '''
|
|
|
|
Node Package Manager (npm) is either not installed or installed to a
|
|
|
|
non-standard path. Please install npm from https://nodejs.org (it comes as an
|
|
|
|
option in the node installation) and try again.
|
|
|
|
|
|
|
|
Valid installation paths:
|
|
|
|
'''.strip()
|
|
|
|
|
2015-07-02 22:18:52 +03:00
|
|
|
|
2013-07-03 04:33:48 +04:00
|
|
|
@CommandProvider
|
|
|
|
class MachCommands(MachCommandBase):
|
|
|
|
@Command('python', category='devenv',
|
|
|
|
description='Run Python.')
|
|
|
|
@CommandArgument('args', nargs=argparse.REMAINDER)
|
|
|
|
def python(self, args):
|
2013-08-15 18:45:09 +04:00
|
|
|
# Avoid logging the command
|
|
|
|
self.log_manager.terminal_handler.setLevel(logging.CRITICAL)
|
2013-12-20 01:48:37 +04:00
|
|
|
|
|
|
|
self._activate_virtualenv()
|
|
|
|
|
|
|
|
return self.run_process([self.virtualenv_manager.python_path] + args,
|
2015-07-02 22:18:52 +03:00
|
|
|
pass_thru=True, # Allow user to run Python interactively.
|
|
|
|
ensure_exit_code=False, # Don't throw on non-zero exit code.
|
2013-08-08 19:06:02 +04:00
|
|
|
# Note: subprocess requires native strings in os.environ on Windows
|
|
|
|
append_env={b'PYTHONDONTWRITEBYTECODE': str('1')})
|
2013-07-03 04:33:48 +04:00
|
|
|
|
|
|
|
@Command('python-test', category='testing',
|
2016-04-18 17:21:56 +03:00
|
|
|
description='Run Python unit tests with an appropriate test runner.')
|
2013-07-03 04:33:48 +04:00
|
|
|
@CommandArgument('--verbose',
|
|
|
|
default=False,
|
|
|
|
action='store_true',
|
|
|
|
help='Verbose output.')
|
|
|
|
@CommandArgument('--stop',
|
|
|
|
default=False,
|
|
|
|
action='store_true',
|
|
|
|
help='Stop running tests after the first error or failure.')
|
2016-04-18 17:21:56 +03:00
|
|
|
@CommandArgument('--path-only',
|
|
|
|
default=False,
|
|
|
|
action='store_true',
|
|
|
|
help=('Collect all tests under given path instead of default '
|
|
|
|
'test resolution. Supports pytest-style tests.'))
|
2016-03-10 20:30:10 +03:00
|
|
|
@CommandArgument('tests', nargs='*',
|
2013-07-03 04:33:48 +04:00
|
|
|
metavar='TEST',
|
2016-03-23 01:53:57 +03:00
|
|
|
help=('Tests to run. Each test can be a single file or a directory. '
|
2016-04-18 17:21:56 +03:00
|
|
|
'Default test resolution relies on PYTHON_UNIT_TESTS.'))
|
2016-03-10 20:30:10 +03:00
|
|
|
def python_test(self,
|
|
|
|
tests=[],
|
|
|
|
test_objects=None,
|
|
|
|
subsuite=None,
|
|
|
|
verbose=False,
|
2016-04-18 17:21:56 +03:00
|
|
|
path_only=False,
|
2016-03-10 20:30:10 +03:00
|
|
|
stop=False):
|
2013-12-20 01:48:37 +04:00
|
|
|
self._activate_virtualenv()
|
2013-07-03 04:33:48 +04:00
|
|
|
|
2016-04-18 17:21:56 +03:00
|
|
|
def find_tests_by_path():
|
|
|
|
import glob
|
|
|
|
files = []
|
|
|
|
for t in tests:
|
|
|
|
if t.endswith('.py') and os.path.isfile(t):
|
|
|
|
files.append(t)
|
|
|
|
elif os.path.isdir(t):
|
|
|
|
for root, _, _ in os.walk(t):
|
|
|
|
files += glob.glob(mozpath.join(root, 'test*.py'))
|
|
|
|
files += glob.glob(mozpath.join(root, 'unit*.py'))
|
|
|
|
else:
|
|
|
|
self.log(logging.WARN, 'python-test',
|
|
|
|
{'test': t},
|
|
|
|
'TEST-UNEXPECTED-FAIL | Invalid test: {test}')
|
|
|
|
if stop:
|
|
|
|
break
|
|
|
|
return files
|
|
|
|
|
2013-07-03 04:33:48 +04:00
|
|
|
# Python's unittest, and in particular discover, has problems with
|
|
|
|
# clashing namespaces when importing multiple test modules. What follows
|
|
|
|
# is a simple way to keep environments separate, at the price of
|
2016-04-18 17:21:56 +03:00
|
|
|
# launching Python multiple times. Most tests are run via mozunit,
|
2013-07-03 04:33:48 +04:00
|
|
|
# which produces output in the format Mozilla infrastructure expects.
|
2016-04-18 17:21:56 +03:00
|
|
|
# Some tests are run via pytest, and these should be equipped with a
|
|
|
|
# local mozunit_report plugin to meet output expectations.
|
2013-07-03 04:33:48 +04:00
|
|
|
return_code = 0
|
2016-03-23 01:53:57 +03:00
|
|
|
found_tests = False
|
2016-03-10 20:30:10 +03:00
|
|
|
if test_objects is None:
|
|
|
|
# If we're not being called from `mach test`, do our own
|
|
|
|
# test resolution.
|
2016-04-18 17:21:56 +03:00
|
|
|
if path_only:
|
|
|
|
if tests:
|
|
|
|
self.virtualenv_manager.install_pip_package(
|
|
|
|
'pytest==2.9.1'
|
|
|
|
)
|
|
|
|
test_objects = [{'path': p} for p in find_tests_by_path()]
|
|
|
|
else:
|
|
|
|
self.log(logging.WARN, 'python-test', {},
|
|
|
|
'TEST-UNEXPECTED-FAIL | No tests specified')
|
|
|
|
test_objects = []
|
2016-03-10 20:30:10 +03:00
|
|
|
else:
|
2016-04-18 17:21:56 +03:00
|
|
|
from mozbuild.testing import TestResolver
|
|
|
|
resolver = self._spawn(TestResolver)
|
|
|
|
if tests:
|
|
|
|
# If we were given test paths, try to find tests matching them.
|
|
|
|
test_objects = resolver.resolve_tests(paths=tests,
|
|
|
|
flavor='python')
|
|
|
|
else:
|
|
|
|
# Otherwise just run everything in PYTHON_UNIT_TESTS
|
|
|
|
test_objects = resolver.resolve_tests(flavor='python')
|
2016-03-10 20:30:10 +03:00
|
|
|
|
|
|
|
for test in test_objects:
|
2016-03-23 01:53:57 +03:00
|
|
|
found_tests = True
|
2016-03-10 20:30:10 +03:00
|
|
|
f = test['path']
|
2015-07-02 22:18:52 +03:00
|
|
|
file_displayed_test = [] # Used as a boolean.
|
|
|
|
|
2013-07-03 04:33:48 +04:00
|
|
|
def _line_handler(line):
|
2016-04-14 20:06:47 +03:00
|
|
|
if not file_displayed_test:
|
|
|
|
output = ('Ran' in line or 'collected' in line or
|
|
|
|
line.startswith('TEST-'))
|
|
|
|
if output:
|
|
|
|
file_displayed_test.append(True)
|
2013-07-03 04:33:48 +04:00
|
|
|
|
|
|
|
inner_return_code = self.run_process(
|
2013-12-20 01:48:37 +04:00
|
|
|
[self.virtualenv_manager.python_path, f],
|
2015-07-02 22:18:52 +03:00
|
|
|
ensure_exit_code=False, # Don't throw on non-zero exit code.
|
2013-07-03 04:33:48 +04:00
|
|
|
log_name='python-test',
|
2013-08-08 19:06:02 +04:00
|
|
|
# subprocess requires native strings in os.environ on Windows
|
|
|
|
append_env={b'PYTHONDONTWRITEBYTECODE': str('1')},
|
2013-07-03 04:33:48 +04:00
|
|
|
line_handler=_line_handler)
|
|
|
|
return_code += inner_return_code
|
|
|
|
|
|
|
|
if not file_displayed_test:
|
2013-12-20 01:48:37 +04:00
|
|
|
self.log(logging.WARN, 'python-test', {'file': f},
|
2013-07-03 04:33:48 +04:00
|
|
|
'TEST-UNEXPECTED-FAIL | No test output (missing mozunit.main() call?): {file}')
|
|
|
|
|
|
|
|
if verbose:
|
|
|
|
if inner_return_code != 0:
|
2013-12-20 01:48:37 +04:00
|
|
|
self.log(logging.INFO, 'python-test', {'file': f},
|
2013-07-03 04:33:48 +04:00
|
|
|
'Test failed: {file}')
|
|
|
|
else:
|
2013-12-20 01:48:37 +04:00
|
|
|
self.log(logging.INFO, 'python-test', {'file': f},
|
2013-07-03 04:33:48 +04:00
|
|
|
'Test passed: {file}')
|
|
|
|
if stop and return_code > 0:
|
|
|
|
return 1
|
|
|
|
|
2016-03-23 01:53:57 +03:00
|
|
|
if not found_tests:
|
2016-04-18 17:21:56 +03:00
|
|
|
message = 'TEST-UNEXPECTED-FAIL | No tests collected'
|
|
|
|
if not path_only:
|
|
|
|
message += ' (Not in PYTHON_UNIT_TESTS? Try --path-only?)'
|
|
|
|
self.log(logging.WARN, 'python-test', {}, message)
|
2016-03-23 01:53:57 +03:00
|
|
|
return 1
|
|
|
|
|
2013-07-03 04:33:48 +04:00
|
|
|
return 0 if return_code == 0 else 1
|
2015-07-02 22:18:52 +03:00
|
|
|
|
2015-09-30 16:44:48 +03:00
|
|
|
@Command('eslint', category='devenv',
|
|
|
|
description='Run eslint or help configure eslint for optimal development.')
|
|
|
|
@CommandArgument('-s', '--setup', default=False, action='store_true',
|
|
|
|
help='configure eslint for optimal development.')
|
2015-12-06 00:17:49 +03:00
|
|
|
@CommandArgument('-e', '--ext', default='[.js,.jsm,.jsx,.xml,.html]',
|
|
|
|
help='Filename extensions to lint, default: "[.js,.jsm,.jsx,.xml,.html]".')
|
2015-07-02 22:18:52 +03:00
|
|
|
@CommandArgument('-b', '--binary', default=None,
|
|
|
|
help='Path to eslint binary.')
|
|
|
|
@CommandArgument('args', nargs=argparse.REMAINDER) # Passed through to eslint.
|
2015-12-03 21:38:29 +03:00
|
|
|
def eslint(self, setup, ext=None, binary=None, args=None):
|
2015-07-02 22:18:52 +03:00
|
|
|
'''Run eslint.'''
|
|
|
|
|
2016-05-13 16:06:08 +03:00
|
|
|
module_path = self.get_eslint_module_path()
|
|
|
|
|
2016-02-10 02:34:37 +03:00
|
|
|
# eslint requires at least node 4.2.3
|
|
|
|
nodePath = self.getNodeOrNpmPath("node", LooseVersion("4.2.3"))
|
|
|
|
if not nodePath:
|
|
|
|
return 1
|
|
|
|
|
2015-09-30 16:44:48 +03:00
|
|
|
if setup:
|
|
|
|
return self.eslint_setup()
|
|
|
|
|
2016-05-13 16:06:08 +03:00
|
|
|
npmPath = self.getNodeOrNpmPath("npm")
|
|
|
|
if not npmPath:
|
|
|
|
return 1
|
|
|
|
|
|
|
|
if self.eslintModuleHasIssues():
|
|
|
|
install = self._prompt_yn("\nContinuing will automatically fix "
|
|
|
|
"these issues. Would you like to "
|
|
|
|
"continue")
|
|
|
|
if install:
|
|
|
|
self.eslint_setup()
|
|
|
|
else:
|
|
|
|
return 1
|
|
|
|
|
|
|
|
# Valid binaries are:
|
|
|
|
# - Any provided by the binary argument.
|
|
|
|
# - Any pointed at by the ESLINT environmental variable.
|
|
|
|
# - Those provided by mach eslint --setup.
|
|
|
|
#
|
|
|
|
# eslint --setup installs some mozilla specific plugins and installs
|
|
|
|
# all node modules locally. This is the preferred method of
|
|
|
|
# installation.
|
|
|
|
|
2015-07-02 22:18:52 +03:00
|
|
|
if not binary:
|
|
|
|
binary = os.environ.get('ESLINT', None)
|
2016-05-13 16:06:08 +03:00
|
|
|
|
2015-07-02 22:18:52 +03:00
|
|
|
if not binary:
|
2016-05-13 16:06:08 +03:00
|
|
|
binary = os.path.join(module_path, "node_modules", ".bin", "eslint")
|
|
|
|
if not os.path.isfile(binary):
|
|
|
|
binary = None
|
2015-07-02 22:18:52 +03:00
|
|
|
|
|
|
|
if not binary:
|
|
|
|
print(ESLINT_NOT_FOUND_MESSAGE)
|
|
|
|
return 1
|
|
|
|
|
2015-12-03 21:38:29 +03:00
|
|
|
self.log(logging.INFO, 'eslint', {'binary': binary, 'args': args},
|
|
|
|
'Running {binary}')
|
2015-07-02 22:18:52 +03:00
|
|
|
|
2015-12-03 21:38:29 +03:00
|
|
|
args = args or ['.']
|
2015-07-02 22:18:52 +03:00
|
|
|
|
|
|
|
cmd_args = [binary,
|
2015-12-06 00:17:49 +03:00
|
|
|
# Enable the HTML plugin.
|
|
|
|
# We can't currently enable this in the global config file
|
|
|
|
# because it has bad interactions with the SublimeText
|
|
|
|
# ESLint plugin (bug 1229874).
|
|
|
|
'--plugin', 'html',
|
|
|
|
'--ext', ext, # This keeps ext as a single argument.
|
|
|
|
] + args
|
2015-07-02 22:18:52 +03:00
|
|
|
|
2015-12-10 22:31:14 +03:00
|
|
|
success = self.run_process(cmd_args,
|
2015-07-02 22:18:52 +03:00
|
|
|
pass_thru=True, # Allow user to run eslint interactively.
|
|
|
|
ensure_exit_code=False, # Don't throw on non-zero exit code.
|
2015-12-01 22:29:47 +03:00
|
|
|
require_unix_environment=True # eslint is not a valid Win32 binary.
|
2015-07-02 22:18:52 +03:00
|
|
|
)
|
2015-12-10 22:31:14 +03:00
|
|
|
|
|
|
|
self.log(logging.INFO, 'eslint', {'msg': ('No errors' if success == 0 else 'Errors')},
|
|
|
|
'Finished eslint. {msg} encountered.')
|
|
|
|
return success
|
|
|
|
|
2015-09-30 16:44:48 +03:00
|
|
|
def eslint_setup(self, update_only=False):
|
|
|
|
"""Ensure eslint is optimally configured.
|
|
|
|
|
|
|
|
This command will inspect your eslint configuration and
|
|
|
|
guide you through an interactive wizard helping you configure
|
|
|
|
eslint for optimal use on Mozilla projects.
|
|
|
|
"""
|
2016-05-02 02:22:31 +03:00
|
|
|
orig_cwd = os.getcwd()
|
2015-09-30 16:44:48 +03:00
|
|
|
sys.path.append(os.path.dirname(__file__))
|
|
|
|
|
2016-05-13 16:06:08 +03:00
|
|
|
module_path = self.get_eslint_module_path()
|
2016-05-02 02:22:31 +03:00
|
|
|
|
|
|
|
# npm sometimes fails to respect cwd when it is run using check_call so
|
|
|
|
# we manually switch folders here instead.
|
|
|
|
os.chdir(module_path)
|
|
|
|
|
2015-09-30 16:44:48 +03:00
|
|
|
npmPath = self.getNodeOrNpmPath("npm")
|
|
|
|
if not npmPath:
|
|
|
|
return 1
|
|
|
|
|
2016-05-13 16:06:08 +03:00
|
|
|
# Install eslint and necessary plugins.
|
|
|
|
for pkg in ESLINT_PACKAGES:
|
|
|
|
name, version = pkg.split("@")
|
|
|
|
success = False
|
2015-09-30 16:44:48 +03:00
|
|
|
|
2016-05-13 16:06:08 +03:00
|
|
|
if self.node_package_installed(pkg, cwd=module_path):
|
|
|
|
success = True
|
|
|
|
else:
|
|
|
|
if pkg.startswith("eslint-plugin-mozilla"):
|
|
|
|
cmd = [npmPath, "install",
|
|
|
|
os.path.join(module_path, "eslint-plugin-mozilla")]
|
|
|
|
else:
|
|
|
|
cmd = [npmPath, "install", pkg]
|
2015-09-30 16:44:48 +03:00
|
|
|
|
2016-05-13 16:06:08 +03:00
|
|
|
print("Installing %s v%s using \"%s\"..."
|
|
|
|
% (name, version, " ".join(cmd)))
|
|
|
|
success = self.callProcess(pkg, cmd)
|
2015-12-06 00:17:49 +03:00
|
|
|
|
2016-05-13 16:06:08 +03:00
|
|
|
if not success:
|
|
|
|
return 1
|
|
|
|
|
|
|
|
eslint_path = os.path.join(module_path, "node_modules", ".bin", "eslint")
|
2015-10-23 19:18:45 +03:00
|
|
|
|
|
|
|
print("\nESLint and approved plugins installed successfully!")
|
2016-05-13 16:06:08 +03:00
|
|
|
print("\nNOTE: Your local eslint binary is at %s\n" % eslint_path)
|
2016-05-02 02:22:31 +03:00
|
|
|
|
|
|
|
os.chdir(orig_cwd)
|
2015-10-23 19:18:45 +03:00
|
|
|
|
|
|
|
def callProcess(self, name, cmd, cwd=None):
|
|
|
|
try:
|
|
|
|
with open(os.devnull, "w") as fnull:
|
|
|
|
subprocess.check_call(cmd, cwd=cwd, stdout=fnull)
|
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
if cwd:
|
|
|
|
print("\nError installing %s in the %s folder, aborting." % (name, cwd))
|
|
|
|
else:
|
|
|
|
print("\nError installing %s, aborting." % name)
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
2015-09-30 16:44:48 +03:00
|
|
|
|
2016-05-13 16:06:08 +03:00
|
|
|
def eslintModuleHasIssues(self):
|
|
|
|
has_issues = False
|
2016-05-18 18:45:49 +03:00
|
|
|
node_module_path = os.path.join(self.get_eslint_module_path(), "node_modules")
|
2016-05-13 16:06:08 +03:00
|
|
|
|
|
|
|
for pkg in ESLINT_PACKAGES:
|
|
|
|
name, req_version = pkg.split("@")
|
2016-05-18 18:45:49 +03:00
|
|
|
path = os.path.join(node_module_path, name, "package.json")
|
2016-05-13 16:06:08 +03:00
|
|
|
|
2016-05-18 18:45:49 +03:00
|
|
|
if not os.path.exists(path):
|
|
|
|
print("%s v%s needs to be installed locally." % (name, req_version))
|
|
|
|
has_issues = True
|
|
|
|
continue
|
|
|
|
|
|
|
|
data = json.load(open(path))
|
|
|
|
|
|
|
|
if data["version"] != req_version:
|
|
|
|
print("%s v%s should be v%s." % (name, version, req_version))
|
2016-05-13 16:06:08 +03:00
|
|
|
has_issues = True
|
|
|
|
|
|
|
|
return has_issues
|
|
|
|
|
|
|
|
def node_package_installed(self, package_name="", globalInstall=False, cwd=None):
|
|
|
|
try:
|
|
|
|
npmPath = self.getNodeOrNpmPath("npm")
|
|
|
|
|
|
|
|
cmd = [npmPath, "ls", "--parseable", package_name]
|
|
|
|
|
|
|
|
if globalInstall:
|
|
|
|
cmd.append("-g")
|
|
|
|
|
|
|
|
with open(os.devnull, "w") as fnull:
|
|
|
|
subprocess.check_call(cmd, stdout=fnull, stderr=fnull, cwd=cwd)
|
|
|
|
|
|
|
|
return True
|
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
return False
|
|
|
|
|
2015-09-30 16:44:48 +03:00
|
|
|
def getPossibleNodePathsWin(self):
|
|
|
|
"""
|
|
|
|
Return possible nodejs paths on Windows.
|
|
|
|
"""
|
|
|
|
if platform.system() != "Windows":
|
|
|
|
return []
|
|
|
|
|
2015-10-23 16:36:24 +03:00
|
|
|
return list({
|
2015-09-30 16:44:48 +03:00
|
|
|
"%s\\nodejs" % os.environ.get("SystemDrive"),
|
|
|
|
os.path.join(os.environ.get("ProgramFiles"), "nodejs"),
|
|
|
|
os.path.join(os.environ.get("PROGRAMW6432"), "nodejs"),
|
|
|
|
os.path.join(os.environ.get("PROGRAMFILES"), "nodejs")
|
2015-10-23 16:36:24 +03:00
|
|
|
})
|
2015-09-30 16:44:48 +03:00
|
|
|
|
2016-02-10 02:34:37 +03:00
|
|
|
def getNodeOrNpmPath(self, filename, minversion=None):
|
2015-09-30 16:44:48 +03:00
|
|
|
"""
|
|
|
|
Return the nodejs or npm path.
|
|
|
|
"""
|
|
|
|
if platform.system() == "Windows":
|
2015-10-23 16:36:24 +03:00
|
|
|
for ext in [".cmd", ".exe", ""]:
|
|
|
|
try:
|
2015-09-30 16:44:48 +03:00
|
|
|
nodeOrNpmPath = which.which(filename + ext,
|
|
|
|
path=self.getPossibleNodePathsWin())
|
2016-02-10 02:34:37 +03:00
|
|
|
if self.is_valid(nodeOrNpmPath, minversion):
|
2015-09-30 16:44:48 +03:00
|
|
|
return nodeOrNpmPath
|
2015-10-23 16:36:24 +03:00
|
|
|
except which.WhichError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
try:
|
2016-02-10 02:34:37 +03:00
|
|
|
nodeOrNpmPath = which.which(filename)
|
|
|
|
if self.is_valid(nodeOrNpmPath, minversion):
|
|
|
|
return nodeOrNpmPath
|
2015-09-30 16:44:48 +03:00
|
|
|
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:
|
2015-10-23 19:18:45 +03:00
|
|
|
print(" - %s" % p)
|
2015-09-30 16:44:48 +03:00
|
|
|
elif platform.system() == "Darwin":
|
|
|
|
print(" - /usr/local/bin/node")
|
|
|
|
elif platform.system() == "Linux":
|
|
|
|
print(" - /usr/bin/nodejs")
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
2016-02-10 02:34:37 +03:00
|
|
|
def is_valid(self, path, minversion = None):
|
2015-09-30 16:44:48 +03:00
|
|
|
try:
|
2016-02-10 02:34:37 +03:00
|
|
|
version_str = subprocess.check_output([path, "--version"],
|
|
|
|
stderr=subprocess.STDOUT)
|
|
|
|
if minversion:
|
|
|
|
# nodejs prefixes its version strings with "v"
|
|
|
|
version = LooseVersion(version_str.lstrip('v'))
|
|
|
|
return version >= minversion
|
|
|
|
return True
|
2016-05-11 17:16:33 +03:00
|
|
|
except (subprocess.CalledProcessError, OSError):
|
2015-09-30 16:44:48 +03:00
|
|
|
return False
|
2016-05-02 02:22:31 +03:00
|
|
|
|
|
|
|
def get_project_root(self):
|
|
|
|
fullpath = os.path.abspath(sys.modules['__main__'].__file__)
|
|
|
|
return os.path.dirname(fullpath)
|
2016-05-13 16:06:08 +03:00
|
|
|
|
|
|
|
def get_eslint_module_path(self):
|
|
|
|
return os.path.join(self.get_project_root(), "testing", "eslint")
|
|
|
|
|
|
|
|
def _prompt_yn(self, msg):
|
|
|
|
if not sys.stdin.isatty():
|
|
|
|
return False
|
|
|
|
|
|
|
|
print('%s? [Y/n]' % msg)
|
|
|
|
|
|
|
|
while True:
|
|
|
|
choice = raw_input().lower().strip()
|
|
|
|
|
|
|
|
if not choice:
|
|
|
|
return True
|
|
|
|
|
|
|
|
if choice in ('y', 'yes'):
|
|
|
|
return True
|
|
|
|
|
|
|
|
if choice in ('n', 'no'):
|
|
|
|
return False
|
|
|
|
|
|
|
|
print('Must reply with one of {yes, no, y, n}.')
|