2012-03-12 :-(
_ 午後
1300 検討
_ [Pylint][Python][lint]Pylint を入れてみた
Python 版 lint みたいなもの。コードを静的解析する。
環境
- Microsoft Windows7 64bit
- cygwin
- python 2.6.7
pylint
各々ページ右側にある download からダウンロードして展開してインストール。または easy_install する。
% wget http://download.logilab.org/pub/astng/logilab-astng-0.23.1.tar.gz % tar xzf logilab-astng-0.23.1.tar.gz % cd logilab-astng-0.23.1 % python setup.py install
% wget http://download.logilab.org/pub/common/logilab-common-0.57.1.tar.gz % tar xzf logilab-common-0.57.1.tar.gz % cd logilab-common-0.57.1 % python setup.py install
% wget http://ftp.logilab.org/pub/pylint/pylint-0.25.1.tar.gz % tar xzf pylint-0.25.1.tar.gz % cd pylint-0.25.1 % python setup.py install
適当なプログラムに対して実行。
% pylint command.py No config file found, using default configuration ************* Module command C: 1: Missing docstring W: 8:Command: Class has no __init__ method C: 8:Command: Missing docstring C: 9:Command.execute: Missing docstring R: 9:Command.execute: Method could be a function R: 8:Command: Too few public methods (1/2) W: 12:Command1: Class has no __init__ method C: 12:Command1: Missing docstring R: 12:Command1: Too few public methods (1/2) W: 16:Command2: Class has no __init__ method C: 16:Command2: Missing docstring R: 16:Command2: Too few public methods (1/2) W: 20:Command3: Class has no __init__ method C: 20:Command3: Missing docstring R: 20:Command3: Too few public methods (1/2) C: 25:Noel: Missing docstring C: 29:Noel.add: Missing docstring C: 32:Noel.run: Missing docstring C: 33:Noel.run: Invalid name "c" (should match [a-z_][a-z0-9_]{2,30}$) C: 37:main: Missing docstring Report ====== 28 statements analysed. Statistics by type ------------------ +---------+-------+-----------+-----------+------------+---------+ |type |number |old number |difference |%documented |%badname | +=========+=======+===========+===========+============+=========+ |module |1 |1 |= |0.00 |0.00 | +---------+-------+-----------+-----------+------------+---------+ |class |5 |5 |= |0.00 |0.00 | +---------+-------+-----------+-----------+------------+---------+ |method |7 |7 |= |57.14 |0.00 | +---------+-------+-----------+-----------+------------+---------+ |function |1 |1 |= |0.00 |0.00 | +---------+-------+-----------+-----------+------------+---------+ Messages by category -------------------- +-----------+-------+---------+-----------+ |type |number |previous |difference | +===========+=======+=========+===========+ |convention |11 |11 |= | +-----------+-------+---------+-----------+ |refactor |5 |5 |= | +-----------+-------+---------+-----------+ |warning |4 |4 |= | +-----------+-------+---------+-----------+ |error |0 |0 |= | +-----------+-------+---------+-----------+ Messages -------- +-----------+------------+ |message id |occurrences | +===========+============+ |C0111 |10 | +-----------+------------+ |W0232 |4 | +-----------+------------+ |R0903 |4 | +-----------+------------+ |R0201 |1 | +-----------+------------+ |C0103 |1 | +-----------+------------+ Global evaluation ----------------- Your code has been rated at 2.86/10 (previous run: 2.86/10) Duplication ----------- +-------------------------+------+---------+-----------+ | |now |previous |difference | +=========================+======+=========+===========+ |nb duplicated lines |0 |0 |= | +-------------------------+------+---------+-----------+ |percent duplicated lines |0.000 |0.000 |= | +-------------------------+------+---------+-----------+ Raw metrics ----------- +----------+-------+------+---------+-----------+ |type |number |% |previous |difference | +==========+=======+======+=========+===========+ |code |27 |72.97 |27 |= | +----------+-------+------+---------+-----------+ |docstring |0 |0.00 |0 |= | +----------+-------+------+---------+-----------+ |comment |5 |13.51 |5 |= | +----------+-------+------+---------+-----------+ |empty |5 |13.51 |5 |= | +----------+-------+------+---------+-----------+
checker
ルールを定義するものをチェッカーと呼ぶらしい。
% export PYTHONPATH=/home/public/python/pylint-0.25.1/examples % pylint --load-plugins=custom command.py
カスタマイズ
--rcfile でいろいろ設定する。以下のように作成すると楽ちん。
% pylint --generate-rcfile > pylintrc
_ [pylint][jenkins]pylint のレポートを jenkins から眺める
jenkins ことはじめ[ 20120222#p04 ] ということで使ってみる。
環境
- Microsoft Windows7 64bit
- cygwin
- python 2.6.7
参考
- Violations - Jenkins - Jenkins Wiki
- How to use Jenkins for python development
- pylint features (Logilab.org)
pylint
インストール済みとする。
rcfile をいじってカスタマイズする。
disable=E1101,C0111,C0322,C0324,C0111.... output-format=parseable reports=yes files-output=no
How to use Jenkins for python development によると output-format=parseable と reports=yes を指定するようなので書いておく。
files-output=yes にすると対象とする .py ごとにレポートされて鬱陶しいので no にしておく。全部 1 つのファイルへリダイレクトしてレポートさせるらしい。
disable は pylint features (Logilab.org) を見ながらテキトーに設定する。
jenkins の Violations プラグイン
jenkins はインストール済みとする。
Violations プラグインを使う Violations - Jenkins - Jenkins Wiki
jenkins - Jenkinsの管理 - プラグインの管理 - 利用可能 - Jenkins Violations plugin をインストール。
インストールするとプロジェクトの「ビルド後の処理」に「Report Violations」が現れるのでチェックする。
「pylint」の「XML filename pattern」に「pylint.log」等と入力しておく。XML と言っているけど XML じゃなくても良いらしい。
「Source encoding」を「UTF-8」にしとく。
プロジェクトの高度なオプション - カスタムワークスペースの使用 - ディレクトリを設定しておく。今回はリポジトリからチェックアウトなどはしない。カスタムワークスペースでいろいろ作業するのでカスタムワークスペースを設定しておく。カスタムワークスペースを設定しておくと Violations はソースファイルを表示するときにカスタムワークスペースの下にある .py を見に行くぽい。
試しに cygwin コンソールで以下のように実行する。
% pylint --rcfile=./pylintrc * >> pylint.log
jenkins プロジェクトを「実行」してみる。
「Violations」にアクセスするとレポートが見れる。