45
46 FileEntry = collections.namedtuple('FileEntry', 'target_name file_contents')
47
48
49 pattern = re.compile("#include \"(.*)\"")
50
51
52 files = []
53 for i in range(len(source)):
54 src = source[i]
55 dst = target[i]
56 f = open(src)
57 cts = f.read()
58 f.close()
59 contents = cts.splitlines()
60 entry = FileEntry(target_name=dst, file_contents=contents)
61 files.append((os.path.basename(src), entry))
62
63
64 files_dict = dict(files)
65
66
67 final_files = []
68 for file in files:
69 done = False
70 tmp_file = file[1].file_contents
71 print(file[1].target_name)
72 while not done:
73 file_count = 0
74 updated_file = []
75 for line in tmp_file:
76 found = pattern.search(line)
77 if found:
78 include_file = found.group(1)
79 data = files_dict[include_file].file_contents
80 updated_file.extend(data)
81 else:
82 updated_file.append(line)
83 file_count += 1
84
85
86 if file_count == len(tmp_file):
87 done = True
88
89
90 tmp_file = updated_file
91
92
93 tmp_file.insert(0, "R\"(\n")
94 tmp_file.append("\n)\"")
95 entry = FileEntry(target_name=file[1].target_name, file_contents=tmp_file)
96 final_files.append((file[0], entry))
97
98
99 for file in final_files:
100 with open(file[1].target_name, 'w+') as out_file:
101 out_file.write("\n".join(file[1].file_contents))
102
103
104