Skip to content

Commit

Permalink
Requirements checker initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
arkq committed Nov 27, 2017
0 parents commit 39c66b1
Show file tree
Hide file tree
Showing 10 changed files with 968 additions and 0 deletions.
37 changes: 37 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*,cover
.hypothesis/
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License

Copyright (c) 2017 Arkadiusz Bokowy <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
22 changes: 22 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Package requirements checker
============================

This module provides a plug-in for [flake8](http://flake8.pycqa.org), which checks/validates
package import requirements. It reports missing and/or not used project direct dependencies.

Installation
------------

You can install, upgrade, or uninstall ``flake8-requirements`` with these commands::

$ pip install flake8-requirements
$ pip install --upgrade flake8-requirements
$ pip uninstall flake8-requirements

Warnings
--------

This package adds new flake8 warnings as follows:

- ``I900``: Package is not listed as a requirement.
- ``I901``: Package is require but not used.
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[bdist_wheel]
universal = 1
50 changes: 50 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from __future__ import with_statement

import re
from os import path

from setuptools import setup


def get_abs_path(pathname):
return path.join(path.dirname(__file__), pathname)


with open(get_abs_path("src/flake8_requirements/checker.py")) as f:
version = re.match(r'.*__version__ = "(.*?)"', f.read(), re.S).group(1)
with open(get_abs_path("README.rst")) as f:
long_description = f.read()

setup(
name="flake8-requirements",
version=version,
author="Arkadiusz Bokowy",
author_email="[email protected]",
url="https://github.com/Arkq/flake8-requirements",
description="Package requirements checker, plugin for flake8",
long_description=long_description,
license="MIT",
package_dir={'': "src"},
packages=["flake8_requirements"],
install_requires=[
"flake8 > 2.0.0",
"setuptools",
],
setup_requires=["pytest-runner"],
tests_require=["pytest"],
entry_points={
'flake8.extension': [
'I90 = flake8_requirements:Flake8Checker',
],
},
classifiers=[
"Framework :: Flake8",
"Intended Audience :: Developers",
"License :: OSI Approved :: MIT License",
"Programming Language :: Python",
"Programming Language :: Python :: 2",
"Programming Language :: Python :: 3",
"Topic :: Software Development :: Libraries :: Python Modules",
"Topic :: Software Development :: Quality Assurance",
],
)
5 changes: 5 additions & 0 deletions src/flake8_requirements/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from .checker import Flake8Checker

__all__ = (
'Flake8Checker',
)
200 changes: 200 additions & 0 deletions src/flake8_requirements/checker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
import ast
import sys
from itertools import chain

from pkg_resources import parse_requirements

from .modules import KNOWN_3RD_PARTIES
from .modules import STDLIB_PY2
from .modules import STDLIB_PY3

# NOTE: Changing this number will alter package version as well.
__version__ = "1.0.0"
__license__ = "MIT"

ERRORS = {
'I900': "I900 '{pkg}' not listed as a requirement",
'I901': "I901 '{pkg}' required but not used",
}

STDLIB = set()
if sys.version_info[0] == 2:
STDLIB.update(STDLIB_PY2)
if sys.version_info[0] == 3:
STDLIB.update(STDLIB_PY3)


class ImportVisitor(ast.NodeVisitor):
"""Import statement visitor."""

def __init__(self, tree):
"""Initialize import statement visitor."""
self.imports = []
self.visit(tree)

def visit_Import(self, node):
self.imports.append((node, node.names[0].name))

def visit_ImportFrom(self, node):
self.imports.append((node, node.module))


class SetupVisitor(ast.NodeVisitor):
"""Package setup visitor.
Warning:
This visitor class executes given Abstract Syntax Tree!
"""

# Set of keywords used by the setup() function.
attributes = {
'required': {
'name',
'version',
},
'one-of': {
'ext_modules',
'packages',
'py_modules',
},
'optional': {
'author',
'author_email',
'classifiers',
'cmdclass',
'configuration',
'convert_2to3_doctests',
'dependency_links',
'description',
'download_url',
'eager_resources',
'entry_points',
'exclude_package_data',
'extras_require',
'features',
'include_package_data',
'install_requires',
'keywords',
'license',
'long_description',
'maintainer',
'maintainer_email',
'message_extractors',
'namespace_packages',
'package_data',
'package_dir',
'platforms',
'python_requires',
'scripts',
'setup_requires',
'test_loader',
'test_suite',
'tests_require',
'url',
'use_2to3',
'use_2to3_fixers',
'zip_safe',
},
}

def __init__(self, tree):
"""Initialize package setup visitor."""
self.redirected = False
self.keywords = {}

# Find setup() call and redirect it.
self.visit(tree)

if not self.redirected:
return

def setup(**kw):
self.keywords = kw

eval(
compile(ast.fix_missing_locations(tree), "<str>", mode='exec'),
{'__file__': "setup.py", '__f8r_setup': setup},
)

def get_requirements(self, install=True, extras=True):
"""Get package requirements."""
requires = []
if install:
requires.extend(parse_requirements(
self.keywords.get('install_requires', ()),
))
if extras:
for r in self.keywords.get('extras_require', {}).values():
requires.extend(parse_requirements(r))
return requires

def visit_Call(self, node):
"""Call visitor - used for finding setup() call."""
self.generic_visit(node)

# Setuptools setup() is a keywords only function.
if not (not node.args and (node.keywords or node.kwargs)):
return

keywords = {x.arg for x in node.keywords}
if node.kwargs:
keywords.update(x.s for x in node.kwargs.keys)

if not keywords.issuperset(self.attributes['required']):
return
if not keywords.intersection(self.attributes['one-of']):
return
if not keywords.issubset(chain(*self.attributes.values())):
return

# Redirect call to our setup() tap function.
node.func = ast.Name(id='__f8r_setup', ctx=node.func.ctx)
self.redirected = True


class Flake8Checker(object):
"""Package requirements checker."""

name = "flake8-requires"
version = __version__

def __init__(self, tree, filename, lines=None):
"""Initialize requirements checker."""
self.setup = self.get_setup()
self.tree = tree

def get_setup(self):
"""Get package setup."""
with open("setup.py") as f:
return SetupVisitor(ast.parse(f.read()))

def run(self):
"""Run checker."""

def modcmp(mod1=(), mod2=()):
"""Compare import modules."""
return all(a == b for a, b in zip(mod1, mod2))

requirements = set()

# Get module names based on requirements.
for requirement in self.setup.get_requirements():
project = requirement.project_name.lower()
modules = [project.replace("-", "_")]
if project in KNOWN_3RD_PARTIES:
modules = KNOWN_3RD_PARTIES[project]
requirements.update(tuple(x.split(".")) for x in modules)

for node, module in ImportVisitor(self.tree).imports:
_module = module.split(".")
if any([_module[0] == x for x in STDLIB]):
continue
if any([modcmp(_module, x) for x in requirements]):
continue
yield (
node.lineno,
node.col_offset,
ERRORS['I900'].format(pkg=module),
Flake8Checker,
)
Loading

0 comments on commit 39c66b1

Please sign in to comment.