from collections import namedtuple
from squad.plugins import Plugin as BasePlugin


class Issue(object):
    name = None
    found = False
    done = False
    max_lines = None

    def __init__(self):
        self.__log__ = []

    @classmethod
    def find(cls, log):
        issues = [issue_type() for issue_type in cls.__subclasses__()]
        for line in log.splitlines():
            for issue in issues:
                issue.feed(line)
        return issues

    def feed(self, line):
        if self.done:
            return
        self.__feed__(line)
        if self.found:
            self.__log__.append(line)
            if self.max_lines and len(self.__log__) >= self.max_lines:
                self.done = True

    @property
    def log(self):
        return "\n".join(self.__log__)


class Oops(Issue):
    name = 'oops'
    max_lines = 50

    def __feed__(self, line):
        if "------------[ cut here ]------------" in line:
            self.found = True


class KernelPanic(Issue):
    name = 'kernel-panic'

    def __feed__(self, line):
        if 'Kernel panic - not syncing:' in line:
            self.found = True
        if '---[ end Kernel panic - not syncing' in line:
            self.done = True


class Plugin(BasePlugin):

    def postprocess_testrun(self, testrun):
        project = testrun.build.project

        suite, _ = project.suites.get_or_create(slug='linux-log-parser')
        for issue in Issue.find(testrun.log_file):
            testrun.tests.create(
                suite=suite,
                name='check-' + issue.name,
                result=(not issue.found),
                log=issue.log
            )

@@ -1297,16 +1297,61 @@
 strun):%0A
+        if testrun.log_file is not None:%0A    
         
@@ -1383,16 +1383,20 @@
 roject%0A%0A
+    
         
@@ -1460,32 +1460,36 @@
 arser')%0A        
+    
 for issue in Iss
@@ -1527,16 +1527,20 @@
         
+    
 testrun.
@@ -1565,24 +1565,28 @@
             
+ 
+   
 suite=suite,
@@ -1586,16 +1586,20 @@
 =suite,%0A
+    
         
@@ -1650,16 +1650,20 @@
         
+   
+ 
 result=(
@@ -1696,16 +1696,20 @@
         
+    
 log=issu
@@ -1714,16 +1714,20 @@
 sue.log%0A
+    
         

quit gracefully when there are no logs