forked from nmakeenkov/source-merger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsource_merger.py
80 lines (63 loc) · 2.16 KB
/
source_merger.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
# coding: utf-8
import argparse
import sys
from os import listdir
from os.path import isfile
from os.path import join
from os.path import dirname
def process(filepath, merged):
if (filepath in merged):
return ""
text = ""
try:
infile = open(filepath)
except IOError, err:
return False
merged.append(filepath)
text += "\n\n // Start of %s\n\n" % filepath
file_lines = infile.readlines()
for current_line in file_lines:
index = current_line.find("#include")
if index == -1:
text += current_line
else:
text += current_line[0:index]
index += len("#include")
while current_line[index] != "\"" and current_line[index] != "<":
index += 1
included_file_name = ""
index += 1
while current_line[index] != "\"" and current_line[index] != ">":
included_file_name += current_line[index]
index += 1
included_file_path = join(dirname(filepath), included_file_name)
included_text = process(included_file_path, merged)
if (included_text == False):
index = current_line.find("#include")
text += current_line[index:]
else:
text += included_text + current_line[index + 1:]
return text
def merge(filenames):
merged = []
text = "\n\n/*\nProcessed with SourceMerger by Michael Nemilov and Nikolai Makeenkov\n" + \
"For more info check out : https://github.com/nmakeenkov/SourceMerger\n*/"
for filename in filenames:
text += process(filename, merged)
return text
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Creates a CPP file from 2 or more "
+ "source files : useful to send your solution "
+ "to testing system")
parser.add_argument("--outfile", "-o",
help="file the result to be written to")
parser.add_argument("sources", nargs="*")
args = parser.parse_args()
if args.outfile is None:
output = sys.stdout
else:
output = open(args.outfile, "w")
sources = ['./' + filename for filename in args.sources]
merged_text = merge(sources)
output.write(merged_text)