jenkins-bot has submitted this change and it was merged. (
https://gerrit.wikimedia.org/r/398262 )
Change subject: tox.ini: Configure Jenkins to fail when lines are longer than 79
characters
......................................................................
tox.ini: Configure Jenkins to fail when lines are longer than 79 characters
Put the checker script under the maintenance directory.
Currently, it only checks the length of changed or added lines of Python files.
Add W503 to ignore list of flake8 as it is against the current PEP
recommendation.[1]
[1]:
https://www.python.org/dev/peps/pep-0008/#should-a-line-break-before-or-aft…
Change-Id: I7c430431ebdc942e9f8c1c8b576feee22d7ff7d3
---
A scripts/maintenance/diff_checker.py
M tox.ini
2 files changed, 80 insertions(+), 3 deletions(-)
Approvals:
jenkins-bot: Verified
Xqt: Looks good to me, approved
diff --git a/scripts/maintenance/diff_checker.py b/scripts/maintenance/diff_checker.py
new file mode 100644
index 0000000..358d0d3
--- /dev/null
+++ b/scripts/maintenance/diff_checker.py
@@ -0,0 +1,72 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+"""Check that the latest commit diff follows the guidelines.
+
+Currently the script only checks line lengths. Lines longer than 79 characters
+should be avoided.
+See [[Manual:Pywikibot/Development/Guidelines]] for more info.
+
+Todo: The following rules can be added in the future:
+ - For any changes or new lines use single quotes for strings, or double
+ quotes if the string contains a single quote. But keep older code
+ unchanged.
+ - Do not use a u'' prefix on strings, as it is meaningless due to
+ __future__.unicode_literals. But keep older code unchanged.
+
+"""
+
+
+from __future__ import absolute_import, unicode_literals
+
+from re import compile as re_compile
+from subprocess import check_output
+
+from unidiff import PatchSet
+
+
+IGNORABLE_LONG_LINE = re_compile(r'^\s*(# )?<?https?://\S+>?$').search
+
+
+def get_latest_patchset():
+ """Return the PatchSet for the latest commit."""
+ # regex from
https://github.com/PyCQA/pylint/blob/master/pylintrc
+ output = check_output(
+ ['git', 'diff', '-U0', '@~..@'],
universal_newlines=True)
+ return PatchSet.from_string(output)
+
+
+def check_line_lengths(latest_patchset):
+ """Check that none of the added/modified lines are longer than 79
chars.
+
+ @return: True if line lengths are OK. False otherwise.
+ @rtype: bool
+ """
+ found_long = False
+ for patched_file in latest_patchset:
+ target_file = patched_file.target_file
+ if not (target_file.endswith('.py') or patched_file.is_removed_file):
+ continue
+ for hunk in patched_file:
+ for line in hunk:
+ if (
+ line.is_added and len(line.value) > 80
+ and not IGNORABLE_LONG_LINE(line.value)
+ ):
+ print(
+ target_file + ':' + str(line.target_line_no) +
':'
+ + str(len(line.value))
+ + ': line is too long (more than 79 characters)'
+ )
+ found_long = True
+ return not found_long
+
+
+def main():
+ """Run this script."""
+ latest_patchset = get_latest_patchset()
+ if not check_line_lengths(latest_patchset):
+ raise SystemExit('diff-checker failed')
+
+
+if __name__ == '__main__':
+ main()
diff --git a/tox.ini b/tox.ini
index a59f7be..906911d 100644
--- a/tox.ini
+++ b/tox.ini
@@ -3,13 +3,13 @@
minversion = 1.7.2
skipsdist = True
skip_missing_interpreters = True
-envlist =
commit-message,flake8,pyflakes-{py26,py3,pypy},doctest-{py27,py34},py26,py27,py34
+envlist =
diff-checker,commit-message,flake8,pyflakes-{py26,py3,pypy},doctest-{py27,py34},py26,py27,py34
[tox:jenkins]
# Override default for WM Jenkins
# Others are run in their own individual jobs on WM Jenkins
# Wikimedia Jenkins does not have Python 2.6
-envlist = commit-message,flake8,pyflakes-{py3,pypy}
+envlist = diff-checker,commit-message,flake8,pyflakes-{py3,pypy}
[params]
doctest_skip = --ignore-files=(gui\.py|botirc\.py|eventstreams\.py)
@@ -20,6 +20,10 @@
PYWIKIBOT2_NO_USER_CONFIG=2
usedevelop = True
commands = python setup.py test
+
+[testenv:diff-checker]
+deps = unidiff
+commands = python pwb.py diff_checker
[testenv:commit-message]
deps = commit-message-validator
@@ -146,6 +150,7 @@
# H404: docstring multiline start
# H405: docstring summary line
# H301,I100,I101,202: import order rules; Pywikibot uses rules H306 and I201
+# W503: line break before binary operator; against current PEP 8 recommendation
# The following are to be fixed
# C401, C402, C405: does not work with py 2.6
@@ -160,7 +165,7 @@
# D413: Missing blank line after last section
# D412: No blank lines allowed between a section header and its content
-ignore =
C401,C402,C405,E402,D105,D211,FI10,FI12,FI13,FI15,FI16,FI17,FI5,H101,H236,H301,H404,H405,H903,I100,I101,I202,N802,N803,N806,D401,D413,D103,D412
+ignore =
C401,C402,C405,E402,D105,D211,FI10,FI12,FI13,FI15,FI16,FI17,FI5,H101,H236,H301,H404,H405,H903,I100,I101,I202,N802,N803,N806,D401,D413,D103,D412,W503
exclude = .tox,.git,./*.egg,ez_setup.py,build,externals,user-config.py,./scripts/i18n/*
min-version = 2.6
max_line_length = 100
--
To view, visit
https://gerrit.wikimedia.org/r/398262
To unsubscribe, visit
https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I7c430431ebdc942e9f8c1c8b576feee22d7ff7d3
Gerrit-PatchSet: 6
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Dalba <dalba.wiki(a)gmail.com>
Gerrit-Reviewer: John Vandenberg <jayvdb(a)gmail.com>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: Zoranzoki21 <zorandori4444(a)gmail.com>
Gerrit-Reviewer: jenkins-bot <>