Mercurial > hg > orthanc
annotate Resources/EmbedResources.py @ 267:2ccf556dc1ce Orthanc-0.3.1
close
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 29 Apr 2013 12:55:01 +0200 |
parents | f1aeec53795b |
children | 052dede32761 |
rev | line source |
---|---|
143 | 1 # Orthanc - A Lightweight, RESTful DICOM Store |
2 # Copyright (C) 2012 Medical Physics Department, CHU of Liege, | |
3 # Belgium | |
4 # | |
5 # This program is free software: you can redistribute it and/or | |
6 # modify it under the terms of the GNU General Public License as | |
7 # published by the Free Software Foundation, either version 3 of the | |
8 # License, or (at your option) any later version. | |
9 # | |
10 # In addition, as a special exception, the copyright holders of this | |
11 # program give permission to link the code of its release with the | |
12 # OpenSSL project's "OpenSSL" library (or with modified versions of it | |
13 # that use the same license as the "OpenSSL" library), and distribute | |
14 # the linked executables. You must obey the GNU General Public License | |
15 # in all respects for all of the code used other than "OpenSSL". If you | |
16 # modify file(s) with this exception, you may extend this exception to | |
17 # your version of the file(s), but you are not obligated to do so. If | |
18 # you do not wish to do so, delete this exception statement from your | |
19 # version. If you delete this exception statement from all source files | |
20 # in the program, then also delete it here. | |
21 # | |
22 # This program is distributed in the hope that it will be useful, but | |
23 # WITHOUT ANY WARRANTY; without even the implied warranty of | |
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
25 # General Public License for more details. | |
26 # | |
27 # You should have received a copy of the GNU General Public License | |
28 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
29 | |
30 | |
0 | 31 import sys |
32 import os | |
33 import os.path | |
34 import pprint | |
129 | 35 import re |
0 | 36 |
37 if len(sys.argv) < 2 or len(sys.argv) % 2 != 0: | |
38 print ('Usage:') | |
39 print ('python %s <TargetBaseFilename> [ <Name> <Source> ]*' % sys.argv[0]) | |
40 exit(-1) | |
41 | |
42 | |
43 try: | |
44 # Make sure the destination directory exists | |
45 os.makedirs(os.path.normpath(os.path.join(sys.argv[1], '..'))) | |
46 except: | |
47 pass | |
48 | |
49 | |
50 ##################################################################### | |
51 ## Read each resource file | |
52 ##################################################################### | |
53 | |
129 | 54 def CheckNoUpcase(s): |
55 if re.search('[A-Z]', s) != None: | |
56 raise Exception("Path in a directory with an upcase letter: %s" % s) | |
57 | |
0 | 58 resources = {} |
59 | |
60 counter = 0 | |
61 i = 2 | |
62 while i < len(sys.argv): | |
63 resourceName = sys.argv[i].upper() | |
64 pathName = sys.argv[i + 1] | |
65 | |
66 if not os.path.exists(pathName): | |
67 raise Exception("Non existing path: %s" % pathName) | |
68 | |
69 if resourceName in resources: | |
70 raise Exception("Twice the same resource: " + resourceName) | |
71 | |
72 if os.path.isdir(pathName): | |
73 # The resource is a directory: Recursively explore its files | |
74 content = {} | |
75 for root, dirs, files in os.walk(pathName): | |
76 base = os.path.relpath(root, pathName) | |
77 for f in files: | |
78 if f.find('~') == -1: # Ignore Emacs backup files | |
79 if base == '.': | |
129 | 80 r = f |
0 | 81 else: |
129 | 82 r = os.path.join(base, f) |
0 | 83 |
129 | 84 CheckNoUpcase(r) |
20 | 85 r = '/' + r.replace('\\', '/') |
0 | 86 if r in content: |
87 raise Exception("Twice the same filename (check case): " + r) | |
88 | |
89 content[r] = { | |
90 'Filename' : os.path.join(root, f), | |
91 'Index' : counter | |
92 } | |
93 counter += 1 | |
94 | |
95 resources[resourceName] = { | |
96 'Type' : 'Directory', | |
97 'Files' : content | |
98 } | |
99 | |
100 elif os.path.isfile(pathName): | |
101 resources[resourceName] = { | |
102 'Type' : 'File', | |
103 'Index' : counter, | |
104 'Filename' : pathName | |
105 } | |
106 counter += 1 | |
107 | |
108 else: | |
109 raise Exception("Not a regular file, nor a directory: " + pathName) | |
110 | |
111 i += 2 | |
112 | |
113 #pprint.pprint(resources) | |
114 | |
115 | |
116 ##################################################################### | |
117 ## Write .h header | |
118 ##################################################################### | |
119 | |
120 header = open(sys.argv[1] + '.h', 'w') | |
121 | |
122 header.write(""" | |
123 #pragma once | |
124 | |
125 #include <string> | |
126 | |
64
71c4a4abe90b
renaming of resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
55
diff
changeset
|
127 namespace Orthanc |
0 | 128 { |
129 namespace EmbeddedResources | |
130 { | |
131 enum FileResourceId | |
132 { | |
133 """) | |
134 | |
135 isFirst = True | |
136 for name in resources: | |
137 if resources[name]['Type'] == 'File': | |
138 if isFirst: | |
139 isFirst = False | |
140 else: | |
141 header.write(',\n') | |
142 header.write(' %s' % name) | |
143 | |
144 header.write(""" | |
145 }; | |
146 | |
147 enum DirectoryResourceId | |
148 { | |
149 """) | |
150 | |
151 isFirst = True | |
152 for name in resources: | |
153 if resources[name]['Type'] == 'Directory': | |
154 if isFirst: | |
155 isFirst = False | |
156 else: | |
157 header.write(',\n') | |
158 header.write(' %s' % name) | |
159 | |
160 header.write(""" | |
161 }; | |
162 | |
163 const void* GetFileResourceBuffer(FileResourceId id); | |
164 size_t GetFileResourceSize(FileResourceId id); | |
165 void GetFileResource(std::string& result, FileResourceId id); | |
166 | |
167 const void* GetDirectoryResourceBuffer(DirectoryResourceId id, const char* path); | |
168 size_t GetDirectoryResourceSize(DirectoryResourceId id, const char* path); | |
169 void GetDirectoryResource(std::string& result, DirectoryResourceId id, const char* path); | |
170 } | |
171 } | |
172 """) | |
173 header.close() | |
174 | |
175 | |
176 | |
177 ##################################################################### | |
178 ## Write the resource content in the .cpp source | |
179 ##################################################################### | |
180 | |
55 | 181 PYTHON_MAJOR_VERSION = sys.version_info[0] |
182 | |
0 | 183 def WriteResource(cpp, item): |
184 cpp.write(' static const uint8_t resource%dBuffer[] = {' % item['Index']) | |
185 | |
186 f = open(item['Filename'], "rb") | |
187 content = f.read() | |
188 f.close() | |
189 | |
190 # http://stackoverflow.com/a/1035360 | |
191 pos = 0 | |
192 for b in content: | |
55 | 193 if PYTHON_MAJOR_VERSION == 2: |
194 c = ord(b[0]) | |
195 else: | |
196 c = b | |
197 | |
0 | 198 if pos > 0: |
199 cpp.write(', ') | |
200 | |
201 if (pos % 16) == 0: | |
202 cpp.write('\n ') | |
203 | |
55 | 204 if c < 0: |
0 | 205 raise Exception("Internal error") |
206 | |
55 | 207 cpp.write("0x%02x" % c) |
0 | 208 pos += 1 |
209 | |
210 cpp.write(' };\n') | |
211 cpp.write(' static const size_t resource%dSize = %d;\n' % (item['Index'], pos)) | |
212 | |
213 | |
214 cpp = open(sys.argv[1] + '.cpp', 'w') | |
215 | |
216 cpp.write(""" | |
217 #include "%s.h" | |
64
71c4a4abe90b
renaming of resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
55
diff
changeset
|
218 #include "%s/Core/OrthancException.h" |
0 | 219 |
220 #include <stdint.h> | |
221 #include <string.h> | |
222 | |
64
71c4a4abe90b
renaming of resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
55
diff
changeset
|
223 namespace Orthanc |
0 | 224 { |
225 namespace EmbeddedResources | |
226 { | |
227 """ % (os.path.basename(sys.argv[1]), | |
228 os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))) | |
229 | |
230 | |
231 for name in resources: | |
232 if resources[name]['Type'] == 'File': | |
233 WriteResource(cpp, resources[name]) | |
234 else: | |
235 for f in resources[name]['Files']: | |
236 WriteResource(cpp, resources[name]['Files'][f]) | |
237 | |
238 | |
239 | |
240 ##################################################################### | |
241 ## Write the accessors to the file resources in .cpp | |
242 ##################################################################### | |
243 | |
244 cpp.write(""" | |
245 const void* GetFileResourceBuffer(FileResourceId id) | |
246 { | |
247 switch (id) | |
248 { | |
249 """) | |
250 for name in resources: | |
251 if resources[name]['Type'] == 'File': | |
252 cpp.write(' case %s:\n' % name) | |
253 cpp.write(' return resource%dBuffer;\n' % resources[name]['Index']) | |
254 | |
255 cpp.write(""" | |
256 default: | |
64
71c4a4abe90b
renaming of resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
55
diff
changeset
|
257 throw OrthancException(ErrorCode_ParameterOutOfRange); |
0 | 258 } |
259 } | |
260 | |
261 size_t GetFileResourceSize(FileResourceId id) | |
262 { | |
263 switch (id) | |
264 { | |
265 """) | |
266 | |
267 for name in resources: | |
268 if resources[name]['Type'] == 'File': | |
269 cpp.write(' case %s:\n' % name) | |
270 cpp.write(' return resource%dSize;\n' % resources[name]['Index']) | |
271 | |
272 cpp.write(""" | |
273 default: | |
64
71c4a4abe90b
renaming of resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
55
diff
changeset
|
274 throw OrthancException(ErrorCode_ParameterOutOfRange); |
0 | 275 } |
276 } | |
277 """) | |
278 | |
279 | |
280 | |
281 ##################################################################### | |
282 ## Write the accessors to the directory resources in .cpp | |
283 ##################################################################### | |
284 | |
285 cpp.write(""" | |
286 const void* GetDirectoryResourceBuffer(DirectoryResourceId id, const char* path) | |
287 { | |
288 switch (id) | |
289 { | |
290 """) | |
291 | |
292 for name in resources: | |
293 if resources[name]['Type'] == 'Directory': | |
294 cpp.write(' case %s:\n' % name) | |
295 isFirst = True | |
296 for path in resources[name]['Files']: | |
297 cpp.write(' if (!strcmp(path, "%s"))\n' % path) | |
298 cpp.write(' return resource%dBuffer;\n' % resources[name]['Files'][path]['Index']) | |
64
71c4a4abe90b
renaming of resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
55
diff
changeset
|
299 cpp.write(' throw OrthancException("Unknown path in a directory resource");\n\n') |
0 | 300 |
301 cpp.write(""" default: | |
64
71c4a4abe90b
renaming of resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
55
diff
changeset
|
302 throw OrthancException(ErrorCode_ParameterOutOfRange); |
0 | 303 } |
304 } | |
305 | |
306 size_t GetDirectoryResourceSize(DirectoryResourceId id, const char* path) | |
307 { | |
308 switch (id) | |
309 { | |
310 """) | |
311 | |
312 for name in resources: | |
313 if resources[name]['Type'] == 'Directory': | |
314 cpp.write(' case %s:\n' % name) | |
315 isFirst = True | |
316 for path in resources[name]['Files']: | |
317 cpp.write(' if (!strcmp(path, "%s"))\n' % path) | |
318 cpp.write(' return resource%dSize;\n' % resources[name]['Files'][path]['Index']) | |
64
71c4a4abe90b
renaming of resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
55
diff
changeset
|
319 cpp.write(' throw OrthancException("Unknown path in a directory resource");\n\n') |
0 | 320 |
321 cpp.write(""" default: | |
64
71c4a4abe90b
renaming of resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
55
diff
changeset
|
322 throw OrthancException(ErrorCode_ParameterOutOfRange); |
0 | 323 } |
324 } | |
325 """) | |
326 | |
327 | |
328 | |
329 | |
330 ##################################################################### | |
331 ## Write the convenience wrappers in .cpp | |
332 ##################################################################### | |
333 | |
334 cpp.write(""" | |
335 void GetFileResource(std::string& result, FileResourceId id) | |
336 { | |
337 size_t size = GetFileResourceSize(id); | |
338 result.resize(size); | |
339 if (size > 0) | |
340 memcpy(&result[0], GetFileResourceBuffer(id), size); | |
341 } | |
342 | |
343 void GetDirectoryResource(std::string& result, DirectoryResourceId id, const char* path) | |
344 { | |
345 size_t size = GetDirectoryResourceSize(id, path); | |
346 result.resize(size); | |
347 if (size > 0) | |
348 memcpy(&result[0], GetDirectoryResourceBuffer(id, path), size); | |
349 } | |
350 } | |
351 } | |
352 """) | |
353 cpp.close() |