Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 50 - User defined exit codes #278

Merged
merged 2 commits into from
Feb 18, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions docs/api/phantom.rst
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,7 @@ exit(code)

It stops the script and SlimerJS exit.

To be compatible with PhantomJS, it accepts an exit code but it is ignored
because of a limitation in Firefox/XulRunner. This code is optional.
It accepts an optional exit code. Default is 0.

.. code-block:: javascript

Expand Down
3 changes: 1 addition & 2 deletions docs/api/slimer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ exit()

It stops the script and SlimerJS exit.

It accepts an optional exit code but it is ignored
because of a limitation in Firefox/XulRunner.
It accepts an optional exit code. Default is 0.

.. code-block:: javascript

Expand Down
1 change: 0 additions & 1 deletion docs/differences-with-phantomjs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ In the API
sandbox
- Modules must be files, not folders. ``node_modules`` folders are not
searched specially (SlimerJS provides ``require.paths``).
- Exit code is ignored with ``phantom.exit()``. There is no API for that in Gecko
- ``phantom.exit()`` or ``slimer.exit()`` is done asynchronously. Your script may continue after
the call of these methods. You can use ``slimer.isExiting()`` to control your processing.
- The callback ``webpage.onNavigationRequest`` receives bad parameters.
Expand Down
6 changes: 2 additions & 4 deletions src/modules/phantom.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -100,18 +100,16 @@ var phantom = {
/**
* quit the application.
*
* The given exit code is not supported because there is no way
* in Mozilla to return this code after the shutdown of the application.
*
* @param integer code the exit code for the shell console. 0 means ok (default value)
* @phantomcompatibilityissue
* @internal to resolve the issue, we should provide our own patched xulrunner
*/
exit : function(code) {
let c = code || 0;
let c = +code || 0;
if (slLauncher.slimerExiting) {
return
}
slUtils.writeExitStatus(c);
slLauncher.slimerExiting = true;
Services.startup.quit(Components.interfaces.nsIAppStartup.eForceQuit);
},
Expand Down
21 changes: 20 additions & 1 deletion src/modules/slUtils.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const scriptableStream = Cc["@mozilla.org/scriptableinputstream;1"].getService(C

Cu.import("resource://gre/modules/Services.jsm");


var slUtils = {};

const isWin = (Services.dirsvc.get("CurWorkD", Ci.nsIFile) instanceof Ci.nsILocalFileWin);
Expand Down Expand Up @@ -174,6 +173,26 @@ slUtils.readChromeFile = function readChromeFile(url) {
return str;
}

/**
* Writes exit status code in `ProfileDir/exitstatus` file.
* Note:
* We must follow the evolution of xulrunner in order to remove this patch.
* If they add API for user defined exit status of xulrunner, we must use it instead of this patch.
*/
slUtils.writeExitStatus = function (status) {
let str = String(status);
let foStream = Cc["@mozilla.org/network/file-output-stream;1"].createInstance(Ci.nsIFileOutputStream);
let file = Services.dirsvc.get("ProfD", Ci.nsIFile);
file.append("exitstatus");
foStream.init(file, (0x02 | 0x08 | 0x20), parseInt("0444", 8), 0);
try {
foStream.write(str, str.length);
}
finally {
foStream.close();
}
}

slUtils.getWebpageFromContentWindow = function getWebpageFromContentWindow(contentWin) {
let browser = slUtils.getBrowserFromContentWindow(contentWin);
if (browser) {
Expand Down
6 changes: 2 additions & 4 deletions src/modules/slimer.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,16 @@ var slimer = {
/**
* quit the application.
*
* The given exit code is not supported because there is no way
* in Mozilla to return this code after the shutdown of the application.
*
* @param integer code the exit code for the shell console. 0 means ok (default value)
* @phantomcompatibilityissue
* @internal to resolve the issue, we should provide our own patched xulrunner
*/
exit : function(code) {
let c = code || 0;
let c = +code || 0;
if (slLauncher.slimerExiting) {
return
}
slUtils.writeExitStatus(c);
slLauncher.slimerExiting = true;
Services.startup.quit(Components.interfaces.nsIAppStartup.eForceQuit);
},
Expand Down
6 changes: 5 additions & 1 deletion src/slimerjs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,11 @@ else
"$SLIMERJSLAUNCHER" -app "$SLIMERDIR/application.ini" $PROFILE -no-remote "$@"
fi

EXITCODE=$?
EXITCODE=0
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By doing this, we will not know when there is a problem with Gecko. I think it is better to keep the "official" exit status code, and read the exitstatus file only if EXITCODE is 0.

EXITFILE="$PROFILE_DIR/exitstatus"
if [ -f $EXITFILE ]; then
EXITCODE=`cat $EXITFILE`
fi

if [ "$PROFILE_DIR" != "" ]; then
rm -rf $PROFILE_DIR
Expand Down
11 changes: 8 additions & 3 deletions src/slimerjs.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,22 +174,27 @@ def showHelp():
SLCMD.extend(PROFILE)
SLCMD.extend(SYS_ARGS)

exitCode = 0
try:
if HIDE_ERRORS:
try:
from subprocess import DEVNULL # py3k
except ImportError:
DEVNULL = open(os.devnull, 'wb')

exitCode = subprocess.call(SLCMD, stderr=DEVNULL)
subprocess.call(SLCMD, stderr=DEVNULL)
else:
exitCode = subprocess.call(SLCMD)
subprocess.call(SLCMD)

except OSError as err:
print('Fatal: %s. Are you sure %s exists?' % (err, SLIMERJSLAUNCHER))
sys.exit(1)

exitCode = 0
exitFile = PROFILE_DIR + '/exitstatus'
if os.path.isfile(exitFile):
with open(exitFile, 'r') as f:
exitCode = int(f.read())

if CREATE_TEMP:
shutil.rmtree(PROFILE_DIR)

Expand Down