def saveTxtFile(fileName='Null', mode='a+', data='Null'): """ 保存文本文档 """ v = Python_version() logging.debug(fileName) if v > 3.0: with open(fileName, mode, encoding='UTF-8') as f: f.write(data) else: f = open(fileName, mode) f.write(data)
def __readTestCases(self): v = Python_version() if v > 3.0: with open('{0}/testcase.txt'.format(Utils.get_sys_path()), mode='r', encoding='utf-8') as f: testcase = f.read() return testcase else: with open('{0}/testcase.txt'.format(Utils.get_sys_path()), mode='r') as f: testcase = f.read() return testcase
def dmesg(dev='Null', logname=None, logon=False): """ dmesg log """ result, out = Utils.adbshell(dev, "dmesg", logon) if logname is None: logname = '{0}/log/{1}-{2}-dmesg.log'.format( Utils.get_sys_path(), dev, TimeUtils.strTimeName()) if result == 0: v = Python_version() if v > 3.0: Utils.saveTxtFile(logname, data=out) else: Utils.saveTxtFile(logname, data=out) return result, out
def run_command(command='NO_COMMAND_GIVEN', logon=False): """ 执行命令行,兼容2.x 与 3.x版本 执行成功返回执行结果,否则为空 """ logging.debug("RUN: '{0}'".format(command)) version = Python_version() if version == 2.7: P = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True) out = P.communicate()[0] out = re.sub(r"\r+\n", r'\n', out) logging.debug("Command '{0}' output: '{1}'".format(command, out)) result = P.wait() if result == 0: return result, out else: if logon: logging.warning( "Command '{0}' failed to run, output:{1}".format( command, out)) if version > 3.3: try: P = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, check=True, encoding='utf-8', shell=True) out = P.stdout out = re.sub(r'\r+\n', r'\n', out) logging.debug("Command '{0}' output: '{1}'".format(command, out)) return 0, out except subprocess.CalledProcessError as error: result = error.returncode out = error.output if logon: logging.warning("RUN: '{0}'".format(out)) else: logging.error( 'Python_version is {0} < 3.3 , sys exit, please update python'. format(version)) sys.exit(-1) return result, out
def com_log(com, dev='Null', logname=None): """ 串口log """ com_port = COMTest._opencom(com) if logname is None: logname = '{0}/log/{1}-{2}'.format(Utils.get_sys_path(), dev, TimeUtils.strTimeName()) while True: result = COMTest._wait_Result(com_port) v = Python_version() if v > 3.0: print(result, end='') else: print(result), result = re.sub(r'\n', '', result) logname_new = '{0}-com_log.log'.format(logname) size = Utils.get_FileSize(logname_new) if size > 2.00: logname = '{0}/log/{1}-{2}'.format(Utils.get_sys_path(), dev, TimeUtils.strTimeName()) logname_new = '{0}-com_log.log'.format(logname) Utils.saveTxtFile(logname_new, data=result)
# -*- coding: utf-8 -*- from Basics.Version import Python_version import sys import logging v = Python_version() if v >= 3.4: try: import serial except ImportError as err: logging.error(err) sys.exit(-1) else: logging.error('AT test (serial) need python_version >= 3.4 ,but the current version = {0}'.format(str(v))) class COMTest: """ 串口测试基类 """ # 打开串口函数 @staticmethod def _opencom(com, baudrate=115200, bytesize=8, parity='N', stopbits=1, timeout=1): """ 打开串口 """ try: com_port = serial.Serial(com, baudrate, bytesize, parity, stopbits, timeout) return com_port