-
Notifications
You must be signed in to change notification settings - Fork 175
/
generate_fakeit_single_header.py
executable file
·146 lines (109 loc) · 3.91 KB
/
generate_fakeit_single_header.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import argparse
import datetime
import io
import os
import re
fakeit_path = os.path.dirname(__file__)
includes_matcher = re.compile(r'\s*#include\s*"(.*)"')
pragma_once_matcher = re.compile(r'\s*#pragma\s*once\s*')
comment_matcher = re.compile(
r'(^)?[^\S\n]*/(?:\*(.*?)\*/[^\S\n]*|/[^\n]*)($)?',
re.DOTALL | re.MULTILINE
)
processed_headers = set([])
def write_line(out, line):
out.write(line + "\n")
def parse_file(out, config, filename):
print("parsing " + filename)
if os.path.exists(os.path.join(fakeit_path, 'config', config, filename)):
file_path = os.path.join(fakeit_path, 'config', config, filename)
elif os.path.exists(os.path.join(fakeit_path, 'include', filename)):
file_path = os.path.join(fakeit_path, 'include', filename)
else:
return
write_line(out, "// #included from: " + filename)
f = open(file_path, 'r')
for line in f:
m = includes_matcher.match(line)
if m:
header = m.group(1)
if header not in processed_headers:
processed_headers.add(header)
parse_file(out, config, header)
else:
write_line(out, line.rstrip())
def write_header_comment(out, config):
write_line(out, "/*")
write_line(out, " * FakeIt - A Simplified C++ Mocking Framework")
write_line(out, " * Copyright (c) Eran Pe'er 2013")
write_line(out, " * Generated: {0}".format(datetime.datetime.now()))
write_line(out, " * Distributed under the MIT License. Please refer to the LICENSE file at:")
write_line(out, " * https://github.com/eranpeer/FakeIt")
write_line(out, " */")
def generate_fakeit_include_file(out, config):
parse_file(out, config, 'fakeit.hpp')
def comment_replacer(match):
start, mid, end = match.group(1, 2, 3)
if mid is None:
# single line comment
return ''
elif start is not None or end is not None:
# multi line comment at start or end of a line
return ''
elif '\n' in mid:
# multi line comment with line break
return '\n'
else:
# multi line comment without line break
return ' '
def remove_comments(text):
return comment_matcher.sub(comment_replacer, text)
def remove_pragma_once(text):
return pragma_once_matcher.sub('\n', text)
def create_fakeit_file_text(config):
text_stream = io.StringIO()
generate_fakeit_include_file(text_stream, config)
text = text_stream.getvalue()
text = remove_comments(text)
text = remove_pragma_once(text)
return text
def create_fakeit_file(config, text):
dest_dir = os.path.join(fakeit_path, 'single_header', config)
if not os.path.exists(dest_dir):
os.makedirs(dest_dir)
output_path = os.path.join(dest_dir, 'fakeit.hpp')
out = open(output_path, 'w')
write_line(out, "#pragma once")
write_header_comment(out, config)
out.write(text)
out.close()
def create_fakeit_file_for_config(config):
processed_headers.clear()
text = create_fakeit_file_text(config)
create_fakeit_file(config, text)
def get_all_configs():
configs = []
with os.scandir(os.path.join(fakeit_path, 'config')) as entries:
for entry in entries:
if entry.is_dir():
configs.append(entry.name)
return configs
def get_parser():
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('-c', '--configs', nargs='+', metavar='config', choices=get_all_configs())
group.add_argument('-a', '--all', action='store_true')
return parser
def main():
args = get_parser().parse_args()
if args.all:
configs = get_all_configs()
else:
configs = args.configs
for config in configs:
print(f"writing single header file for {config}")
create_fakeit_file_for_config(config)
if __name__ == '__main__':
main()