Skip to content

Commit

Permalink
Discover Python files in dirs without __init__.py. Fixes #790
Browse files Browse the repository at this point in the history
also renames the module b/c there's another checker which will
fit in here
  • Loading branch information
atodorov committed May 23, 2019
1 parent b113076 commit a9cdb57
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 24 deletions.
4 changes: 3 additions & 1 deletion kiwi_lint/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
from .bulk_create import BulkCreateChecker
from .objects_update import ObjectsUpdateChecker
from .tags import TagsChecker
from .empty_module import EmptyModuleChecker
from .empty import EmptyModuleChecker
from .empty import ModuleInDirectoryWithoutInitChecker
from .nested_definition import NestedDefinitionChecker
from .missing_permissions import MissingPermissionsChecker

Expand All @@ -26,5 +27,6 @@ def register(linter):
linter.register_checker(ObjectsUpdateChecker(linter))
linter.register_checker(TagsChecker(linter))
linter.register_checker(EmptyModuleChecker(linter))
linter.register_checker(ModuleInDirectoryWithoutInitChecker(linter))
linter.register_checker(NestedDefinitionChecker(linter))
linter.register_checker(MissingPermissionsChecker(linter))
75 changes: 75 additions & 0 deletions kiwi_lint/empty.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Copyright (c) 2018 Alexander Todorov <[email protected]>

# Licensed under the GPL 2.0: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html

import os

from pylint import interfaces
from pylint import checkers
from pylint.checkers import utils


class EmptyModuleChecker(checkers.BaseChecker):
__implements__ = (interfaces.IAstroidChecker,)

name = 'empty-module-checker'

msgs = {'E4481': ("Remove empty module from git!",
'remove-empty-module',
"Kiwi TCMS doesn't need to carry around modules which are empty. "
"They must be removed from the source code!")}

@utils.check_messages('remove-empty-module')
def visit_module(self, node):
if not node.body and not node.path[0].endswith('__init__.py'):
self.add_message('remove-empty-module', node=node)


class ModuleInDirectoryWithoutInitChecker(checkers.BaseChecker):
__implements__ = (interfaces.IAstroidChecker,)

name = 'dir-without-init-checker'

msgs = {'R4482': ("File '%s' is in directory without __init__.py",
'module-in-directory-without-init',
"Python module is found inside a directory which is "
"missing __init__.py! This will lead to missing packages when "
"tarball is built for distribution on PyPI! See "
"https://github.com/kiwitcms/Kiwi/issues/790")}

project_root = os.path.abspath(
os.path.join(os.path.dirname(__file__), '..', 'tcms'))

# NOTE: this works against tcms/ directory and will not take into account
# if we want to examine only a sub-dir or a few files
# all files found by os.walk
all_python_files = set()
# all modules found by pylint, which conveniently skips files/dirs
# with missing __init__.py
discovered_python_files = set()

def open(self):
for root, _dirs, files in os.walk(self.project_root, topdown=False):
# skip migrations
if root.find('migrations') > -1:
continue

for file_name in files:
if file_name.endswith('.py'):
self.all_python_files.add(
os.path.join(self.project_root, root, file_name))

def visit_module(self, node):
for file_name in node.path:
self.discovered_python_files.add(file_name)

@utils.check_messages('module-in-directory-without-init')
def close(self):
diff = self.all_python_files - self.discovered_python_files
diff = list(diff)
diff.sort()

dir_prefix = os.path.dirname(self.project_root) + '/'
for fname in diff:
fname = fname.replace(dir_prefix, '')
self.add_message('module-in-directory-without-init', args=(fname,))
23 changes: 0 additions & 23 deletions kiwi_lint/empty_module.py

This file was deleted.

0 comments on commit a9cdb57

Please sign in to comment.