Skip to content
This repository was archived by the owner on May 16, 2024. It is now read-only.

[REF] pylint script: Migrate bash pylint script to python. #229

Merged
merged 2 commits into from
Aug 20, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions travis/run_pylint.py
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']:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All messages are errors?

Copy link
Contributor Author

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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the clarification

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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should you add this path always or only in case there's __init__.py file?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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))
33 changes: 15 additions & 18 deletions travis/test_pylint
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)
2 changes: 1 addition & 1 deletion travis/travis_install_nightly
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

pip install -q QUnitSuite flake8 coveralls pylint > /dev/null 2>&1
pip install -q QUnitSuite flake8 coveralls pylint Click > /dev/null 2>&1

# We can exit here and do nothing if this only a LINT check
if [ "${LINT_CHECK}" == "1" ] ; then
Expand Down