From e86414bb76a45c608160a81248d6ccb41d8cac99 Mon Sep 17 00:00:00 2001 From: Michael Johnson Date: Wed, 3 May 2017 18:40:53 -0700 Subject: [PATCH] Add some hacking checks borrowed from neutron - [O343] Python 3: do not use basestring. - [O344] Python 3: do not use dict.iteritems. - [O345] Usage of Python eventlet module not allowed Change-Id: I0d70bbc4608cc08f0944b836814462acdcf7269f --- HACKING.rst | 3 +++ octavia/hacking/checks.py | 47 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/HACKING.rst b/HACKING.rst index f5cdcaca55..cb122accc2 100644 --- a/HACKING.rst +++ b/HACKING.rst @@ -25,6 +25,9 @@ Octavia Specific Commandments - [O340] Don't use xrange() - [O341] Don't translate logs. - [0342] Exception messages should be translated +- [O343] Python 3: do not use basestring. +- [O344] Python 3: do not use dict.iteritems. +- [O345] Usage of Python eventlet module not allowed Creating Unit Tests ------------------- diff --git a/octavia/hacking/checks.py b/octavia/hacking/checks.py index 8e10d85291..a0f9df2921 100644 --- a/octavia/hacking/checks.py +++ b/octavia/hacking/checks.py @@ -223,6 +223,50 @@ def check_raised_localized_exceptions(logical_line, filename): yield (logical_line.index(exception_msg), msg) +def check_no_basestring(logical_line): + """O343 - basestring is not Python3-compatible. + + :param logical_line: The logical line to check. + :returns: None if the logical line passes the check, otherwise a tuple + is yielded that contains the offending index in logical line and a + message describe the check validation failure. + """ + if re.search(r"\bbasestring\b", logical_line): + msg = ("O343: basestring is not Python3-compatible, use " + "six.string_types instead.") + yield(0, msg) + + +def check_python3_no_iteritems(logical_line): + """O344 - Use dict.items() instead of dict.iteritems(). + + :param logical_line: The logical line to check. + :returns: None if the logical line passes the check, otherwise a tuple + is yielded that contains the offending index in logical line and a + message describe the check validation failure. + """ + if re.search(r".*\.iteritems\(\)", logical_line): + msg = ("O344: Use dict.items() instead of dict.iteritems() to be " + "compatible with both Python 2 and Python 3. In Python 2, " + "dict.items() may be inefficient for very large dictionaries. " + "If you can prove that you need the optimization of an " + "iterator for Python 2, then you can use six.iteritems(dict).") + yield(0, msg) + + +def check_no_eventlet_imports(logical_line): + """O345 - Usage of Python eventlet module not allowed. + + :param logical_line: The logical line to check. + :returns: None if the logical line passes the check, otherwise a tuple + is yielded that contains the offending index in logical line and a + message describe the check validation failure. + """ + if re.match(r'(import|from)\s+[(]?eventlet', logical_line): + msg = 'O345 Usage of Python eventlet module not allowed' + yield logical_line.index('eventlet'), msg + + def factory(register): register(assert_true_instance) register(assert_equal_or_not_none) @@ -235,3 +279,6 @@ def factory(register): register(no_log_warn) register(no_xrange) register(check_raised_localized_exceptions) + register(check_no_basestring) + register(check_python3_no_iteritems) + register(check_no_eventlet_imports)