Author Topic: PJSR creating subprocess  (Read 9197 times)

Offline georg.viehoever

  • PTeam Member
  • PixInsight Jedi Master
  • ******
  • Posts: 2129
    • View Profile
PJSR creating subprocess
« on: 2010 April 02 03:31:42 »
Hi,

I would like to call another program/a subprocess (e.g. a bash shell script) from within a PJSR script. Is this possible? (in UNIX, this would be system(), in python subprocess.popen(), in tcl exec ..., etc.) . This would be really handy, for example to call file converters from within PI.

Georg
Georg (6 inch Newton, unmodified Canon EOS40D+80D, unguided EQ5 mount)

Offline georg.viehoever

  • PTeam Member
  • PixInsight Jedi Master
  • ******
  • Posts: 2129
    • View Profile
PCL module for subprocesses, was Re: PJSR creating subprocess
« Reply #1 on: 2010 April 04 15:44:57 »
Hi,
since it appears that there is no such thing like a system command in PJSR, I have written a small PCL that allows to run shell commands from within PI. It just launches the command (for example some file converter, or a browser), and waits until the command returns. stdOut/stdErr are going to your terminal (not the PI-Console). SystemCommand considers an exit-code !=0 to indicate an error.

How to use: after installing the module, you can use it in 4 modes:
- Console Window: SystemCommand ls # launches the UNIX ls command
- PJSR scripts:      
      var pSystemCommand=new SystemCommand;
      with (pSystemCommand) {
         commandLine=sCommandLine;
      }
      pSystemCommand.executeGlobal();
- GUI: It can be found under Process/<etc>/SystemCommand. See Screenshot below.
- C++: Not for the faint of heart...

Currently this module works only on Linux-x64 (tested with Fedora11, but I don't expect difficulties with other Linuxes or FreeBSD), with the right makefiles (that are not difficult to create) it should also work on Linux-x32 or MacOS-X. It is probably a bit more difficult to get it running on Windows... . Maybe someone finds the time to convert it from UNIX's system() command to Qt's
QProcess (which makes it usable on Windows too), and also redirects stdOut/stdErr to the PI console. For my current purposes, it works fine...

Georg

PS: More recent version (that in theory also runs on Windows) also need the Qt4 development files for compilation. On Fedora 11, you can install them using the System>Administration>Add/Remove Software tool. Search for "qt-devel". The description of the package reads "Development Files for the Qt Toolkit". On Fedora 11, the package currently has the version "qt-devel-1:4.6.2-14.fc11(x86_64)". While the exact version is not critical, you need to make sure to install a QT4 package (qt-devel-1:4.6.2-14.fc11(x86_64)) for your architecure (here qt-devel-1:4.6.2-14.fc11(x86_64)).
« Last Edit: 2010 May 08 08:15:24 by georg.viehoever »
Georg (6 inch Newton, unmodified Canon EOS40D+80D, unguided EQ5 mount)

Offline georg.viehoever

  • PTeam Member
  • PixInsight Jedi Master
  • ******
  • Posts: 2129
    • View Profile
Re: PJSR creating subprocess
« Reply #2 on: 2010 April 10 01:03:08 »
Hi,

attached is an improved version of this module. New features:

- output of the executed command now goes to the PI console window (instead of the terminal where PI was started).
- it is possible to specify a "maxTime", after which the child process will be terminated. This is handy when you accidentially started a non-terminating process. Default maxTime is 100 seconds.
- The module should compile on all platforms including Windows, since I replaced the UNIX specific calls with equivalent ones from Qt. There still is only a Makefile for Linux-x64 (that the only system where I can test), but creating makefiles for the other platforms should not be an issue
- The module has been ported to the recently released PI 1.60.

Enjoy,

Georg
 
Georg (6 inch Newton, unmodified Canon EOS40D+80D, unguided EQ5 mount)

Offline Juan Conejero

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 6688
    • View Profile
    • http://pixinsight.com/
Re: PJSR creating subprocess
« Reply #3 on: 2010 April 10 13:41:48 »
Hi Georg,

Nice work! Welcome to PCL development!  :)

Note that a JavaScript script can run a command using the Console.execute method:

