The nova.hacking.checks Module

class BaseASTChecker(tree, filename)

Bases: ast.NodeVisitor

Provides a simple framework for writing AST-based checks.

Subclasses should implement visit_* methods like any other AST visitor implementation. When they detect an error for a particular node the method should call self.add_error(offending_node). Details about where in the code the error occurred will be pulled from the node object.

Subclasses should also provide a class variable named CHECK_DESC to be used for the human readable error message.

add_error(node, message=None)

Add an error caused by a node to the list of errors for pep8.

run()

Called automatically by pep8.

class CheckForStrUnicodeExc(tree, filename)

Bases: nova.hacking.checks.BaseASTChecker

Checks for the use of str() or unicode() on an exception.

This currently only handles the case where str() or unicode() is used in the scope of an exception handler. If the exception is passed into a function, returned from an assertRaises, or used on an exception created in the same scope, this does not catch it.

CHECK_DESC = 'N325 str() and unicode() cannot be used on an exception. Remove or use six.text_type()'
visit_Call(node)
visit_TryExcept(node)
class CheckForTransAdd(tree, filename)

Bases: nova.hacking.checks.BaseASTChecker

Checks for the use of concatenation on a translated string.

Translations should not be concatenated with other strings, but should instead include the string being added to the translated string to give the translators the most information.

CHECK_DESC = 'N326 Translated messages cannot be concatenated. String should be included in translated message.'
TRANS_FUNC = ['_', '_LI', '_LW', '_LE', '_LC']
visit_BinOp(node)
assert_equal_in(logical_line)

Check for assertEqual(A in B, True), assertEqual(True, A in B), assertEqual(A in B, False) or assertEqual(False, A in B) sentences

N338

assert_equal_none(logical_line)

Check for assertEqual(A, None) or assertEqual(None, A) sentences

N318

assert_equal_type(logical_line)

Check for assertEqual(type(A), B) sentences

N317

assert_raises_regexp(logical_line)

Check for usage of deprecated assertRaisesRegexp

N335

assert_true_instance(logical_line)

Check for assertTrue(isinstance(a, b)) sentences

N316

assert_true_or_false_with_in(logical_line)

Check for assertTrue/False(A in B), assertTrue/False(A not in B), assertTrue/False(A in B, message) or assertTrue/False(A not in B, message) sentences.

N334

capital_cfg_help(logical_line, tokens)
cfg_help_with_enough_text(logical_line, tokens)
check_api_version_decorator(logical_line, previous_logical, blank_before, filename)
check_config_option_in_central_place(logical_line, filename)
check_doubled_words(physical_line, filename)

Check for the common doubled-word typos

N343

check_explicit_underscore_import(logical_line, filename)

Check for explicit import of the _ function

We need to ensure that any files that are using the _() function to translate logs are explicitly importing the _ function. We can’t trust unit test to catch whether the import has been added so we need to check for it here.

check_greenthread_spawns(logical_line, physical_line, filename)

Check for use of greenthread.spawn(), greenthread.spawn_n(), eventlet.spawn(), and eventlet.spawn_n()

N340

check_http_not_implemented(logical_line, physical_line, filename)
check_no_contextlib_nested(logical_line, filename)
check_python3_no_iteritems(logical_line)
check_python3_no_iterkeys(logical_line)
check_python3_no_itervalues(logical_line)
dict_constructor_with_list_copy(logical_line)
factory(register)
import_no_db_in_virt(logical_line, filename)

Check for db calls from nova/virt

As of grizzly-2 all the database calls have been removed from nova/virt, and we want to keep it that way.

N307

import_no_virt_driver_config_deps(physical_line, filename)

Check virt drivers’ config vars aren’t used by other drivers

Modules under each virt driver’s directory are considered private to that virt driver. Other drivers in Nova must not use their config vars. Any config vars that are to be shared should be moved into a common module

N312

import_no_virt_driver_import_deps(physical_line, filename)

Check virt drivers’ modules aren’t imported by other drivers

Modules under each virt driver’s directory are considered private to that virt driver. Other drivers in Nova must not access those drivers. Any code that is to be shared should be refactored into a common module

N311

no_db_session_in_public_api(logical_line, filename)
no_import_translation_in_tests(logical_line, filename)

Check for ‘from nova.i18n import _’ N337

no_mutable_default_args(logical_line)
no_os_popen(logical_line)

Disallow ‘os.popen(‘

Deprecated library function os.popen() Replace it using subprocess https://bugs.launchpad.net/tempest/+bug/1529836

N348

no_setting_conf_directly_in_tests(logical_line, filename)

Check for setting CONF.* attributes directly in tests

The value can leak out of tests affecting how subsequent tests run. Using self.flags(option=value) is the preferred method to temporarily set config options in tests.

N320

no_translate_debug_logs(logical_line, filename)

Check for ‘LOG.debug(_(‘

As per our translation policy, https://wiki.openstack.org/wiki/LoggingStandards#Log_Translation we shouldn’t translate debug level logs.

  • This check assumes that ‘LOG’ is a logger.
  • Use filename so we can start enforcing this in specific folders instead of needing to do so all at once.

N319

no_vi_headers(physical_line, line_number, lines)

Check for vi editor configuration in source files.

By default vi modelines can only appear in the first or last 5 lines of a source file.

N314

use_jsonutils(logical_line, filename)
use_timeutils_utcnow(logical_line, filename)
validate_log_translations(logical_line, physical_line, filename)

Previous topic

The nova.filters Module

Next topic

The nova.hooks Module

Project Source

This Page