Mercurial > hg > orthanc
annotate OrthancFramework/Resources/EmbedResources.py @ 4944:f377d5643538 more-tags
new Warnings configuration + InstanceAvailability tag
author | Alain Mazy <am@osimis.io> |
---|---|
date | Thu, 17 Mar 2022 17:03:59 +0100 |
parents | 43e613a7756b |
children | 0ea402b4d901 |
rev | line source |
---|---|
1467
5068de14eef1
Inject version information into Windows binaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1377
diff
changeset
|
1 #!/usr/bin/python |
5068de14eef1
Inject version information into Windows binaries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1377
diff
changeset
|
2 |
143 | 3 # Orthanc - A Lightweight, RESTful DICOM Store |
1900 | 4 # Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
1288
6e7e5ed91c2d
upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1204
diff
changeset
|
5 # Department, University Hospital of Liege, Belgium |
4870
43e613a7756b
upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4831
diff
changeset
|
6 # Copyright (C) 2017-2022 Osimis S.A., Belgium |
43e613a7756b
upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4831
diff
changeset
|
7 # Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
143 | 8 # |
9 # This program is free software: you can redistribute it and/or | |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
10 # modify it under the terms of the GNU Lesser General Public License |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
11 # as published by the Free Software Foundation, either version 3 of |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
12 # the License, or (at your option) any later version. |
143 | 13 # |
14 # This program is distributed in the hope that it will be useful, but | |
15 # WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
17 # Lesser General Public License for more details. |
143 | 18 # |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
19 # You should have received a copy of the GNU Lesser General Public |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
20 # License along with this program. If not, see |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
21 # <http://www.gnu.org/licenses/>. |
143 | 22 |
23 | |
0 | 24 import sys |
25 import os | |
26 import os.path | |
27 import pprint | |
129 | 28 import re |
0 | 29 |
325
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
30 UPCASE_CHECK = True |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
31 USE_SYSTEM_EXCEPTION = False |
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
32 EXCEPTION_CLASS = 'OrthancException' |
2690 | 33 OUT_OF_RANGE_EXCEPTION = '::Orthanc::OrthancException(::Orthanc::ErrorCode_ParameterOutOfRange)' |
34 INEXISTENT_PATH_EXCEPTION = '::Orthanc::OrthancException(::Orthanc::ErrorCode_InexistentItem)' | |
4030
100fbe970762
DANGEROUS commit: removing HAS_EMBEDDED_RESOURCES
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
35 NAMESPACE = 'Orthanc.EmbeddedResources' |
4034
78ee0155ec67
trying to use orthanc frameworking with web-viewer plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4030
diff
changeset
|
36 FRAMEWORK_PATH = None |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
37 |
325
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 |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
44 elif sys.argv[i].lower() == '--system-exception': |
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
45 USE_SYSTEM_EXCEPTION = True |
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
46 EXCEPTION_CLASS = '::std::runtime_error' |
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
47 OUT_OF_RANGE_EXCEPTION = '%s("Parameter out of range")' % EXCEPTION_CLASS |
1584
39ecd34fb1c7
fix standalone build
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1469
diff
changeset
|
48 INEXISTENT_PATH_EXCEPTION = '%s("Unknown path in a directory resource")' % EXCEPTION_CLASS |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
49 elif sys.argv[i].startswith('--namespace='): |
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
50 NAMESPACE = sys.argv[i][sys.argv[i].find('=') + 1 : ] |
4034
78ee0155ec67
trying to use orthanc frameworking with web-viewer plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4030
diff
changeset
|
51 elif sys.argv[i].startswith('--framework-path='): |
78ee0155ec67
trying to use orthanc frameworking with web-viewer plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4030
diff
changeset
|
52 FRAMEWORK_PATH = sys.argv[i][sys.argv[i].find('=') + 1 : ] |
325
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
53 |
326 | 54 if len(ARGS) < 2 or len(ARGS) % 2 != 0: |
55 print ('Usage:') | |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
56 print ('python %s [--no-upcase-check] [--system-exception] [--namespace=<Namespace>] <TargetBaseFilename> [ <Name> <Source> ]*' % sys.argv[0]) |
326 | 57 exit(-1) |
58 | |
325
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
59 TARGET_BASE_FILENAME = ARGS[1] |
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
60 SOURCES = ARGS[2:] |
0 | 61 |
62 try: | |
63 # Make sure the destination directory exists | |
325
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
64 os.makedirs(os.path.normpath(os.path.join(TARGET_BASE_FILENAME, '..'))) |
0 | 65 except: |
66 pass | |
67 | |
68 | |
69 ##################################################################### | |
70 ## Read each resource file | |
71 ##################################################################### | |
72 | |
129 | 73 def CheckNoUpcase(s): |
325
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
74 global UPCASE_CHECK |
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
75 if (UPCASE_CHECK and |
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
76 re.search('[A-Z]', s) != None): |
129 | 77 raise Exception("Path in a directory with an upcase letter: %s" % s) |
78 | |
0 | 79 resources = {} |
80 | |
81 counter = 0 | |
325
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
82 i = 0 |
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
83 while i < len(SOURCES): |
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
84 resourceName = SOURCES[i].upper() |
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
85 pathName = SOURCES[i + 1] |
0 | 86 |
87 if not os.path.exists(pathName): | |
88 raise Exception("Non existing path: %s" % pathName) | |
89 | |
90 if resourceName in resources: | |
91 raise Exception("Twice the same resource: " + resourceName) | |
92 | |
93 if os.path.isdir(pathName): | |
94 # The resource is a directory: Recursively explore its files | |
95 content = {} | |
96 for root, dirs, files in os.walk(pathName): | |
3587
76b3228f99b0
Sort file lists
Bernhard M. Wiedemann <bernhard+hgcommit@lsmod.de>
parents:
3354
diff
changeset
|
97 dirs.sort() |
76b3228f99b0
Sort file lists
Bernhard M. Wiedemann <bernhard+hgcommit@lsmod.de>
parents:
3354
diff
changeset
|
98 files.sort() |
0 | 99 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
|
100 |
6babe0c7aa27
Fix issue #24 (Build fails on OSX when directory has .DS_Store files)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
101 # 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
|
102 # 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
|
103 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
|
104 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
|
105 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
|
106 |
0 | 107 for f in files: |
108 if f.find('~') == -1: # Ignore Emacs backup files | |
109 if base == '.': | |
129 | 110 r = f |
0 | 111 else: |
129 | 112 r = os.path.join(base, f) |
0 | 113 |
129 | 114 CheckNoUpcase(r) |
20 | 115 r = '/' + r.replace('\\', '/') |
0 | 116 if r in content: |
117 raise Exception("Twice the same filename (check case): " + r) | |
118 | |
119 content[r] = { | |
120 'Filename' : os.path.join(root, f), | |
121 'Index' : counter | |
122 } | |
123 counter += 1 | |
124 | |
125 resources[resourceName] = { | |
126 'Type' : 'Directory', | |
127 'Files' : content | |
128 } | |
129 | |
130 elif os.path.isfile(pathName): | |
131 resources[resourceName] = { | |
132 'Type' : 'File', | |
133 'Index' : counter, | |
134 'Filename' : pathName | |
135 } | |
136 counter += 1 | |
137 | |
138 else: | |
139 raise Exception("Not a regular file, nor a directory: " + pathName) | |
140 | |
141 i += 2 | |
142 | |
143 #pprint.pprint(resources) | |
144 | |
145 | |
146 ##################################################################### | |
147 ## Write .h header | |
148 ##################################################################### | |
149 | |
325
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
150 header = open(TARGET_BASE_FILENAME + '.h', 'w') |
0 | 151 |
152 header.write(""" | |
153 #pragma once | |
154 | |
155 #include <string> | |
323
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
156 #include <list> |
0 | 157 |
2174
8453d3a26421
disable warning with MSVC
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
158 #if defined(_MSC_VER) |
8453d3a26421
disable warning with MSVC
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
159 # pragma warning(disable: 4065) // "Switch statement contains 'default' but no 'case' labels" |
8453d3a26421
disable warning with MSVC
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
160 #endif |
8453d3a26421
disable warning with MSVC
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
161 |
4030
100fbe970762
DANGEROUS commit: removing HAS_EMBEDDED_RESOURCES
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
162 """) |
100fbe970762
DANGEROUS commit: removing HAS_EMBEDDED_RESOURCES
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
163 |
100fbe970762
DANGEROUS commit: removing HAS_EMBEDDED_RESOURCES
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
164 |
100fbe970762
DANGEROUS commit: removing HAS_EMBEDDED_RESOURCES
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
165 for ns in NAMESPACE.split('.'): |
100fbe970762
DANGEROUS commit: removing HAS_EMBEDDED_RESOURCES
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
166 header.write('namespace %s {\n' % ns) |
100fbe970762
DANGEROUS commit: removing HAS_EMBEDDED_RESOURCES
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
167 |
100fbe970762
DANGEROUS commit: removing HAS_EMBEDDED_RESOURCES
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
168 |
100fbe970762
DANGEROUS commit: removing HAS_EMBEDDED_RESOURCES
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
169 header.write(""" |
0 | 170 enum FileResourceId |
171 { | |
4030
100fbe970762
DANGEROUS commit: removing HAS_EMBEDDED_RESOURCES
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
172 """) |
0 | 173 |
174 isFirst = True | |
175 for name in resources: | |
176 if resources[name]['Type'] == 'File': | |
177 if isFirst: | |
178 isFirst = False | |
179 else: | |
180 header.write(',\n') | |
181 header.write(' %s' % name) | |
182 | |
183 header.write(""" | |
184 }; | |
185 | |
186 enum DirectoryResourceId | |
187 { | |
188 """) | |
189 | |
190 isFirst = True | |
191 for name in resources: | |
192 if resources[name]['Type'] == 'Directory': | |
193 if isFirst: | |
194 isFirst = False | |
195 else: | |
196 header.write(',\n') | |
197 header.write(' %s' % name) | |
198 | |
199 header.write(""" | |
200 }; | |
201 | |
202 const void* GetFileResourceBuffer(FileResourceId id); | |
203 size_t GetFileResourceSize(FileResourceId id); | |
204 void GetFileResource(std::string& result, FileResourceId id); | |
205 | |
206 const void* GetDirectoryResourceBuffer(DirectoryResourceId id, const char* path); | |
207 size_t GetDirectoryResourceSize(DirectoryResourceId id, const char* path); | |
208 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
|
209 |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
210 void ListResources(std::list<std::string>& result, DirectoryResourceId id); |
4030
100fbe970762
DANGEROUS commit: removing HAS_EMBEDDED_RESOURCES
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
211 |
0 | 212 """) |
4030
100fbe970762
DANGEROUS commit: removing HAS_EMBEDDED_RESOURCES
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
213 |
100fbe970762
DANGEROUS commit: removing HAS_EMBEDDED_RESOURCES
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
214 |
100fbe970762
DANGEROUS commit: removing HAS_EMBEDDED_RESOURCES
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
215 for ns in NAMESPACE.split('.'): |
100fbe970762
DANGEROUS commit: removing HAS_EMBEDDED_RESOURCES
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
216 header.write('}\n') |
100fbe970762
DANGEROUS commit: removing HAS_EMBEDDED_RESOURCES
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
217 |
0 | 218 header.close() |
219 | |
220 | |
221 | |
222 ##################################################################### | |
223 ## Write the resource content in the .cpp source | |
224 ##################################################################### | |
225 | |
55 | 226 PYTHON_MAJOR_VERSION = sys.version_info[0] |
227 | |
0 | 228 def WriteResource(cpp, item): |
229 cpp.write(' static const uint8_t resource%dBuffer[] = {' % item['Index']) | |
230 | |
231 f = open(item['Filename'], "rb") | |
232 content = f.read() | |
233 f.close() | |
234 | |
235 # http://stackoverflow.com/a/1035360 | |
236 pos = 0 | |
3354
e60e194531e5
optimized EmbedResources.py (especially in speed: 2-3x faster but also in space: 20% smaller)
Alain Mazy <alain@mazy.be>
parents:
3060
diff
changeset
|
237 buffer = [] # instead of appending a few bytes at a time to the cpp file, |
e60e194531e5
optimized EmbedResources.py (especially in speed: 2-3x faster but also in space: 20% smaller)
Alain Mazy <alain@mazy.be>
parents:
3060
diff
changeset
|
238 # we first append each chunk to a list, join it and write it |
e60e194531e5
optimized EmbedResources.py (especially in speed: 2-3x faster but also in space: 20% smaller)
Alain Mazy <alain@mazy.be>
parents:
3060
diff
changeset
|
239 # to the file. We've measured that it was 2-3 times faster in python3. |
e60e194531e5
optimized EmbedResources.py (especially in speed: 2-3x faster but also in space: 20% smaller)
Alain Mazy <alain@mazy.be>
parents:
3060
diff
changeset
|
240 # Note that speed is important since if generation is too slow, |
e60e194531e5
optimized EmbedResources.py (especially in speed: 2-3x faster but also in space: 20% smaller)
Alain Mazy <alain@mazy.be>
parents:
3060
diff
changeset
|
241 # cmake might try to compile the EmbeddedResources.cpp file while it is |
e60e194531e5
optimized EmbedResources.py (especially in speed: 2-3x faster but also in space: 20% smaller)
Alain Mazy <alain@mazy.be>
parents:
3060
diff
changeset
|
242 # still being generated ! |
0 | 243 for b in content: |
55 | 244 if PYTHON_MAJOR_VERSION == 2: |
245 c = ord(b[0]) | |
246 else: | |
247 c = b | |
248 | |
0 | 249 if pos > 0: |
3354
e60e194531e5
optimized EmbedResources.py (especially in speed: 2-3x faster but also in space: 20% smaller)
Alain Mazy <alain@mazy.be>
parents:
3060
diff
changeset
|
250 buffer.append(",") |
0 | 251 |
252 if (pos % 16) == 0: | |
3354
e60e194531e5
optimized EmbedResources.py (especially in speed: 2-3x faster but also in space: 20% smaller)
Alain Mazy <alain@mazy.be>
parents:
3060
diff
changeset
|
253 buffer.append("\n") |
0 | 254 |
55 | 255 if c < 0: |
0 | 256 raise Exception("Internal error") |
257 | |
3354
e60e194531e5
optimized EmbedResources.py (especially in speed: 2-3x faster but also in space: 20% smaller)
Alain Mazy <alain@mazy.be>
parents:
3060
diff
changeset
|
258 buffer.append("0x%02x" % c) |
0 | 259 pos += 1 |
260 | |
3354
e60e194531e5
optimized EmbedResources.py (especially in speed: 2-3x faster but also in space: 20% smaller)
Alain Mazy <alain@mazy.be>
parents:
3060
diff
changeset
|
261 cpp.write("".join(buffer)) |
1377
601d34afdab9
patch for recent versions of MinGW
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
262 # Zero-size array are disallowed, so we put one single void character in it. |
601d34afdab9
patch for recent versions of MinGW
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
263 if pos == 0: |
601d34afdab9
patch for recent versions of MinGW
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
264 cpp.write(' 0') |
601d34afdab9
patch for recent versions of MinGW
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
265 |
0 | 266 cpp.write(' };\n') |
267 cpp.write(' static const size_t resource%dSize = %d;\n' % (item['Index'], pos)) | |
268 | |
269 | |
325
71d30b9ce801
ignore upcase check
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
323
diff
changeset
|
270 cpp = open(TARGET_BASE_FILENAME + '.cpp', 'w') |
0 | 271 |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
272 cpp.write('#include "%s.h"\n' % os.path.basename(TARGET_BASE_FILENAME)) |
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
273 |
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
274 if USE_SYSTEM_EXCEPTION: |
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
275 cpp.write('#include <stdexcept>') |
4034
78ee0155ec67
trying to use orthanc frameworking with web-viewer plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4030
diff
changeset
|
276 elif FRAMEWORK_PATH != None: |
78ee0155ec67
trying to use orthanc frameworking with web-viewer plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4030
diff
changeset
|
277 cpp.write('#include "%s/OrthancException.h"' % FRAMEWORK_PATH) |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
278 else: |
4034
78ee0155ec67
trying to use orthanc frameworking with web-viewer plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4030
diff
changeset
|
279 cpp.write('#include <OrthancException.h>') |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
280 |
0 | 281 cpp.write(""" |
282 #include <stdint.h> | |
283 #include <string.h> | |
284 | |
4030
100fbe970762
DANGEROUS commit: removing HAS_EMBEDDED_RESOURCES
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
285 """) |
100fbe970762
DANGEROUS commit: removing HAS_EMBEDDED_RESOURCES
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
286 |
100fbe970762
DANGEROUS commit: removing HAS_EMBEDDED_RESOURCES
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
287 for ns in NAMESPACE.split('.'): |
100fbe970762
DANGEROUS commit: removing HAS_EMBEDDED_RESOURCES
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
288 cpp.write('namespace %s {\n' % ns) |
0 | 289 |
290 | |
291 for name in resources: | |
292 if resources[name]['Type'] == 'File': | |
293 WriteResource(cpp, resources[name]) | |
294 else: | |
295 for f in resources[name]['Files']: | |
296 WriteResource(cpp, resources[name]['Files'][f]) | |
297 | |
298 | |
299 | |
300 ##################################################################### | |
301 ## Write the accessors to the file resources in .cpp | |
302 ##################################################################### | |
303 | |
304 cpp.write(""" | |
305 const void* GetFileResourceBuffer(FileResourceId id) | |
306 { | |
307 switch (id) | |
308 { | |
309 """) | |
310 for name in resources: | |
311 if resources[name]['Type'] == 'File': | |
312 cpp.write(' case %s:\n' % name) | |
313 cpp.write(' return resource%dBuffer;\n' % resources[name]['Index']) | |
314 | |
315 cpp.write(""" | |
316 default: | |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
317 throw %s; |
0 | 318 } |
319 } | |
320 | |
321 size_t GetFileResourceSize(FileResourceId id) | |
322 { | |
323 switch (id) | |
324 { | |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
325 """ % OUT_OF_RANGE_EXCEPTION) |
0 | 326 |
327 for name in resources: | |
328 if resources[name]['Type'] == 'File': | |
329 cpp.write(' case %s:\n' % name) | |
330 cpp.write(' return resource%dSize;\n' % resources[name]['Index']) | |
331 | |
332 cpp.write(""" | |
333 default: | |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
334 throw %s; |
0 | 335 } |
336 } | |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
337 """ % OUT_OF_RANGE_EXCEPTION) |
0 | 338 |
339 | |
340 | |
341 ##################################################################### | |
342 ## Write the accessors to the directory resources in .cpp | |
343 ##################################################################### | |
344 | |
345 cpp.write(""" | |
346 const void* GetDirectoryResourceBuffer(DirectoryResourceId id, const char* path) | |
347 { | |
348 switch (id) | |
349 { | |
350 """) | |
351 | |
352 for name in resources: | |
353 if resources[name]['Type'] == 'Directory': | |
354 cpp.write(' case %s:\n' % name) | |
355 isFirst = True | |
356 for path in resources[name]['Files']: | |
357 cpp.write(' if (!strcmp(path, "%s"))\n' % path) | |
358 cpp.write(' return resource%dBuffer;\n' % resources[name]['Files'][path]['Index']) | |
1584
39ecd34fb1c7
fix standalone build
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1469
diff
changeset
|
359 cpp.write(' throw %s;\n\n' % INEXISTENT_PATH_EXCEPTION) |
0 | 360 |
361 cpp.write(""" default: | |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
362 throw %s; |
0 | 363 } |
364 } | |
365 | |
366 size_t GetDirectoryResourceSize(DirectoryResourceId id, const char* path) | |
367 { | |
368 switch (id) | |
369 { | |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
370 """ % OUT_OF_RANGE_EXCEPTION) |
0 | 371 |
372 for name in resources: | |
373 if resources[name]['Type'] == 'Directory': | |
374 cpp.write(' case %s:\n' % name) | |
375 isFirst = True | |
376 for path in resources[name]['Files']: | |
377 cpp.write(' if (!strcmp(path, "%s"))\n' % path) | |
378 cpp.write(' return resource%dSize;\n' % resources[name]['Files'][path]['Index']) | |
1584
39ecd34fb1c7
fix standalone build
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1469
diff
changeset
|
379 cpp.write(' throw %s;\n\n' % INEXISTENT_PATH_EXCEPTION) |
0 | 380 |
381 cpp.write(""" default: | |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
382 throw %s; |
0 | 383 } |
384 } | |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
385 """ % OUT_OF_RANGE_EXCEPTION) |
0 | 386 |
387 | |
388 | |
389 | |
390 ##################################################################### | |
323
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
391 ## List the resources in a directory |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
392 ##################################################################### |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
393 |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
394 cpp.write(""" |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
395 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
|
396 { |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
397 result.clear(); |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
398 |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
399 switch (id) |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
400 { |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
401 """) |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
402 |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
403 for name in resources: |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
404 if resources[name]['Type'] == 'Directory': |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
405 cpp.write(' case %s:\n' % name) |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
406 for path in sorted(resources[name]['Files']): |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
407 cpp.write(' result.push_back("%s");\n' % path) |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
408 cpp.write(' break;\n\n') |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
409 |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
410 cpp.write(""" default: |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
411 throw %s; |
323
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
412 } |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
413 } |
1469
bf9b8bfea0e7
more generic EmbedResources.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1467
diff
changeset
|
414 """ % OUT_OF_RANGE_EXCEPTION) |
323
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
415 |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
416 |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
417 |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
418 |
052dede32761
list files in resource directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
143
diff
changeset
|
419 ##################################################################### |
0 | 420 ## Write the convenience wrappers in .cpp |
421 ##################################################################### | |
422 | |
423 cpp.write(""" | |
424 void GetFileResource(std::string& result, FileResourceId id) | |
425 { | |
426 size_t size = GetFileResourceSize(id); | |
427 result.resize(size); | |
428 if (size > 0) | |
429 memcpy(&result[0], GetFileResourceBuffer(id), size); | |
430 } | |
431 | |
432 void GetDirectoryResource(std::string& result, DirectoryResourceId id, const char* path) | |
433 { | |
434 size_t size = GetDirectoryResourceSize(id, path); | |
435 result.resize(size); | |
436 if (size > 0) | |
437 memcpy(&result[0], GetDirectoryResourceBuffer(id, path), size); | |
438 } | |
439 """) | |
4030
100fbe970762
DANGEROUS commit: removing HAS_EMBEDDED_RESOURCES
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
440 |
100fbe970762
DANGEROUS commit: removing HAS_EMBEDDED_RESOURCES
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
441 |
100fbe970762
DANGEROUS commit: removing HAS_EMBEDDED_RESOURCES
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
442 for ns in NAMESPACE.split('.'): |
100fbe970762
DANGEROUS commit: removing HAS_EMBEDDED_RESOURCES
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
443 cpp.write('}\n') |
100fbe970762
DANGEROUS commit: removing HAS_EMBEDDED_RESOURCES
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
444 |
0 | 445 cpp.close() |