comparison Resources/EmbedStaticAssets.py @ 11:165447afdcde

make the build of static assets reproducible
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sat, 05 Aug 2023 11:40:36 +0200
parents 4e889a8e8be2
children dd0cd39e6259
comparison
equal deleted inserted replaced
10:549ba7ada071 11:165447afdcde
93 93
94 index = {} 94 index = {}
95 count = 0 95 count = 0
96 96
97 for root, dirs, files in os.walk(SOURCE): 97 for root, dirs, files in os.walk(SOURCE):
98 files.sort()
99 dirs.sort()
100
98 for f in files: 101 for f in files:
99 fullPath = os.path.join(root, f) 102 fullPath = os.path.join(root, f)
100 relativePath = os.path.relpath(os.path.join(root, f), SOURCE) 103 relativePath = os.path.relpath(os.path.join(root, f), SOURCE)
101 variable = 'data_%06d' % count 104 variable = 'data_%06d' % count
102 105
104 content = source.read() 107 content = source.read()
105 108
106 if sys.version_info < (3, 0): 109 if sys.version_info < (3, 0):
107 # Python 2.7 110 # Python 2.7
108 fileobj = io.BytesIO() 111 fileobj = io.BytesIO()
109 gzip.GzipFile(fileobj=fileobj, mode='w').write(content) 112 gzip.GzipFile(fileobj=fileobj, mode='w', mtime=0).write(content)
110 compressed = fileobj.getvalue() 113 compressed = fileobj.getvalue()
111 else: 114 else:
112 # Python 3.x 115 # Python 3.x
113 compressed = gzip.compress(content) 116 compressed = gzip.compress(content, mtime=0)
114 117
115 EncodeFileAsCString(g, variable, compressed) 118 EncodeFileAsCString(g, variable, compressed)
116 WriteChecksum(g, variable + '_md5', content) 119 WriteChecksum(g, variable + '_md5', content)
117 120
118 index[relativePath] = variable 121 index[relativePath] = variable
119 122
120 count += 1 123 count += 1
121 124
122 g.write('void ReadStaticAsset(std::string& target, const std::string& path)\n') 125 g.write('void ReadStaticAsset(std::string& target, const std::string& path)\n')
123 g.write('{\n') 126 g.write('{\n')
124 for (path, variable) in index.items(): 127 for (path, variable) in sorted(index.items()):
125 g.write(' if (path == "%s")\n' % path) 128 g.write(' if (path == "%s")\n' % path)
126 g.write(' {\n') 129 g.write(' {\n')
127 g.write(' Uncompress(target, %s, sizeof(%s) - 1, %s_md5);\n' % (variable, variable, variable)) 130 g.write(' Uncompress(target, %s, sizeof(%s) - 1, %s_md5);\n' % (variable, variable, variable))
128 g.write(' return;\n') 131 g.write(' return;\n')
129 g.write(' }\n\n') 132 g.write(' }\n\n')