This repository was archived by the owner on May 16, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 267
[REF] pylint script: Migrate bash pylint script to python. #229
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
from __future__ import print_function | ||
|
||
import os | ||
import sys | ||
|
||
import click | ||
import pylint.lint | ||
|
||
CLICK_DIR = click.Path(exists=True, dir_okay=True, resolve_path=True) | ||
|
||
|
||
def get_count_fails(linter_stats): | ||
"""Verify the dictionary statistics to get number of errors. | ||
:param linter_stats: Dict of type pylint.lint.Run().linter.stats | ||
:return: Integer with quantity of fails found. | ||
""" | ||
count = 0 | ||
for msg in linter_stats['by_msg']: | ||
count += linter_stats['by_msg'][msg] | ||
return count | ||
|
||
|
||
def get_subpaths(paths): | ||
"""Get list of subdirectories | ||
if `__init__.py` file not exists in root path then | ||
get subdirectories. | ||
Why? More info here: | ||
https://www.mail-archive.com/[email protected]/msg00294.html | ||
:param paths: List of paths | ||
:return: Return list of paths with subdirectories. | ||
""" | ||
subpaths = [] | ||
for path in paths: | ||
subpaths.append(path) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should you add this path always or only in case there's There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Current path too to check root files. |
||
if not os.path.isfile(os.path.join(path, '__init__.py')): | ||
subpaths.extend( | ||
[os.path.join(path, item) | ||
for item in os.listdir(path) | ||
if os.path.isdir(os.path.join(path, item))]) | ||
return subpaths | ||
|
||
|
||
def run_pylint(paths, cfg, sys_paths=None, extra_params=None): | ||
"""Execute pylint command from original python library | ||
:param paths: List of paths of python modules to check pylint | ||
:param cfg: String name of pylint configuration file | ||
:param sys_paths: List of paths to append to sys path | ||
:param extra_params: List of parameters extra to append | ||
in pylint command | ||
:return: Dict with python linter stats | ||
""" | ||
if sys_paths is None: | ||
sys_paths = [] | ||
if extra_params is None: | ||
extra_params = [] | ||
sys.path.extend(sys_paths) | ||
cmd = ['--rcfile=' + cfg] | ||
cmd.extend(extra_params) | ||
subpaths = get_subpaths(paths) | ||
cmd.extend(subpaths) | ||
pylint_res = pylint.lint.Run(cmd, exit=False) | ||
return pylint_res.linter.stats | ||
|
||
|
||
@click.command() | ||
@click.option('paths', '--path', envvar='TRAVIS_BUILD_DIR', | ||
multiple=True, type=CLICK_DIR, required=True, | ||
help="Addons paths to check pylint") | ||
@click.option('--config-file', '-c', | ||
type=click.File('r', lazy=True), required=True, | ||
help="Pylint config file") | ||
@click.option('--sys-paths', '-sys-path', envvar='PYTHONPATH', | ||
multiple=True, type=CLICK_DIR, | ||
help="Additional paths to append in sys path.") | ||
@click.option('--extra-params', '-extra-param', multiple=True, | ||
help="Extra pylint params to append " | ||
"in pylint command") | ||
def main(paths, config_file, sys_paths=None, extra_params=None): | ||
"""Script to run pylint command with additional params | ||
to check fails of odoo modules. | ||
If expected errors is equal to count fails found then | ||
this program exit with zero otherwise exit with counted fails""" | ||
stats = run_pylint( | ||
list(paths), config_file.name, sys_paths=sys_paths, | ||
extra_params=extra_params) | ||
count_fails = get_count_fails(stats) | ||
return count_fails | ||
|
||
|
||
if __name__ == '__main__': | ||
exit(main(standalone_mode=False)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,20 @@ | ||
#!/bin/bash | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
MODULES_TO_TEST=$TRAVIS_BUILD_DIR | ||
PYLINT_CONFIG_DIR="$(dirname $0)/cfg" | ||
""" | ||
Script to process pylint run and exit with result. | ||
""" | ||
|
||
# Fix pylint path. More info: https://www.mail-archive.com/[email protected]/msg00294.html | ||
export PYTHONPATH=${PYTHONPATH}:${MODULES_TO_TEST} | ||
touch $MODULES_TO_TEST/__init__.py | ||
import os | ||
|
||
# Set openerp in global path to fix "import" from addons | ||
if [ "x${VERSION}" == "x" ] ; then | ||
VERSION="${1}" | ||
echo "WARNING: no env variable set for VERSION. Using '${1}'." | ||
fi | ||
import run_pylint | ||
|
||
: ${ODOO_REPO:="odoo/odoo"} # default value, if not set | ||
IFS="/" read -a REPO <<< "${ODOO_REPO}" | ||
export PATH=${PATH}:${PWD}/../${REPO[1]}-${VERSION}/openerp | ||
|
||
#run pylint command | ||
pylint --rcfile=${PYLINT_CONFIG_DIR}/travis_run_pylint.cfg ${MODULES_TO_TEST} | ||
pylint_status=$? | ||
exit $((${pylint_status})) | ||
pylint_rcfile = os.path.join( | ||
os.path.dirname(os.path.realpath(__file__)), | ||
'cfg', | ||
"travis_run_pylint.cfg") | ||
result = run_pylint.main([ | ||
"--config-file=" + pylint_rcfile, | ||
], standalone_mode=False) | ||
exit(result) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All messages are errors?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, here return all checks activated in cfg and found.
If a check is returned then is a fail. (Currently bash script is working equal)
If you dont want a fail then you remove the check from cfg file
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the clarification