mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 02:00:20 +00:00
Have nox reuse pre-commit's virtualenv
This commit is contained in:
parent
a79891c630
commit
02d1a7d984
2 changed files with 94 additions and 24 deletions
|
@ -435,7 +435,7 @@ repos:
|
||||||
files: ^((setup|noxfile)|salt/.*)\.py$
|
files: ^((setup|noxfile)|salt/.*)\.py$
|
||||||
args:
|
args:
|
||||||
- -e
|
- -e
|
||||||
- lint-salt
|
- lint-salt-pre-commit
|
||||||
- --
|
- --
|
||||||
|
|
||||||
- repo: https://github.com/saltstack/salt-nox-pre-commit
|
- repo: https://github.com/saltstack/salt-nox-pre-commit
|
||||||
|
@ -447,5 +447,5 @@ repos:
|
||||||
files: ^tests/.*\.py$
|
files: ^tests/.*\.py$
|
||||||
args:
|
args:
|
||||||
- -e
|
- -e
|
||||||
- lint-tests
|
- lint-tests-pre-commit
|
||||||
- --
|
- --
|
||||||
|
|
114
noxfile.py
114
noxfile.py
|
@ -913,7 +913,7 @@ class Tee:
|
||||||
return self._first.fileno()
|
return self._first.fileno()
|
||||||
|
|
||||||
|
|
||||||
def _lint(session, rcfile, flags, paths):
|
def _lint(session, rcfile, flags, paths, tee_output=True):
|
||||||
_install_requirements(session, 'zeromq')
|
_install_requirements(session, 'zeromq')
|
||||||
requirements_file = 'requirements/static/lint.in'
|
requirements_file = 'requirements/static/lint.in'
|
||||||
distro_constraints = [
|
distro_constraints = [
|
||||||
|
@ -927,39 +927,79 @@ def _lint(session, rcfile, flags, paths):
|
||||||
'--constraint', distro_constraint
|
'--constraint', distro_constraint
|
||||||
])
|
])
|
||||||
session.install(*install_command, silent=PIP_INSTALL_SILENT)
|
session.install(*install_command, silent=PIP_INSTALL_SILENT)
|
||||||
session.run('pylint', '--version')
|
|
||||||
pylint_report_path = os.environ.get('PYLINT_REPORT')
|
if tee_output:
|
||||||
|
session.run('pylint', '--version')
|
||||||
|
pylint_report_path = os.environ.get('PYLINT_REPORT')
|
||||||
|
|
||||||
cmd_args = [
|
cmd_args = [
|
||||||
'pylint',
|
'pylint',
|
||||||
'--rcfile={}'.format(rcfile)
|
'--rcfile={}'.format(rcfile)
|
||||||
] + list(flags) + list(paths)
|
] + list(flags) + list(paths)
|
||||||
|
|
||||||
stdout = tempfile.TemporaryFile(mode='w+b')
|
cmd_kwargs = {
|
||||||
|
'env': {'PYTHONUNBUFFERED': '1'}
|
||||||
|
}
|
||||||
|
|
||||||
|
if tee_output:
|
||||||
|
stdout = tempfile.TemporaryFile(mode='w+b')
|
||||||
|
cmd_kwargs['stdout'] = Tee(stdout, sys.__stdout__)
|
||||||
|
|
||||||
lint_failed = False
|
lint_failed = False
|
||||||
try:
|
try:
|
||||||
session.run(*cmd_args,
|
session.run(*cmd_args, **cmd_kwargs)
|
||||||
stdout=Tee(stdout, sys.__stdout__),
|
|
||||||
env={'PYTHONUNBUFFERED': '1'})
|
|
||||||
except CommandFailed:
|
except CommandFailed:
|
||||||
lint_failed = True
|
lint_failed = True
|
||||||
raise
|
raise
|
||||||
finally:
|
finally:
|
||||||
stdout.seek(0)
|
if tee_output:
|
||||||
contents = stdout.read()
|
stdout.seek(0)
|
||||||
if contents:
|
contents = stdout.read()
|
||||||
if IS_PY3:
|
if contents:
|
||||||
contents = contents.decode('utf-8')
|
if IS_PY3:
|
||||||
else:
|
contents = contents.decode('utf-8')
|
||||||
contents = contents.encode('utf-8')
|
else:
|
||||||
sys.stdout.write(contents)
|
contents = contents.encode('utf-8')
|
||||||
sys.stdout.flush()
|
sys.stdout.write(contents)
|
||||||
if pylint_report_path:
|
sys.stdout.flush()
|
||||||
# Write report
|
if pylint_report_path:
|
||||||
with open(pylint_report_path, 'w') as wfh:
|
# Write report
|
||||||
wfh.write(contents)
|
with open(pylint_report_path, 'w') as wfh:
|
||||||
session.log('Report file written to %r', pylint_report_path)
|
wfh.write(contents)
|
||||||
stdout.close()
|
session.log('Report file written to %r', pylint_report_path)
|
||||||
|
stdout.close()
|
||||||
|
|
||||||
|
|
||||||
|
def _lint_pre_commit(session, rcfile, flags, paths):
|
||||||
|
if 'VIRTUAL_ENV' not in os.environ:
|
||||||
|
session.error(
|
||||||
|
'This should be running from within a virtualenv and '
|
||||||
|
'\'VIRTUAL_ENV\' was not found as an environment variable.'
|
||||||
|
)
|
||||||
|
if 'pre-commit' not in os.environ['VIRTUAL_ENV']:
|
||||||
|
session.error(
|
||||||
|
'This should be running from within a pre-commit virtualenv and '
|
||||||
|
'\'VIRTUAL_ENV\'({}) does not appear to be a pre-commit virtualenv.'.format(
|
||||||
|
os.environ['VIRTUAL_ENV']
|
||||||
|
)
|
||||||
|
)
|
||||||
|
from nox.virtualenv import VirtualEnv
|
||||||
|
# Let's patch nox to make it run inside the pre-commit virtualenv
|
||||||
|
try:
|
||||||
|
session._runner.venv = VirtualEnv( # pylint: disable=unexpected-keyword-arg
|
||||||
|
os.environ['VIRTUAL_ENV'],
|
||||||
|
interpreter=session._runner.func.python,
|
||||||
|
reuse_existing=True,
|
||||||
|
venv=True
|
||||||
|
)
|
||||||
|
except TypeError:
|
||||||
|
# This is still nox-py2
|
||||||
|
session._runner.venv = VirtualEnv(
|
||||||
|
os.environ['VIRTUAL_ENV'],
|
||||||
|
interpreter=session._runner.func.python,
|
||||||
|
reuse_existing=True,
|
||||||
|
)
|
||||||
|
_lint(session, rcfile, flags, paths, tee_output=False)
|
||||||
|
|
||||||
|
|
||||||
@nox.session(python='3')
|
@nox.session(python='3')
|
||||||
|
@ -1001,6 +1041,36 @@ def lint_tests(session):
|
||||||
_lint(session, '.pylintrc', flags, paths)
|
_lint(session, '.pylintrc', flags, paths)
|
||||||
|
|
||||||
|
|
||||||
|
@nox.session(python=False, name='lint-salt-pre-commit')
|
||||||
|
def lint_salt_pre_commit(session):
|
||||||
|
'''
|
||||||
|
Run PyLint against Salt. Set PYLINT_REPORT to a path to capture output.
|
||||||
|
'''
|
||||||
|
flags = [
|
||||||
|
'--disable=I'
|
||||||
|
]
|
||||||
|
if session.posargs:
|
||||||
|
paths = session.posargs
|
||||||
|
else:
|
||||||
|
paths = ['setup.py', 'noxfile.py', 'salt/']
|
||||||
|
_lint_pre_commit(session, '.pylintrc', flags, paths)
|
||||||
|
|
||||||
|
|
||||||
|
@nox.session(python=False, name='lint-tests-pre-commit')
|
||||||
|
def lint_tests_pre_commit(session):
|
||||||
|
'''
|
||||||
|
Run PyLint against Salt and it's test suite. Set PYLINT_REPORT to a path to capture output.
|
||||||
|
'''
|
||||||
|
flags = [
|
||||||
|
'--disable=I'
|
||||||
|
]
|
||||||
|
if session.posargs:
|
||||||
|
paths = session.posargs
|
||||||
|
else:
|
||||||
|
paths = ['tests/']
|
||||||
|
_lint_pre_commit(session, '.pylintrc', flags, paths)
|
||||||
|
|
||||||
|
|
||||||
@nox.session(python='3')
|
@nox.session(python='3')
|
||||||
@nox.parametrize('update', [False, True])
|
@nox.parametrize('update', [False, True])
|
||||||
@nox.parametrize('compress', [False, True])
|
@nox.parametrize('compress', [False, True])
|
||||||
|
|
Loading…
Add table
Reference in a new issue