From c5b07cad1218e3517bf1748deb9e842effd3d8c4 Mon Sep 17 00:00:00 2001 From: Monty Taylor Date: Fri, 3 May 2013 18:58:12 -0400 Subject: [PATCH] Implement a proxy checker class. Add a proxy checker class for local checks that doesn't do anything, but instead registers a command option that allows for local, in-tree checks. Change-Id: I3f7567639ae47cc422c84f319f1b607ad193d901 Reviewed-on: https://review.openstack.org/28220 Reviewed-by: James E. Blair Reviewed-by: Giampaolo Lauria Reviewed-by: Sean Dague Approved: Joe Gordon Reviewed-by: Joe Gordon Tested-by: Jenkins --- README.rst | 21 +++++++++++++++++++++ hacking/core.py | 41 +++++++++++++++++++++++++++++++++++++++++ setup.cfg | 1 + 3 files changed, 63 insertions(+) diff --git a/README.rst b/README.rst index 68e770b5..13c89f3c 100644 --- a/README.rst +++ b/README.rst @@ -301,3 +301,24 @@ and how to split up commits into a series of changes, consult the project wiki: http://wiki.openstack.org/GitCommitMessages + +Local Checks +============ + +hacking supports having local changes in a source tree. They can be configured +to run in two different ways. They can be registered individually, or with +a factory function. + +For individual registration, put a comma separated list of pep8 compatible +check functions into the hacking section of tox.ini. Like + + [hacking] + local-check = nova.tests.hacking.bad_code_is_terrible + +Alternately, you can specify the location of a callable that will be called +at registration time and will be passed the registration function. The callable +should expect to call the passed in function on everything if wants to +register. Such as: + + [hacking] + local-check-factory = nova.tests.hacking.factory diff --git a/hacking/core.py b/hacking/core.py index c3d0884c..db689c01 100755 --- a/hacking/core.py +++ b/hacking/core.py @@ -21,6 +21,7 @@ Built as a sets of pep8 checks using flake8. """ +import ConfigParser import gettext import imp import logging @@ -31,6 +32,9 @@ import sys import tokenize import traceback +import d2to1.util +import pep8 + # Don't need this for testing logging.disable('LOG') @@ -560,6 +564,9 @@ class GlobalCheck(object): for r in ret: yield ret + def run_once(self): + pass + class GitCheck(GlobalCheck): @@ -634,3 +641,37 @@ class OnceGitCheckCommitTitleLength(GitCheck): "H802: git commit title ('%s') should be under 50 chars" % title.strip(), self.name) + + +class ProxyChecks(GlobalCheck): + """Provide a mechanism for locally defined checks.""" + + name = 'ProxyChecker' + + def __init__(self, tree, *args): + pass + + @classmethod + def add_options(cls, parser): + # Abusing this method because of when it gets called + if not os.path.exists('tox.ini'): + return + tox_ini = ConfigParser.RawConfigParser() + tox_ini.read('tox.ini') + if not tox_ini.has_section('hacking'): + return + + # We're looking for local checks, so we need to include the local + # dir in the search path + sys.path.append('.') + if tox_ini.has_option('hacking', 'local-check'): + for check_path in set( + tox_ini.get('hacking', 'local-check').split(",")): + if check_path.strip(): + checker = d2to1.util.resolve_name(check_path) + pep8.register_check(checker) + if tox_ini.has_option('hacking', 'local-check-factory'): + factory = d2to1.util.resolve_name( + tox_ini.get('hacking', 'local-check-factory')) + factory(pep8.register_check) + sys.path.pop() diff --git a/setup.cfg b/setup.cfg index 4a88e47c..546ffded 100644 --- a/setup.cfg +++ b/setup.cfg @@ -26,6 +26,7 @@ setup-hooks = [entry_points] flake8.extension = + H000 = hacking.core:ProxyChecks H101 = hacking.core:hacking_todo_format H201 = hacking.core:hacking_except_format H202 = hacking.core:hacking_except_format_assert