Python--Linux (ubuntu) Shell 環境 顯卡測試 Python操作SQLSERVER測試數據交互

#coding=utf-8
import pymssql#加載SQLSERVER連接
import os#加載系統操作
import uuid#加載讀取本機Mac Address
import sys#獲取命令行參數

#定義全局變量
Temp="" #操作全局變量使用global A
Isn=""
IMac=""
BiosInfo=['','','','','']
BiosInfoValues=['','','','','']
BiosInfoArgs=['Product Name','Bios Config File','Bios P/N','Bios Version','Bios Date']
OrderName=""

def createsConnection(server,uid,pwd,database_str):#設置數據庫連接
    conn=pymssql.connect(server=server,user=uid,password=pwd,database=database_str)
    print("\033[0m")
    return conn

def closeConnection(conn):#關閉數據庫連接
    conn.close()

def Read_Diag_Test_Program_Name(conn,Stored_Str,n):#讀取數據庫對應鍵值字符串
    cursor=conn.cursor()
    cursor.execute(Stored_Str)
    row=cursor.fetchone()
    return str(row[n])

def InsertTestData(conn,SN,Mac_Address):#插入DIAG測試數據
    curs=conn.cursor()
    curs.callproc('usp_Insert_Graogucs_Card_Diag_Data',(SN,Mac_Address,1))
    conn.commit()
    curs.execute("select count(*) from Graogucs_Card_Diag where SN=%s",SN)
    for row in curs:
        if(row[0]==0):
            print("\033[31mInsert Diag Test Data Fail!!")#字體顏色設置爲紅色
            print("\033[0m")
            closeConnection(conn)
            return 1
        else:
            print("\033[32mInsert Diag Test Data Succeed!!")
            print("\033[0m")
            closeConnection(conn)
            return 0

def Insert_Upadte_BiosInfo(conn,DiagPackageName):#插入或更新線體BIOS臨時信息
    My_Mac_Address=Get_MacAddress()
    Querying_Str="select * from ThePositionInformation where Mac_Address="+"'"+My_Mac_Address+"'"#查詢該Mac_Address所在線體
    LineBody=Read_Diag_Test_Program_Name(conn, Querying_Str,1)
    curs=conn.cursor()
    curs.callproc('usp_InsertOrUpdate_NB_DiagTest_Args',(LineBody,BiosInfoValues[0],BiosInfoValues[1],BiosInfoValues[2],
                  BiosInfoValues[3],BiosInfoValues[4],DiagPackageName,1))
    conn.commit()
    curs.execute("select count(*) from NB_Diag_Test_Args where Code_Name=%s",LineBody)
    for row in curs:
        if(row[0]==0):
            print("\033[31mInsert Or Update Bios Info Temp Information Fail!!")
            print("\033[0m")
            closeConnection(conn)
            return 1
        else:
            print("\033[32mInsert Or Update Bios Info Temp Information Succeed!!")
            print("\033[0m")
            closeConnection(conn)
            return 0
def Insert_Upadte_BiosInfo_2(conn,DiagPackageName,OrderName):#插入或更新當前線機程式Bios_1
    My_Mac_Address=Get_MacAddress()
    Querying_Str="select * from ThePositionInformation where Mac_Address="+"'"+My_Mac_Address+"'"#查詢該Mac_Address所在線體
    #LineBody=Read_Diag_Test_Program_Name(conn, Querying_Str,1)
    curs=conn.cursor()
    curs.callproc('usp_InsertOrUpdate_NB_DiagTest_Args',(OrderName,BiosInfoValues[0],BiosInfoValues[1],BiosInfoValues[2],
                  BiosInfoValues[3],BiosInfoValues[4],DiagPackageName,1))
    conn.commit()
    curs.execute("select count(*) from NB_Diag_Test_Args where Code_Name=%s",OrderName)
    for row in curs:
        if(row[0]==0):
            print("\033[31mInsert Or Update Bios_1 Info Temp Information Fail!!")
            print("\033[0m")
            closeConnection(conn)
            return 1
        else:
            print("\033[32mInsert Or Update Bios_1 Info Temp Information Succeed!!")
            print("\033[0m")
            closeConnection(conn)
            return 0

