[wpkg-users] Capture of StdOut and StdErr of exec command

Jean Le Bail jean.lebail at etab.ac-caen.fr
Sun Apr 22 13:54:20 CEST 2007


Hi,

Thank you for your software WPKG !

I have modified wpkg.js to handle stdout and stderr of exec command.

This patch solves the problem mentioned at :
http://wpkg.org/index.php/Packages.xml
"When the ouput of a command (think of: copy,cacls) is too much, the command will fail. You have to redirect the output to NUL or a file (example: "%command% >nul")."

Moreover, when wpkg.js runs with /debug or /verbose option,
messages from the exec command, if any, are displayed.

Here is the modified 'function exec(cmd, timeout)' and two new functions : 
function readStd(se)
function displayStd(sStdOut, sStdErr)

Feel free to include this patch within your next release.

Regards, Jean

----------------- part of wpkg.js -----------------
/**
 * Executes a shell command and blocks until it is completed, returns the
 * program's exit code. Command times out and is terminated after the
 * specified number of seconds.
 */
function exec(cmd, timeout) {

    if (dryrun) {
        return 0;
    }

    try {
        var shell = new ActiveXObject("WScript.Shell");
        
        // timeout after an hour by default
        if (timeout == 0) {
            timeout = 3600;
        }
        
        var shellExec = shell.exec(cmd);
        
        // var count = 0;
        // while (shellExec.status == 0) {
        //     WScript.sleep(1000);
        //     count++;
        //     
        //     if (count >= timeout) {
        //         // return -1;
        //     }
        // }
        // WScript.sleep(1000);
        
        var now = new Date();
        var watchdog = now.getTime() + timeout*1000;
        var count = 0;
        var end = false;
        while ( ( ! end ) && (now < watchdog) ) {
            if ( ! readStd(shellExec) ) {
                WScript.sleep(300);
                if (shellExec.status != 0) {
                    if (count < 10) {
                        // Nothing to read, 3 seconds after execution ended
                        end = true;
                    }
                    count++;
                }
            } else {
                WScript.sleep(50);
                count = 0;
            }
            now = new Date();
        }
        
        return shellExec.exitCode;
    } catch (e) {
        throw new Error(0, "Command \"" + cmd
            + "\" was not successful.\n" + e.description);
    }
}
function readStd(se) {
    // Get StdOut and StdErr from exec(cmd) 
    var sStdOut = ""; 
    var sStdErr = ""; 
    var somethingRead = false;

    if ( ! se.StdOut.AtEndOfStream ) {
        sStdOut += se.StdOut.ReadAll();
        somethingRead = true;
    }
    if ( ! se.StdErr.AtEndOfStream ) {
        sStdErr += se.StdErr.ReadAll();
        somethingRead = true;
    }
    if (somethingRead && debug) {
        displayStd(sStdOut, sStdErr)
    }
    return somethingRead;
}
function displayStd(sStdOut, sStdErr) {
    // Display StdOut and StdErr if /debug option
    if ( sStdOut.length > 0 ) {
        alert(sStdOut);
    }
    if ( sStdErr.length > 0 ) {
        alert(sStdErr);
    }
}
----------------------------------------------


wpkg-users mailing list
wpkg-users at lists.wpkg.org
http://lists.wpkg.org/mailman/listinfo/wpkg-users



More information about the wpkg-users mailing list