Code: [Select]
void Console.execute( String command )
However, this method cannot be used by default because the default security settings policy forbids it. See Edit > Security Settings > Allow execution of commands from JavaScript scripts.

Allowing execution of commands from a script is extremely dangerous --there is no limit on what a script can do to your system if you enable this option. However, it can be extremely useful, too :)

A script can know whether this security option is enabled before trying to execute a command:

Code: [Select]
Object Settings.readGlobal( String key, int dataType )
In your case you should use something similar to:

Code: [Select]
var commandsAllowed = Settings.readGlobal( "Security/AllowJavaScriptCommandExecution", DataType_Boolean );
and issue a warning message to inform the user that your script cannot run commands due to current security settings.

Hope this helps.

Juan Conejero
PixInsight Development Team
http://pixinsight.com/

Offline georg.viehoever

  • PTeam Member
  • PixInsight Jedi Master
  • ******
  • Posts: 2129
    • View Profile
Re: PJSR creating subprocess
« Reply #4 on: 2010 April 19 11:26:51 »
Hi Juan,

thanks for pointing me to Console.execute(). Had I found this earlier, I probably would not have started using PCL... so you are kind of lucky  ;)
Regardining the security concerns: I dont think this is much of an issue here. In this case, JavaScript or the PCL module are not running as part of a Webpage, where the ability to start external processes would indeed call for problems.

PCL modules and PJSR scripts are always executed in response to deliberate actions triggered by the user. So there should not be any problems as long as nobody starts distributing rogue PCL modules or PJSR scripts. After all, many other programs allow to start external processes in response to user actions, for example editors like emacs or shells like bash.

