#!/usr/bin/env python
###############################################################################
# (c) Copyright 2016 CERN                                                     #
#                                                                             #
# This software is distributed under the terms of the GNU General Public      #
# Licence version 3 (GPL Version 3), copied verbatim in the file "COPYING".   #
#                                                                             #
# In applying this licence, CERN does not waive the privileges and immunities #
# granted to it by virtue of its status as an Intergovernmental Organization  #
# or submit itself to any jurisdiction.                                       #
###############################################################################
'''
Request for a periodic test to be run

'''
__author__ = 'Ben Couturier <ben.couturier@cern.ch>'

import sys
import LbMsg.TestMsg
from LbNightlyTools.Scripts.Common import PlainScript


class Script(PlainScript):
    '''
    request for a periodic test run to be done
    '''
    __usage__ = '%prog <slot> <buildId> <project> <config> <group> <env>'
    __version__ = ''


    def defineOpts(self):
        '''Define options.'''
        from LbNightlyTools.Scripts.Common import addBasicOptions

        self.parser.add_option('-r', '--runner', action='store',
                               default="lhcbpr",
                               help='Runner to be used for the periodic tests')
        self.parser.add_option('-l', '--os_label', action='store',
                               default="perf",
                               help='OS Label for the test to be run on Jenkins')

        addBasicOptions(self.parser)

    def main(self):
        '''
        Main function of the script.
        '''

        # Checking the arguments
        if len(self.args) != 6:
            self.log.error('Please specify <slot> <buildid> <project> <config> '
                            '<group> <env>. For example: lbq-requesttest 1467 '
                            'lhcb-sim09  Gauss x86_64-slc6-gcc49-opt '
                            '"GAUSS-RADLENGTHSCAN" "lb-run|RadLengthHandler"')
            exit(1)

        slot = self.args[0]
        buildId = self.args[1]
        project = self.args[2]
        config = self.args[3]
        group = self.args[4]
        env = self.args[5]

        msg = LbMsg.TestMsg.TestMessenger()
        msg.requestTest(slot, buildId, project, config,
                        group, env, self.options.runner,
                        self.options.os_label)

if __name__ == "__main__":
    sys.exit(Script().run())
