Mercurial > hg > orthanc
annotate Resources/EmbedResources.py @ 344:cd6749e53a03
anonymization from orthanc explorer
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 21 Jan 2013 13:23:26 +0100 |
parents | e7276309b269 |
children | 5962898fb7c8 |
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 |
325
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
37 UPCASE_CHECK = True |
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
38 ARGS = [] |
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
39 for i in range(len(sys.argv)): |
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
40 if not sys.argv[i].startswith('--'): |
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
41 ARGS.append(sys.argv[i]) |
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
42 elif sys.argv[i].lower() == '--no-upcase-check': |
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
43 UPCASE_CHECK = False |
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
44 |
326 | 45 if len(ARGS) < 2 or len(ARGS) % 2 != 0: |
46 print ('Usage:') | |
47 print ('python %s [--no-upcase-check] <TargetBaseFilename> [ <Name> <Source> ]*' % sys.argv[0]) | |
48 exit(-1) | |
49 | |
325
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
50 TARGET_BASE_FILENAME = ARGS[1] |
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
51 SOURCES = ARGS[2:] |
0 | 52 |
53 try: | |
54 # Make sure the destination directory exists | |
325
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
55 os.makedirs(os.path.normpath(os.path.join(TARGET_BASE_FILENAME, '..'))) |
0 | 56 except: |
57 pass | |
58 | |
59 | |
60 ##################################################################### | |
61 ## Read each resource file | |
62 ##################################################################### | |
63 | |
129 | 64 def CheckNoUpcase(s): |
325
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
65 global UPCASE_CHECK |
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
66 if (UPCASE_CHECK and |
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
67 re.search('[A-Z]', s) != None): |
129 | 68 raise Exception("Path in a directory with an upcase letter: %s" % s) |
69 | |
0 | 70 resources = {} |
71 | |
72 counter = 0 | |
325
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
73 i = 0 |
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
74 while i < len(SOURCES): |
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
75 resourceName = SOURCES[i].upper() |
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
76 pathName = SOURCES[i + 1] |
0 | 77 |
78 if not os.path.exists(pathName): | |
79 raise Exception("Non existing path: %s" % pathName) | |
80 | |
81 if resourceName in resources: | |
82 raise Exception("Twice the same resource: " + resourceName) | |
83 | |
84 if os.path.isdir(pathName): | |
85 # The resource is a directory: Recursively explore its files | |
86 content = {} | |
87 for root, dirs, files in os.walk(pathName): | |
88 base = os.path.relpath(root, pathName) | |
89 for f in files: | |
90 if f.find('~') == -1: # Ignore Emacs backup files | |
91 if base == '.': | |
129 | 92 r = f |
0 | 93 else: |
129 | 94 r = os.path.join(base, f) |
0 | 95 |
129 | 96 CheckNoUpcase(r) |
20 | 97 r = '/' + r.replace('\\', '/') |
0 | 98 if r in content: |
99 raise Exception("Twice the same filename (check case): " + r) | |
100 | |
101 content[r] = { | |
102 'Filename' : os.path.join(root, f), | |
103 'Index' : counter | |
104 } | |
105 counter += 1 | |
106 | |
107 resources[resourceName] = { | |
108 'Type' : 'Directory', | |
109 'Files' : content | |
110 } | |
111 | |
112 elif os.path.isfile(pathName): | |
113 resources[resourceName] = { | |
114 'Type' : 'File', | |
115 'Index' : counter, | |
116 'Filename' : pathName | |
117 } | |
118 counter += 1 | |
119 | |
120 else: | |
121 raise Exception("Not a regular file, nor a directory: " + pathName) | |
122 | |
123 i += 2 | |
124 | |
125 #pprint.pprint(resources) | |
126 | |
127 | |
128 ##################################################################### | |
129 ## Write .h header | |
130 ##################################################################### | |
131 | |
325
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
132 header = open(TARGET_BASE_FILENAME + '.h', 'w') |
0 | 133 |
134 header.write(""" | |
135 #pragma once | |
136 | |
137 #include <string> | |
323
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
138 #include <list> |
0 | 139 |
64
71c4a4abe90b
renaming of resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
55
diff
changeset
|
140 namespace Orthanc |
0 | 141 { |
142 namespace EmbeddedResources | |
143 { | |
144 enum FileResourceId | |
145 { | |
146 """) | |
147 | |
148 isFirst = True | |
149 for name in resources: | |
150 if resources[name]['Type'] == 'File': | |
151 if isFirst: | |
152 isFirst = False | |
153 else: | |
154 header.write(',\n') | |
155 header.write(' %s' % name) | |
156 | |
157 header.write(""" | |
158 }; | |
159 | |
160 enum DirectoryResourceId | |
161 { | |
162 """) | |
163 | |
164 isFirst = True | |
165 for name in resources: | |
166 if resources[name]['Type'] == 'Directory': | |
167 if isFirst: | |
168 isFirst = False | |
169 else: | |
170 header.write(',\n') | |
171 header.write(' %s' % name) | |
172 | |
173 header.write(""" | |
174 }; | |
175 | |
176 const void* GetFileResourceBuffer(FileResourceId id); | |
177 size_t GetFileResourceSize(FileResourceId id); | |
178 void GetFileResource(std::string& result, FileResourceId id); | |
179 | |
180 const void* GetDirectoryResourceBuffer(DirectoryResourceId id, const char* path); | |
181 size_t GetDirectoryResourceSize(DirectoryResourceId id, const char* path); | |
182 void GetDirectoryResource(std::string& result, DirectoryResourceId id, const char* path); | |
323
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
183 |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
184 void ListResources(std::list<std::string>& result, DirectoryResourceId id); |
0 | 185 } |
186 } | |
187 """) | |
188 header.close() | |
189 | |
190 | |
191 | |
192 ##################################################################### | |
193 ## Write the resource content in the .cpp source | |
194 ##################################################################### | |
195 | |
55 | 196 PYTHON_MAJOR_VERSION = sys.version_info[0] |
197 | |
0 | 198 def WriteResource(cpp, item): |
199 cpp.write(' static const uint8_t resource%dBuffer[] = {' % item['Index']) | |
200 | |
201 f = open(item['Filename'], "rb") | |
202 content = f.read() | |
203 f.close() | |
204 | |
205 # http://stackoverflow.com/a/1035360 | |
206 pos = 0 | |
207 for b in content: | |
55 | 208 if PYTHON_MAJOR_VERSION == 2: |
209 c = ord(b[0]) | |
210 else: | |
211 c = b | |
212 | |
0 | 213 if pos > 0: |
214 cpp.write(', ') | |
215 | |
216 if (pos % 16) == 0: | |
217 cpp.write('\n ') | |
218 | |
55 | 219 if c < 0: |
0 | 220 raise Exception("Internal error") |
221 | |
55 | 222 cpp.write("0x%02x" % c) |
0 | 223 pos += 1 |
224 | |
225 cpp.write(' };\n') | |
226 cpp.write(' static const size_t resource%dSize = %d;\n' % (item['Index'], pos)) | |
227 | |
228 | |
325
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
229 cpp = open(TARGET_BASE_FILENAME + '.cpp', 'w') |
0 | 230 |
231 cpp.write(""" | |
232 #include "%s.h" | |
64
71c4a4abe90b
renaming of resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
55
diff
changeset
|
233 #include "%s/Core/OrthancException.h" |
0 | 234 |
235 #include <stdint.h> | |
236 #include <string.h> | |
237 | |
64
71c4a4abe90b
renaming of resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
55
diff
changeset
|
238 namespace Orthanc |
0 | 239 { |
240 namespace EmbeddedResources | |
241 { | |
325
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
242 """ % (os.path.basename(TARGET_BASE_FILENAME), |
0 | 243 os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))) |
244 | |
245 | |
246 for name in resources: | |
247 if resources[name]['Type'] == 'File': | |
248 WriteResource(cpp, resources[name]) | |
249 else: | |
250 for f in resources[name]['Files']: | |
251 WriteResource(cpp, resources[name]['Files'][f]) | |
252 | |
253 | |
254 | |
255 ##################################################################### | |
256 ## Write the accessors to the file resources in .cpp | |
257 ##################################################################### | |
258 | |
259 cpp.write(""" | |
260 const void* GetFileResourceBuffer(FileResourceId id) | |
261 { | |
262 switch (id) | |
263 { | |
264 """) | |
265 for name in resources: | |
266 if resources[name]['Type'] == 'File': | |
267 cpp.write(' case %s:\n' % name) | |
268 cpp.write(' return resource%dBuffer;\n' % resources[name]['Index']) | |
269 | |
270 cpp.write(""" | |
271 default: | |
64
71c4a4abe90b
renaming of resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
55
diff
changeset
|
272 throw OrthancException(ErrorCode_ParameterOutOfRange); |
0 | 273 } |
274 } | |
275 | |
276 size_t GetFileResourceSize(FileResourceId id) | |
277 { | |
278 switch (id) | |
279 { | |
280 """) | |
281 | |
282 for name in resources: | |
283 if resources[name]['Type'] == 'File': | |
284 cpp.write(' case %s:\n' % name) | |
285 cpp.write(' return resource%dSize;\n' % resources[name]['Index']) | |
286 | |
287 cpp.write(""" | |
288 default: | |
64
71c4a4abe90b
renaming of resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
55
diff
changeset
|
289 throw OrthancException(ErrorCode_ParameterOutOfRange); |
0 | 290 } |
291 } | |
292 """) | |
293 | |
294 | |
295 | |
296 ##################################################################### | |
297 ## Write the accessors to the directory resources in .cpp | |
298 ##################################################################### | |
299 | |
300 cpp.write(""" | |
301 const void* GetDirectoryResourceBuffer(DirectoryResourceId id, const char* path) | |
302 { | |
303 switch (id) | |
304 { | |
305 """) | |
306 | |
307 for name in resources: | |
308 if resources[name]['Type'] == 'Directory': | |
309 cpp.write(' case %s:\n' % name) | |
310 isFirst = True | |
311 for path in resources[name]['Files']: | |
312 cpp.write(' if (!strcmp(path, "%s"))\n' % path) | |
313 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
|
314 cpp.write(' throw OrthancException("Unknown path in a directory resource");\n\n') |
0 | 315 |
316 cpp.write(""" default: | |
64
71c4a4abe90b
renaming of resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
55
diff
changeset
|
317 throw OrthancException(ErrorCode_ParameterOutOfRange); |
0 | 318 } |
319 } | |
320 | |
321 size_t GetDirectoryResourceSize(DirectoryResourceId id, const char* path) | |
322 { | |
323 switch (id) | |
324 { | |
325 """) | |
326 | |
327 for name in resources: | |
328 if resources[name]['Type'] == 'Directory': | |
329 cpp.write(' case %s:\n' % name) | |
330 isFirst = True | |
331 for path in resources[name]['Files']: | |
332 cpp.write(' if (!strcmp(path, "%s"))\n' % path) | |
333 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
|
334 cpp.write(' throw OrthancException("Unknown path in a directory resource");\n\n') |
0 | 335 |
336 cpp.write(""" default: | |
64
71c4a4abe90b
renaming of resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
55
diff
changeset
|
337 throw OrthancException(ErrorCode_ParameterOutOfRange); |
0 | 338 } |
339 } | |
340 """) | |
341 | |
342 | |
343 | |
344 | |
345 ##################################################################### | |
323
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
346 ## List the resources in a directory |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
347 ##################################################################### |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
348 |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
349 cpp.write(""" |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
350 void ListResources(std::list<std::string>& result, DirectoryResourceId id) |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
351 { |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
352 result.clear(); |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
353 |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
354 switch (id) |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
355 { |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
356 """) |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
357 |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
358 for name in resources: |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
359 if resources[name]['Type'] == 'Directory': |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
360 cpp.write(' case %s:\n' % name) |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
361 for path in sorted(resources[name]['Files']): |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
362 cpp.write(' result.push_back("%s");\n' % path) |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
363 cpp.write(' break;\n\n') |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
364 |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
365 cpp.write(""" default: |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
366 throw OrthancException(ErrorCode_ParameterOutOfRange); |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
367 } |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
368 } |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
369 """) |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
370 |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
371 |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
372 |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
373 |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
374 ##################################################################### |
0 | 375 ## Write the convenience wrappers in .cpp |
376 ##################################################################### | |
377 | |
378 cpp.write(""" | |
379 void GetFileResource(std::string& result, FileResourceId id) | |
380 { | |
381 size_t size = GetFileResourceSize(id); | |
382 result.resize(size); | |
383 if (size > 0) | |
384 memcpy(&result[0], GetFileResourceBuffer(id), size); | |
385 } | |
386 | |
387 void GetDirectoryResource(std::string& result, DirectoryResourceId id, const char* path) | |
388 { | |
389 size_t size = GetDirectoryResourceSize(id, path); | |
390 result.resize(size); | |
391 if (size > 0) | |
392 memcpy(&result[0], GetDirectoryResourceBuffer(id, path), size); | |
393 } | |
394 } | |
395 } | |
396 """) | |
397 cpp.close() |