How to hide the code of the script module?

Hello @jcfr,

I appreciate your suggested hints.

I’m wondering if your solution will work when generating the installer. Specifically, after creating the installer from Visual Studio and when a user installs slicer.exe, I want the installer directory to contain only .pyc files in the qt-scripted-directory.

I aim to automate this process to ensure that, in the end, the user only sees bytecode files. Is this achievable?

Yes, this solution will work when generating the installer: qt-scripted-modules folders will not contain any .py files.

1 Like

Great.

So, @jcfr and @lassoan, since this is my first time contributing to open source, like ctk, I will need your guidance throughout the process of making these changes.

Following @jcfr’s suggested hints, here’s what I did:

Firstly, I’ve used this version of ctk_compile_python_scripts.cmake.in. I’ve also defined an option at the top of the file ctkMacroCompilePythonScript.cmake:

# For default behavior
set(CTK_COMPILE_PYTHON_SCRIPT_SKIP_SCRIPT_COPY FALSE)

Then,

In ctkFunctionAddCompilePythonScriptTargets function , I’ve made the following changes:

if(NOT MY_GLOBAL_TARGET)
    ctkFunctionAddCompilePythonScriptTargets(${target} ${CTK_COMPILE_PYTHON_SCRIPT_SKIP_SCRIPT_COPY})
  endif()
function(ctkFunctionAddCompilePythonScriptTargets target SKIP_SCRIPT_COPY)
  if (NOT ${SKIP_SCRIPT_COPY})
    _ctk_add_copy_python_files_target(${target} Script ${ARGN})
  endif()
  _ctk_add_copy_python_files_target(${target} Resource ${ARGN})
  _ctk_add_compile_python_directories_target(${target})
endfunction()

and in Slicer/CmakeLists.txt:

# Create targets CopySlicerPython{Resource, Script}Files, CompileSlicerPythonFiles
if(Slicer_USE_PYTHONQT)
  slicerFunctionAddPythonQtResourcesTargets(SlicerPythonResources)
  ctkFunctionAddCompilePythonScriptTargets(
    ${CTK_COMPILE_PYTHON_SCRIPTS_GLOBAL_TARGET_NAME}
    ${CTK_COMPILE_PYTHON_SCRIPT_SKIP_SCRIPT_COPY}
    DEPENDS SlicerPythonResources
    )
....

Finally,

In SlicerConfig.cmake.in , I’m setting up the flag of CTK_COMPILE_PYTHON_SCRIPT_SKIP_SCRIPT_COPY to be TRUE :

set(CTK_COMPILE_PYTHON_SCRIPT_SKIP_SCRIPT_COPY TRUE)

If I understood correctly, when the `SKIP_SCRIPT_COPY` argument is set to TRUE, I don't think the compilation step will work, since `CompileSlicerPythonFiles` depends on `CopySlicerPythonScriptFiles` target. Is this correct, or did I make a mistake in the changes?

@jcfr, I’d like to provide an update on this. I’ve observed that all scripts are compiled as .pyc files except for the SegmentEditorEffects/__init__.py script.

@jcfr and @cpinter, What I have found is that the CMake file:

slicersources-src\Modules\Loadable\Segmentations\EditorEffects\Python\CMakeLists.txt

does not include an entry for the SegmentEditorEffects/__init__.py file when setting SegmentEditorEffects_PYTHON_SCRIPTS, but instead uses the template SegmentEditorEffects.__init__.py.in for configuration.

As per my understanding, we have two ways to handle the compilation for the __init__.py file:

  1. We can configure SegmentEditorEffects.__init__.py.in to be in the same directory slicersources-src\Modules\Loadable\Segmentations\EditorEffects\Python\ instead of the scripted modules directory and add an entry for it.
  2. Or we can completely replace the template file with the __init__.py file and add the entry directly, as I see there is nothing to be configurable in the template file.

What are your thoughts on this?




Also, following up the PR: https://github.com/commontk/CTK/pull/1192.

To completely support ‘.pyc’ for the scripted module SubjectHierarchy,
the file slicersources-src\Modules\Loadable\SubjectHierarchy\Widgets\Python\__init__.py should also be updated to check the file extention:

From:
if fileExtension == '.py' and fileNameNoExtension != '__init__'
To:
if fileExtension in ('.py', '.pyc') and fileNameNoExtension != '__init__'

@jcfr, Do we have any update for the PR?

Corresponding approach was revisited by instead introduce the option CTK_COMPILE_PYTHON_SCRIPT_KEEP_ONLY_PYC to support removing *.py script once the corresponding *.pyc file has been generated in the destination directory.

See https://github.com/commontk/CTK/pull/1192

@MJamal At your convenience, consider reviewing and testing the proposed changes :pray:

1 Like