## ------------------------------------------------------------------------
## iSiLK - A python-based graphical user interface for SiLK
## ------------------------------------------------------------------------
## Copyright (C) 2006-2007 Carnegie Mellon University. All Rights Reserved.
## ------------------------------------------------------------------------
## Authors: Andrew Kompanek (ajk@cert.org)
## ------------------------------------------------------------------------
## GNU General Public License (GPL) Rights pursuant to Version 2, June 1991
## Government Purpose License Rights (GPLR) pursuant to DFARS 252.225-7013
## ------------------------------------------------------------------------
## See the LICENSE file included in this distribution
## ------------------------------------------------------------------------

import logging
import constants
import silk
import constants
import silk
import tools
import resultset

class OldShellTool(tools.AnalysisTool):
    def __init__(self, command, resultClass=resultset.FilterResult, **kargs):
        tools.AnalysisTool.__init__(self, **kargs)
        self.command = command
        self.resultClass = resultClass
        
    def CreateDialog(parentResult, parent, *args, **kargs):
        return None
    
    def CanRun(parentResult):
        return parentResult.IsFilterResult()
        
    def _GetResultClass(self):
        return self.resultClass

    def _GetResultName(self):
        name = "Shell Result - %s" % self.command.split(" ")[0]
        return name

    def _RunAnalysis(self, context, inputFileName, outputFileName):
        # Build the command line
        self.command = self.command.replace("$input", inputFileName)
        self.command = self.command.replace("$output", outputFileName)
        cmd = silk._build_commandline(context.runRemote, self.command, cmdDir=context.runDir)
        f = silk.exec_command(context, cmd + " 2>&1")
        output = f.readlines()
        if f.close():
            raise silk.CommandError("Error running command", "%s\n\n%s" % (cmd, "".join(output)))
