import re
import os


def get_message(case_path, case_name):
    """
    从注释中获取某个文件的 case tag 信息
    """
    tags = []
    pyfile = open(case_path + os.sep + case_name + '.py', 'r', encoding='utf-8')
    lines = pyfile.readlines()
    for line in lines:
        if 'case_tag' in line:
            line = line.split('case_tag')[-1]
            #对不规范的写法做一些处理，如中文分号, 中文逗号，多空格等。。
            line = re.sub(' ', '', line)
            line = line.replace('，', ',').replace(':', '').replace('：', '')
            line = line.strip().strip('\n')
            tags = line.split(',')
    pyfile.close()
    return tags


def get_case_tag_list(case_path, cases):
    """
    遍历case目录下的文件，从文件中获取case tag,加入列表中
    再加个判断，判断是跑线上还是线下
    """
    tagList = cases.split('#')
    env = os.environ['ENV']
    case_list = []
    for f in os.listdir(case_path):
        nf_path = os.path.join(case_path, f)
        if os.path.isdir(nf_path) and nf_path.endswith(".air") == False:
            for nf in os.listdir(nf_path):
                script = os.path.join(nf_path, nf)
                if script.endswith(".air"):
                    air_name = nf.replace('.air', '')
                    case_tags = get_message(script, air_name)
                    flag = False
                    for con in tagList:
                        conList = con.split(',')
                        if list(set(case_tags) & set(conList)):
                            flag = True
                        else:
                            flag = False
                            break
                    if flag and (env in case_tags or env in ('sit', 'sita')):
                        case_hash = {}
                        case_hash["air_path"] = str(script)
                        case_hash["air_name"] = str(air_name)
                        case_hash["module"] = str(script.split(os.sep)[-2])
                        case_list.append(case_hash)
        else:
            script = os.path.join(case_path, f)
            if f.endswith(".air"):
                air_name = f.replace('.air', '')
                case_tags = get_message(script, air_name)
                flag = False
                for con in tagList:
                    conList = con.split(',')
                    if list(set(case_tags) & set(conList)):
                        flag = True
                    else:
                        flag = False
                        break
                if flag and (env in case_tags or env in ('sit', 'sita')):
                    case_hash = {}
                    case_hash["air_path"] = str(script)
                    case_hash["air_name"] = str(air_name)
                    case_hash["module"] = str(script.split(os.sep)[-2])
                    case_list.append(case_hash)
    return case_list


def get_case_tag_list_2(case_path, cases):
    """
    遍历case目录下的文件，从文件中获取case tag,加入列表中
    """
    tagList = cases.split('#')
    case_list = []
    for f in os.listdir(case_path):
        script = os.path.join(case_path, f)
        air_name = f.replace('.air', '')
        case_tags = get_message(script, air_name)
        flag = False
        for con in tagList:
            conList = con.split(',')
            if list(set(case_tags) & set(conList)):
                flag = True
            else:
                flag = False
                break
        if flag:
            case_list.append(air_name)
    return case_list


