platform_Util.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. # -*- coding:utf-8 -*-
  2. import platform
  3. import os,sys
  4. reload(sys)
  5. sys.setdefaultencoding('utf-8')
  6. import shutil
  7. import string
  8. import subprocess
  9. class Environment():
  10. # 测试结果相关参数
  11. resultFolder = "result"
  12. logFolder = "log"
  13. detailFolder = "detail"
  14. screenFolder = "screen"
  15. scriptFolder = "scripts"
  16. resultDir = ""
  17. resultDetailDir = ""
  18. logDir = ""
  19. screenDir = ""
  20. scriptDir = ""
  21. allResultName = "all-result.xml"
  22. # 测试平台相关配置参数
  23. windowsCfg = "Environment_windows.cfg"
  24. linuxCfg = "Environment_linux.cfg"
  25. configFolder = "config"
  26. taskFile = ""
  27. tv_platform = ""
  28. projectID = ""
  29. tvIP = ""
  30. testType = ""
  31. taskName=""
  32. changeProjectIDTaskFile = ""
  33. changeProjectIDTaskName = "task_change_projectid"
  34. taskRootDir=""
  35. HOME_DIR=".."
  36. ANDROID_TV_SCRIPTS_DIR=""
  37. ANDROID_TV_TEST_DIR=""
  38. LINUX_TV_SCRIPTS_DIR=""
  39. LINUX_TV_TEST_DIR = ""
  40. tvServerSW = "SmokingTest.jar"
  41. SDK_Relative_Path = "/Lib/site-packages/ssat_sdk/"
  42. def getTvSererSWPath():
  43. return Environment.HOME_DIR + "/../../" + Environment.tvServerSW
  44. def getTVScriptDir():
  45. if (Environment.tv_platform == "android"):
  46. return Environment.ANDROID_TV_SCRIPTS_DIR
  47. elif (Environment.tv_platform == "linux"):
  48. return Environment.LINUX_TV_SCRIPTS_DIR
  49. def getTVTestDir():
  50. if (Environment.tv_platform == "android"):
  51. return Environment.ANDROID_TV_TEST_DIR
  52. elif (Environment.tv_platform == "linux"):
  53. return Environment.LINUX_TV_SCRIPTS_DIR
  54. def getPlatformType():
  55. return platform.system()
  56. def getFileSeprator():
  57. sys_str = getPlatformType()
  58. if (sys_str == "Windows"):
  59. return "/"
  60. elif (sys_str == "Linux"):
  61. return "/"
  62. else:
  63. return "/"
  64. def makeResultDir(inDir):
  65. sys_str = getPlatformType()
  66. Environment.resultDir = inDir + getFileSeprator() + Environment.resultFolder
  67. Environment.logDir = Environment.resultDir + getFileSeprator() + Environment.logFolder
  68. Environment.resultDetailDir = Environment.resultDir + getFileSeprator() + Environment.detailFolder
  69. Environment.screenDir = Environment.resultDir + getFileSeprator() + Environment.screenFolder
  70. Environment.scriptDir = Environment.resultDir + getFileSeprator() + Environment.scriptFolder
  71. if (os.path.isdir(Environment.resultDir)
  72. and os.path.isdir(Environment.logDir)
  73. and os.path.isdir(Environment.resultDetailDir)
  74. and os.path.isdir(Environment.screenDir)
  75. and os.path.isdir(Environment.scriptDir)):
  76. return Environment.resultDir
  77. os.mkdir(Environment.resultDir)
  78. os.mkdir(Environment.logDir)
  79. os.mkdir(Environment.screenDir)
  80. os.mkdir(Environment.scriptDir)
  81. os.mkdir(Environment.resultDetailDir)
  82. return Environment.resultDir
  83. def getFileFromAndroidPath(filepath):
  84. strarr = string.split(filepath, '/')
  85. filename = strarr[strarr.__len__() - 1]
  86. return filename
  87. def getFileFromLocalPath(filepath):
  88. strarr = string.split(filepath, getFileSeprator())
  89. filename = strarr[strarr.__len__() - 1]
  90. return filename
  91. def getTaskNameFromFile(taskfile):
  92. filename = getFileFromLocalPath(taskfile)
  93. strarr2 = string.split(filename, '.')
  94. taskname = strarr2[0]
  95. return taskname
  96. def appendFilePath(dir, filepath):
  97. filename = getFileFromAndroidPath(filepath)
  98. sys_str = getPlatformType()
  99. return dir + getFileSeprator() + filename
  100. def getFileSuffix(filepath):
  101. filename = getFileFromAndroidPath(filepath)
  102. strarr2 = string.split(filename, '.')
  103. suffix = strarr2[strarr2.__len__() - 1]
  104. print "Current file suffix:", suffix
  105. return suffix
  106. def getDirFromLocalFilePath(filepath):
  107. print "getDirFromLocalFilePath,filepath=", filepath
  108. strarr = string.split(filepath, getFileSeprator())
  109. fileDir = ""
  110. for index in range(0, strarr.__len__() - 1):
  111. if (index == 0):
  112. fileDir = strarr[0]
  113. else:
  114. fileDir = fileDir + getFileSeprator() + strarr[index]
  115. return fileDir
  116. def copyFile(filepath, destdir):
  117. sys_str = getPlatformType()
  118. filepath = Environment.HOME_DIR + getFileSeprator() + Environment.configFolder + getFileSeprator() + filepath
  119. destFile = destdir + getFileSeprator() + getFileFromLocalPath(filepath)
  120. print "copyFile,destFile=",destFile
  121. shutil.copyfile(filepath, destFile)
  122. def setEnvironment(info):
  123. strarr = info.split("=")
  124. print "setEnvironment,", strarr
  125. if (strarr.__len__() == 2):
  126. if (strarr[0] == "resultDir"):
  127. Environment.resultDir = strarr[1]
  128. elif (strarr[0] == "taskFile"):
  129. Environment.taskFile = strarr[1]
  130. Environment.taskName = getTaskNameFromFile(Environment.taskFile)
  131. Environment.taskRootDir = getDirFromLocalFilePath(Environment.taskFile)
  132. Environment.changeProjectIDTaskFile = Environment.taskRootDir + getFileSeprator() + Environment.changeProjectIDTaskName + ".xml"
  133. elif (strarr[0] == "tv_platform"):
  134. Environment.tv_platform = strarr[1]
  135. elif (strarr[0] == "projectID"):
  136. Environment.projectID = strarr[1]
  137. elif (strarr[0] == "tvIP"):
  138. Environment.tvIP = strarr[1]
  139. elif (strarr[0] == "testType"):
  140. Environment.testType = strarr[1]
  141. elif (strarr[0] == "ANDROID_TV_SCRIPTS_DIR"):
  142. Environment.ANDROID_TV_SCRIPTS_DIR = strarr[1]
  143. elif (strarr[0] == "ANDROID_TV_TEST_DIR"):
  144. Environment.ANDROID_TV_TEST_DIR = strarr[1]
  145. elif (strarr[0] == "LINUX_TV_SCRIPTS_DIR"):
  146. Environment.LINUX_TV_SCRIPTS_DIR = strarr[1]
  147. elif (strarr[0] == "HOME_DIR"):
  148. Environment.HOME_DIR = strarr[1]
  149. def getConfigFilePath(config):
  150. return Environment.HOME_DIR + getFileSeprator() + Environment.configFolder + getFileSeprator() + config
  151. def parseConfig(config):
  152. configpath = getConfigFilePath(config)
  153. print "parseConfig,configpath=",configpath
  154. cfgFile = open(configpath,"r",-1)
  155. strline = "init"
  156. while (strline <> None and strline <> ""):
  157. strline = cfgFile.readline().strip("\n").strip("\r")
  158. print "position=", cfgFile.tell(), ";line:", strline
  159. info = strline.decode("utf-8", "error")
  160. #防止文本读取结束时,“#”判断出错
  161. if (info.__len__() > 1 and info[0] <> "#"):
  162. #print "position=", cfgFile.tell(), ";line:", strline
  163. setEnvironment(strline)
  164. cfgFile.close()
  165. def getScriptsConfig(config):
  166. print "getScriptsConfig,config=", config
  167. conDic = {}
  168. cfgFile = open(config, "r", -1)
  169. strline = "init"
  170. while (strline <> None and strline <> ""):
  171. strline = cfgFile.readline().strip("\n").strip("\r")
  172. info = strline.decode("utf-8", "error")
  173. # 防止文本读取结束时,“#”判断出错
  174. if (info.__len__() > 1 and info[0] <> "#"):
  175. print "position=", cfgFile.tell(), ";line:", info.encode("utf-8")
  176. strarr = info.split("=")
  177. print "getConfig,", strarr
  178. if (strarr.__len__() > 1):
  179. conDic[strarr[0]] = strarr[1]
  180. cfgFile.close()
  181. return conDic
  182. def loadConfig():
  183. sys_str = getPlatformType()
  184. if (sys_str == "Windows"):
  185. parseConfig(Environment.windowsCfg)
  186. elif (sys_str == "Linux"):
  187. parseConfig(Environment.linuxCfg)
  188. else:
  189. parseConfig(Environment.linuxCfg)
  190. #在一行长文本中,用空格分开字符串,寻找第findNumber个 数字字符串,
  191. def findNumber(findNumber, strinfo):
  192. print strinfo
  193. strarr = string.split(strinfo, " ")
  194. print strarr
  195. findCount = 0
  196. for index in range(0, strarr.__len__()):
  197. if (strarr[index].__len__() > 0):
  198. flag = False
  199. for char in strarr[index]:
  200. if (char >= '0' and char <= '9'):
  201. flag = True
  202. continue
  203. else:
  204. flag = False
  205. break
  206. if (flag == True):
  207. findCount = findCount + 1
  208. print "findNumber=",findNumber, ";;findCount=", findCount, ",str=", strarr[index]
  209. if (findCount == findNumber):
  210. return strarr[index]
  211. return ""
  212. def checkTVTestProcessAndClose():
  213. sys_str = getPlatformType()
  214. if (Environment.tv_platform == "android" and sys_str == "Windows"):
  215. cmd = "adb -s " + Environment.tvIP + ":5555 shell ps| find \"uiautomator\""
  216. print cmd
  217. cprocess = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
  218. resstr = cprocess.stdout.read()
  219. uaPid = findNumber(1, resstr)
  220. cmd = "adb -s " + Environment.tvIP + ":5555 shell kill " + uaPid
  221. print cmd
  222. krocess = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
  223. resstr = krocess.stdout.read()
  224. '''
  225. 根据传入的进程id,检测进程是否存在
  226. '''
  227. def checkProcess(pid):
  228. cmd = "tasklist | findstr " + str(pid)
  229. cprocess = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
  230. psInfo = cprocess.stdout.readline()
  231. if psInfo.__len__() > 0:
  232. return True
  233. else:
  234. return False