From 3759ab468fe5e9a6be1a0fc24ea539cc7cc56f03 Mon Sep 17 00:00:00 2001 From: Thanh Ha Date: Mon, 16 Apr 2018 19:48:28 -0400 Subject: [PATCH] Add script to scan for best practices Script ensures we are following best practices in our documentation. Change-Id: Ibb7f62230657fb0cc1fb7e186f315aefd02dada2 Signed-off-by: Thanh Ha --- .coafile | 9 ++++++++ check-best-practices.py | 57 +++++++++++++++++++++++++++++++++++++++++++++++++ tox.ini | 4 ++++ 3 files changed, 70 insertions(+) create mode 100644 check-best-practices.py diff --git a/.coafile b/.coafile index 38f6f36..b7909cd 100644 --- a/.coafile +++ b/.coafile @@ -22,3 +22,12 @@ files = docs/**/*.rst bears = MarkdownBear,SpaceConsistencyBear,WriteGoodLintBear files = **.md, **.markdown use_spaces = true + +[all.Python] +bears = BanditBear, + PEP8Bear, + PyCommentedCodeBear, + PyDocStyleBear, + PyFlakesBear, + PyImportSortBear +files = *.py diff --git a/check-best-practices.py b/check-best-practices.py new file mode 100644 index 0000000..97ac5fd --- /dev/null +++ b/check-best-practices.py @@ -0,0 +1,57 @@ +# -*- coding: utf-8 -*- +# SPDX-License-Identifier: EPL-1.0 +############################################################################## +# Copyright (c) 2018 The Linux Foundation and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Eclipse Public License v1.0 +# which accompanies this distribution, and is available at +# http://www.eclipse.org/legal/epl-v10.html +############################################################################## +"""Scan documentation for bad practices.""" + +__author__ = 'Thanh Ha' + + +import fnmatch +import os +import re +import sys + + +def check_sudo_pip(filename): + """Scan for `sudo pip`. + + Returns false if file is clear of `sudo pip` and true if detected. + """ + counter = 0 + + print("Scanning {}".format(filename)) + with open(filename, 'r') as _file: + for num, line in enumerate(_file, 1): + if re.search('sudo pip', line): + counter += 1 + print('{}: {}'.format(num, line)) + + if counter: + print('ERROR: pip should never be used as a sudo command.') + print('Consider one of the following solutions:') + print('1. Use a virtualenv') + print(' (https://virtualenv.pypa.io/en/stable/)') + print('2. Use PEP370 instead via pip\'s --user parameter.') + print(' (https://www.python.org/dev/peps/pep-0370/)') + return True + + return False + + +if __name__ == "__main__": + counter = 0 + for root, dirnames, filenames in os.walk('docs'): + for filename in fnmatch.filter(filenames, '*.rst'): + if check_sudo_pip(os.path.join(root, filename)): + counter += 1 + + if counter: + print("Found {} files with violations.".format(counter)) + sys.exit(1) diff --git a/tox.ini b/tox.ini index f2e79ad..5677326 100644 --- a/tox.ini +++ b/tox.ini @@ -1,11 +1,15 @@ [tox] minversion = 1.6 envlist = + check-best-practices, coala, docs, docs-linkcheck skipsdist=true +[testenv:check-best-practices] +commands = python {toxinidir}/check-best-practices.py + [testenv:coala] basepython = python3 deps = -- 2.16.6