How to use hacking - 10 common examples

To help you get started, we’ve selected a few hacking examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github openstack / sahara / sahara / utils / hacking / import_checks.py View on Github external
third party library imports are together, and project imports are
    together

    Okay: import os\nimport sys\n\nimport six\n\nimport hacking
    Okay: import six\nimport znon_existent_package
    Okay: import os\nimport threading
    S366: import mock\nimport os
    S366: import hacking\nimport os
    S366: import hacking\nimport nonexistent
    S366: import hacking\nimport mock
    """
    if (noqa or blank_before > 0 or
            indent_level != previous_indent_level):
        return

    normalized_line = core.import_normalize(logical_line.strip()).split()
    normalized_previous = core.import_normalize(previous_logical.
                                                strip()).split()

    def compatible(previous, current):
        if previous == current:
            return True

    if normalized_line and normalized_line[0] == 'import':
        current_type = _get_import_type(normalized_line[1])
        if normalized_previous and normalized_previous[0] == 'import':
            previous_type = _get_import_type(normalized_previous[1])
            if not compatible(previous_type, current_type):
                yield(0, 'S366: imports not grouped correctly '
                      '(%s: %s, %s: %s)' %
                      (normalized_previous[1], previous_type,
                       normalized_line[1], current_type))
github openstack / sahara / sahara / utils / hacking / import_checks.py View on Github external
together

    Okay: import os\nimport sys\n\nimport six\n\nimport hacking
    Okay: import six\nimport znon_existent_package
    Okay: import os\nimport threading
    S366: import mock\nimport os
    S366: import hacking\nimport os
    S366: import hacking\nimport nonexistent
    S366: import hacking\nimport mock
    """
    if (noqa or blank_before > 0 or
            indent_level != previous_indent_level):
        return

    normalized_line = core.import_normalize(logical_line.strip()).split()
    normalized_previous = core.import_normalize(previous_logical.
                                                strip()).split()

    def compatible(previous, current):
        if previous == current:
            return True

    if normalized_line and normalized_line[0] == 'import':
        current_type = _get_import_type(normalized_line[1])
        if normalized_previous and normalized_previous[0] == 'import':
            previous_type = _get_import_type(normalized_previous[1])
            if not compatible(previous_type, current_type):
                yield(0, 'S366: imports not grouped correctly '
                      '(%s: %s, %s: %s)' %
                      (normalized_previous[1], previous_type,
                       normalized_line[1], current_type))
github openstack / hacking / hacking / checks / imports.py View on Github external
def hacking_import_alphabetical(logical_line, blank_before, previous_logical,
                                indent_level, previous_indent_level):
    r"""Check for imports in alphabetical order.

    OpenStack HACKING guide recommendation for imports:
    imports in human alphabetical order

    Okay: import os\nimport sys\n\nimport nova\nfrom nova import test
    Okay: import os\nimport sys
    H306: import sys\nimport os
    Okay: import sys\n\n# foo\nimport six
    """
    # handle import x
    # use .lower since capitalization shouldn't dictate order
    if blank_before < 1 and indent_level == previous_indent_level:
        split_line = core.import_normalize(logical_line.
                                           strip()).lower().split()
        split_previous = core.import_normalize(previous_logical.
                                               strip()).lower().split()
        length = [2, 4]
        if (len(split_line) in length and len(split_previous) in length and
                split_line[0] == "import" and split_previous[0] == "import"):
            if split_line[1] < split_previous[1]:
                yield (0, "H306: imports not in alphabetical order (%s, %s)"
                       % (split_previous[1], split_line[1]))
github openstack / hacking / hacking / checks / python23.py View on Github external
def hacking_no_removed_module(logical_line, noqa):
    r"""Check for removed modules in Python 3.

    Examples:
    Okay: from os import path
    Okay: from os import path as p
    Okay: from os import (path as p)
    Okay: import os.path
    H237: import thread
    Okay: import thread  # noqa
    H237: import commands
    H237: import md5 as std_md5
    """
    if noqa:
        return
    line = core.import_normalize(logical_line.strip())
    if line and line.split()[0] == 'import':
        module_name = line.split()[1].split('.')[0]
        if module_name in removed_modules:
            yield 0, ("H237: module %s is "
                      "removed in Python 3" % module_name)
