fused_iutest_files   A
last analyzed

Complexity

Total Complexity 40

Size/Duplication

Total Lines 257
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 179
dl 0
loc 257
rs 9.2
c 0
b 0
f 0
wmc 40

7 Methods

Rating   Name   Duplication   Size   Complexity  
A IutestFused.RestoreStrings() 0 6 2
C IutestFused.Minimze() 0 78 9
A IutestFused.IsUnnecessaryIncludeGuard() 0 9 4
A IutestFused.StoreMinimze() 0 14 1
A IutestFused.StoreStrings() 0 11 2
A IutestFused.Flush() 0 5 2
F IutestFused.Translate() 0 61 14

3 Functions

Rating   Name   Duplication   Size   Complexity  
A FusedSrc() 0 3 1
A FusedAll() 0 5 2
A main() 0 9 3

How to fix   Complexity   

Complexity

Complex classes like fused_iutest_files often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
#
4
# fuse_iutest_files.py
5
#
6
# Copyright (C) 2014-2016, Takazumi Shirayanagi
7
# This software is released under the new BSD License,
8
# see LICENSE
9
#
10
11
""" fuse_iutest_files.py
12
fuse_iutest_files.py [IUTEST_ROOT_DIR] OUTPUT_DIR
13
"""
14
15
import os
16
import sys
17
import re
18
import codecs
19
20
IUTEST_INCLUDE_DIR = os.path.join(os.path.dirname(__file__), '../../include')
21
IUTEST_APPROVAL_INCLUDE_GUARD = {
22
    'INCG_IRIS_IUTEST_HPP_',
23
    'INCG_IRIS_IUTEST_SWITCH_HPP_',
24
    'INCG_IRIS_IUTEST_SPI_HPP_'
25
}
26
27
28
class IutestFused:
29
    INCLUDE_REGEX = re.compile(r'^\s*#\s*include\s*"(.*iutest.*)"')
30
    IFDEF_REGEX = re.compile(r'^\s*#\s*if(def\s*\S*|\s*defined\s*\(.*\))')
31
    PREPRO_REGEX = re.compile(r'^\s*#')
32
    COMMENT_REGEX = re.compile(r'^\s*//.*')
33
    C_COMMENT_BEGIN_REGEX = re.compile(r'^\s*(.*)/\*.*')
34
    C_COMMENT_END_REGEX = re.compile(r'^\s*\*/(.*)')
35
    STRING_REGEX = re.compile(r'(".*?")')
36
    INCG_REGEX = re.compile(r'^\s*#\s*(ifndef|define|endif)[/\s]*(INCG_\S*)\s*\Z')
37
    c_comment = False
38
    store_line = ""
39
40
    def IsUnnecessaryIncludeGuard(self, line):
41
        m = self.INCG_REGEX.match(line)
42
        if m:
43
            incg = m.group(2)
44
            if m.group(1) == 'define':
45
                return False
46
            if incg not in IUTEST_APPROVAL_INCLUDE_GUARD:
47
                return True
48
        return False
49
50
    def StoreStrings(self, src, list):
51
        tmp = src.replace(r'\"', '$$')
52
        prev = 0
53
        dst = ""
54
        for m in self.STRING_REGEX.finditer(tmp):
55
            dst += tmp[prev:m.start()]
56
            dst += '"@[email protected]"'
57
            prev = m.end()
58
            list.append(m.group(0))
59
        dst += tmp[prev:]
60
        return dst
61
62
    def RestoreStrings(self, src, list):
63
        dst = src
64
        for s in list:
65
            dst = dst.replace('"@[email protected]"', s, 1)
66
        dst = dst.replace('$$', r'\"')
67
        return dst
68
69
    def StoreMinimze(self, line):
70
        store = self.store_line + line
71
        # string store
72
        str_l = []
73
        store = self.StoreStrings(store, str_l)
