changeset 3:6002fc2c98cf

compatibility with Python 2.7 for our CIS
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 22 Mar 2023 12:27:58 +0100
parents ad2fc4ec99fb
children 111b7f10cf4d 7c6bb471317a
files CMakeLists.txt Resources/EmbedStaticAssets.py
diffstat 2 files changed, 24 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/CMakeLists.txt	Wed Mar 22 11:51:32 2023 +0100
+++ b/CMakeLists.txt	Wed Mar 22 12:27:58 2023 +0100
@@ -58,7 +58,7 @@
 if (ORTHANC_FRAMEWORK_SOURCE STREQUAL "system")
   if (ORTHANC_FRAMEWORK_USE_SHARED)
     include(FindBoost)
-    find_package(Boost COMPONENTS system)
+    find_package(Boost COMPONENTS system thread)
     
     if (NOT Boost_FOUND)
       message(FATAL_ERROR "Unable to locate Boost on this system")
@@ -150,6 +150,7 @@
   OUTPUT
   ${AUTOGENERATED_DIR}/StaticAssets.cpp
   COMMAND
+  ${PYTHON_EXECUTABLE}
   ${CMAKE_SOURCE_DIR}/Resources/EmbedStaticAssets.py
   ${CMAKE_SOURCE_DIR}/VolView/dist
   ${AUTOGENERATED_DIR}/StaticAssets.cpp
--- a/Resources/EmbedStaticAssets.py	Wed Mar 22 11:51:32 2023 +0100
+++ b/Resources/EmbedStaticAssets.py	Wed Mar 22 12:27:58 2023 +0100
@@ -22,12 +22,10 @@
 
 import gzip
 import hashlib
+import io
 import os
 import sys
 
-if (sys.version_info < (3, 0)):
-    raise Exception('You must use Python 3.x to run this script')
-
 if len(sys.argv) != 3:
     raise Exception('Usage: %s [source folder] [target C++]' % sys.argv[0])
 
@@ -43,9 +41,17 @@
     
     column = 0
         
-    for i in content:
+    for c in content:
+        
+        if sys.version_info < (3, 0):
+            # Python 2.7
+            i = ord(c)
+        else:
+            # Python 3.x
+            i = c
+            
         if i < 32 or i >= 127 or i == ord('?'):
-            f.write('\\{0:03o}'.format(i))  # Octal formatting
+            f.write('\\{0:03o}'.format(i))
         elif i in [ ord('"'), ord('\\') ]:
             f.write('\\' + chr(i))
         else:
@@ -96,8 +102,17 @@
 
             with open(fullPath, 'rb') as source:
                 content = source.read()
-            
-            EncodeFileAsCString(g, variable, gzip.compress(content))
+
+            if sys.version_info < (3, 0):
+                # Python 2.7
+                fileobj = io.BytesIO()
+                gzip.GzipFile(fileobj=fileobj, mode='w').write(content)
+                compressed = fileobj.getvalue()
+            else:
+                # Python 3.x
+                compressed = gzip.compress(content)
+
+            EncodeFileAsCString(g, variable, compressed)
             WriteChecksum(g, variable + '_md5', content)
 
             index[relativePath] = variable