Mercurial > hg > orthanc
comparison Resources/EmbedResources.py @ 0:3959d33612cc
initial commit
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 19 Jul 2012 14:32:22 +0200 |
parents | |
children | a56b6a8b89ba |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:3959d33612cc |
---|---|
1 import sys | |
2 import os | |
3 import os.path | |
4 import pprint | |
5 | |
6 if len(sys.argv) < 2 or len(sys.argv) % 2 != 0: | |
7 print ('Usage:') | |
8 print ('python %s <TargetBaseFilename> [ <Name> <Source> ]*' % sys.argv[0]) | |
9 exit(-1) | |
10 | |
11 | |
12 try: | |
13 # Make sure the destination directory exists | |
14 os.makedirs(os.path.normpath(os.path.join(sys.argv[1], '..'))) | |
15 except: | |
16 pass | |
17 | |
18 | |
19 ##################################################################### | |
20 ## Read each resource file | |
21 ##################################################################### | |
22 | |
23 resources = {} | |
24 | |
25 counter = 0 | |
26 i = 2 | |
27 while i < len(sys.argv): | |
28 resourceName = sys.argv[i].upper() | |
29 pathName = sys.argv[i + 1] | |
30 | |
31 if not os.path.exists(pathName): | |
32 raise Exception("Non existing path: %s" % pathName) | |
33 | |
34 if resourceName in resources: | |
35 raise Exception("Twice the same resource: " + resourceName) | |
36 | |
37 if os.path.isdir(pathName): | |
38 # The resource is a directory: Recursively explore its files | |
39 content = {} | |
40 for root, dirs, files in os.walk(pathName): | |
41 base = os.path.relpath(root, pathName) | |
42 for f in files: | |
43 if f.find('~') == -1: # Ignore Emacs backup files | |
44 if base == '.': | |
45 r = f.lower() | |
46 else: | |
47 r = os.path.join(base, f).lower() | |
48 | |
49 r = '/' + r | |
50 if r in content: | |
51 raise Exception("Twice the same filename (check case): " + r) | |
52 | |
53 content[r] = { | |
54 'Filename' : os.path.join(root, f), | |
55 'Index' : counter | |
56 } | |
57 counter += 1 | |
58 | |
59 resources[resourceName] = { | |
60 'Type' : 'Directory', | |
61 'Files' : content | |
62 } | |
63 | |
64 elif os.path.isfile(pathName): | |
65 resources[resourceName] = { | |
66 'Type' : 'File', | |
67 'Index' : counter, | |
68 'Filename' : pathName | |
69 } | |
70 counter += 1 | |
71 | |
72 else: | |
73 raise Exception("Not a regular file, nor a directory: " + pathName) | |
74 | |
75 i += 2 | |
76 | |
77 #pprint.pprint(resources) | |
78 | |
79 | |
80 ##################################################################### | |
81 ## Write .h header | |
82 ##################################################################### | |
83 | |
84 header = open(sys.argv[1] + '.h', 'w') | |
85 | |
86 header.write(""" | |
87 #pragma once | |
88 | |
89 #include <string> | |
90 | |
91 namespace Palantir | |
92 { | |
93 namespace EmbeddedResources | |
94 { | |
95 enum FileResourceId | |
96 { | |
97 """) | |
98 | |
99 isFirst = True | |
100 for name in resources: | |
101 if resources[name]['Type'] == 'File': | |
102 if isFirst: | |
103 isFirst = False | |
104 else: | |
105 header.write(',\n') | |
106 header.write(' %s' % name) | |
107 | |
108 header.write(""" | |
109 }; | |
110 | |
111 enum DirectoryResourceId | |
112 { | |
113 """) | |
114 | |
115 isFirst = True | |
116 for name in resources: | |
117 if resources[name]['Type'] == 'Directory': | |
118 if isFirst: | |
119 isFirst = False | |
120 else: | |
121 header.write(',\n') | |
122 header.write(' %s' % name) | |
123 | |
124 header.write(""" | |
125 }; | |
126 | |
127 const void* GetFileResourceBuffer(FileResourceId id); | |
128 size_t GetFileResourceSize(FileResourceId id); | |
129 void GetFileResource(std::string& result, FileResourceId id); | |
130 | |
131 const void* GetDirectoryResourceBuffer(DirectoryResourceId id, const char* path); | |
132 size_t GetDirectoryResourceSize(DirectoryResourceId id, const char* path); | |
133 void GetDirectoryResource(std::string& result, DirectoryResourceId id, const char* path); | |
134 } | |
135 } | |
136 """) | |
137 header.close() | |
138 | |
139 | |
140 | |
141 ##################################################################### | |
142 ## Write the resource content in the .cpp source | |
143 ##################################################################### | |
144 | |
145 def WriteResource(cpp, item): | |
146 cpp.write(' static const uint8_t resource%dBuffer[] = {' % item['Index']) | |
147 | |
148 f = open(item['Filename'], "rb") | |
149 content = f.read() | |
150 f.close() | |
151 | |
152 # http://stackoverflow.com/a/1035360 | |
153 pos = 0 | |
154 for b in content: | |
155 if pos > 0: | |
156 cpp.write(', ') | |
157 | |
158 if (pos % 16) == 0: | |
159 cpp.write('\n ') | |
160 | |
161 if ord(b[0]) < 0: | |
162 raise Exception("Internal error") | |
163 | |
164 cpp.write("0x%02x" % ord(b[0])) | |
165 pos += 1 | |
166 | |
167 cpp.write(' };\n') | |
168 cpp.write(' static const size_t resource%dSize = %d;\n' % (item['Index'], pos)) | |
169 | |
170 | |
171 cpp = open(sys.argv[1] + '.cpp', 'w') | |
172 | |
173 cpp.write(""" | |
174 #include "%s.h" | |
175 #include "%s/Core/PalantirException.h" | |
176 | |
177 #include <stdint.h> | |
178 #include <string.h> | |
179 | |
180 namespace Palantir | |
181 { | |
182 namespace EmbeddedResources | |
183 { | |
184 """ % (os.path.basename(sys.argv[1]), | |
185 os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))) | |
186 | |
187 | |
188 for name in resources: | |
189 if resources[name]['Type'] == 'File': | |
190 WriteResource(cpp, resources[name]) | |
191 else: | |
192 for f in resources[name]['Files']: | |
193 WriteResource(cpp, resources[name]['Files'][f]) | |
194 | |
195 | |
196 | |
197 ##################################################################### | |
198 ## Write the accessors to the file resources in .cpp | |
199 ##################################################################### | |
200 | |
201 cpp.write(""" | |
202 const void* GetFileResourceBuffer(FileResourceId id) | |
203 { | |
204 switch (id) | |
205 { | |
206 """) | |
207 for name in resources: | |
208 if resources[name]['Type'] == 'File': | |
209 cpp.write(' case %s:\n' % name) | |
210 cpp.write(' return resource%dBuffer;\n' % resources[name]['Index']) | |
211 | |
212 cpp.write(""" | |
213 default: | |
214 throw PalantirException(ErrorCode_ParameterOutOfRange); | |
215 } | |
216 } | |
217 | |
218 size_t GetFileResourceSize(FileResourceId id) | |
219 { | |
220 switch (id) | |
221 { | |
222 """) | |
223 | |
224 for name in resources: | |
225 if resources[name]['Type'] == 'File': | |
226 cpp.write(' case %s:\n' % name) | |
227 cpp.write(' return resource%dSize;\n' % resources[name]['Index']) | |
228 | |
229 cpp.write(""" | |
230 default: | |
231 throw PalantirException(ErrorCode_ParameterOutOfRange); | |
232 } | |
233 } | |
234 """) | |
235 | |
236 | |
237 | |
238 ##################################################################### | |
239 ## Write the accessors to the directory resources in .cpp | |
240 ##################################################################### | |
241 | |
242 cpp.write(""" | |
243 const void* GetDirectoryResourceBuffer(DirectoryResourceId id, const char* path) | |
244 { | |
245 switch (id) | |
246 { | |
247 """) | |
248 | |
249 for name in resources: | |
250 if resources[name]['Type'] == 'Directory': | |
251 cpp.write(' case %s:\n' % name) | |
252 isFirst = True | |
253 for path in resources[name]['Files']: | |
254 cpp.write(' if (!strcmp(path, "%s"))\n' % path) | |
255 cpp.write(' return resource%dBuffer;\n' % resources[name]['Files'][path]['Index']) | |
256 cpp.write(' throw PalantirException("Unknown path in a directory resource");\n\n') | |
257 | |
258 cpp.write(""" default: | |
259 throw PalantirException(ErrorCode_ParameterOutOfRange); | |
260 } | |
261 } | |
262 | |
263 size_t GetDirectoryResourceSize(DirectoryResourceId id, const char* path) | |
264 { | |
265 switch (id) | |
266 { | |
267 """) | |
268 | |
269 for name in resources: | |
270 if resources[name]['Type'] == 'Directory': | |
271 cpp.write(' case %s:\n' % name) | |
272 isFirst = True | |
273 for path in resources[name]['Files']: | |
274 cpp.write(' if (!strcmp(path, "%s"))\n' % path) | |
275 cpp.write(' return resource%dSize;\n' % resources[name]['Files'][path]['Index']) | |
276 cpp.write(' throw PalantirException("Unknown path in a directory resource");\n\n') | |
277 | |
278 cpp.write(""" default: | |
279 throw PalantirException(ErrorCode_ParameterOutOfRange); | |
280 } | |
281 } | |
282 """) | |
283 | |
284 | |
285 | |
286 | |
287 ##################################################################### | |
288 ## Write the convenience wrappers in .cpp | |
289 ##################################################################### | |
290 | |
291 cpp.write(""" | |
292 void GetFileResource(std::string& result, FileResourceId id) | |
293 { | |
294 size_t size = GetFileResourceSize(id); | |
295 result.resize(size); | |
296 if (size > 0) | |
297 memcpy(&result[0], GetFileResourceBuffer(id), size); | |
298 } | |
299 | |
300 void GetDirectoryResource(std::string& result, DirectoryResourceId id, const char* path) | |
301 { | |
302 size_t size = GetDirectoryResourceSize(id, path); | |
303 result.resize(size); | |
304 if (size > 0) | |
305 memcpy(&result[0], GetDirectoryResourceBuffer(id, path), size); | |
306 } | |
307 } | |
308 } | |
309 """) | |
310 cpp.close() |