def Insert_Upadte_BiosInfo_Bios2(conn,DiagPackageName,OrderName):#插入或更新當前線機程式Bios_2
    My_Mac_Address=Get_MacAddress()
    Querying_Str="select * from ThePositionInformation where Mac_Address="+"'"+My_Mac_Address+"'"#查詢該Mac_Address所在線體
    #LineBody=Read_Diag_Test_Program_Name(conn, Querying_Str,1)
    curs=conn.cursor()
    curs.callproc('usp_Insert_Update_NB_Diag_Test_Args_Bios2',(OrderName,BiosInfoValues[0],BiosInfoValues[1],BiosInfoValues[2],
                  BiosInfoValues[3],BiosInfoValues[4],DiagPackageName,1))
    conn.commit()
    curs.execute("select count(*) from NB_Diag_Test_Args_Bios2 where Code_Name=%s",OrderName)
    for row in curs:
        if(row[0]==0):
            print("\033[31mInsert Or Update Bios_2 Info Temp Information Fail!!")
            print("\033[0m")
            closeConnection(conn)
            return 1
        else:
            print("\033[32mInsert Or Update Bios_2 Info Temp Information Succeed!!")
            print("\033[0m")
            closeConnection(conn)
            return 0

def Title():#顯示標題
    os.system("clear")
    print("\033[37mProgram Read_Diag_Args Version.1.00")#將字體設置爲灰色
    print("\033[37mProerty in Copyright Ferex ShenBo.")
    print("\033[37m------------------------------------------------------------------------------------")

def Help():#幫助信息
    os.system("clear")
    print("\033[37mProgram Version.1.00")
    print("\033[37mProperty in Copyright Ferex ShenBo.")
    print("\033[37m====================================================================================")
    print("\033[37m/Uploading               Uploading Test Data In DataBase!!")
    print("\033[37m/RdDiagPackageName       Read Diag Test Program Package Name!!")
    print("\033[37m/ChkBiosTest             Check Graphics Card Bios_1 Test!!")
    print("\033[37m/ChkBiosTest2            Check Graphics Card Bios_2 Test!!")
    print("\033[37m/InUpDiagArgs            Inset Or Update Graphics Card Diag Test Information!!")
    print("\033[37m/NowInUpDiagArgs         Now Inset Or Update Graphics Card Diag Bios_1 Test Information!!")
    print("\033[37m/NowInUpDiagArgs_Bios2   Now Inset Or Update Graphics Card Diag Bios_2 Test Information!!")
    print("\033[36mExample_1:*.exe /Uploading sn.dat")
    print("\033[36mExample_2:*.exe /RdDiagPackageName")
    print("\033[36mExample_3:*.exe /ChkBiosTest  BiosLog.dat")
    print("\033[36mExample_3:*.exe /ChkBiosTest2 BiosLog.dat")
    print("\033[36mExample_4:*.exe /InUpDiagArgs BiosLog.dat DiagPackage_Name")
    print("\033[36mExample_4:*.exe /NowInUpDiagArgs BiosLog.dat 'DiagPackage_Name' 'OrderName'")
    print("\033[36mExample_4:*.exe /NowInUpDiagArgs_Bios2 BiosLog.dat 'DiagPackage_Name' 'OrderName'")
    print("\033[0m")#恢復當前的顏色

def Write_BathFile(FileName,Str):#將字符串寫入文件中
    with open(FileName,'w',encoding='utf-8') as fw:
        fw.write(Str)
    fw.closed

def Read_CfgFile(FileName):#讀取配置文件
    with open(FileName,'r',encoding='utf-8') as fr:
        Temp=str(fr.read())
    fr.closed
    return Temp

def Get_MacAddress():#讀取本機Mac_Address
    Mac=uuid.UUID(int=uuid.getnode()).hex[-12:].upper()
    return "".join([Mac[e:e+2] for e in range(0,11,2)])

