class Test(unittest.TestCase):
    def setUp(self):
        import tempfile
        import os
        from comoonics.tools.ComSystemInformation import getSystemInformation
        from comoonics.tools.ComSysreport import Sysreport
        self.tmpdir=tempfile.mkdtemp()
        print("tmpdir: %s" %self.tmpdir)
        self.systeminformation=getSystemInformation()
        self.sysreportdir=os.path.dirname(os.path.realpath(__file__))
        print "sysreportdir: %s, cwd: %s" %(self.sysreportdir, os.path.realpath(os.path.curdir))
        self.sysreport=Sysreport(self.systeminformation, self.tmpdir, None, self.sysreportdir)

    def testName(self):
        possibletests2execute=[u'sysreport-redhat-base', u'sysreport-redhat-rhn', u'sysreport-redhat-selinux', 
                               u'sysreport-rpm', u'sysreport-linux-base', u'sysreport-linux-network', 
                               u'sysreport-linux-network-enhanced', u'sysreport-linux-boot', u'sysreport-linux-proc', 
                               u'sysreport-linux-logs', u'sysreport-linux-filesystem',
                               u'sysreport-redhatcluster']
        tests2execute=self.sysreport.getSetNames()
        found=0
        for test2execute in tests2execute:
            if test2execute not in possibletests2execute:
                self.fail("Test %s is not in possibletests2execute %s." %(test2execute, possibletests2execute))
            else:
                found+=1
        if found < len(tests2execute):
            self.fail("Could not find all tests in possibletests. %s != %s" %(tests2execute, possibletests2execute))

    def testDoSets1(self):
        self.__testDoSet('sysreport-linux-base')
        
    def __testDoSet(self, name):
        try:
            self.sysreport.doSets([name])
        except Exception, e:
#            import traceback
#            trace=traceback.format_exc()
            self.fail("Could not execute sysreport modificationset %s, error: %s" %(name, e))
class SysreportPlugin(Plugin):
    """
The sysreport plugin creates gives the functionality to create a sysreport as plugin. You can either create a
sysreport or just execute differents part of the sysreport.
    """
    TEMPLATE_DIR="/usr/share/sysreport/templates"
    TIMEFORMAT="%Y%m%d-%H%M%S"
    TMPDIR="/var/spool/sysreport-%s"
    def __init__(self, templatedir=TEMPLATE_DIR, tmpdir=None, tarfile=None):
        super(SysreportPlugin, self).__init__("sysreport")
        self.tmpdir=tmpdir
        self.tarfile=tarfile
        self.templatedir=templatedir
        self.sysreport=None
        self.addCommand("sysreport", self.doSysreport)
        self.addCommand("sysreportshowparts", self.doSysreportShowParts)

    def getTmpdir(self):
        return getattr(self, "tmpdir", self.TMPDIR %time.strftime(self.TIMEFORMAT))
    def getTarfile(self):
        return getattr(self, "tarfile", "/tmp/sysreport-%s.tar.gz" %time.strftime(self.TIMEFORMAT))

    def setupSysreport(self, shell=None):
        if not shell:
            print >>self.stdout,  "Setting up systeminformation"
            _sysinfo=getSystemInformation()
            print >>self.stdout,  "OK"
        else:
            _sysinfo=shell.sysinfo
        if not self.sysreport:
            print >>self.stdout,  "Setting up sysreport.."
            self.sysreport=Sysreport(_sysinfo, self.tmpdir, None, self.templatedir)
            print >>self.stdout,  "OK"

    def doSysreport(self, *params, **kwds):
        """
Does the sysreport after the following syntax:
sysreport [part=..]+ [tarfile=..] [tmpdir=..] [nosaveset] [noheadset]
sysreport [part]+ [tarfile]
Whereas the parameters are to be interpreted as follows:
part: is the part of the sysreport you want to execute (see sysreport-show-parts)
tarfile: is the tarfile the report should be stored in.
tmpdir: is the path where temporary files will be stored.
        """
        SysreportPlugin.logger.debug("doSysreport(params: %s, kwds: %s)" %(params, kwds))
        saveset=True
        headset=True
        shell=None
        parts=None
        if params and len(params)>0:
            self.tarfile=params[-1]
            parts=params[:-1]
        elif kwds:
            self.tarfile=kwds.get("tarfile", self.getTarfile())
            self.tmpdir=kwds.get("tmpdir", self.getTmpdir())
            parts=kwds.get("part", None)
            headset=not kwds.has_key("noheadset")
            saveset=not kwds.has_key("nosaveset")
            shell=kwds.get("shell", None)
        self.setupSysreport(shell)
        self.sysreport.destination=self.getTmpdir()
        self.sysreport.tarfile=self.getTarfile()
        self.sysreport.doSets(parts, headset, saveset)

    def doSysreportShowParts(self, *params, **kwds):
        """
Shows the parts that a sysreport can execute. You can select each of those.
        """
        SysreportPlugin.logger.debug("doSysreportParts(params: %s, kwds: %s)" %(params, kwds))
        _shell=None
        if kwds:
            _shell=kwds.get("shell", None)
        self.setupSysreport(_shell)
        print >>self.stdout,  "The following parts can be called."
        print >>self.stdout,  ", ".join(self.sysreport.getSetNames())