109 if endPos >= 0: |
109 if endPos >= 0: |
110 comment = comment[:endPos] |
110 comment = comment[:endPos] |
111 flags = [f.strip() for f in comment.split() |
111 flags = [f.strip() for f in comment.split() |
112 if (f.startswith("__") and f.endswith("__"))] |
112 if (f.startswith("__") and f.endswith("__"))] |
113 flags += [f.strip().lower() for f in comment.split() |
113 flags += [f.strip().lower() for f in comment.split() |
114 if f in ("noqa", "NOQA")] |
114 if f in ("noqa", "NOQA", |
|
115 "nosec", "NOSEC", |
|
116 "secok", "SECOK")] |
115 return flags |
117 return flags |
116 |
118 |
117 |
119 |
118 def ignoreCode(code, lineFlags): |
120 def ignoreCode(code, lineFlags): |
119 """ |
121 """ |
128 """ |
130 """ |
129 if lineFlags: |
131 if lineFlags: |
130 |
132 |
131 if ( |
133 if ( |
132 "__IGNORE_WARNING__" in lineFlags or |
134 "__IGNORE_WARNING__" in lineFlags or |
133 "noqa" in lineFlags |
135 "noqa" in lineFlags or |
|
136 "nosec" in lineFlags |
134 ): |
137 ): |
135 # ignore all warning codes |
138 # ignore all warning codes |
136 return True |
139 return True |
137 |
140 |
138 for flag in lineFlags: |
141 for flag in lineFlags: |
139 # check individual warning code |
142 # check individual warning code |
140 if flag.startswith("__IGNORE_WARNING_"): |
143 if flag.startswith("__IGNORE_WARNING_"): |
141 ignoredCode = flag[2:-2].rsplit("_", 1)[-1] |
144 ignoredCode = flag[2:-2].rsplit("_", 1)[-1] |
142 if code.startswith(ignoredCode): |
145 if code.startswith(ignoredCode): |
143 return True |
146 return True |
|
147 |
|
148 return False |
|
149 |
|
150 |
|
151 def securityOk(code, lineFlags): |
|
152 """ |
|
153 Function to check, if the given code is an acknowledged security report. |
|
154 |
|
155 @param code error code to be checked |
|
156 @type str |
|
157 @param lineFlags list of line flags to check against |
|
158 @type list of str |
|
159 @return flag indicating an acknowledged security report |
|
160 @rtype bool |
|
161 """ |
|
162 if lineFlags: |
|
163 return "secok" in lineFlags |
144 |
164 |
145 return False |
165 return False |
146 |
166 |
147 |
167 |
148 def codeStyleCheck(filename, source, args): |
168 def codeStyleCheck(filename, source, args): |
393 deferredFixes = {} |
413 deferredFixes = {} |
394 results = [] |
414 results = [] |
395 for lineno, errorsList in errorsDict.items(): |
415 for lineno, errorsList in errorsDict.items(): |
396 errorsList.sort(key=lambda x: x[0], reverse=True) |
416 errorsList.sort(key=lambda x: x[0], reverse=True) |
397 for _, error in errorsList: |
417 for _, error in errorsList: |
|
418 error.update({ |
|
419 "ignored": False, |
|
420 "fixed": False, |
|
421 "autofixing": False, |
|
422 "fixcode": "", |
|
423 "fixargs": [], |
|
424 "securityOk": False, |
|
425 }) |
|
426 |
398 if source: |
427 if source: |
399 code = error["code"] |
428 code = error["code"] |
400 lineFlags = extractLineFlags(source[lineno - 1].strip()) |
429 lineFlags = extractLineFlags(source[lineno - 1].strip()) |
401 try: |
430 try: |
402 lineFlags += extractLineFlags(source[lineno].strip(), |
431 lineFlags += extractLineFlags(source[lineno].strip(), |
403 flagsLine=True) |
432 flagsLine=True) |
404 except IndexError: |
433 except IndexError: |
405 pass |
434 pass |
406 if not ignoreCode(code, lineFlags): |
435 |
|
436 if securityOk(code, lineFlags): |
|
437 error["securityOk"] = True |
|
438 |
|
439 if ignoreCode(code, lineFlags): |
|
440 error["ignored"] = True |
|
441 else: |
407 if fixer: |
442 if fixer: |
408 pass |
|
409 res, fixcode, fixargs, id_ = fixer.fixIssue( |
443 res, fixcode, fixargs, id_ = fixer.fixIssue( |
410 lineno, error["offset"], code) |
444 lineno, error["offset"], code) |
411 if res == -1: |
445 if res == -1: |
412 deferredFixes[id_] = error |
446 deferredFixes[id_] = error |
413 else: |
447 else: |
414 error.update({ |
448 error.update({ |
415 "ignored": False, |
|
416 "fixed": res == 1, |
449 "fixed": res == 1, |
417 "autofixing": True, |
450 "autofixing": True, |
418 "fixcode": fixcode, |
451 "fixcode": fixcode, |
419 "fixargs": fixargs, |
452 "fixargs": fixargs, |
420 }) |
453 }) |
421 else: |
|
422 error.update({ |
|
423 "ignored": False, |
|
424 "fixed": False, |
|
425 "autofixing": False, |
|
426 "fixcode": "", |
|
427 "fixargs": [], |
|
428 }) |
|
429 else: |
|
430 error.update({ |
|
431 "ignored": True, |
|
432 "fixed": False, |
|
433 "autofixing": False, |
|
434 "fixcode": "", |
|
435 "fixargs": [], |
|
436 }) |
|
437 else: |
|
438 error.update({ |
|
439 "ignored": False, |
|
440 "fixed": False, |
|
441 "autofixing": False, |
|
442 "fixcode": "", |
|
443 "fixargs": [], |
|
444 }) |
|
445 |
454 |
446 results.append(error) |
455 results.append(error) |
447 |
456 |
448 if fixer: |
457 if fixer: |
449 deferredResults = fixer.finalize() |
458 deferredResults = fixer.finalize() |