github openstack / sahara / sahara / utils / hacking / import_checks.py View on Github external
Okay: import eventlet\neventlet.monkey_patch()\n\nimport copy
    S367: import mock\n\nimport six
    S367: import os\n\nimport sys
    S367: import mock\nimport os\n\nimport sys
    """
    if line_number == 1 or filename != together_data.current_filename:
        together_data.current_group = None
    together_data.current_filename = filename

    if noqa:
        return

    def update_current_group(current):
        together_data.current_group = current

    normalized_line = core.import_normalize(logical_line.strip()).split()
    if normalized_line:
        if normalized_line[0] == 'import':
            current_type = _get_import_type(normalized_line[1])
            previous_import = together_data.current_import
            together_data.current_import = normalized_line[1]
            matched = current_type == together_data.current_group
            update_current_group(current_type)
            if (matched and indent_level == previous_indent_level and
                    blank_lines >= 1):
                yield(0, 'S367: like imports should be grouped together (%s '
                      'and %s from %s are separated by whitespace)' %
                      (previous_import,
                       together_data.current_import,
                       current_type))
        else:
            # Reset on non-import code
github openstack / hacking / hacking / checks / imports.py View on Github external
r"""Check for imports in alphabetical order.

    OpenStack HACKING guide recommendation for imports:
    imports in human alphabetical order

    Okay: import os\nimport sys\n\nimport nova\nfrom nova import test
    Okay: import os\nimport sys
    H306: import sys\nimport os
    Okay: import sys\n\n# foo\nimport six
    """
    # handle import x
    # use .lower since capitalization shouldn't dictate order
    if blank_before < 1 and indent_level == previous_indent_level:
        split_line = core.import_normalize(logical_line.
                                           strip()).lower().split()
        split_previous = core.import_normalize(previous_logical.
                                               strip()).lower().split()
        length = [2, 4]
        if (len(split_line) in length and len(split_previous) in length and
                split_line[0] == "import" and split_previous[0] == "import"):
            if split_line[1] < split_previous[1]:
                yield (0, "H306: imports not in alphabetical order (%s, %s)"
                       % (split_previous[1], split_line[1]))
github ansible / ansible / hacking / build_library / build_ansible / command_plugins / porting_guide.py View on Github external
version_list = version.split('.')
    version_list[-1] = str(int(version_list[-1]) - 1)
    previous_version = '.'.join(version_list)

    content = template.render(ver=version, prev_ver=previous_version)
    return content


def write_guide(version, guide_content):
    filename = 'porting_guide_{0}.rst'.format(version)
    with open(filename, 'w') as out_file:
        out_file.write(guide_content)


class PortingGuideCommand(Command):
    name = 'porting-guide'

    @classmethod
    def init_parser(cls, add_parser):
        parser = add_parser(cls.name, description="Generate a fresh porting guide template")
        parser.add_argument("--version", dest="version", type=str, required=True, action='store',
                            help="Version of Ansible to write the porting guide for")

    @staticmethod
    def main(args):
        guide_content = generate_porting_guide(args.version)
        write_guide(args.version, guide_content)
        return 0
github ansible / ansible / hacking / build_library / build_ansible / command_plugins / plugin_formatter.py View on Github external
text = templates['support_list'].render(template_data)
        write_data(text, output_dir, data['output'])


def validate_options(options):
    ''' validate option parser options '''

    if not options.module_dir:
        sys.exit("--module-dir is required", file=sys.stderr)
    if not os.path.exists(options.module_dir):
        sys.exit("--module-dir does not exist: %s" % options.module_dir, file=sys.stderr)
    if not options.template_dir:
        sys.exit("--template-dir must be specified")


class DocumentPlugins(Command):
    name = 'document-plugins'

    @classmethod
    def init_parser(cls, add_parser):
        parser = add_parser(cls.name, description='Generate module documentation from metadata')

        parser.add_argument("-A", "--ansible-version", action="store", dest="ansible_version",
                            default="unknown", help="Ansible version number")
        parser.add_argument("-M", "--module-dir", action="store", dest="module_dir",
                            default=MODULEDIR, help="Ansible library path")
        parser.add_argument("-P", "--plugin-type", action="store", dest="plugin_type",
                            default='module', help="The type of plugin (module, lookup, etc)")
        parser.add_argument("-T", "--template-dir", action="append", dest="template_dir",
                            help="directory containing Jinja2 templates")
        parser.add_argument("-t", "--type", action='store', dest='type', choices=['rst'],
                            default='rst', help="Document type")
github openstack / hacking / hacking / checks / mock_checks.py View on Github external
def check_missing_autospec(self, call_node):

        def find_autospec_keyword(keyword_node):
            for keyword_obj in keyword_node:
                keyword = keyword_obj.arg
                # If they have defined autospec or new then it is okay
                if keyword in self.spec_keywords:
                    return True
            return False

        if isinstance(call_node, ast.Call):
            func_info = FunctionNameFinder(self.filename)
            func_info.visit(call_node)

            # We are only looking at our patchers
            if func_info.function_name not in self.patchers:
                return

            min_args = self.patchers[func_info.function_name]

            if not find_autospec_keyword(call_node.keywords):
                if len(call_node.args) < min_args:
                    self.messages.append(
                        (call_node.lineno, call_node.col_offset,
                         "H210 Missing 'autospec' or 'spec_set' keyword in "
                         "mock.patch/mock.patch.object", MockCheckVisitor)
                    )
github openstack / hacking / hacking / checks / mock_checks.py View on Github external
def visit(self, node):
        # If we get called with an ast.Call node, then work on the 'node.func',
        # as we want the function name.
        if isinstance(node, ast.Call):
            return super(FunctionNameFinder, self).visit(node.func)
        return super(FunctionNameFinder, self).visit(node)