View Issue Details

IDProjectCategoryView StatusLast Update
0002039Slicer4Core: CLI infrastructurepublic2017-06-07 23:27
Reporterdomibel Assigned Tojcfr  
PrioritynormalSeverityminorReproducibilityhave not tried
Status closedResolutionfixed 
Product Version 
Target VersionSlicer 4.3.0Fixed in VersionSlicer 4.3.0 
Summary0002039: Support self-describing CLI
Description

Implementing CLIs in python is not supported.

From Dominique - May 14, 2010:

I am trying to implement a CLI module for Slicer4 written in python.
Unfortunately I don't see a way
how to load the script successfully into Slicer.

Here is a minimalistic example:

$ cat ~/Slicer4-SuperBuild-Debug/Slicer-build/lib/Slicer-4.1/cli-modules/minicli.py
#!/usr/bin/env python

print '<?xml version="1.0" encoding="utf-8"?>'
print '<executable>'
print '<title>A title</title>'
print '<description>A description</description>'
print ' <parameters>'
print ' myparameter'
print ' </parameters>'
print '</executable>'

It always fails with:

$ ./Slicer
Number of registered modules: 105
Register compression libraries
<?xml version="1.0" encoding="utf-8"?>
<executable>
<title>A title</title>
<description>A description</description>
<parameters>
myparameter
</parameters>
</executable>
RuntimeError: qSlicerScriptedLoadableModule::setPythonSource - Failed
to load scripted pythonqt module class definition minicli from
/home/domibel/Slicer4-SuperBuild-Debug/Slicer-build/lib/Slicer-4.1/cli-modules/minicli.py
Fail to instantiate module "minicli"
Number of instantiated modules: 104

TagsNo tags attached.

Activities

jcfr

jcfr

2012-05-14 12:59

administrator   ~0004383

Last edited: 2012-05-14 13:01

Within Slicer, the associated XML used to be displayed using "print" statement.

In Slicer4, instead we should probably keep the description of such module in its own separate XML file and assume its available in the same directory. This will be consistent with how module data are specified.

domibel

domibel

2012-05-14 15:21

developer   ~0004385

Please support self-describing CLIs e.g.

mytool --xml

will returns a valid xml document which defines how to call the CLI.

pieper

pieper

2012-05-15 03:48

administrator   ~0004392

+1 to the request to preserve self-describing CLI modules written in any language. It is klunky to require a second .xml file IMO.

jcfr

jcfr

2012-05-15 03:58

administrator   ~0004393

Just to clarify, there are two concepts here:
1) Self describing module
2) Best practice to write a python module

The intent of this issue (0002039) is to address "self-describing" module.

Best practice could be discussed independently :)

pieper

pieper

2012-05-15 09:46

administrator   ~0004403

Hi Dominique -

I took a look - I think the reason this didn't work for you is that slicer4 is looking at the .py extension and trying to load this as a scripted module. Removing the extension and fixing up the xml results in a python script that looks to slicer like a cli.

I'll put more notes in the bug report.

Best,
-Steve

2012-05-15 09:46

 

mini (262 bytes)
mini (262 bytes)
pieper

pieper

2012-05-15 09:49

administrator   ~0004404

If you make the attached file 'mini' executable and put it in the module path then slicer will recognize it as a cli.

I made this with something like:

./lib/Slicer4.1/CLIModuleTest --xml > mini

and then editing the result to be a python script (I may have the name of the source module wrong).

To make this work on windows we should be able to pick another extension, say .cli, and bind that to be opened with the python interpreter but I have not tried that -- I'm guessing that Dominique's use case is on Debian anyway ;)

domibel

domibel

2012-05-15 16:12

developer   ~0004424

Thanks, but this didn't work on my side. Can you send me your working 'mini' script. Here is what I did:

./lib/Slicer-4.1/cli-modules/CLIModule4Test --xml > mini
#make it a python script with proper output
chmod 755 mini
mv mini ./lib/Slicer-4.1/cli-modules/
./Slicer

Error output is

Error(s):
CLI executable: /home/domibel/Slicer4-SuperBuild-Debug/Slicer-build/lib/Slicer-4.1/cli-modules/mini
ImportError: No module named site

CLI executable: /home/domibel/Slicer4-SuperBuild-Debug/Slicer-build/lib/Slicer-4.1/cli-modules/mini
Failed to retrieve Xml Description

Fail to instantiate module "mini"

pieper

pieper

2012-05-16 05:05

administrator   ~0004431

Hi Dominique -

See the file 'mini' attached to this report - sorry I wasn't clear about that.

-Steve

2012-05-16 05:20

 

minicli (2,156 bytes)
minicli (2,156 bytes)
pieper

pieper

2012-05-16 05:20

administrator   ~0004432

Ah - I see now I attached your original file as 'mini'. See the newly attached file 'minicli' for a corrected version.

domibel

domibel

2012-05-16 19:56

developer   ~0004449

Hi Steve,

I tried this again with your example, but I am still getting the same error.

