How to call for a script from withing another script

Asked by Eugene S

Hi,

There are many questions (and answers) about how to call a function from another script. However what I am interested in, is to start a standalone Sikuli script from within another script and wait until it's finished before proceeding.

For example, I have a full end to end sikuli script, which is saved under:
C:\SikuliX\tests\Scenario1.sikuli

and another script saved under:
C:\SikuliX\tests\Scenario2.sikuli

Scenario1 has to be executed prior to Scenario2, hence I want to call Scenario1.sikuli from within Scenario2 and continue only when the Scenario1 is finished. (Scenario 1 creates some data in files which I use during Scenario2).

I have tried to do something like that from within another script(this is using 1.0.0 but now I use 1.0.1):
os.system('call C:\Sikuli\sikuli-script.cmd -j6 -r C:\Sikuli\Tests\testFlows\Test.sikuli')

In this case, it doesn't throw any error but nothing happens, the execution just continues...

Is there any way to create a flow like that? Or maybe I'm missing something..

Thanks!
Eugene

Question information

Language:
English Edit question
Status:
Answered
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:

This question was reopened

Revision history for this message
RaiMan (raimund-hocke) said :
#1

import os
script = r"C:\Sikuli\Tests\testFlows\Test.sikuli"
save = getBundlePath()
setBundlePath(script)
execfile(os.path.join(script, "Test.py")
setBundlePath(save)

this works like just copying the code from Test.sikuli into current script at this place

Revision history for this message
RaiMan (raimund-hocke) said :
#3

at 1.
Just run the main (wrapping script) with --args (BTW: with 1.0.1+ -- is sufficient as delimiter for the trailing user args)
At time of exec file(), the executed script just has access to sys.argv the same way, as having been called directly.

You might even set new args :
sys.argv[n] = value # overwrite existing args
sys.argv.append(value) # add additional args to the end

at 2.
I will check after begin of September, wether there is some blocking when using subprocess this way.

You could do me a favour and report this part as a bug: [1.0.1] using subprocess seems to hang with click()

Revision history for this message
Eugene S (shragovich) said :
#4

Hi RaiMan,

Thanks you for the answers!

The arguments passed to the wrapper script were definitely available in the "called" script as well. Thanks for that tip!
However now I'm trying to figure out how can I terminate the "called" script and return to the "calling" script? If I just use "exit(0)", it will terminate all running scripts. So that doesn't help me.

P.S. I have opened a defect related to the subprocess issue:
https://bugs.launchpad.net/sikuli/+bug/1215218

Cheers!
Eugene

Revision history for this message
RaiMan (raimund-hocke) said :
#5

that is not possible as such with code "included" with execfile().

the possibilities:
--1. pack the execfile() in a try:except: and raise a specific exception (e.g. assert False at the place you want to "exit"). This will bring you to the except block

import os
script = r"C:\Sikuli\Tests\testFlows\Test.sikuli"
save = getBundlePath()
setBundlePath(script)
try:
    execfile(os.path.join(script, "Test.py")
except AssertionError:
    print "script has exited"
setBundlePath(save)

--2. pack the whole stuff into a def() (e.g. runTest() ) and use return , where you want to "exit"

import os
script = r"C:\Sikuli\Tests\testFlows\Test.sikuli"
save = getBundlePath()
setBundlePath(script)
execfile(os.path.join(script, "Test.py")
runTest()
setBundlePath(save)

when using option 2, then you could switch totally to using Sikuli's import feature. Then you would have as a convenience the automatic image path handling on top.

Revision history for this message
Eugene S (shragovich) said :
#6

Thank you RaiMan!

Revision history for this message
Eugene S (shragovich) said :
#7

Greetings RaiMan,

I have asked you this question a while ago however, unfortunately, I still couldn't get it working.

At the moment I have the line which I would usually run from cmd:
C:\SikuliX\runScript.cmd -r C:\SikuliX\Tests\testFlows\IAMTests\AD.sikuli

If I apply the solution you have proposed before, which is:

import os
script = r"C:\Sikuli\Tests\testFlows\Test.sikuli"
save = getBundlePath()
setBundlePath(script)
execfile(os.path.join(script, "Test.py")
setBundlePath(save)

I won't get the desired cmd like string.

So are there any other options I can try?

Thanks!
Eugene

Revision history for this message
RaiMan (raimund-hocke) said :
#8

If the "Call script" should do the same as the given command, it should be (with a slight modification to make it more flexible)

import os
test = "AD"
script = r"C:\SikuliX\Tests\testFlows\IAMTests\" + test + ".sikuli"
save = getBundlePath()
setBundlePath(script)
execfile(os.path.join(script, test +".py")
setBundlePath(save)

Can you help with this problem?

Provide an answer of your own, or ask Eugene S for more information if necessary.

To post a message you must log in.