import os def comment_line(line): return "/* {} */ /* Commented by amalgamation script */".format(line) def amalgamate(config): with open(config["output_file"], "w") as output_f: output_f.write("/* The file was GENERATED by an amalgamation script.*/\n") output_f.write("/* DO NOT EDIT BY HAND!!! */\n\n") for hdr_file in config["header_files"]: with open(config["src_dir"] + "/" + hdr_file, "r") as input_f: txt = input_f.read() output_f.write('\n/********************************************************\n') output_f.write(' Begin of file "{}"\n'.format(hdr_file)) output_f.write(' ********************************************************/\n\n') output_f.write(txt) output_f.write('\n/********************************************************\n') output_f.write(' End of file "{}"\n'.format(hdr_file)) output_f.write(' ********************************************************/\n\n') for src_file in config["src_files"]: with open(config["src_dir"] + "/" + src_file, "r") as input_f: txt = input_f.read() output_f.write('\n/********************************************************\n') output_f.write(' Begin of file "{}"\n'.format(src_file)) output_f.write(' ********************************************************/\n\n') output_f.write(txt) output_f.write('\n/********************************************************\n') output_f.write(' End of file "{}"\n'.format(src_file)) output_f.write(' ********************************************************/\n\n') with open(config["output_file"]) as f: lines = f.readlines() forbidden_strings = map(lambda hdr_name: '#include "{}"'.format(hdr_name), config["header_files"]) lines = map(lambda line: comment_line(line.strip()) + "\n" if line.strip() in forbidden_strings else line, lines) with open(config["output_file"], "w") as f: for line in lines: f.write(line) def is_c_header_file(file): return ".h" in file def is_c_source_file(file): return ".c" in file def main(): config = {} config["output_file"] = "./include/fort.c" config["src_dir"] = "./src" all_files = os.listdir(config["src_dir"]) config["src_files"] = filter(is_c_source_file, all_files) # config["header_files"] = filter(is_c_header_file, all_files) config["header_files"] = [ "fort_utils.h", "vector.h", "wcwidth.h", "string_buffer.h", "options.h", "cell.h", "row.h", "table.h" ]; amalgamate(config) if __name__ == '__main__': main()