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.