def register_port(self, port_names): """ Register the port. Examples: register_port('AT..DUT1') Args: port_names: the port name you want to register. Returns: none. """ self.mPort = Port() port_names = self.order_port_list(port_names) for backend_cookie in port_names: peer("\n\n Loop is <{}>".format(backend_cookie)) backend = self.mPort.match(backend_cookie) if backend.name == 'AT': self.at = backend elif backend.name == "ADB": self.adb = backend else: pass
def __init__(self): """ the ACISMiscer class constructor function, init necessary conditions Args: none Returns: none """ self.limit_name = 'ft' # Maybe get this var from environment better. try: self.prefix = os.environ["REPORT_PATH"] + '/' \ + os.environ["PLATFORM"] + '/' \ + os.environ["ACIS_DIFF"] except KeyError as e: peer( "Can't get vaild environments from master. \nStack info: \n<{}>.\nSo switch to default branch." .format(e)) if not os.path.exists('/tmp/acis/testlog/' + self.limit_name): os.makedirs('/tmp/acis/testlog/' + self.limit_name, mode=0o744) self.prefix = '/tmp/acis/testlog' self.at = None self.adb = None self.envs = {}
def match(self, aka_name): """ The method to match the port object. Examples: match("AT..DUT1") Args: aka_name: The port object you want to match Returns: return the matched port object(adb/AT). """ backend_name, type_name = self.name_split(aka_name) backend_name = backend_name.upper() if type_name != 'any': type_name = type_name.upper() conf = self.parser.get_conf(backend_name, type_name) peer(conf) if backend_name == "AT": self.at = self.factory.which_backend(backend_name, type_name, conf) return self.at elif backend_name == "ADB": self.adb = self.factory.which_backend(backend_name, type_name, conf) return self.adb
def __narrow_config(self): """ the method purpose to drop self.configs items that without in devices. Args: none Returns: none """ try: output = subprocess.check_output( 'adb devices', shell=True).decode('utf-8').strip() except subprocess.CalledProcessError as err: raise err else: # devices = [ 'serial_id_1', 'serial_id_2', ...] devices = [] for line in output.split('\n'): g = re.match('\s*?(.*)\s*device$', line) if g: devices.append(g.group(1).strip()) pop = [] for i in self.configs: if self.configs[i]['serial'] not in devices: pop.append(i) for item in pop: peer("Narrowing - Drop item: {}".format( self.configs.pop(item))) peer("Final <configs> : {}".format(self.configs))
def __init__(self, serial_id): """ _ADB constructor function, init adb port,get serial_id Args: serial_id: the DUT serial_id. Returns: none """ self.serial_id = serial_id peer(self)
def deal_log_path(self, case_file): """ Deal and provide the log path, the testcase log will saved in the path. Examples: deal_log_path('/home/jenkins/tmp/loop_test/testcases/Driver/LowSpeedBus/ACIS_A_D_LSBUS_SPI_TEST.py') Args: case_file: the case path, it will be made for the log. Returns: the log path("/tmp/acis/testlog/testcases/Driver/LowSpeedBus/*"). """ dname, fname = os.path.split(case_file) dname = dname.split(self.limit_name + '/')[1] mprefix = self.prefix + '/' + self.limit_name + '/' + dname + '/' self.which_log = mprefix + fname.replace('.py', '.log') self.case_output = mprefix + fname.replace('.py', '') if not os.path.exists(self.case_output): os.makedirs(self.case_output, mode=0o755) peer("Case Log Location: {}".format(self.which_log)) return self.which_log
def send_cmd(self, command, timeout=30): """ Send ADB commands with adb -s serial_id command. Examples: send_cmd('shell "echo %s >/dev/ttyHS0" ' % string) send_cmd('shell "cat /dev/ttyHS0>/tmp/at.txt" ', timeout = 60) Args: command: the adb command you want to send. Kwargs: timeout: adb command timeout Returns: return the command output. """ try: start_time = datetime.now() dt = datetime.now() timeDisplay = "(%0.2d:%0.2d:%0.2d:%0.3d) Snd" % ( dt.hour, dt.minute, dt.second, dt.microsecond / 1000) cmd = 'adb -s %s %s' % (self.serial_id, command) peer(timeDisplay + " ADB " + self.serial_id + " [" + cmd + "]") #p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines = True) # > 'exec ' + cmd can FIX the process kill issue p = subprocess.Popen("exec " + cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) t = threading.Timer(timeout, self.__killSubProcess, args=(p, )) t.start() output = p.communicate()[0] if command.strip() in ("shell init 6", "shell init 0", "shell \"poweroff\"", "reboot-bootloader", "reboot"): peer("framework hook <Module reboot> commands [{}]".format( command.strip())) _AT.objs[self.serial_id].close() try: if t is not None: if t.isAlive(): peer("Terminate the monitoring process") t.cancel() peer("%s : Timer is cancelled" % datetime.now().strftime("%y/%m/%d %H:%M:%S")) else: peer( "Monitoring process expired, actively kill the thread." ) else: peer("Timer NOT set.") except Exception as e: peer(e) traceback.print_exc() peer("---->Problem when terminating mornitor process !!!") timeDisplay = "(%0.2d:%0.2d:%0.2d:%0.3d) Rcv" % ( dt.hour, dt.minute, dt.second, dt.microsecond / 1000) diff_time = datetime.now() - start_time diff_time_ms = diff_time.seconds * 1000 + diff_time.microseconds / 1000 for each_line in output.split('\n'): peer(timeDisplay + " ADB " + self.serial_id + " [" + each_line.replace("\r", "<CR>").replace("\n", "<CR>") + "]" + " @" + str(diff_time_ms) + " ms ") return output except Exception as e: peer(e) peer("----->Problem: Exception comes up when send command !!!") return "\r\nERROR\r\n"
def info(self): peer("My name is : {name}\n- conf:\n{conf}".format(name=ADB.name, conf=self.conf))
def get_conf(self, backend_name, type_name): """ The method to get the port object config. Examples: get_conf("AT", "DUT1") Args: backend_name: The port name(AT/adb). type_name: The DUT name(DUT1/DUT2/any) Returns: return the port object config. Examples: 'type_name' : >> 'DUT1' or 'DUT2' or 'any' \n 'mapto' : >> only 'any' has this prop. \n 'backend' : >> 'AT' or 'ADB' \n 'dev_link' : >> eg: AT > /dev/acis/DUT1/AT \n 'serial_id' : >> adb serial id. """ if type_name == "DUT1": if "DUT1" not in self.configs: raise NotFindTypeNameInRule( "Can NOT find type name <{}> in udev-rules file.".format( type_name)) if backend_name == "AT": if not subprocess.call("lsof {where}".format( where='/dev/' + self.configs[type_name][backend_name]), shell=True): raise ATportBusyErr("AT port is using.") if subprocess.call('ls {where}'.format( where='/dev/' + self.configs[type_name][backend_name]), shell=True): raise ATdevLinkNotExist( "Can NOT find dev-link [{}] for test.".format( '/dev/' + self.configs[type_name][backend_name])) return { 'type_name': type_name, 'mapto': type_name, 'backend': backend_name, 'dev_link': '/dev/' + self.configs[type_name][backend_name], 'serial_id': self.configs[type_name]["serial"] } elif backend_name == "DM": raise UnsupportBackendErr( "NOT support backend <{backend}>.".format( backend=backend_name)) elif backend_name == "ADB": return { 'type_name': type_name, 'mapto': type_name, 'backend': backend_name, 'serial_id': self.configs[type_name]["serial"] } elif type_name == "DUT2": if "DUT2" not in self.configs: raise NotFindTypeNameInRule( "Can NOT find type name <{}> in udev-rules file.".format( type_name)) if backend_name == "AT": if not subprocess.call("lsof {where}".format( where='/dev/' + self.configs[type_name][backend_name]), shell=True): raise ATportBusyErr("AT port is using.") if subprocess.call('ls {where}'.format( where='/dev/' + self.configs[type_name][backend_name]), shell=True): raise ATdevLinkNotExist( "Can NOT find dev-link [{}] for test.".format( '/dev/' + self.configs[type_name][backend_name])) return { 'type_name': type_name, 'mapto': type_name, 'backend': backend_name, 'dev_link': '/dev/' + self.configs[type_name][backend_name], 'serial_id': self.configs[type_name]["serial"] } elif backend_name == "DM": raise UnsupportBackendErr( "NOT support backend <{backend}>.".format( backend=backend_name)) elif backend_name == "ADB": return { 'type_name': type_name, 'mapto': type_name, 'backend': backend_name, 'serial_id': self.configs[type_name]["serial"] } elif type_name == "any": if not self.configs: raise NotFindTypeNameInRule( "Can NOT find any type names <DUT2 or DUT1> in udev-rules file." ) if len(self.configs) == 2: if backend_name == "AT": sel = choice(["DUT1", "DUT2"]) peer("First get type is {name}".format(name=sel)) if not subprocess.call("lsof {where}".format( where='/dev/' + self.configs[sel][backend_name]), shell=True): peer("Port Busy! Try another...") for another in ["DUT1", "DUT2"]: if sel != another: if not subprocess.call("lsof {where}".format( where='/dev/' + self.configs[another][backend_name]), shell=True): raise ATportBusyErr( "Double AT ports had been using.") else: if subprocess.call('ls {where}'.format( where='/dev/' + self.configs[sel][backend_name]), shell=True): raise ATdevLinkNotExist( "Can NOT find dev-link [{}] for test." .format('/dev/' + self.configs[sel] [backend_name])) self.any_conf[type_name] = another return { 'type_name': type_name, 'mapto': self.any_conf[type_name], 'backend': backend_name, 'dev_link': '/dev/' + self.configs[self.any_conf[type_name]] [backend_name], 'serial_id': self.configs[ self.any_conf[type_name]]["serial"] } else: if subprocess.call('ls {where}'.format( where='/dev/' + self.configs[sel][backend_name]), shell=True): raise ATdevLinkNotExist( "Can NOT find dev-link [{}] for test.".format( '/dev/' + self.configs[sel][backend_name])) self.any_conf[type_name] = sel return { 'type_name': type_name, 'mapto': self.any_conf[type_name], 'backend': backend_name, 'dev_link': '/dev/' + self.configs[self.any_conf[type_name]] [backend_name], 'serial_id': self.configs[self.any_conf[type_name]]["serial"] } elif backend_name == "DM": raise UnsupportBackendErr( "NOT support backend <{backend}>.".format( backend=backend_name)) elif backend_name == "ADB": return { 'type_name': type_name, 'mapto': self.any_conf[type_name], 'backend': backend_name, 'serial_id': self.configs[self.any_conf[type_name]]["serial"] } elif len(self.configs) == 1: if 'DUT1' in self.configs: if backend_name == "AT": sel = 'DUT1' if not subprocess.call("lsof {where}".format( where='/dev/' + self.configs[sel][backend_name]), shell=True): raise ATportBusyErr( "Only one module register to udev-rules: <{name}>, but this port is using." .format(name=sel)) else: if subprocess.call('ls {where}'.format( where='/dev/' + self.configs[sel][backend_name]), shell=True): raise ATdevLinkNotExist( "Can NOT find dev-link [{}] for test.". format('/dev/' + self.configs[sel][backend_name])) self.any_conf[type_name] = sel return { 'type_name': type_name, 'mapto': self.any_conf[type_name], 'backend': backend_name, 'dev_link': '/dev/' + self.configs[ self.any_conf[type_name]][backend_name], 'serial_id': self.configs[self.any_conf[type_name]] ["serial"] } elif backend_name == "DM": raise UnsupportBackendErr( "NOT support backend <{backend}>.".format( backend=backend_name)) elif backend_name == "ADB": return { 'type_name': type_name, 'mapto': self.any_conf[type_name], 'backend': backend_name, 'serial_id': self.configs[self.any_conf[type_name]]["serial"] } elif 'DUT2' in self.configs: if backend_name == "AT": sel = 'DUT2' if not subprocess.call("lsof {where}".format( where='/dev/' + self.configs[sel][backend_name]), shell=True): raise ATportBusyErr( "Only one module register to udev-rules: <{name}>, but this port is using." .format(name=sel)) else: if subprocess.call('ls {where}'.format( where='/dev/' + self.configs[sel][backend_name]), shell=True): raise ATdevLinkNotExist( "Can NOT find dev-link [{}] for test.". format('/dev/' + self.configs[sel][backend_name])) self.any_conf[type_name] = sel return { 'type_name': type_name, 'mapto': self.any_conf[type_name], 'backend': backend_name, 'dev_link': '/dev/' + self.configs[ self.any_conf[type_name]][backend_name], 'serial_id': self.configs[self.any_conf[type_name]] ["serial"] } elif backend_name == "DM": raise UnsupportBackendErr( "NOT support backend <{backend}>.".format( backend=backend_name)) elif backend_name == "ADB": return { 'type_name': type_name, 'mapto': self.any_conf[type_name], 'backend': backend_name, 'serial_id': self.configs[self.any_conf[type_name]]["serial"] } else: raise UnsupportTypeErr( "This type that your input [{}] is NOT support now.".format( type_name))
def _pick_info(self, _file): """ The method to Pick some information from udev file 'self.configs' content: eg.\n { "DUT1" : { "serial" : xxx, "link" : xxx, << acis/DUT1 "AT" : xxx, << acis/DUT1/AT "DM" : xxx},<< acis/DUT1/DM "DUT2" : { "serial" : xxx, "link" : xxx, << acis/DUT2 "AT" : xxx, << acis/DUT2/AT "DM" : xxx},<< acis/DUT2/DM } Examples: _pick_info('/etc/udev/rules.d/11-acis.rules') Args: _file: the udev file you want to pick information Returns: none. """ if not os.path.exists(_file): raise AcisRuleFileNotExist() with open(_file, mode='r') as f: for line in f: g = re.match(r'\s*ATTRS{serial}=="(.*)",\s*GOTO="(.*)\s*"', line) if g: if g.group(2) == "acis_DUT1": self.configs["DUT1"] = {"serial": g.group(1)} elif g.group(2) == "acis_DUT2": self.configs["DUT2"] = {"serial": g.group(1)} g = re.match( r"\s*SUBSYSTEMS==\"usb\",\s*DRIVERS==\"GobiSerial\",\s*SYMLINK\+=\"(acis/(.*))/(.*)\",\s*ATTRS{bInterfaceNumber}==\"(.*)\"\s*", line) if g: if g.group(2) == "DUT1": self.configs["DUT1"]["link"] = g.group(1) if g.group(4) == "03": self.configs["DUT1"]["AT"] = g.group( 1) + '/' + g.group(3) if g.group(4) == "00": self.configs["DUT1"]["DM"] = g.group( 1) + '/' + g.group(3) elif g.group(2) == "DUT2": self.configs["DUT2"]["link"] = g.group(1) if g.group(4) == "03": self.configs["DUT2"]["AT"] = g.group( 1) + '/' + g.group(3) if g.group(4) == "00": self.configs["DUT2"]["DM"] = g.group( 1) + '/' + g.group(3) peer("<Rules> configs: {}".format(self.configs)) self.__narrow_config()