Are you able to load your example cli into Slicer4? Where in the module list does it appear? We have to be careful with that example, there is another 'Add Scalar Volumes' module with the same name in the list. But even after changing the name the new module doesn't show up in the module list.

-Dominique

pieper

pieper

2012-05-17 02:38

administrator   ~0004450

Hi Dominique -

Yes, it works for me - did you put the file in the cli-modules directory? It's acting as an executable not as a script.

You'll know when it's loaded because it defines a custom category:

<category>PYTHON TEST CLI</category>

You can also try the --verbose-module-discovery option to see what's going on with it.

-Steve

domibel

domibel

2012-05-18 19:30

developer   ~0004507

This is what I get:

./Slicer-build/Slicer --verbose-module-discovery

[...]
Attempt to instantiate "Welcome" ..................................... [OK]
Attempt to instantiate "minicli" ..................................... [Failed]
Error(s):
CLI executable: /home/domibel/Slicer4-SuperBuild-Debug/Slicer-build/lib/Slicer-4.1/cli-modules/minicli
ImportError: No module named site

CLI executable: /home/domibel/Slicer4-SuperBuild-Debug/Slicer-build/lib/Slicer-4.1/cli-modules/minicli
Failed to retrieve Xml Description

Fail to instantiate module "minicli"
[...]

This is similar to http://na-mic.org/Bug/view.php?id=1848

I used Ubuntu 12.04 for this test. I guess I have to switch to a different version. What is considered as Slicer's reference OS?

pieper

pieper

2012-05-18 19:46

administrator   ~0004508

Hi Dominique -

Can you run minicli at a regular terminal? I believe it should be using standard python startup conventions.

It runs fine on my ubuntu 10.10 machine (although I understand that I'm a dinosaur for still running that version).

-Steve

domibel

domibel

2012-05-18 19:52

developer   ~0004509

Hi Steve,

minicli's permissions are set to 755.
I can call it in a terminal without problems. I assume Slicer is resetting a python path or something similar.

I will try to test it on Debian testing and on Ubuntu 10.04 (and maybe 10.10) over the weekend.

Thanks
-Dominique

jcfr

jcfr

2013-02-04 12:28

administrator   ~0007813

Last edited: 2013-02-04 12:29

Add support for CLI having ".bat" extension.
See http://viewvc.slicer.org/viewvc.cgi/Slicer4?view=revision&amp;revision=21662

pieper

pieper

2013-02-04 13:17

administrator   ~0007814

Thanks for adding the note Jc - you are too fast for me :)

Also note that with this extra output helps with debugging modules:

http://viewvc.slicer.org/viewvc.cgi/Slicer4?view=revision&amp;revision=21663

pieper

pieper

2013-02-04 13:24

administrator   ~0007815

Last edited: 2013-02-04 13:45

The following process worked for me to make a python cli that works on both windows and mac - this could used for developers who want to make a python wrapper around their matlab code.

First, I made a .bat file run the python with the following contents:

"c:\Program Files\Slicer 4.2.0-2012-11-30\Slicer.exe" --no-main-window --disable-cli-modules --disable-loadable-modules --disable-scripted-modules --python-script minicli

Remarks:
(1) Any python interpreter could be used, but slicer is maybe a good idea, since it is going to be available on all installations.

Then I made the actual python script (minicli in this case) be executable (chmod a+x minicli) so it will be runable on linux/mac, but without the .py extension so it won't be treated as a loadable scripted module.

For now, this just exits, but it could parse the passed arguments and invoke matlab with them.

The contents of minicli are:

#!/usr/bin/env python
xml = """<?xml version="1.0" encoding="utf-8"?>
<executable>
<category>PYTHON TEST CLI</category>
<title>PYTHON TEST Add Scalar Volumes</title>
<description><![CDATA[Adds two images. Although all image types are supported on input, only signed types are produced. The two images do not have to have the same dimensions.]]></description>
<version>0.1.0.$Revision: 19608 $(alpha)</version>
<documentation-url>http://slicer.org/slicerWiki/index.php/Documentation/4.1/Modules/Add&lt;/documentation-url>
<license/>
<contributor>Bill Lorensen (GE)</contributor>
<acknowledgements><![CDATA[This work is part of the National Alliance for Medical Image Computing (NAMIC), funded by the National Institutes of Health through the NIH Roadmap for Medical Research, Grant U54 EB005149.]]></acknowledgements>
<parameters>
<label>IO</label>
<description><![CDATA[Input/output parameters]]></description>
<image>
<name>inputVolume1</name>
<label>Input Volume 1</label>
<channel>input</channel>
<index>0</index>
<description><![CDATA[Input volume 1]]></description>
</image>
<image>
<name>inputVolume2</name>
<label>Input Volume 2</label>
<channel>input</channel>
<index>1</index>
<description><![CDATA[Input volume 2]]></description>
</image>
<image>
<name>outputVolume</name>
<label>Output Volume</label>
<channel>output</channel>
<index>2</index>
<description><![CDATA[Volume1 + Volume2]]></description>
</image>
</parameters>
<parameters>
<label>Controls</label>
<description><![CDATA[Control how the module operates]]></description>
<integer-enumeration>
<name>order</name>
<label>Interpolation order</label>
<default>1</default>
<element>0</element>
<element>1</element>
<element>2</element>
<element>3</element>
<longflag>order</longflag>
<description><![CDATA[Interpolation order if two images are in different coordinate frames or have different sampling.]]></description>
</integer-enumeration>
</parameters>
</executable>
"""

