178
179
Okay: from os import path
180
Okay: from os import path as p
181
Okay: from os import (path as p)
179
182
Okay: import os.path
180
183
Okay: from nova.compute import rpcapi
181
184
N302: from os.path import dirname as dirname2
185
N302: from os.path import (dirname as dirname2)
182
186
N303: from os.path import *
183
187
N304: from .compute import rpcapi
186
190
# pass the doctest, since the relativity depends on the file's locality
188
192
def is_module_for_sure(mod, search_path=sys.path):
193
mod = mod.replace('(', '') # Ignore parentheses
191
196
while '.' in mod_name:
201
206
# NOTE(vish): the import error might be due
202
207
# to a missing dependency
203
208
missing = str(exc).split()[-1]
204
if missing != mod.split('.')[-1]:
209
if (missing != mod.split('.')[-1] or
210
"cannot import" in str(exc)):
205
211
_missingImport.add(missing)
214
except Exception, exc:
215
# NOTE(jogo) don't stack trace if unexpected import error,
217
traceback.print_exc()
210
221
def is_module(mod):
302
313
yield (0, "N307: nova.db import not allowed in nova/virt/*")
305
def in_docstring_position(previous_logical):
306
return (previous_logical.startswith("def ") or
307
previous_logical.startswith("class "))
316
def is_docstring(physical_line, previous_logical):
317
"""Return True if found docstring
318
'A docstring is a string literal that occurs as the first statement in a
319
module, function, class,'
320
http://www.python.org/dev/peps/pep-0257/#what-is-a-docstring
322
line = physical_line.lstrip()
323
start = max([line.find(i) for i in START_DOCSTRING_TRIPLE])
324
end = max([line[-4:-1] == i for i in END_DOCSTRING_TRIPLE])
325
if (previous_logical.startswith("def ") or
326
previous_logical.startswith("class ")):
330
# Handle multi line comments
331
return end and start in (-1, len(line) - 4)
310
334
def nova_docstring_start_space(physical_line, previous_logical):
314
338
Docstring should not start with space
316
340
Okay: def foo():\n '''This is good.'''
341
Okay: def foo():\n a = ''' This is not a docstring.'''
342
Okay: def foo():\n pass\n ''' This is not.'''
317
343
N401: def foo():\n ''' This is not.'''
319
345
# short circuit so that we don't fail on our own fail test
324
350
# it's important that we determine this is actually a docstring,
325
351
# and not a doc block used somewhere after the first line of a
327
if in_docstring_position(previous_logical):
353
if is_docstring(physical_line, previous_logical):
328
354
pos = max([physical_line.find(i) for i in START_DOCSTRING_TRIPLE])
329
if pos != -1 and len(physical_line) > pos + 4:
330
if physical_line[pos + 3] == ' ':
331
return (pos, "N401: docstring should not start with"
335
def nova_docstring_one_line(physical_line):
355
if physical_line[pos + 3] == ' ':
356
return (pos, "N401: docstring should not start with"
360
def nova_docstring_one_line(physical_line, previous_logical):
336
361
r"""Check one line docstring end.
338
363
nova HACKING guide recommendation for one line docstring:
339
364
A one line docstring looks like this and ends in punctuation.
341
Okay: '''This is good.'''
342
Okay: '''This is good too!'''
343
Okay: '''How about this?'''
344
N402: '''This is not'''
345
N402: '''Bad punctuation,'''
366
Okay: def foo():\n '''This is good.'''
367
Okay: def foo():\n '''This is good too!'''
368
Okay: def foo():\n '''How about this?'''
369
Okay: def foo():\n a = '''This is not a docstring'''
370
Okay: def foo():\n pass\n '''This is not a docstring'''
371
Okay: class Foo:\n pass\n '''This is not a docstring'''
372
N402: def foo():\n '''This is not'''
373
N402: def foo():\n '''Bad punctuation,'''
374
N402: class Foo:\n '''Bad punctuation,'''
347
376
#TODO(jogo) make this apply to multi line docstrings as well
348
377
line = physical_line.lstrip()
350
if line.startswith('"') or line.startswith("'"):
378
if is_docstring(physical_line, previous_logical):
351
379
pos = max([line.find(i) for i in START_DOCSTRING_TRIPLE]) # start
352
380
end = max([line[-4:-1] == i for i in END_DOCSTRING_TRIPLE]) # end
356
384
return pos, "N402: one line docstring needs punctuation."
359
def nova_docstring_multiline_end(physical_line, previous_logical):
387
def nova_docstring_multiline_end(physical_line, previous_logical, tokens):
360
388
r"""Check multi line docstring end.
362
390
nova HACKING guide recommendation for docstring:
363
391
Docstring should end on a new line
365
393
Okay: '''foobar\nfoo\nbar\n'''
366
N403: def foo():\n'''foobar\nfoo\nbar\n d'''\n\n
394
Okay: def foo():\n '''foobar\nfoo\nbar\n'''
395
Okay: class Foo:\n '''foobar\nfoo\nbar\n'''
396
Okay: def foo():\n a = '''not\na\ndocstring'''
397
Okay: def foo():\n pass\n'''foobar\nfoo\nbar\n d'''
398
N403: def foo():\n '''foobar\nfoo\nbar\ndocstring'''
399
N403: class Foo:\n '''foobar\nfoo\nbar\ndocstring'''\n\n
368
if in_docstring_position(previous_logical):
401
# if find OP tokens, not a docstring
402
ops = [t for t, _, _, _, _ in tokens if t == tokenize.OP]
403
if (is_docstring(physical_line, previous_logical) and len(tokens) > 0 and
369
405
pos = max(physical_line.find(i) for i in END_DOCSTRING_TRIPLE)
370
if pos != -1 and len(physical_line) == pos + 4:
371
if physical_line.strip() not in START_DOCSTRING_TRIPLE:
372
return (pos, "N403: multi line docstring end on new line")
406
if physical_line.strip() not in START_DOCSTRING_TRIPLE:
407
return (pos, "N403: multi line docstring end on new line")
375
410
def nova_docstring_multiline_start(physical_line, previous_logical, tokens):
379
414
Docstring should start with A multi line docstring has a one-line summary
381
416
Okay: '''foobar\nfoo\nbar\n'''
382
N404: def foo():\n'''\nfoo\nbar\n''' \n\n
417
Okay: def foo():\n a = '''\nnot\na docstring\n'''
418
N404: def foo():\n'''\nfoo\nbar\n'''\n\n
384
if in_docstring_position(previous_logical):
420
if is_docstring(physical_line, previous_logical):
385
421
pos = max([physical_line.find(i) for i in START_DOCSTRING_TRIPLE])
386
422
# start of docstring when len(tokens)==0
387
423
if len(tokens) == 0 and pos != -1 and len(physical_line) == pos + 4:
393
429
def nova_no_cr(physical_line):
394
r"""Check that we only use newlines not cariage returns.
430
r"""Check that we only use newlines not carriage returns.
396
432
Okay: import os\nimport sys
397
433
# pep8 doesn't yet replace \r in strings, will work on an
560
596
nova HACKING recommends not referencing a bug or blueprint in first line,
561
597
it should provide an accurate description of the change
563
N802 Title limited to 50 chars
599
N802 Title limited to 72 chars
565
601
#Get title of most recent commit
585
621
"description of the change, not just a reference to a bug "
586
622
"or blueprint" % title.strip())
624
# HACKING.rst recommends commit titles 50 chars or less, but enforces
625
# a 72 character limit
588
626
if len(title.decode('utf-8')) > 72:
589
627
print ("N802: git commit title ('%s') should be under 50 chars"