#!/usr/bin/python
"""
Utility to run all possible tests and provide overall results. 

Test plan:

* Test LibTest with CPython
* Test all utilities:

  * pyjsbuild
  * pyjscompile
  * pyv8run
  * pyjampiler

* Test LibTest with PyV8
* Test LibTest in browsers (needs further research)
* Compile all examples
* Test examples in browsers (needs further research)
* Test examples in pyjd

Should be also reflected at http://pyjs.org/wiki/githowto/

"""

from optparse import OptionParser
from os import path, mkdir
import subprocess
import os
import re
import urllib2
import csv
import collections
from tempfile import mkdtemp

test_msg_re = re.compile("""\s*([\w]+) (Known issue|Test failed) \(([\w\/]+)\) \: (.+)$""")
issue_no_re = re.compile("""\#(\d+)""")
test_passed_re = re.compile("""\s*(\w+)\: Passed (\d+) \/ (\d+) tests( \((\d+) failed\))?""")
currentdir = path.abspath(path.dirname(__file__))

class PyjamasTester(object):
    parser = OptionParser()
    parser.add_option(
        "--verbose", "-v",
        dest="verbose",
        action="store_true",
        default=False,
        help="Show detailed information")
    parser.add_option(
        "--no-tracker",
        dest="tracker",
        action="store_false",
        default=True,
        help="Do not load data from issue tracker")
    parser.add_option(
        "--cpython",
        dest="cpython",
        action="store",
        default="/usr/bin/python",
        help="Path to CPython executable"
        )
    parser.add_option(
        "--pyv8",
        dest="pyv8",
        action="store",
        default="pyv8/pyv8run.py",
        help="Path to PyV8-based interpreter"
        )
    parser.add_option(
        "--no-libtest",
        dest="libtest_run",
        action="store_false",
        default=True,
        help="Do not run any LibTest tests"
        )
    parser.add_option(
        "--no-pyv8",
        dest="pyv8_run",
        action="store_false",
        default=True,
        help="Do not run any PyV8 tests"
        )
    parser.add_option(
        "--no-browsers",
        dest="browsers_run",
        action="store_false",
        default=True,
        help="Do not run any browsers tests"
        )
    parser.add_option(
        "--no-pyjd",
        dest="pyjd_run",
        action="store_false",
        default=True,
        help="Do not run any browsers tests"
        )
    parser.add_option(
        "--no-utils",
        dest="utils_run",
        action="store_false",
        default=True,
        help="Do not test utilities"
        )
    parser.add_option(
        "--no-examples",
        dest="examples_run",
        action="store_false",
        default=False,
        help="Do not test examples"
        )
    parser.add_option(
        "--examples",
        dest="examples_run",
        action="store_true",
        help="Test examples"
        )    
    parser.add_option(
        "--examples-path",
        dest="examples_path",
        action="store",
        default="examples/",
        help="Path to examples dir"
        )
    parser.add_option(
        "--tracker-report",
        dest="tracker_report",
        action="store_true",
        default=False,
        help="Provide report on tracker issues lacking failing tests"
        )
        
    def __init__(self):
        self.options, args = self.parser.parse_args()
        
        self.tmpdir = mkdtemp(prefix='pyjs')
        self.root = path.dirname(__file__)
        
        print "Output will be produced in %s" % self.tmpdir
        
        self.tracker_url = "http://code.google.com/p/pyjamas/issues/csv"
        if not path.isabs(self.options.pyv8):
            self.options.pyv8 = path.join(currentdir, self.options.pyv8)
        
        if not path.isabs(self.options.examples_path):
            self.options.examples_path = path.join(currentdir, self.options.examples_path)
            
        self.testsresults = []
        self.testsknown = []
        
        if self.options.libtest_run:
            self._test(self.test_libtest_cpython)

        if self.options.libtest_run:
            if self.options.pyv8_run:
                self._test(self.test_libtest_pyv8)
                
        if self.options.utils_run:
            self._test(self.test_pyjsbuild)
            self._test(self.test_pyjscompile)
            self._test(self.test_pyjampiler)
            
        if self.options.examples_run:
            self._test(self.test_examples)
        
        self.issues = {}
        if self.options.tracker:
            self.get_tracker_issues()
        
        self.print_results()
        


    def run_cmd(self, cmd=None, opts=None, cwd=None):
        if not cmd:
            cmd = self.options.cpython
        cmd = path.abspath(cmd)
        if not cwd:
            cwd = '.'
        cwd = path.abspath(cwd)
        if opts: