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