#!/usr/bin/python
# -*- Mode: Python -*-
# vi:si:et:sw=4:sts=4:ts=4

# Flumotion - a streaming media server
# Copyright (C) 2004,2005,2006,2007,2008,2009 Fluendo, S.L.
# Copyright (C) 2010,2011 Flumotion Services, S.A.
# All rights reserved.
#
# This file may be distributed and/or modified under the terms of
# the GNU Lesser General Public License version 2.1 as published by
# the Free Software Foundation.
# This file is distributed without any warranty; without even the implied
# warranty of merchantability or fitness for a particular purpose.
# See "LICENSE.LGPL" in the source distribution for more information.
#
# Headers in this file shall remain intact.

# This is a trial replacement with support for storing the
# intermediate coverage results in a file. Useful for running trial a
# couple of times with different reactors and building up the final
# coverage.


import os
import sys

from optparse import OptionParser
from twisted.python import usage

import twisted.scripts.trial

# Store the class we'll be overriding in local namespace
_Options = twisted.scripts.trial.Options


class FOptions(_Options):

    optFlags = [
        ['skip-slow', None,
         "Skip slow test cases (marked with a 'slow = True' attribute)."]]

    optParameters = [['saved-coverage', None, None,
                      'Filename to store coverage results.'
                      ' Can only be used with --coverage.']]

    def postOptions(self):
        _Options.postOptions(self)
        if self['saved-coverage'] and not self.tracer:
            raise usage.UsageError(
                "Cannot specify --saved-coverage without --coverage")
        if self['saved-coverage'] and self.tracer:
            # need to replace the tracer with one using our saved-coverage file
            print 'Saving coverage in', self['saved-coverage']
            import trace
            self.tracer = trace.Trace(count=1, trace=0,
                                      infile=self['saved-coverage'],
                                      outfile=self['saved-coverage'])
            sys.settrace(self.tracer.globaltrace)

        # make options available to flumotion.testsuite
        def getConfig():
            return dict(self)
        twisted.scripts.trial.getConfig = getConfig

twisted.scripts.trial.Options = FOptions

# without this snippet, copied from twisted's trial, for some reason
# this script misses modules it should get coverage data for,
# like flumotion.extern.log

### Twisted Preamble
# This makes sure that users don't have to set up their environment
# specially in order to run these programs from bin/.
import sys, os, string
if string.find(os.path.abspath(sys.argv[0]), os.sep+'Twisted') != -1:
    sys.path.insert(0, os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]), os.pardir, os.pardir)))
if hasattr(os, "getuid") and os.getuid() != 0:
    sys.path.insert(0, os.curdir)
### end of preamble

# begin chdir armor
sys.path[:] = map(os.path.abspath, sys.path)
# end chdir armor

from twisted.scripts.trial import run
run()