def Get_Bios_Info():#讀取BIOS信息
    conn=createsConnection("192.167.10.12\server2","sa","adminsystem","E_Graphics_Card")
    My_Mac_Address=Get_MacAddress()
    Querying_Str="select * from ThePositionInformation where Mac_Address="+"'"+My_Mac_Address+"'"#查詢該Mac_Address所在線體
    LineBody=Read_Diag_Test_Program_Name(conn, Querying_Str,1)
    if LineBody.strip():#判斷返回的Querying_Str字符串是否爲空
        Querying_Str="select * from Line_Body_Program where Line_BOdy="+"'"+LineBody+"'"#查詢該線體產品訂單號
        Code_Name_Str=Read_Diag_Test_Program_Name(conn,Querying_Str,1)
        if Code_Name_Str.strip():#判斷返回的Code_Name_Str字符串是否爲空
            Querying_Str="select * from NB_Diag_Test_Args where Code_Name="+"'"+Code_Name_Str+"'"#查詢調用Diag測試包名稱
            cursor=conn.cursor()
            cursor.execute(Querying_Str)
            row=cursor.fetchone()
            i=0
            global BiosInfo
            while(i<5):
                BiosInfo[i]=str(row[i+2])
                if not str(BiosInfo[i]).strip():
                    print("\033[31mRead Order Number ",Code_Name_Str," DataBase BiosInfo ",i," Is Null",)
                    return 1
                i+=1
            print("\033[32mRead Order Number ",Code_Name_Str," DataBase BiosInfo Succeed!!")
            return 0
        else:
            print("\033[31mQuerying DataBase Read Code_Name is Null")
            closeConnection(conn)
            return 2
    else:
        print("\033[31mQuerying DataBase Read LineBody is Null")
        closeConnection(conn)
        return 3

def ReadTestLog(FileName):#讀取BIOS INFO LOG File Info
    Title()
    global BiosInfoValues
    t=0
    with open(FileName,'r',encoding='utf-8') as rf:
        list=rf.readlines()
        for l in list:
            n=0
            while(n<len(BiosInfoArgs)):
                if(t==len(BiosInfoArgs)):
                    print("\033[32mRead ",FileName," Succeed!!")
                    print("\033[0m")
                    return 0
                else:
                    if BiosInfoArgs[t] in l:
                        l=l[l.find(":")+1:l.find("\n")]
                        s=0
                        while(l[s]==" "):
                            s+=1
                        l=l[s:]
                        e=len(l)-1
                        while(l[e]==" "):
                            e-=1
                        l=l[:e]
                        BiosInfoValues[t]=l
                        t+=1
                        break
                n+=1
    rf.closed
    if(t<len(BiosInfoArgs)):
        print("\033[31m Read ",FileName," Fail!!")
        print("\033[0m")
        return 1

def ReadDiagPackageName():#讀取Diag包名稱
    Title()
    conn=createsConnection("192.167.10.12\server2","sa","adminsystem","E_Graphics_Card")
    My_Mac_Address=Get_MacAddress()
    Querying_Str="select * from ThePositionInformation where Mac_Address="+"'"+My_Mac_Address+"'"#查詢該Mac_Address所在線體
    LineBody=Read_Diag_Test_Program_Name(conn, Querying_Str,1)
    if LineBody.strip():#判斷返回的Querying_Str字符串是否爲空
        Querying_Str="select * from Line_Body_Program where Line_BOdy="+"'"+LineBody+"'"#查詢該線體產品訂單號
        Code_Name_Str=Read_Diag_Test_Program_Name(conn,Querying_Str,1)
        if Code_Name_Str.strip():#判斷返回的Code_Name_Str字符串是否爲空
            Querying_Str="select * from NB_Diag_Test_Args where Code_Name="+"'"+Code_Name_Str+"'"#查詢調用Diag測試包名稱
            Call_Diag_Name_Str=Read_Diag_Test_Program_Name(conn,Querying_Str,7)
            if Call_Diag_Name_Str.strip():#判斷返回的Call_Diag_Name_St字符串是否爲空
                closeConnection(conn)
                Write_BathFile("fpath",Call_Diag_Name_Str)
                print("\033[32mRead DataBase Call_Diag_Name Succeed!!")
                closeConnection(conn)
                return 0
            else:
                print("\033[31mQuerying DataBase Read Call_Diag_Name is Null")
                closeConnection(conn)
                return 1
        else:
            print("\033[31mQuerying DataBase Read Code_Name is Null")
            closeConnection(conn)
            return 2
    else:
        print("\033[31mQuerying DataBase Read LineBody is Null")
        closeConnection(conn)
        return 3

