| 1 | # -*- coding: utf-8 -*- |
|---|
| 2 | # |
|---|
| 3 | # Copyright (C) 2010 by Atzm WATANABE <atzm@atzm.org> |
|---|
| 4 | # |
|---|
| 5 | # This program is free software; you can redistribute it and/or modify it |
|---|
| 6 | # under the terms of the GNU General Public License (version 2) as |
|---|
| 7 | # published by the Free Software Foundation. It is distributed in the |
|---|
| 8 | # hope that it will be useful, but WITHOUT ANY WARRANTY; without even the |
|---|
| 9 | # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
|---|
| 10 | # PURPOSE. See the GNU General Public License for more details. |
|---|
| 11 | # |
|---|
| 12 | # $Id$ |
|---|
| 13 | # |
|---|
| 14 | |
|---|
| 15 | import os |
|---|
| 16 | import sys |
|---|
| 17 | import zlib |
|---|
| 18 | import token |
|---|
| 19 | import parser |
|---|
| 20 | |
|---|
| 21 | import cc |
|---|
| 22 | import pep8 |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | def _compseq(seq, level): |
|---|
| 26 | c = zlib.compressobj(level) |
|---|
| 27 | return ''.join([c.compress(chunk) for chunk in seq] + [c.flush()]) |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | def _maxstrlen(seq, iferr=0): |
|---|
| 31 | try: |
|---|
| 32 | return max(len(l) for l in seq) |
|---|
| 33 | except: |
|---|
| 34 | return iferr |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | class Scorer: |
|---|
| 38 | def __init__(self, lines, compress_level=9): |
|---|
| 39 | self._code = ''.join(lines) |
|---|
| 40 | self._ccode = _compseq(lines, compress_level) |
|---|
| 41 | self._lines = lines |
|---|
| 42 | self._nlines = [l for l in lines |
|---|
| 43 | if l.strip() and not l.strip().startswith('#')] |
|---|
| 44 | |
|---|
| 45 | self._astpl = parser.suite(self.code()).totuple() |
|---|
| 46 | self._token_cache = {} |
|---|
| 47 | |
|---|
| 48 | self.complexity() |
|---|
| 49 | self.style_errors() |
|---|
| 50 | |
|---|
| 51 | def tupletree(self): |
|---|
| 52 | return self._astpl |
|---|
| 53 | |
|---|
| 54 | def code(self): |
|---|
| 55 | return self._code |
|---|
| 56 | |
|---|
| 57 | def lines(self): |
|---|
| 58 | return self._lines |
|---|
| 59 | |
|---|
| 60 | def uncommented_lines(self): |
|---|
| 61 | return self._nlines |
|---|
| 62 | |
|---|
| 63 | def bytes(self): |
|---|
| 64 | return len(self.code()) or 1 |
|---|
| 65 | |
|---|
| 66 | def uncommented_bytes(self): |
|---|
| 67 | return sum(len(l) for l in self.uncommented_lines()) or 1 |
|---|
| 68 | |
|---|
| 69 | def rows(self): |
|---|
| 70 | return len(self.lines()) or 1 |
|---|
| 71 | |
|---|
| 72 | def uncommented_rows(self): |
|---|
| 73 | return len(self.uncommented_lines()) or 1 |
|---|
| 74 | |
|---|
| 75 | def compressibility(self): |
|---|
| 76 | return float(len(self._ccode)) / self.bytes() |
|---|
| 77 | |
|---|
| 78 | def token_bytes(self, types): |
|---|
| 79 | def _bytes(stp, bytes=0): |
|---|
| 80 | if not stp: |
|---|
| 81 | return bytes |
|---|
| 82 | if stp[0] in types: |
|---|
| 83 | bytes += len(stp[1]) |
|---|
| 84 | else: |
|---|
| 85 | for p in stp[1:]: |
|---|
| 86 | bytes = _bytes(p, bytes) |
|---|
| 87 | return bytes |
|---|
| 88 | |
|---|
| 89 | types = tuple(types) |
|---|
| 90 | |
|---|
| 91 | try: |
|---|
| 92 | ret = self._token_cache[types] |
|---|
| 93 | except KeyError: |
|---|
| 94 | ret = self._token_cache[types] = _bytes(self.tupletree()) |
|---|
| 95 | |
|---|
| 96 | return ret |
|---|
| 97 | |
|---|
| 98 | def complexity(self, item='all', iferr=(256, 1)): |
|---|
| 99 | try: |
|---|
| 100 | stat = cc.measure_complexity(self._code, item) |
|---|
| 101 | ccpp = cc.PrettyPrinter(None, True) |
|---|
| 102 | cplx = zip(*ccpp.flatten_stats(stat))[-1] |
|---|
| 103 | self._ccsum = sum(cplx) or 1 |
|---|
| 104 | self._cclen = len(cplx) or 1 |
|---|
| 105 | except: |
|---|
| 106 | self._ccsum, self._cclen = iferr |
|---|
| 107 | return self._ccsum, self._cclen |
|---|
| 108 | |
|---|
| 109 | def style_errors(self, name='', iferr=256): |
|---|
| 110 | pep8.process_options([name]) |
|---|
| 111 | p8ck = pep8.Checker(None) |
|---|
| 112 | p8ck.lines = self._lines |
|---|
| 113 | p8ck.filename = name |
|---|
| 114 | |
|---|
| 115 | try: |
|---|
| 116 | tmpfp = sys.stdout |
|---|
| 117 | sys.stdout = open(os.devnull, 'w') |
|---|
| 118 | self._estyle = p8ck.check_all() or 1 |
|---|
| 119 | except: |
|---|
| 120 | self._estyle = iferr |
|---|
| 121 | finally: |
|---|
| 122 | sys.stdout = tmpfp |
|---|
| 123 | return self._estyle |
|---|
| 124 | |
|---|
| 125 | def complex_score(self, coefficient=48): |
|---|
| 126 | maxlen = _maxstrlen(self.lines(), 256) |
|---|
| 127 | score = self._cclen * coefficient |
|---|
| 128 | score /= (maxlen / (coefficient * 2)) or 1 |
|---|
| 129 | score /= (self._ccsum / ((coefficient / 3) or 1)) or 1 |
|---|
| 130 | score *= self.compressibility() |
|---|
| 131 | return int(score) or 1 |
|---|
| 132 | |
|---|
| 133 | def style_score(self, coefficient=64): |
|---|
| 134 | data_bytes = self.token_bytes([token.NUMBER, token.STRING, |
|---|
| 135 | token.NAME, token.COLON]) |
|---|
| 136 | score = self.uncommented_bytes() - data_bytes |
|---|
| 137 | score /= (self.uncommented_rows() / coefficient) or 1 |
|---|
| 138 | score /= self._estyle |
|---|
| 139 | score *= (coefficient / 12) or 1 |
|---|
| 140 | score *= self.compressibility() |
|---|
| 141 | return int(score) or 1 |
|---|
| 142 | |
|---|
| 143 | def lines_score(self, coefficient=2.5): |
|---|
| 144 | maxlen = _maxstrlen(self.lines(), 256) |
|---|
| 145 | data_bytes = self.token_bytes([token.NUMBER, token.STRING, |
|---|
| 146 | token.NAME, token.COLON]) |
|---|
| 147 | score = self.uncommented_bytes() - data_bytes |
|---|
| 148 | score /= (maxlen / self.uncommented_rows()) or 1 |
|---|
| 149 | score /= self.uncommented_rows() |
|---|
| 150 | score *= coefficient |
|---|
| 151 | score *= self.compressibility() |
|---|
| 152 | return int(score) or 1 |
|---|
| 153 | |
|---|
| 154 | def const_score(self, coefficient=2): |
|---|
| 155 | const_bytes = self.token_bytes([token.NUMBER, token.STRING]) or 1 |
|---|
| 156 | score = self.uncommented_bytes() / const_bytes |
|---|
| 157 | score *= coefficient |
|---|
| 158 | score *= self.compressibility() |
|---|
| 159 | return int(score) or 1 |
|---|
| 160 | |
|---|
| 161 | def colon_score(self, coefficient=1.5): |
|---|
| 162 | colon_bytes = self.token_bytes([token.COLON]) or 1 |
|---|
| 163 | score = self.uncommented_bytes() / colon_bytes |
|---|
| 164 | score *= coefficient |
|---|
| 165 | score *= self.compressibility() |
|---|
| 166 | return int(score) or 1 |
|---|
| 167 | |
|---|
| 168 | def name_score(self, coefficient=48): |
|---|
| 169 | name_bytes = self.token_bytes([token.NAME]) or 1 |
|---|
| 170 | score = self.uncommented_bytes() |
|---|
| 171 | score /= (name_bytes / coefficient) or 1 |
|---|
| 172 | score *= self.compressibility() |
|---|
| 173 | return int(score) or 1 |
|---|