annotate Resources/EmbedStaticAssets.py @ 17:154cb76a042f

make the build of static assets reproducible
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sat, 05 Aug 2023 11:36:33 +0200
parents 39585ba26f20
children 36049c04ee27
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 #!/usr/bin/python3
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 # SPDX-FileCopyrightText: 2023 Sebastien Jodogne, UCLouvain, Belgium
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 # SPDX-License-Identifier: GPL-3.0-or-later
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 # OHIF plugin for Orthanc
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 # Copyright (C) 2023 Sebastien Jodogne, UCLouvain, Belgium
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 #
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 # This program is free software: you can redistribute it and/or
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 # modify it under the terms of the GNU General Public License as
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 # published by the Free Software Foundation, either version 3 of the
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 # License, or (at your option) any later version.
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 #
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 # This program is distributed in the hope that it will be useful, but
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 # General Public License for more details.
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 #
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 # You should have received a copy of the GNU General Public License
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 # along with this program. If not, see <http://www.gnu.org/licenses/>.
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 import gzip
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 import hashlib
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 import io
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 import os
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 import sys
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 if len(sys.argv) != 3:
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 raise Exception('Usage: %s [source folder] [target C++]' % sys.argv[0])
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32 SOURCE = sys.argv[1]
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 TARGET = sys.argv[2]
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 if not os.path.isdir(SOURCE):
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 raise Exception('Nonexistent source folder: %s' % SOURCE)
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 def EncodeFileAsCString(f, variable, content):
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 f.write('static const uint8_t %s[%d] = \n "' % (variable, len(content) + 1))
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 column = 0
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 for c in content:
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 if sys.version_info < (3, 0):
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 # Python 2.7
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 i = ord(c)
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 else:
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 # Python 3.x
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 i = c
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 if i < 32 or i >= 127 or i == ord('?'):
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 f.write('\\{0:03o}'.format(i))
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 elif i in [ ord('"'), ord('\\') ]:
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 f.write('\\' + chr(i))
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 else:
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 f.write(chr(i))
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 column += 1
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 if column >= 120:
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 f.write('"\n "')
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 column = 0
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 f.write('";\n\n')
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 def WriteChecksum(f, variable, content):
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 md5 = hashlib.md5(content).hexdigest()
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 g.write('static const char* %s = "%s";\n\n' % (variable, md5))
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 with open(TARGET, 'w') as g:
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 g.write('''
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 #include <stdint.h>
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 #include <Compression/GzipCompressor.h>
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 #include <OrthancException.h>
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 #include <Toolbox.h>
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 static void Uncompress(std::string& target, const void* data, size_t size, const std::string& md5Expected)
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 {
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 Orthanc::GzipCompressor compressor;
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 compressor.Uncompress(target, data, size);
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 std::string md5Actual;
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 Orthanc::Toolbox::ComputeMD5(md5Actual, target);
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 if (md5Actual != md5Expected)
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 {
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 throw Orthanc::OrthancException(Orthanc::ErrorCode_CorruptedFile);
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 }
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 }
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 ''')
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 index = {}
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 count = 0
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 for root, dirs, files in os.walk(SOURCE):
17
154cb76a042f make the build of static assets reproducible
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
98 files.sort()
154cb76a042f make the build of static assets reproducible
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
99 dirs.sort()
154cb76a042f make the build of static assets reproducible
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
100
0
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 for f in files:
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 fullPath = os.path.join(root, f)
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 relativePath = os.path.relpath(os.path.join(root, f), SOURCE)
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 variable = 'data_%06d' % count
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 with open(fullPath, 'rb') as source:
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 content = source.read()
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 if sys.version_info < (3, 0):
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110 # Python 2.7
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 fileobj = io.BytesIO()
17
154cb76a042f make the build of static assets reproducible
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
112 gzip.GzipFile(fileobj=fileobj, mode='w', mtime=0).write(content)
0
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 compressed = fileobj.getvalue()
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 else:
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 # Python 3.x
17
154cb76a042f make the build of static assets reproducible
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
116 compressed = gzip.compress(content, mtime=0)
0
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118 EncodeFileAsCString(g, variable, compressed)
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119 WriteChecksum(g, variable + '_md5', content)
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121 index[relativePath] = variable
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 count += 1
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 g.write('void ReadStaticAsset(std::string& target, const std::string& path)\n')
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 g.write('{\n')
17
154cb76a042f make the build of static assets reproducible
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 0
diff changeset
127 for (path, variable) in sorted(index.items()):
0
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128 g.write(' if (path == "%s")\n' % path)
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129 g.write(' {\n')
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130 g.write(' Uncompress(target, %s, sizeof(%s) - 1, %s_md5);\n' % (variable, variable, variable))
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131 g.write(' return;\n')
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132 g.write(' }\n\n')
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134 g.write(' throw Orthanc::OrthancException(Orthanc::ErrorCode_InexistentItem, "Unknown OHIF resource: " + path);\n')
39585ba26f20 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135 g.write('}\n')