1 #!/usr/bin/env python 2 # Copyright (c) 2013 The Chromium Authors. All rights reserved. 3 # Use of this source code is governed by a BSD-style license that can be 4 # found in the LICENSE file. 5 6 """generates files to embed the gles2 conformance test data in executable.""" 7 8 import os 9 import sys 10 11 class GenerateEmbeddedFiles(object): 12 """generates files to embed the gles2 conform test data in executable""" 13 14 paths_to_ignore = set([ 15 ".", 16 "..", 17 ".svn", 18 ".git", 19 ".hg", 20 ]) 21 22 extensions_to_include = set([ 23 ".vert", 24 ".frag", 25 ".test", 26 ".run", 27 ]) 28 29 def __init__(self, scan_dir, base_dir): 30 self.scan_dir = scan_dir 31 self.base_dir = base_dir 32 self.count = 0; 33 if self.base_dir != None: 34 self.files_data_h = open(os.path.join(base_dir, "FilesDATA.h"), "wb") 35 self.files_data_c = open(os.path.join(base_dir, "FilesDATA.c"), "wb") 36 self.files_toc_c = open(os.path.join(base_dir, "FilesTOC.c"), "wb") 37 38 self.files_data_h.write("#ifndef FilesDATA_h\n\n") 39 self.files_data_h.write("#define FilesDATA_h\n\n"); 40 41 self.files_data_c.write("#include \"FilesDATA.h\"\n\n") 42 43 self.files_toc_c.write("#include \"FilesTOC.h\"\n\n"); 44 self.files_toc_c.write("struct GTFVectorFileEntry tempFiles;\n\n"); 45 self.files_toc_c.write("struct FileEntry files[] = {\n"); 46 47 self.AddFiles(scan_dir) 48 49 if self.base_dir != None: 50 self.files_toc_c.write("\n};\n\n"); 51 self.files_toc_c.write( 52 "int numFileEntrys = sizeof(files) / sizeof(struct FileEntry);\n"); 53 54 self.files_data_h.write("\n\n#endif // FilesDATA_h\n"); 55 56 self.files_data_c.close() 57 self.files_data_h.close() 58 self.files_toc_c.close() 59 60 def AddFiles(self, scan_dir): 61 """Scan a folder and embed the contents of files.""" 62 files = os.listdir(scan_dir) 63 sub_dirs = [] 64 for file in files: 65 full_path = os.path.join(scan_dir, file) 66 ext = os.path.splitext(file)[1] 67 base_path = full_path[len(self.scan_dir) + 1:] 68 if os.path.isdir(full_path): 69 if not file in GenerateEmbeddedFiles.paths_to_ignore: 70 sub_dirs.append(full_path) 71 elif ext in GenerateEmbeddedFiles.extensions_to_include: 72 if self.base_dir == None: 73 print full_path.replace("\\", "/") 74 else: 75 self.count += 1 76 name = "_FILE_%s_%d" % (ext.upper(), self.count) 77 name = name.replace(".", "_") 78 79 self.files_data_h.write("extern const char %s[];\n" % name) 80 self.files_data_c.write("const char %s[] = \n" % name) 81 82 data = open(full_path, "r") 83 lines = data.readlines(); 84 data.close() 85 86 for line in lines: 87 line = line.replace("\n", "") 88 line = line.replace("\r", "") 89 line = line.replace("\\", "\\\\") 90 line = line.replace("\"", "\\\"") 91 92 self.files_data_c.write('"%s\\n"\n' % line) 93 94 self.files_data_c.write(";\n") 95 self.files_toc_c.write("\t{ \"%s\", %s, 0 },\n" % ( 96 base_path.replace("\\", "/"), name)) 97 98 for sub_dir in sub_dirs: 99 self.AddFiles(sub_dir) 100 101 102 def main(argv): 103 """This is the main function.""" 104 105 if len(argv) >= 1: 106 scan_dir = argv[0] 107 else: 108 scan_dir = '.' 109 110 if len(argv) >= 2: 111 base_dir = argv[1] 112 else: 113 base_dir = None 114 115 GenerateEmbeddedFiles(scan_dir, base_dir) 116 return 0 117 118 119 if __name__ == '__main__': 120 sys.exit(main(sys.argv[1:])) 121