forked from arkq/flake8-requirements
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchecker.py
381 lines (324 loc) · 11.9 KB
/
checker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import ast
import os
import re
import sys
from collections import namedtuple
from functools import wraps
from logging import getLogger
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.1.0"
__license__ = "MIT"
LOG = getLogger('flake8.plugin.requires')
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)
def memoize(f):
"""Cache value returned by the function."""
@wraps(f)
def w(*args, **kw):
memoize.mem[f] = v = f(*args, **kw)
return v
return w
# Initialize cache memory block.
memoize.mem = {}
def project2module(project):
"""Convert project name into a module name."""
# Name unification in accordance with PEP 426.
project = project.lower().replace("-", "_")
if project.startswith("python_"):
# Remove conventional "python-" prefix.
project = project[7:]
return project
class ImportVisitor(ast.NodeVisitor):
"""Import statement visitor."""
# Convenience structure for storing import statement.
Import = namedtuple('Import', ('line', 'offset', 'mod', 'alt'))
def __init__(self, tree):
"""Initialize import statement visitor."""
self.imports = []
self.visit(tree)
def visit_Import(self, node):
self.imports.append(ImportVisitor.Import(
node.lineno,
node.col_offset,
node.names[0].name,
node.names[0].name,
))
def visit_ImportFrom(self, node):
if node.level != 0:
# Omit relative imports (local modules).
return
self.imports.append(ImportVisitor.Import(
node.lineno,
node.col_offset,
node.module,
# Alternative module name which covers:
# > from namespace import module
".".join((node.module, node.names[0].name)),
))
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 = {
# Attributes present in almost every setup(), however
# due to very generic name the score is not very high.
'name': 0.7,
'version': 0.7,
# One of these attributes is present in every setup(),
# scoring depends on the name uniqueness.
'ext_modules': 1.0,
'packages': 0.8,
'py_modules': 1.0,
# Mostly used (hence listed here) optional attributes.
'author': 0.5,
'author_email': 0.6,
'classifiers': 0.6,
'cmdclass': 0.6,
'convert_2to3_doctests': 1.0,
'dependency_links': 0.7,
'description': 0.5,
'download_url': 0.5,
'eager_resources': 0.7,
'entry_points': 0.7,
'exclude_package_data': 0.9,
'extras_require': 0.7,
'include_package_data': 0.9,
'install_requires': 0.7,
'keywords': 0.5,
'license': 0.5,
'long_description': 0.5,
'maintainer': 0.5,
'maintainer_email': 0.6,
'namespace_packages': 0.6,
'package_data': 0.6,
'package_dir': 0.6,
'platforms': 0.5,
'python_requires': 0.7,
'scripts': 0.5,
'setup_requires': 0.7,
'test_loader': 0.6,
'test_suite': 0.6,
'tests_require': 0.7,
'url': 0.5,
'use_2to3': 0.9,
'use_2to3_fixers': 1.0,
'zip_safe': 0.6,
}
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):
"""Setup() arguments hijacking."""
self.keywords = kw
# XXX: If evaluated script (setup.py) depends on local modules we
# have to add its root directory to the import search path.
# Note however, that this hack might break further imports
# for OUR Python instance (we're changing our own sys.path)!
sys.path.insert(0, os.getcwd())
try:
tree = ast.fix_missing_locations(tree)
eval(compile(tree, "<str>", mode='exec'), {
'__name__': "__main__",
'__file__': "setup.py",
'__f8r_setup': setup,
})
except Exception as e:
# XXX: Exception during setup.py evaluation might not necessary
# mean "fatal error". This exception might occur if e.g.
# we have hijacked local setup() function (due to matching
# heuristic for function arguments). Anyway, we shall not
# break flake8 execution due to out eval() usage.
LOG.exception("Couldn't evaluate setup.py: %s", e)
# Restore import search path.
sys.path.pop(0)
def get_requirements(
self, install=True, extras=True, setup=False, tests=False):
"""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))
if setup:
requires.extend(parse_requirements(
self.keywords.get('setup_requires', ()),
))
if tests:
requires.extend(parse_requirements(
self.keywords.get('tests_require', ()),
))
return requires
def visit_Call(self, node):
"""Call visitor - used for finding setup() call."""
self.generic_visit(node)
# Setup() is a keywords-only function.
if node.args:
return
keywords = set()
for k in node.keywords:
if k.arg is not None:
keywords.add(k.arg)
# Simple case for dictionary expansion for Python >= 3.5.
if k.arg is None and isinstance(k.value, ast.Dict):
keywords.update(x.s for x in k.value.keys)
# Simple case for dictionary expansion for Python <= 3.4.
if getattr(node, 'kwargs', ()) and isinstance(node.kwargs, ast.Dict):
keywords.update(x.s for x in node.kwargs.keys)
# The bare minimum number of arguments seems to be around five, which
# includes author, name, version, module/package and something extra.
if len(keywords) < 5:
return
score = sum(
self.attributes.get(x, 0)
for x in keywords
) / len(keywords)
if score < 0.5:
LOG.debug(
"Scoring for setup%r below 0.5: %.2f",
tuple(keywords),
score)
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__
# User defined project->modules mapping.
known_modules = {}
def __init__(self, tree, filename, lines=None):
"""Initialize requirements checker."""
self.tree = tree
self.filename = filename
self.lines = lines
self.requirements = self.get_requirements()
self.setup = self.get_setup()
@classmethod
def add_options(cls, manager):
"""Register plug-in specific options."""
manager.add_option(
"--known-modules",
action='store',
parse_from_config=True,
default="",
help=(
"User defined mapping between a project name and a list of"
" provided modules. For example: ``--known-modules=project:"
"[Project],extra-project:[extras,utilities]``."
),
)
@classmethod
def parse_options(cls, options):
"""Parse plug-in specific options."""
cls.known_modules = {
project2module(k): v.split(",")
for k, v in [
x.split(":[")
for x in re.split(r"],?", options.known_modules)[:-1]
]
}
@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):
"""Get package setup."""
try:
with open("setup.py") as f:
return SetupVisitor(ast.parse(f.read()))
except IOError as e:
LOG.warning("Couldn't open setup file: %s", e)
return SetupVisitor(ast.parse(""))
@property
def processing_setup_py(self):
"""Determine whether we are processing setup.py file."""
try:
return os.path.samefile(self.filename, "setup.py")
except OSError:
return False
def run(self):
"""Run checker."""
def split(module):
"""Split module into submodules."""
return tuple(module.split("."))
def modcmp(lib=(), test=()):
"""Compare import modules."""
if len(lib) > len(test):
return False
return all(a == b for a, b in zip(lib, test))
mods_1st_party = set()
mods_3rd_party = set()
# Get 1st party modules (used for absolute imports).
modules = [project2module(self.setup.keywords.get('name', ""))]
if modules[0] in self.known_modules:
modules = self.known_modules[modules[0]]
mods_1st_party.update(split(x) for x in modules)
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:
modules = [project2module(requirement.project_name)]
if modules[0] in KNOWN_3RD_PARTIES:
modules = KNOWN_3RD_PARTIES[modules[0]]
if modules[0] in self.known_modules:
modules = self.known_modules[modules[0]]
mods_3rd_party.update(split(x) for x in modules)
# When processing setup.py file, forcefully add setuptools to the
# project requirements. Setuptools might be required to build the
# project, even though it is not listed as a requirement - this
# package is required to run setup.py, so listing it as a setup
# requirement would be pointless.
if self.processing_setup_py:
mods_3rd_party.add(split("setuptools"))
for node in ImportVisitor(self.tree).imports:
_mod = split(node.mod)
_alt = split(node.alt)
if any([_mod[0] == x for x in STDLIB]):
continue
if any([modcmp(x, _mod) or modcmp(x, _alt)
for x in mods_1st_party]):
continue
if any([modcmp(x, _mod) or modcmp(x, _alt)
for x in mods_3rd_party]):
continue
yield (
node.line,
node.offset,
ERRORS['I900'].format(pkg=node.mod),
Flake8Checker,
)