print(xml)
exit()

lassoan

lassoan

2013-06-27 07:34

developer   ~0008812

A solution is available now, so the issue can be closed. See details below.

I confirm that the solution suggested above (0007815) works most of the time. When it fails it is due to the slow starting of Slicer.exe and the 5sec timeout in the CLI engine. I ended up using a .bat file to return the xml file contents immediately:

@set MODULE_NAME=test
@echo off
if "%1"=="--xml" goto :print_xml
"%SLICER_HOME%/Slicer.exe" --launcher-no-splash --python-script %MODULE_NAME% %*
if errorlevel 1 exit /b 1
rem Return with success
exit /b 0
rem Print CLI descriptor XML
:print_xml
type %MODULE_NAME%.xml
rem Return with success
exit /b 0

Similar launcher can be used on linux/mac (this script has not been tested):

#!/bin/sh

If this proxy is called with the --xml parameter then just return the module decriptor XML file,

otherwise run the associated module script.

MODULE_NAME="test"
if [ $1 = "--xml" ]; then
cat $MODULE_NAME.xml
exit 0
fi
"$SLICER_HOME/Slicer" --launcher-no-splash --python-script $MODULE_NAME $*
exit $?

jcfr

jcfr

2014-03-06 04:56

administrator   ~0010815

Closing resolved issues that have not been updated in more than 3 months

Related Changesets

Slicer: 2145-support-for-installing-extension-from-file 705aee4f

2013-02-04 17:16:23

pieper

Details Diff
ENH: allow .bat files to be used as command line modules (issue 0002039)

This way people can write simple wrapper scripts on windows
for other executable types, such as matlab scripts.

git-svn-id: http://svn.slicer.org/Slicer4/trunk@21662 3bd1e089-480b-0410-8dfb-8563597acbee
mod - Base/QTCore/qSlicerUtils.cxx Diff File

Issue History

Date Modified Username Field Change
2012-05-14 12:59 jcfr New Issue
2012-05-14 12:59 jcfr Reporter jcfr => domibel
2012-05-14 12:59 jcfr Description Updated
2012-05-14 12:59 jcfr Note Added: 0004383
2012-05-14 13:00 jcfr Description Updated
2012-05-14 13:01 jcfr Note Edited: 0004383
2012-05-14 15:21 domibel Note Added: 0004385
2012-05-14 15:51 jcfr Assigned To => jcfr
2012-05-14 15:51 jcfr Status new => assigned
2012-05-14 15:51 jcfr Summary Support scripted CLI => Support self-describing CLI
2012-05-14 15:52 jcfr Target Version => Slicer 4.3.0
2012-05-15 03:48 pieper Note Added: 0004392
2012-05-15 03:58 jcfr Note Added: 0004393
2012-05-15 09:46 pieper Note Added: 0004403
2012-05-15 09:46 pieper File Added: mini
2012-05-15 09:49 pieper Note Added: 0004404
2012-05-15 16:12 domibel Note Added: 0004424
2012-05-16 05:05 pieper Note Added: 0004431
2012-05-16 05:20 pieper File Added: minicli
2012-05-16 05:20 pieper Note Added: 0004432
2012-05-16 19:56 domibel Note Added: 0004449
2012-05-17 02:38 pieper Note Added: 0004450
2012-05-18 19:30 domibel Note Added: 0004507
2012-05-18 19:46 pieper Note Added: 0004508
2012-05-18 19:52 domibel Note Added: 0004509
2013-02-04 12:28 jcfr Note Added: 0007813
2013-02-04 12:29 jcfr Note Edited: 0007813
2013-02-04 13:17 pieper Note Added: 0007814
2013-02-04 13:24 pieper Note Added: 0007815
2013-02-04 13:39 jcfr Note Edited: 0007815
2013-02-04 13:40 jcfr Note Edited: 0007815
2013-02-04 13:41 jcfr Note Edited: 0007815
2013-02-04 13:45 jcfr Note Edited: 0007815
2013-06-27 07:34 lassoan Note Added: 0008812
2013-06-27 07:34 lassoan Status assigned => resolved
2013-06-27 07:34 lassoan Resolution open => fixed
2013-07-23 21:02 jcfr Fixed in Version => Slicer 4.3.0
2014-03-06 04:56 jcfr Note Added: 0010815
2014-03-06 04:58 jcfr Status resolved => closed
2017-06-07 23:27 pieper Changeset attached => Slicer 2145-support-for-installing-extension-from-file 705aee4f