def UploadingTestData_In_DataBase(Isn_FileName):#上傳測試數據至數據庫
    Title()
    conn=createsConnection("192.167.10.12\server2","sa","adminsystem","E_Graphics_Card")
    My_Mac_Address=Get_MacAddress()
    Isn=Read_CfgFile(Isn_FileName)
    if(InsertTestData(conn,Isn,My_Mac_Address)==1):
        print("\033[31mUpload Test Data In DataBase Fail!!")
        print("\033[0m")
        return 1
    else:
        print("\033[32mUpload Test Data In DataBase Succeed!1")
        print("\033[0m")
        return 0

def CheckBiosInfo(LogFileName):#覈對BIOS信息
    n=0
    if(Get_Bios_Info()==0):
        if(ReadTestLog(LogFileName)==0):
            while(n<len(BiosInfo)):
                if(BiosInfo[n]!=BiosInfoValues[n]):
                    print("\033[31m Check Bios Info ",BiosInfoArgs[n],":",BiosInfo[n]," Fail!!")
                    print("\033[0m")
                    return 1
                n+=1
            print("\033[32n Check Bios Info All Items Pass!!")
            print("\033[0m")
            return 0
        else:
            return 1
    else:
        return 1

def Inset_Update_NB_DiagTest_Args(LogFileName,DiagPackageName,n):#插入或更新藍寶Diag測試參數
    Title()
    if(ReadTestLog(LogFileName)==0):
        conn=createsConnection("192.167.10.12\server2","sa","adminsystem","E_Graphics_Card")
        if n==1:
            if(Insert_Upadte_BiosInfo(conn,DiagPackageName)==0):
                return 0
            else:
                return 1
        elif n==2:
            if(Insert_Upadte_BiosInfo_2(conn,DiagPackageName,OrderName)==0):
                return 0
            else:
                return 1
        elif n==3:
            if(Insert_Upadte_BiosInfo_Bios2(conn,DiagPackageName,OrderName)==0):
                return 0
            else:
                return 1
    else:
        return 1


if(len(sys.argv))>=2:
    if sys.argv[1] in "/Uploading" and len(sys.argv)==3:
        if(UploadingTestData_In_DataBase(sys.argv[2])==0):
            exit(0)
        else:
            exit(1)
    elif sys.argv[1] in "/RdDiagPackageName":
        if(ReadDiagPackageName()==0):
            exit(0)
        else:
            exit(1)
    elif sys.argv[1] in "/ChkBiosTest" and len(sys.argv)==3:
        if(CheckBiosInfo(sys.argv[2])==0):
            exit(0)
        else:
            exit(1)
    elif sys.argv[1] in "/InUpDiagArgs" and len(sys.argv)==4:
        if(Inset_Update_NB_DiagTest_Args(sys.argv[2],sys.argv[3],1)==0):
            exit(0)
        else:
            exit(1)
    elif sys.argv[1] in "/NowInUpDiagArgs" and len(sys.argv)==5:
        OrderName=sys.argv[4]
        if(Inset_Update_NB_DiagTest_Args(sys.argv[2],sys.argv[3],2)==0):
            exit(0)
        else:
            exit(1)
    elif sys.argv[1] in "/NowInUpDiagArgs_Bios2" and len(sys.argv)==5:
        OrderName=sys.argv[4]
        if(Inset_Update_NB_DiagTest_Args(sys.argv[2],sys.argv[3],3)==0):
            exit(0)
        else:
            exit(1)
    else:
        Help()
        exit(1)
else:
    Help()
    exit(1)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章