| | 309 | |
|---|
| | 310 | def length(L): |
|---|
| | 311 | """Overall length of all strings in list. |
|---|
| | 312 | |
|---|
| | 313 | >>> length(['a', 'bc', 'd', '', 'efg']) |
|---|
| | 314 | 7 |
|---|
| | 315 | """ |
|---|
| | 316 | return sum(map(lambda x: len(x), L)) |
|---|
| | 317 | |
|---|
| | 318 | def generate_arguments(arguments, max_length): |
|---|
| | 319 | """Pass list of strings in chunks of size not greater than max_length. |
|---|
| | 320 | |
|---|
| | 321 | >>> for x in generate_arguments(['abc', 'def'], 4): |
|---|
| | 322 | ... print x |
|---|
| | 323 | ['abc'] |
|---|
| | 324 | ['def'] |
|---|
| | 325 | |
|---|
| | 326 | >>> for x in generate_arguments(['a', 'bc', 'd', 'e', 'f'], 2): |
|---|
| | 327 | ... print x |
|---|
| | 328 | ['a'] |
|---|
| | 329 | ['bc'] |
|---|
| | 330 | ['d', 'e'] |
|---|
| | 331 | ['f'] |
|---|
| | 332 | |
|---|
| | 333 | If a single argument is larger than max_length, ValueError is raised. |
|---|
| | 334 | >>> L = [] |
|---|
| | 335 | >>> for x in generate_arguments(['abc', 'de', 'fghijk', 'l'], 4): |
|---|
| | 336 | ... L.append(x) |
|---|
| | 337 | Traceback (most recent call last): |
|---|
| | 338 | ... |
|---|
| | 339 | ValueError: Argument 'fghijk' larger than 4. |
|---|
| | 340 | >>> L |
|---|
| | 341 | [['abc'], ['de']] |
|---|
| | 342 | """ |
|---|
| | 343 | L = [] |
|---|
| | 344 | i = 0 |
|---|
| | 345 | |
|---|
| | 346 | # We have to look ahead, so C-style loop here. |
|---|
| | 347 | while arguments: |
|---|
| | 348 | if L == [] and len(arguments[i]) > max_length: |
|---|
| | 349 | raise ValueError("Argument '%s' larger than %d." % (arguments[i], max_length)) |
|---|
| | 350 | |
|---|
| | 351 | L.append(arguments[i]) |
|---|
| | 352 | |
|---|
| | 353 | # End of arguments: yield then terminate. |
|---|
| | 354 | if i == len(arguments) - 1: |
|---|
| | 355 | yield L |
|---|
| | 356 | break |
|---|
| | 357 | |
|---|
| | 358 | # Adding next argument would exceed max_length, so yield now. |
|---|
| | 359 | if length(L) + len(arguments[i+1]) > max_length: |
|---|
| | 360 | yield L |
|---|
| | 361 | L = [] |
|---|
| | 362 | |
|---|
| | 363 | i += 1 |
|---|
| 975 | | self.cheesecake.log.debug("Running pylint on files: %s." % ' '.join(files_to_lint)) |
|---|
| 976 | | |
|---|
| 977 | | rc, output = run_cmd("pylint %s %s" % (' '.join(files_to_lint), self.pylint_args)) |
|---|
| 978 | | if rc: |
|---|
| 979 | | self.cheesecake.log.debug("encountered an error (%d):\n***\n%s\n***\n" % (rc, output)) |
|---|
| | 1033 | pylint_score = 0 |
|---|
| | 1034 | count = 0 |
|---|
| | 1035 | error_count = 0 |
|---|
| | 1036 | |
|---|
| | 1037 | for filenames in generate_arguments(files_to_lint, max_arguments_length - len(self.pylint_args)): |
|---|
| | 1038 | filenames = ' '.join(filenames) |
|---|
| | 1039 | self.cheesecake.log.debug("Running pylint on files: %s." % filenames) |
|---|
| | 1040 | |
|---|
| | 1041 | rc, output = run_cmd("pylint %s %s" % (filenames, self.pylint_args)) |
|---|
| | 1042 | if rc: |
|---|
| | 1043 | self.cheesecake.log.debug("encountered an error (%d):\n***\n%s\n***\n" % (rc, output)) |
|---|
| | 1044 | error_count += 1 |
|---|
| | 1045 | else: |
|---|
| | 1046 | # Extract score from pylint output. |
|---|
| | 1047 | score_line = output.split("\n")[-3] |
|---|
| | 1048 | s = re.search(r" (-?\d+\.\d+)/10", score_line) |
|---|
| | 1049 | if s: |
|---|
| | 1050 | pylint_score += float(s.group(1)) |
|---|
| | 1051 | count += 1 |
|---|
| 984 | | # Extract score from pylint output. |
|---|
| 985 | | score_line = output.split("\n")[-3] |
|---|
| 986 | | s = re.search(r" (\d+\.\d+)/10", score_line) |
|---|
| 987 | | |
|---|
| 988 | | # Scores below 0 are treated as 0. |
|---|
| 989 | | pylint_score = 0 |
|---|
| 990 | | if s: |
|---|
| 991 | | pylint_score = float(s.group(1)) |
|---|
| 992 | | |
|---|
| | 1056 | if count: |
|---|
| | 1057 | pylint_score = float(pylint_score)/float(count) |
|---|
| | 1058 | self.details = "pylint score was %.2f out of 10" % pylint_score |
|---|
| | 1059 | elif error_count: |
|---|
| | 1060 | self.details = "encountered an error during pylint execution" |
|---|
| | 1061 | else: |
|---|
| | 1062 | self.details = "no files to check found" |
|---|
| | 1063 | |
|---|
| | 1064 | # Assume scores below zero as zero for means of index value computation. |
|---|
| | 1065 | if pylint_score < 0: |
|---|
| | 1066 | pylint_score = 0 |
|---|