# -*- coding: utf-8 -*-
__author__ = 'huaxuemin'

import argparse
from runner9 import CaseRunner
usage_info = '''
usage:
$ case_runner.py -w %WORKSPACE% -c %case_path% -t tc1,tc2 -i p0,p1,p2 -e norun,del -r True
run tag cases in workspace
'''


def usage():
    print(usage_info)
    exit(-1)


def get_test_case_name(build_id):
    try:
        if build_id:
            import pymysql
            db = pymysql.connect(host="mysql.qa.huohua.cn", user="qa-dev", password="jaeg3SCQt0", database="sparkatp",
                                 charset='utf8')
            cursor = db.cursor()
            get_sql = "SELECT test_case_id FROM sparkatp.build_params WHERE build_info_id={}".format(build_id)
            cursor.execute(get_sql)
            res = cursor.fetchone()
            test_case_name = ""
            if res:
                test_case_name = res[0].replace("(", "*").replace(")", "*").replace(" ", "*")
            cursor.close()
            db.close()
            return test_case_name
    except Exception as e:
        print(e)
        return ""


def main():
    parser = argparse.ArgumentParser(description="This is for introduction parameter")
    parser.add_argument("-w", dest='workspace', help="case_runner workspace")
    parser.add_argument("-c", dest='case_path', help="case path")
    parser.add_argument("-t", dest='test_case', default='', help="test case")
    parser.add_argument("-i", dest='include', default='', help="include tag")
    parser.add_argument("-e", dest='exclude', default='', help="exclude tag")
    parser.add_argument("-r", dest='rerun', default='true', help="rerun failed case")
    parser.add_argument("-env", dest='special_env', default='', help="special env")
    parser.add_argument("-team", dest='team', default='', help="job name with team ")
    parser.add_argument("-penv", dest='physics_env', help="QA or SIM", default='QA')
    parser.add_argument("-b", dest='business', help="hh or hhi", default='hh')
    parser.add_argument("-b_id", dest='build_info_id', help="build_info_id", default='')
    parser.add_argument("-b_url", dest='build_url', help="build_url", default='')
    parser.add_argument("-is_db", dest='is_use_db', help="is_use_db", default='0')
    args = parser.parse_args()

    if args.workspace is None:
        usage()

    workspace = args.workspace
    case_path = args.case_path
    test_case = args.test_case
    include = args.include
    exclude = args.exclude
    rerun = args.rerun
    special_env = args.special_env
    team = args.team.split("-")[0]
    physics_env = args.physics_env
    business = args.business
    build_info_id = args.build_info_id
    build_url = args.build_url
    is_use_db = args.is_use_db
    if str(is_use_db) == "1" and build_info_id:
        test_case_name = get_test_case_name(build_info_id)
        test_case = test_case_name if test_case_name else test_case

    # if team not in ["CC", "EN", "H2R", "LaLive", "SCM", "TMO", "TO", "ASOP", "ASTWB", "ASORG"]:
    #     team = ""

    case_runner = CaseRunner(workspace, case_path, test_case, include, exclude, rerun, special_env, team, physics_env,
                             business)
    try:
        # 更新独立环境代号到配置文件
        case_runner.update_platform()
        # 检查工作目录名字对应的端口，有就使用，没有就分配
        case_runner.assign_port()
        # 查找对应端口进程并杀掉
        case_runner.check_port()
        # 更新KWL和RF中的端口
        case_runner.update_port()
        # 更新重名测试套
        case_runner.update_suite()
        # 启动KWL
        case_runner.start_kwl()
        # 构建用例
        case_runner.run_cases()
        # 构建完成后，停止KWL
        case_runner.check_port()
    except Exception as e:
        print(e)
    # 构建完成后，入库build_url
    case_runner.record_build_url(build_info_id, build_url)


if __name__ == '__main__':
    main()