BTW: For the moment I will continue with my SystemCommand module. It allows to cancel an external process after a specified amount of elapsed time, which is handy for debugging. I still struggle to teach it to abort on user request (see http://pixinsight.com/forum/index.php?topic=1849.0), and I would like to return the console output as a method result or property (see http://pixinsight.com/forum/index.php?topic=1819.0). I believe these functionalites would not be possible with Console.execute().

Any chance to make SystemCommand a standard PCL module in future releases?

Georg

Georg (6 inch Newton, unmodified Canon EOS40D+80D, unguided EQ5 mount)

Offline georg.viehoever

  • PTeam Member
  • PixInsight Jedi Master
  • ******
  • Posts: 2129
    • View Profile
Re: PJSR creating subprocess
« Reply #5 on: 2010 April 19 11:35:47 »
Hi,

here is updated version of the module, with bugfixes and some steps towards new functionality (not yet available). Regarding security concerns and possible future functionalities, please see the previous message.

Georg
Georg (6 inch Newton, unmodified Canon EOS40D+80D, unguided EQ5 mount)

Offline georg.viehoever

  • PTeam Member
  • PixInsight Jedi Master
  • ******
  • Posts: 2129
    • View Profile
Re: PJSR creating subprocess
« Reply #6 on: 2010 April 24 04:18:47 »
Hi,

no additional changes to this tool (did not have time to resolve the remaining kinks). Here just some hints on how to compile and install:

- Standard Linux tools plus a PI installation are sufficient.
- Modify your environment to point to your source and PI directories. In my case, I added this to ~/.bashrc:
Code: [Select]
export PCLDIR=$HOME/Pixinsight
export PCLSRCDIR=$HOME/tmp
export PATH=$PCLDIR/bin:$PATH
export PCLINCDIR=$PCLDIR/include
export PCLLIBDIR32=$PCLDIR/lib/x86
export PCLLIBDIR64=$PCLDIR/lib/x86_64
export PCLBINDIR32=$PCLDIR/bin
export PCLBINDIR64=$PCLDIR/bin
- change into the compilation directory, in my case:
Code: [Select]
cd ~/tmp/modules/processes/SystemCommand/linux/g++/
- Compile with
Code: [Select]
make
- Start PixInsight.sh, and add the module via Process>Modules>Install Modules ...
- In the Processing Cosole, you should now be able to run "ls" via
Code: [Select]
SystemCommand ls

SystemCommand: Global context
Running command (see terminal for ouput): ls
Running with maxTime=100
Sandbox
SystemCommand
SystemCommand_1.59
Did run (see terminal for ouput): ls
0.267 s

- Porting to other platforms: SystemCommand is based on the Sandbox module, available in $PCLDIR/src/modules/processes/Sandbox/, and does not use any platform specific constructs. Porting should essentially mean to create the necessary Makefiles. Get inspiration from Sandbox or the existring Linux64 makefile.

Georg
Georg (6 inch Newton, unmodified Canon EOS40D+80D, unguided EQ5 mount)

Offline Carlos Milovic

  • PTeam Member
  • PixInsight Jedi Master
  • ******
  • Posts: 2173
  • Join the dark side... we have cookies
    • View Profile
    • http://www.astrophoto.cl
Re: PJSR creating subprocess
« Reply #7 on: 2010 April 24 15:07:22 »
Also, remember the Makefile script, under Development category. It creates a MSVC project file, which allows to compile PCL modules in windows.
Regards,

Carlos Milovic F.
--------------------------------
PixInsight Project Developer
http://www.pixinsight.com

Offline vicent_peris

  • PTeam Member
  • PixInsight Padawan
  • ****
  • Posts: 984
    • View Profile
    • http://www.astrofoto.es/
Re: PJSR creating subprocess
« Reply #8 on: 2010 April 29 10:48:24 »
- Compile with
Code: [Select]
[/quote]

Hi Georg,

I have a problem here. It doesn't compiles at all! I get this error:

[code]
En el fichero incluído de ../../SystemCommandInterface.cpp:22:
../../SystemCommandInterface.h:25:34: aviso: pcl/ProcessInterface.h: No existe el fichero o el directorio
../../SystemCommandInterface.h:27:23: aviso: pcl/Sizer.h: No existe el fichero o el directorio
../../SystemCommandInterface.h:28:23: aviso: pcl/Label.h: No existe el fichero o el directorio
../../SystemCommandInterface.h:29:22: aviso: pcl/Edit.h: No existe el fichero o el directorio
../../SystemCommandInterface.h:30:25: aviso: pcl/SpinBox.h: No existe el fichero o el directorio
En el fichero incluído de ../../SystemCommandInterface.h:33,
                 de ../../SystemCommandInterface.cpp:22:
../../SystemCommandInstance.h:25:39: aviso: pcl/ProcessImplementation.h: No existe el fichero o el directorio
../../SystemCommandInstance.h:26:57: aviso: pcl/MetaParameter.h: No existe el fichero o el directorio
En el fichero incluído de ../../SystemCommandInterface.cpp:23:
../../SystemCommandProcess.h:25:29: aviso: pcl/MetaProcess.h: No existe el fichero o el directorio

I reviewed the file locations and all is correct, but the make command doesn't find the files.  ???

After these errors, you get another bunch of errors...

What's happening?


Thanks!
Vicent.[/code]

Offline Juan Conejero

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 6688
    • View Profile
    • http://pixinsight.com/
Re: PJSR creating subprocess
« Reply #9 on: 2010 April 29 11:19:31 »
Hi Vicent,

That looks like you haven't defined the correct environment variables.

What does your console say when you enter this command:

Code: [Select]
echo $PCLINCDIR
« Last Edit: 2010 April 29 11:46:57 by Juan Conejero »
Juan Conejero
PixInsight Development Team
http://pixinsight.com/

Offline georg.viehoever

  • PTeam Member
  • PixInsight Jedi Master
  • ******
  • Posts: 2129
    • View Profile
Re: PJSR creating subprocess
« Reply #10 on: 2010 April 29 12:45:48 »
...
Code: [Select]
echo $PCLINCDIR...
Juan's analysis is probably corrrect. If it is not, please send the output of the following shell command:
Code: [Select]
env|grep PCL

Georg
Georg (6 inch Newton, unmodified Canon EOS40D+80D, unguided EQ5 mount)

Offline vicent_peris

  • PTeam Member
  • PixInsight Padawan
  • ****
  • Posts: 984
    • View Profile
    • http://www.astrofoto.es/
Re: PJSR creating subprocess
« Reply #11 on: 2010 April 30 04:51:03 »
Hi Vicent,

That looks like you haven't defined the correct environment variables.

What does your console say when you enter this command:

Code: [Select]
echo $PCLINCDIR

This command returns:

Code: [Select]
/home/vperis/Pixinsight/include

Juan's analysis is probably corrrect. If it is not, please send the output of the following shell command:
Code: [Select]
env|grep PCL

Georg


Code: [Select]
And this one says:
PCLBINDIR32=/home/vperis/Pixinsight/bin
PCLDIR=/home/vperis/Pixinsight
PCLLIBDIR64=/home/vperis/Pixinsight/lib/x86_64
PCLSRCDIR=/home/vperis/Pixinsight/src
PCLLIBDIR32=/home/vperis/Pixinsight/lib/x86
PCLBINDIR64=/home/vperis/Pixinsight/bin
PCLINCDIR=/home/vperis/Pixinsight/include


Thanks,
V.

Offline georg.viehoever

  • PTeam Member
  • PixInsight Jedi Master
  • ******
  • Posts: 2129
    • View Profile
Re: PJSR creating subprocess
« Reply #12 on: 2010 April 30 04:56:59 »
What is the contents of directory /home/vperis/Pixinsight/include? Does it include files such as pcl/ProcessInterface.h ? If not: You may need to install the PCL version of Pixinsight (which is a superset of PI that includes the development sources such as headers), not only the pure PI executable version. Juan always releases both variants.

Georg
Georg (6 inch Newton, unmodified Canon EOS40D+80D, unguided EQ5 mount)

Offline vicent_peris

  • PTeam Member
  • PixInsight Padawan
  • ****
  • Posts: 984
    • View Profile
    • http://www.astrofoto.es/
Re: PJSR creating subprocess
« Reply #13 on: 2010 April 30 05:20:25 »
Yes, I've installed the full PCL, of course. I don't know why this happens... if I put in .bashrc "$HOME/Pixinsight/bin" instead of "/home/vperis/Pixinsight/bin", then make doesn't find the files. So make now finds the files, but then I get these errors:

Code: [Select]
../../SystemCommandInstance.cpp:24:20: aviso: QProcess: No existe el fichero o el directorio
../../SystemCommandInstance.cpp:25:17: aviso: QTime: No existe el fichero o el directorio
../../SystemCommandInstance.cpp: In member function ‘virtual bool pcl::SystemCommandInstance::ExecuteGlobal()’:
../../SystemCommandInstance.cpp:81: error: ‘QProcess’ no se declaró en este ámbito
../../SystemCommandInstance.cpp:81: error: expected `;' before ‘systemProcess’
../../SystemCommandInstance.cpp:83: error: ‘systemProcess’ no se declaró en este ámbito
../../SystemCommandInstance.cpp:83: error: ‘QProcess’ is not a class or namespace
../../SystemCommandInstance.cpp:85: error: ‘QString’ no se declaró en este ámbito
../../SystemCommandInstance.cpp:91: error: ‘QTime’ no se declaró en este ámbito
../../SystemCommandInstance.cpp:91: error: expected `;' before ‘timer’
../../SystemCommandInstance.cpp:92: error: ‘timer’ no se declaró en este ámbito
../../SystemCommandInstance.cpp:95: error: ‘QProcess’ is not a class or namespace
../../SystemCommandInstance.cpp:98: error: ‘QByteArray’ no se declaró en este ámbito
../../SystemCommandInstance.cpp:98: error: expected `;' before ‘outputString’
../../SystemCommandInstance.cpp:99: error: ‘outputString’ no se declaró en este ámbito
../../SystemCommandInstance.cpp:112: error: ‘QByteArray’ no se declaró en este ámbito
../../SystemCommandInstance.cpp:112: error: expected `;' before ‘outputString’
../../SystemCommandInstance.cpp:113: error: ‘outputString’ no se declaró en este ámbito
make[1]: *** [x86_64/Release/SystemCommandInstance.o] Error 1
make: *** [all] Error 2

What's happening??


V.


Offline vicent_peris

  • PTeam Member
  • PixInsight Padawan
  • ****
  • Posts: 984
    • View Profile
    • http://www.astrofoto.es/
Re: PJSR creating subprocess
« Reply #14 on: 2010 April 30 05:32:16 »
And now I get another error:

When I launch PixInsight, the main app doesn't find any of the modules! I must then install them manually.


V.