Style-fixes to hacking/core.py

Makes a few small style-cleanups:

    * Removes need for duplicated empty __init__ methods
    * Changes `return(` to `return (`
    * Turns `_has_run_registry` from a global dict to a class-scoped set
    * Moves GIT_REGEX to the class that uses it

Change-Id: I3b15b74fe5006461edf3a28c62674ba152a07249
This commit is contained in:
Rick Harris 2013-05-24 19:10:58 +00:00 committed by Joe Gordon
parent 32f820bcaf
commit 80664dd471

View File

@ -625,13 +625,16 @@ def hacking_not_in(logical_line):
yield (logical_line.find('not'), "H902: Use the 'not in' " yield (logical_line.find('not'), "H902: Use the 'not in' "
"operator for collection membership evaluation") "operator for collection membership evaluation")
_has_run_registry = dict()
class GlobalCheck(object): class GlobalCheck(object):
"""Base class for checks that should be run only once.""" """Base class for checks that should be run only once."""
name = None
version = '0.0.1' version = '0.0.1'
_has_run = set()
def __init__(self, tree, *args):
pass
def run(self): def run(self):
"""Make run a no-op if run() has been called before. """Make run a no-op if run() has been called before.
@ -641,9 +644,8 @@ class GlobalCheck(object):
This way, since pep8 is file/line based, we don't wind up re-running This way, since pep8 is file/line based, we don't wind up re-running
a check on a git commit message over and over again. a check on a git commit message over and over again.
""" """
global _has_run_registry if self.name and self.name not in self.__class__._has_run:
if self.name not in _has_run_registry.keys(): self.__class__._has_run.add(self.name)
_has_run_registry[self.name] = True
ret = self.run_once() ret = self.run_once()
if ret is not None: if ret is not None:
yield ret yield ret
@ -653,17 +655,7 @@ class GlobalCheck(object):
class GitCheck(GlobalCheck): class GitCheck(GlobalCheck):
"""Base-class for Git related checks."""
#From https://github.com/openstack/openstack-ci-puppet
# /blob/master/modules/gerrit/manifests/init.pp#L74
#Changeid|bug|blueprint
git_keywords = (r'(I[0-9a-f]{8,40})|'
'([Bb]ug|[Ll][Pp])[\s\#:]*(\d+)|'
'([Bb]lue[Pp]rint|[Bb][Pp])[\s\#:]*([A-Za-z0-9\\-]+)')
GIT_REGEX = re.compile(git_keywords)
def __init__(self, tree, *args):
pass
def _get_commit_title(self): def _get_commit_title(self):
if not os.path.exists('.git'): if not os.path.exists('.git'):
@ -688,8 +680,13 @@ class OnceGitCheckCommitTitleBug(GitCheck):
""" """
name = "GitCheckCommitTitleBug" name = "GitCheckCommitTitleBug"
def __init__(self, tree, *args): #From https://github.com/openstack/openstack-ci-puppet
pass # /blob/master/modules/gerrit/manifests/init.pp#L74
#Changeid|bug|blueprint
GIT_REGEX = re.compile(
r'(I[0-9a-f]{8,40})|'
'([Bb]ug|[Ll][Pp])[\s\#:]*(\d+)|'
'([Bb]lue[Pp]rint|[Bb][Pp])[\s\#:]*([A-Za-z0-9\\-]+)')
def run_once(self): def run_once(self):
title = self._get_commit_title() title = self._get_commit_title()
@ -697,10 +694,10 @@ class OnceGitCheckCommitTitleBug(GitCheck):
#NOTE(jogo) if match regex but over 3 words, acceptable title #NOTE(jogo) if match regex but over 3 words, acceptable title
if (title and self.GIT_REGEX.search(title) is not None if (title and self.GIT_REGEX.search(title) is not None
and len(title.split()) <= 3): and len(title.split()) <= 3):
return(1, 0, return (1, 0,
"H801: git commit title ('%s') should provide an accurate " "H801: git commit title ('%s') should provide an accurate "
"description of the change, not just a reference to a bug " "description of the change, not just a reference to a bug "
"or blueprint" % title.strip(), self.name) "or blueprint" % title.strip(), self.name)
class OnceGitCheckCommitTitleLength(GitCheck): class OnceGitCheckCommitTitleLength(GitCheck):
@ -713,14 +710,11 @@ class OnceGitCheckCommitTitleLength(GitCheck):
""" """
name = "GitCheckCommitTitleLength" name = "GitCheckCommitTitleLength"
def __init__(self, tree, *args):
pass
def run_once(self): def run_once(self):
title = self._get_commit_title() title = self._get_commit_title()
if title and len(title.decode('utf-8')) > 72: if title and len(title.decode('utf-8')) > 72:
return( return (
1, 0, 1, 0,
"H802: git commit title ('%s') should be under 50 chars" "H802: git commit title ('%s') should be under 50 chars"
% title.strip(), % title.strip(),
@ -729,12 +723,8 @@ class OnceGitCheckCommitTitleLength(GitCheck):
class ProxyChecks(GlobalCheck): class ProxyChecks(GlobalCheck):
"""Provide a mechanism for locally defined checks.""" """Provide a mechanism for locally defined checks."""
name = 'ProxyChecker' name = 'ProxyChecker'
def __init__(self, tree, *args):
pass
@classmethod @classmethod
def add_options(cls, parser): def add_options(cls, parser):
# Abusing this method because of when it gets called # Abusing this method because of when it gets called