Mercurial > hg > orthanc
annotate Resources/EmbedResources.py @ 1317:55b9b6d9f8cc Orthanc-0.8.6
Orthanc-0.8.6
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 12 Feb 2015 14:45:43 +0100 |
parents | 6e7e5ed91c2d |
children | 601d34afdab9 |
rev | line source |
---|---|
143 | 1 # Orthanc - A Lightweight, RESTful DICOM Store |
1288
6e7e5ed91c2d
upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1204
diff
changeset
|
2 # Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics |
6e7e5ed91c2d
upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1204
diff
changeset
|
3 # Department, University Hospital of Liege, Belgium |
143 | 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) | |
1204
6babe0c7aa27
Fix issue #24 (Build fails on OSX when directory has .DS_Store files)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
89 |
6babe0c7aa27
Fix issue #24 (Build fails on OSX when directory has .DS_Store files)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
90 # Fix issue #24 (Build fails on OSX when directory has .DS_Store files): |
6babe0c7aa27
Fix issue #24 (Build fails on OSX when directory has .DS_Store files)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
91 # Ignore folders whose name starts with a dot (".") |
6babe0c7aa27
Fix issue #24 (Build fails on OSX when directory has .DS_Store files)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
92 if base.find('/.') != -1: |
6babe0c7aa27
Fix issue #24 (Build fails on OSX when directory has .DS_Store files)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
93 print('Ignoring folder: %s' % root) |
6babe0c7aa27
Fix issue #24 (Build fails on OSX when directory has .DS_Store files)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
94 continue |
6babe0c7aa27
Fix issue #24 (Build fails on OSX when directory has .DS_Store files)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
95 |
0 | 96 for f in files: |
97 if f.find('~') == -1: # Ignore Emacs backup files | |
98 if base == '.': | |
129 | 99 r = f |
0 | 100 else: |
129 | 101 r = os.path.join(base, f) |
0 | 102 |
129 | 103 CheckNoUpcase(r) |
20 | 104 r = '/' + r.replace('\\', '/') |
0 | 105 if r in content: |
106 raise Exception("Twice the same filename (check case): " + r) | |
107 | |
108 content[r] = { | |
109 'Filename' : os.path.join(root, f), | |
110 'Index' : counter | |
111 } | |
112 counter += 1 | |
113 | |
114 resources[resourceName] = { | |
115 'Type' : 'Directory', | |
116 'Files' : content | |
117 } | |
118 | |
119 elif os.path.isfile(pathName): | |
120 resources[resourceName] = { | |
121 'Type' : 'File', | |
122 'Index' : counter, | |
123 'Filename' : pathName | |
124 } | |
125 counter += 1 | |
126 | |
127 else: | |
128 raise Exception("Not a regular file, nor a directory: " + pathName) | |
129 | |
130 i += 2 | |
131 | |
132 #pprint.pprint(resources) | |
133 | |
134 | |
135 ##################################################################### | |
136 ## Write .h header | |
137 ##################################################################### | |
138 | |
325
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
139 header = open(TARGET_BASE_FILENAME + '.h', 'w') |
0 | 140 |
141 header.write(""" | |
142 #pragma once | |
143 | |
144 #include <string> | |
323
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
145 #include <list> |
0 | 146 |
64
71c4a4abe90b
renaming of resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
55
diff
changeset
|
147 namespace Orthanc |
0 | 148 { |
149 namespace EmbeddedResources | |
150 { | |
151 enum FileResourceId | |
152 { | |
153 """) | |
154 | |
155 isFirst = True | |
156 for name in resources: | |
157 if resources[name]['Type'] == 'File': | |
158 if isFirst: | |
159 isFirst = False | |
160 else: | |
161 header.write(',\n') | |
162 header.write(' %s' % name) | |
163 | |
164 header.write(""" | |
165 }; | |
166 | |
167 enum DirectoryResourceId | |
168 { | |
169 """) | |
170 | |
171 isFirst = True | |
172 for name in resources: | |
173 if resources[name]['Type'] == 'Directory': | |
174 if isFirst: | |
175 isFirst = False | |
176 else: | |
177 header.write(',\n') | |
178 header.write(' %s' % name) | |
179 | |
180 header.write(""" | |
181 }; | |
182 | |
183 const void* GetFileResourceBuffer(FileResourceId id); | |
184 size_t GetFileResourceSize(FileResourceId id); | |
185 void GetFileResource(std::string& result, FileResourceId id); | |
186 | |
187 const void* GetDirectoryResourceBuffer(DirectoryResourceId id, const char* path); | |
188 size_t GetDirectoryResourceSize(DirectoryResourceId id, const char* path); | |
189 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
|
190 |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
191 void ListResources(std::list<std::string>& result, DirectoryResourceId id); |
0 | 192 } |
193 } | |
194 """) | |
195 header.close() | |
196 | |
197 | |
198 | |
199 ##################################################################### | |
200 ## Write the resource content in the .cpp source | |
201 ##################################################################### | |
202 | |
55 | 203 PYTHON_MAJOR_VERSION = sys.version_info[0] |
204 | |
0 | 205 def WriteResource(cpp, item): |
206 cpp.write(' static const uint8_t resource%dBuffer[] = {' % item['Index']) | |
207 | |
208 f = open(item['Filename'], "rb") | |
209 content = f.read() | |
210 f.close() | |
211 | |
212 # http://stackoverflow.com/a/1035360 | |
213 pos = 0 | |
214 for b in content: | |
55 | 215 if PYTHON_MAJOR_VERSION == 2: |
216 c = ord(b[0]) | |
217 else: | |
218 c = b | |
219 | |
0 | 220 if pos > 0: |
221 cpp.write(', ') | |
222 | |
223 if (pos % 16) == 0: | |
224 cpp.write('\n ') | |
225 | |
55 | 226 if c < 0: |
0 | 227 raise Exception("Internal error") |
228 | |
55 | 229 cpp.write("0x%02x" % c) |
0 | 230 pos += 1 |
231 | |
232 cpp.write(' };\n') | |
233 cpp.write(' static const size_t resource%dSize = %d;\n' % (item['Index'], pos)) | |
234 | |
235 | |
325
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
236 cpp = open(TARGET_BASE_FILENAME + '.cpp', 'w') |
0 | 237 |
238 cpp.write(""" | |
239 #include "%s.h" | |
64
71c4a4abe90b
renaming of resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
55
diff
changeset
|
240 #include "%s/Core/OrthancException.h" |
0 | 241 |
242 #include <stdint.h> | |
243 #include <string.h> | |
244 | |
64
71c4a4abe90b
renaming of resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
55
diff
changeset
|
245 namespace Orthanc |
0 | 246 { |
247 namespace EmbeddedResources | |
248 { | |
325
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
249 """ % (os.path.basename(TARGET_BASE_FILENAME), |
0 | 250 os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))) |
251 | |
252 | |
253 for name in resources: | |
254 if resources[name]['Type'] == 'File': | |
255 WriteResource(cpp, resources[name]) | |
256 else: | |
257 for f in resources[name]['Files']: | |
258 WriteResource(cpp, resources[name]['Files'][f]) | |
259 | |
260 | |
261 | |
262 ##################################################################### | |
263 ## Write the accessors to the file resources in .cpp | |
264 ##################################################################### | |
265 | |
266 cpp.write(""" | |
267 const void* GetFileResourceBuffer(FileResourceId id) | |
268 { | |
269 switch (id) | |
270 { | |
271 """) | |
272 for name in resources: | |
273 if resources[name]['Type'] == 'File': | |
274 cpp.write(' case %s:\n' % name) | |
275 cpp.write(' return resource%dBuffer;\n' % resources[name]['Index']) | |
276 | |
277 cpp.write(""" | |
278 default: | |
64
71c4a4abe90b
renaming of resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
55
diff
changeset
|
279 throw OrthancException(ErrorCode_ParameterOutOfRange); |
0 | 280 } |
281 } | |
282 | |
283 size_t GetFileResourceSize(FileResourceId id) | |
284 { | |
285 switch (id) | |
286 { | |
287 """) | |
288 | |
289 for name in resources: | |
290 if resources[name]['Type'] == 'File': | |
291 cpp.write(' case %s:\n' % name) | |
292 cpp.write(' return resource%dSize;\n' % resources[name]['Index']) | |
293 | |
294 cpp.write(""" | |
295 default: | |
64
71c4a4abe90b
renaming of resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
55
diff
changeset
|
296 throw OrthancException(ErrorCode_ParameterOutOfRange); |
0 | 297 } |
298 } | |
299 """) | |
300 | |
301 | |
302 | |
303 ##################################################################### | |
304 ## Write the accessors to the directory resources in .cpp | |
305 ##################################################################### | |
306 | |
307 cpp.write(""" | |
308 const void* GetDirectoryResourceBuffer(DirectoryResourceId id, const char* path) | |
309 { | |
310 switch (id) | |
311 { | |
312 """) | |
313 | |
314 for name in resources: | |
315 if resources[name]['Type'] == 'Directory': | |
316 cpp.write(' case %s:\n' % name) | |
317 isFirst = True | |
318 for path in resources[name]['Files']: | |
319 cpp.write(' if (!strcmp(path, "%s"))\n' % path) | |
320 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
|
321 cpp.write(' throw OrthancException("Unknown path in a directory resource");\n\n') |
0 | 322 |
323 cpp.write(""" default: | |
64
71c4a4abe90b
renaming of resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
55
diff
changeset
|
324 throw OrthancException(ErrorCode_ParameterOutOfRange); |
0 | 325 } |
326 } | |
327 | |
328 size_t GetDirectoryResourceSize(DirectoryResourceId id, const char* path) | |
329 { | |
330 switch (id) | |
331 { | |
332 """) | |
333 | |
334 for name in resources: | |
335 if resources[name]['Type'] == 'Directory': | |
336 cpp.write(' case %s:\n' % name) | |
337 isFirst = True | |
338 for path in resources[name]['Files']: | |
339 cpp.write(' if (!strcmp(path, "%s"))\n' % path) | |
340 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
|
341 cpp.write(' throw OrthancException("Unknown path in a directory resource");\n\n') |
0 | 342 |
343 cpp.write(""" default: | |
64
71c4a4abe90b
renaming of resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
55
diff
changeset
|
344 throw OrthancException(ErrorCode_ParameterOutOfRange); |
0 | 345 } |
346 } | |
347 """) | |
348 | |
349 | |
350 | |
351 | |
352 ##################################################################### | |
323
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
353 ## List the resources in a directory |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
354 ##################################################################### |
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 cpp.write(""" |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
357 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
|
358 { |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
359 result.clear(); |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
360 |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
361 switch (id) |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
362 { |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
363 """) |
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 for name in resources: |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
366 if resources[name]['Type'] == 'Directory': |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
367 cpp.write(' case %s:\n' % name) |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
368 for path in sorted(resources[name]['Files']): |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
369 cpp.write(' result.push_back("%s");\n' % path) |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
370 cpp.write(' break;\n\n') |
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 cpp.write(""" default: |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
373 throw OrthancException(ErrorCode_ParameterOutOfRange); |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
374 } |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
375 } |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
376 """) |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
377 |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
378 |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
379 |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
380 |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
381 ##################################################################### |
0 | 382 ## Write the convenience wrappers in .cpp |
383 ##################################################################### | |
384 | |
385 cpp.write(""" | |
386 void GetFileResource(std::string& result, FileResourceId id) | |
387 { | |
388 size_t size = GetFileResourceSize(id); | |
389 result.resize(size); | |
390 if (size > 0) | |
391 memcpy(&result[0], GetFileResourceBuffer(id), size); | |
392 } | |
393 | |
394 void GetDirectoryResource(std::string& result, DirectoryResourceId id, const char* path) | |
395 { | |
396 size_t size = GetDirectoryResourceSize(id, path); | |
397 result.resize(size); | |
398 if (size > 0) | |
399 memcpy(&result[0], GetDirectoryResourceBuffer(id, path), size); | |
400 } | |
401 } | |
402 } | |
403 """) | |
404 cpp.close() |