summaryrefslogtreecommitdiff
path: root/code.py
blob: 6d90c460e1ef3f448c4e6b54101996846c747a2f (plain)
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
# -*- coding: utf-8 -*-

import os
from metrics.metrics import process


class CodeMetrics:
    """Handle source code metrics."""


    def __init__(self, rootdir):
        """Init the code metrics."""

        self._rootdir = rootdir

        self._languages = {}


    def __str__(self):
        """Display the collected metrics."""

        desc = ''

        for l in self._languages:

            desc += l + ': ' + '%d' % self._languages[l]['sloc'] + '\n'

        return desc


    def process(self, msg):
        """Collect code metrics."""

        # Get all files

        all_files = []

        baselen = len(self._rootdir) + len(os.sep)

        for root, dirnames, filenames in os.walk(self._rootdir):

            if '.git' in root:
                continue

            for filename in filenames:

                full_access = os.path.join(root, filename)
                rel_access = full_access[baselen:]

                all_files.append(rel_access)

        # Compute metrics

        count = len(all_files)

        i = 0

        for filename in all_files:

            print(msg + ' -> Collecting code metrics... %d%%' % ((i * 100) / count), end='')

            unsupported = {
                '.l' : 'Flex',
                '.y' : 'Bison'
            }

            supported = True

            lang = None

            for ext, lang in unsupported.items():

                if filename.endswith(ext):

                    supported = False
                    break

            if supported:

                stats = self._analyse_supported_file(filename)

                if stats is None:
                    i += 1
                    continue

                lang = stats['language']

            else:

                stats = self._analyse_unsupported_file(os.path.join(self._rootdir, filename))

            if not(lang in self._languages):

                self._languages[lang] = {}

                self._languages[lang]['sloc'] = 0
                self._languages[lang]['comments'] = 0

            self._languages[lang]['sloc'] += stats['sloc']
            self._languages[lang]['comments'] += stats['comments']

            i += 1

        msg = msg + ' -> Collecting code metrics... %d%%' % ((i * 100) / count)

        print(msg, end='')

        return msg


    def _analyse_supported_file(self, filename):
        """Process a file supported by the Python module."""

        context = {}

        context['include_metrics'] = [ ('sloc', 'SLOCMetric') ]

        context['quiet'] = True
        context['verbose'] = False
        context['root_dir'] = self._rootdir
        context['in_file_names'] = [ filename ]
        context['output_format'] = None

        stats = process(context)

        if filename in stats:

            stats = stats[filename]

            if stats['language'] == 'TASM':
                stats['language'] = 'Asm'

            if stats['language'] == 'Gettext Catalog':
                stats = None

        else:
            stats = None

        return stats


    def _analyse_unsupported_file(self, filename):
        """Process a file unsupported by the Python module."""

        stats = {}

        stats['sloc'] = 0
        stats['comments'] = 0

        with open(filename, 'r') as fin:

            for line in fin:

                length = len(line)

                if not(length == 0 or (length == 1 and line == '\n') or (length == 2 and line == '\r\n')):
                    stats['sloc'] += 1

        return stats


    def count_all_lines(self):
        """Count all single lines of code."""

        result = 0

        for l in self._languages:

            result += self._languages[l]['sloc']

        return result


    def get_most_used(self, count):
        """Compute the list of most used languages."""

        max_count = len(self._languages.keys())

        if count > max_count:
            count = max_count

        languages = []

        for l in self._languages:

            languages.append((self._languages[l]['sloc'], l))

        languages = sorted(languages, reverse=True)

        if count < max_count:

            selected = languages[:count - 1]

            remaining = 0

            for n, _ in languages[count:]:

                remaining += n

            selected.append((remaining, 'Others'))

        else:

            selected = languages

        return selected