74
75
        store = re.sub('"@[email protected]""@[email protected]"', '"@[email protected]"##+##"@[email protected]"', store)
76
        store = re.sub('\):\s+', '):', store)
77
78
        # string restore
79
        store = self.RestoreStrings(store, str_l)
80
81
        store = store.replace('"##+##"', '')
82
        self.store_line = store
83
84
    def Minimze(self, line):
85
        # if defined -> ifdef
86
        line = re.sub(r'\s*#\s*if\s*defined\((\S*)\)\s*\Z', r'#ifdef \1', line)
87
        # if !defined -> ifndef
88
        line = re.sub(r'\s*#\s*if\s*!defined\((\S*)\)\s*\Z', r'#ifndef \1', line)
89
90
        # C comment
91
        if self.c_comment:
92
            m = self.C_COMMENT_END_REGEX.match(line)
93
            if m:
94
                self.c_comment = False
95
                line = m.group(1)
96
            else:
97
                return ""
98
        m = self.C_COMMENT_BEGIN_REGEX.match(line)
99
        if m:
100
            self.c_comment = True
101
            line = m.group(1)
102
        line = line.strip()
103
        if len(line) == 0:
104
            return ""
105
106
        # C++ comment
107
        if self.COMMENT_REGEX.match(line):
108
            return ""
109
        # include guard
110
        if self.IsUnnecessaryIncludeGuard(line):
111
            return ""
112
113
        # string store
114
        str_l = []
115
        line = self.StoreStrings(line, str_l)
116
117
        # remove comment and strip
118
        line = re.sub('//[\S \t]*', '', line)
119
        line = line.strip(' \t')
120
        # remvoe \r
121
        line = line.rstrip()
122
        line += '\n'
123
        # remove preprocessor directive unnecessary whitespace
124
        line = re.sub('^\s*#\s*', '#', line)
125
        line = re.sub('^\s*#(.+?)[ \t]+', r'#\1 ', line)
126
127
        # remove unnecessary whitespace
128
        line = re.sub('\s+(".*?")', r' \1', line)
129
        line = re.sub(r'\)\s+>', r')>', line)
130
        line = re.sub(';[ \t]+', ';', line)
131
        line = re.sub('[ \t]+', ' ', line)
132
        line = re.sub('([\w)\]]+)\s+([&|\+\-<>=\?]+)[ \t]+([^>])', r'\1\2\3', line)
133
        line = re.sub('\s*([{\+\-\*/%=<>&|!]+=)[ \t]*', r'\1', line)
134
        line = re.sub('<\s+(\w)', r'<\1', line)
135
        line = re.sub('\s+:[ \t]+(\w)', r':\1', line)
136
        line = re.sub('\s*,[ \t]*', ',', line)
137
        line = re.sub('\s*\)', ')', line)
138
        line = re.sub('\)\s+{', '){', line)
139
        line = re.sub('\)\s+const', ')const', line)
140
        if not re.match('#define\s+.*\s+{.*', line):
141
            line = re.sub('\s*{\s*', '{', line)
142
            line = re.sub('\s*}\s*', '}', line)
143
144
        # define HOGE(x) vs define HOGE (x)
145
        m = re.match('^#define\s+(\w+)\s+(\([^)]*?\))$', line)
146
        line = re.sub('\s*\([ \t]*', '(', line)
147
        if m:
148
            line = re.sub('^#define\s+(\w+)(\([^)]*?\))$', r'#define \1 \2', line)
149
        else:
150
            line = re.sub('^#define\s+(\w+\([^)]*?\))(\S+)', r'#define \1 \2', line)
151
152
        # 0x00000X => 0xX
153
        line = re.sub('0x0+([0-9A-Fa-f])', r'0x\1', line)
154
        # 0x0 => 0
155
        line = re.sub('0x([0-9])([^0-9A-Fa-f])', r'\1\2', line)
156
157
        # string restore
158
        line = self.RestoreStrings(line, str_l)
