Completed
Push — master ( d5fdbe...1b7e63 )
by srz
08:59 queued 04:24
created

fused_iutest_files.IutestFused.Minimze()   F

Complexity

Conditions 9

Size

Total Lines 78
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 53
nop 2
dl 0
loc 78
rs 3.8611
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 += '"@STRING@"'
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('"@STRING@"', 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('"@STRING@""@STRING@"', '"@STRING@"##+##"@STRING@"', 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
            for line in codecs.open(path, 'r', 'utf-8-sig'):
188
                line = re.sub('/\*.*?\*/', '', line)
189
                m = self.INCLUDE_REGEX.match(line)
190
                if m:
191
                    self.Flush(output_file)
192
                    include_file = m.group(1)
193
                    if find_ifdef:
194
                        s = set(fileset)
195
                        ProcessFile(os.path.dirname(path), include_file, s, minimum)
196
                    else:
197
                        ProcessFile(os.path.dirname(path), include_file, fileset, minimum)
198
                else:
199
                    find_ifdef = bool(self.IFDEF_REGEX.match(line))
200
                    if minimum:
201
                        line = self.Minimze(line)
202
                        if len(line):
203
                            if self.PREPRO_REGEX.match(self.store_line):
204
                                line = line.strip()
205
                                if line.endswith('\\'):
206
                                    self.StoreMinimze(line.rstrip(r'\\'))
207
                                else:
208
                                    self.StoreMinimze(line)
209
                                    self.Flush(output_file)
210
                            elif self.PREPRO_REGEX.match(line):
211
                                self.Flush(output_file)
212
                                if line.strip().endswith('\\'):
213
                                    self.StoreMinimze(line.strip().rstrip(r'\\'))
214
                                else:
215
                                    output_file.write(line)
216
                            else:
217
                                strip_line = line.strip()
218
                                self.StoreMinimze(strip_line)
219
                                #if not re.match('.*[{};\(\)<>]$', strip_line):
220
                                #   self.Flush(output_file)
221
                                if re.match('.*(:|IUTEST_CXX_DEFAULT_FUNCTION)$', strip_line):
222
                                    self.store_line += " "
223
                    else:
224
                        output_file.write(line)
225
            self.Flush(output_file)
226
227
        ProcessFile(root, filename, processed_files, minimum)
228
        output_file.close()
229
230
231
def FusedSrc(root, filename, output, output_dir, minimum):
232
    f = IutestFused()
233
    f.Translate(root, filename, output, output_dir, minimum)
234
235
236
def FusedAll(root, output_dir):
237
    if not os.path.exists(output_dir):
238
        os.makedirs(output_dir, True)
239
    FusedSrc(root, 'iutest.hpp', 'iutest.hpp', output_dir, False)
240
    FusedSrc(root, 'iutest.hpp', 'iutest.min.hpp', output_dir, True)
241
242
243
def main():
244
    argc = len(sys.argv)
245
    if argc == 2:
246
        FusedAll(IUTEST_INCLUDE_DIR, sys.argv[1])
247
    elif argc == 3:
248
        FusedAll(os.path.join(sys.argv[1], "include"), sys.argv[2])
249
    else:
250
        print(__doc__)
251
        sys.exit(1)
252
253
254
if __name__ == '__main__':
255
    main()
256