Skip to content

Commit

Permalink
Basic support for requirements.txt file
Browse files Browse the repository at this point in the history
  • Loading branch information
arkq committed Jan 23, 2018
1 parent 06359c0 commit aa26fe6
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License

Copyright (c) 2017 Arkadiusz Bokowy <[email protected]>
Copyright (c) 2017-2018 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
Expand Down
3 changes: 2 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ Important notice

In order to collect project's dependencies, this checker evaluates Python code from the
``setup.py`` file stored in the project's root directory. Code evaluation is done with the
`eval() <https://docs.python.org/3/library/functions.html#eval>`_ function.
`eval() <https://docs.python.org/3/library/functions.html#eval>`_ function. As a fall-back
method, this checker also tries to load dependencies from the ``requirements.txt`` file.

At this point it is very important to be aware of the consequences of the above approach. One
might inject malicious code into the ``setup.py`` file, which will be executed by this checker.
Expand Down
25 changes: 20 additions & 5 deletions src/flake8_requirements/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from .modules import STDLIB_PY3

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

LOG = getLogger('flake8.plugin.requires')
Expand Down Expand Up @@ -256,6 +256,7 @@ def __init__(self, tree, filename, lines=None):
self.tree = tree
self.filename = filename
self.lines = lines
self.requirements = self.get_requirements()
self.setup = self.get_setup()

@classmethod
Expand Down Expand Up @@ -284,6 +285,17 @@ def parse_options(cls, options):
]
}

@classmethod
@memoize
def get_requirements(cls):
"""Get package requirements."""
try:
with open("requirements.txt") as f:
return tuple(parse_requirements(f.readlines()))
except IOError as e:
LOG.debug("Couldn't open requirements file: %s", e)
return ()

@classmethod
@memoize
def get_setup(cls):
Expand Down Expand Up @@ -325,10 +337,13 @@ def modcmp(lib=(), test=()):
modules = self.known_modules[modules[0]]
mods_1st_party.update(split(x) for x in modules)

requirements = self.setup.get_requirements(
setup=self.processing_setup_py,
tests=True,
)
requirements = self.requirements
if self.setup.redirected:
# Use requirements from setup if available.
requirements = self.setup.get_requirements(
setup=self.processing_setup_py,
tests=True,
)

# Get 3rd party module names based on requirements.
for requirement in requirements:
Expand Down
1 change: 1 addition & 0 deletions test/test_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
class SetupVisitorMock(checker.SetupVisitor):

def __init__(self):
self.redirected = True
self.keywords = {
'name': "flake8-requires",
'install_requires': [
Expand Down

0 comments on commit aa26fe6

Please sign in to comment.