159
160
        line = re.sub('^#define\s+(\w+)=', r'#define \1 =', line)
161
        return line
162
163
    def Flush(self, output_file):
164
        if len(self.store_line) > 0:
165
            output_file.write(self.store_line.strip())
166
            output_file.write('\n')
167
            self.store_line = ""
168
169
    def Translate(self, root, filename, output, output_dir, minimum):
170
        output_file = codecs.open(os.path.join(output_dir, output), 'w', 'utf-8-sig')
171
        processed_files = set()
172
        # fused-min not support gtest switch
173
        if minimum:
174
            processed_files.add(os.path.normpath(os.path.join(root, "gtest/iutest_switch.hpp")))
175
176
        def ProcessFile(curr, filename, fileset, minimum):
177
            path = os.path.join(root, filename)
178
            if not os.path.exists(path):
179
                path = os.path.join(curr, filename)
180
181
            path = os.path.normpath(path)
182
            if path in fileset:
183
                return
184
185
            find_ifdef = False
186
            fileset.add(path)
187
            # print(filename)
188
            for line in codecs.open(path, 'r', 'utf-8-sig'):
189
                line = re.sub('/\*.*?\*/', '', line)
190
                m = self.INCLUDE_REGEX.match(line)
191
                if m:
192
                    self.Flush(output_file)
193
                    include_file = m.group(1)
194
                    if find_ifdef:
195
                        s = set(fileset)
196
                        ProcessFile(os.path.dirname(path), include_file, s, minimum)
197
                    else:
198
                        ProcessFile(os.path.dirname(path), include_file, fileset, minimum)
199
                else:
200
                    find_ifdef = bool(self.IFDEF_REGEX.match(line))
201
                    if minimum:
202
                        line = self.Minimze(line)
203
                        if len(line):
204
                            if self.PREPRO_REGEX.match(self.store_line):
205
                                line = line.strip()
206
                                if line.endswith('\\'):
207
                                    self.StoreMinimze(line.rstrip(r'\\'))
208
                                else:
209
                                    self.StoreMinimze(line)
210
                                    self.Flush(output_file)
211
                            elif self.PREPRO_REGEX.match(line):
212
                                self.Flush(output_file)
213
                                if line.strip().endswith('\\'):
214
                                    self.StoreMinimze(line.strip().rstrip(r'\\'))
215
                                else:
216
                                    output_file.write(line)
217
                            else:
218
                                strip_line = line.strip()
219
                                self.StoreMinimze(strip_line)
220
                                #if not re.match('.*[{};\(\)<>]$', strip_line):
221
                                #   self.Flush(output_file)
222
                                if re.match('.*(:|IUTEST_CXX_DEFAULT_FUNCTION)$', strip_line):
223
                                    self.store_line += " "
224
                    else:
225
                        output_file.write(line)
226
            self.Flush(output_file)
227
228
        ProcessFile(root, filename, processed_files, minimum)
229
        output_file.close()
230
231
232
def FusedSrc(root, filename, output, output_dir, minimum):
233
    f = IutestFused()
234
    f.Translate(root, filename, output, output_dir, minimum)
235
236
237
def FusedAll(root, output_dir):
238
    if not os.path.exists(output_dir):
239
        os.makedirs(output_dir, True)
240
    FusedSrc(root, 'iutest.hpp', 'iutest.hpp', output_dir, False)
241
    FusedSrc(root, 'iutest.hpp', 'iutest.min.hpp', output_dir, True)
242
243
244
def main():
245
    argc = len(sys.argv)
246
    if argc == 2:
247
        FusedAll(IUTEST_INCLUDE_DIR, sys.argv[1])
248
    elif argc == 3:
249
        FusedAll(os.path.join(sys.argv[1], "include"), sys.argv[2])
250
    else:
251
        print(__doc__)
252
        sys.exit(1)
253
254
255
if __name__ == '__main__':
256
    main()
257