Add script to scan for best practices 52/10052/6
authorThanh Ha <thanh.ha@linuxfoundation.org>
Mon, 16 Apr 2018 23:48:28 +0000 (19:48 -0400)
committerThanh Ha <thanh.ha@linuxfoundation.org>
Wed, 11 Jul 2018 16:42:34 +0000 (12:42 -0400)
Script ensures we are following best practices in our documentation.

Change-Id: Ibb7f62230657fb0cc1fb7e186f315aefd02dada2
Signed-off-by: Thanh Ha <thanh.ha@linuxfoundation.org>
.coafile
check-best-practices.py [new file with mode: 0644]
tox.ini

index 38f6f36..b7909cd 100644 (file)
--- 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 (file)
index 0000000..97ac5fd
--- /dev/null
@@ -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 (file)
--- 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 =