def newfunc(*arg, **kwargs): timer = Timer(limit, handler) try: log.debug('starting timer in %s for %s', str(datetime.datetime.now()), str(limit)) timer.start() ret = func(*arg, **kwargs) except KeyboardInterrupt: if timer.isAlive: timer.cancel() raise except: if timer.isAlive(): timer.cancel() log.debug('canceled timer in %s because of failure', str(datetime.datetime.now())) raise else: raise TimeExpired, 'Time expired ( and test raised: ' \ + str(sys.exc_info()) + ')', sys.exc_info()[2] if timer.isAlive(): timer.cancel() log.debug('canceled timer in %s', str(datetime.datetime.now())) else: log.debug('timer has expired', str(datetime.datetime.now())) raise TimeExpired("time limit exceeded") return ret
def runOSProcess(command, logger, timeout=60, shell=False): logger.debug("Begin OS process run of %s" % (command)) # if this is Linux use the shell if platform.system() != "Windows": shell = True proc = Popen(command, stdout=PIPE, stderr=PIPE, shell=shell) timer = Timer(timeout, proc.kill) try: timer.start() stdout, stderr = proc.communicate() finally: timer.cancel() if not timer.isAlive(): res = False logger.warn("OS process timed out after %s seconds, for command %s" % (timeout, command)) proc.terminate() return "", "timeout after %s seconds" % (timeout), False else: if proc.returncode != 0: logger.debug("OS process exited with non-zero code of %s, for command %s" % (proc.returncode, command)) res = False else: logger.debug("OS process exited with zero code, for command %s" % (command)) res = True return str(stdout), str(stderr), res
def run_command(command="",abort_on_failure=True,abort_on_timeout=True,timeout_returncode=-2,print_command=True,print_output=False,timeout=default_timeout): if print_command: print "Executing command:" print " ".join(command) P=Popen(command,stdin=PIPE,stdout=PIPE,stderr=PIPE) timer=Timer(timeout,lambda p:p.kill(),[P]) timer.daemon=True; timer.start() P.wait() [s1,s2]=P.communicate(); if timer.isAlive(): timer.cancel() else: if abort_on_timeout: print "It looks like this code entered an infinite loop, or was stuck waiting for input, or just refused to exit. I had to kill it manually. " fail() else: returncode=timeout_returncode if P.returncode != 0 and abort_on_failure: print s1 print s2 print "Error. The return code was non-zero, meaning that this code crashed." fail() returncode=P.returncode if print_output: print s1 print s2 return returncode,s1,s2
def onAction(self, eventInfo): zone = eventInfo.getZone() zoneManager = eventInfo.getZoneManager() def sendAlert(): msg = 'The {} door has been opened for {} minutes.'.format( zone.getName(), self.maxElapsedTimeInSeconds / 60) alert = Alert.createWarningAlert(msg) AlertManager.processAlert(alert, zoneManager) for door in zone.getDevicesByType(Door): timer = self.timers[door] if door in self.timers else None if door.isOpen(): if None != timer: timer.cancel() del self.timers[door] timer = Timer(self.maxElapsedTimeInSeconds, sendAlert) timer.start() self.timers[door] = timer else: if None != timer: if timer.isAlive(): timer.cancel() else: # alert door now closed if a warning was previous sent msg = 'The {} door is now closed.'.format( zone.getName()) alert = Alert.createWarningAlert(msg) AlertManager.processAlert(alert, zoneManager) del self.timers[door] return True
class timer: # start timer with n seconds and go to self.timerExpired def startTimer(self, n): self.t = Timer(n, self.timerExpired) print 'Timer started for %d seconds'% n #start the timer instance self.t.start() # Called if argument is true to stop it. def stopTimer(self): # First look if the timer is active if self.t.isAlive(): print 'Timer found active' #Stop the timer instance self.t.cancel() print 'Timer stopped' # If not active, do nothing else: print 'Timer inactive' # Put code in here to execute after timer has expired. def timerExpired(self): print 'Timer expired, go to do what you want'
def run_module(self, host, port): t = Timer(5.0, self.__check_module_state) t.start() vectors = self._get_default_vector2() if not vectors: vectors = self.vectors.get_vectors_by_interpreters( self.modhandler.loaded_shells) for vector in vectors: self.last_vector = vector.name self.__execute_payload(vector, [host, port]) if t.isAlive(): t.cancel() if not self.done: self.last_vector = None self.mprint( "[%s] No reverse backdoor method worked. Assure remote port is" % (self.name)) self.mprint( "[%s] listening using commands like \'nc -v -l -p <port>\'" % (self.name))
class CustomTimer: def __init__(self, value, f): self.timer = Timer(value, f) self.f = f def reset(self, value): if not self.timer.isAlive(): raise CustomTimerException("don't reset when timer not started") self.timer.cancel() self.timer = Timer(value, self.f) self.timer.start() def start(self): if self.timer.isAlive(): raise CustomTimerException("don't start a timer when he's already up") self.timer.start()
class timer: # start timer with n seconds and go to self.timerExpired def startTimer(self, n): self.t = Timer(n, self.timerExpired) print 'Timer started for %d seconds' % n #start the timer instance self.t.start() # Called if argument is true to stop it. def stopTimer(self): # First look if the timer is active if self.t.isAlive(): print 'Timer found active' #Stop the timer instance self.t.cancel() print 'Timer stopped' # If not active, do nothing else: print 'Timer inactive' # Put code in here to execute after timer has expired. def timerExpired(self): print 'Timer expired, go to do what you want'
class Controller: def __init__(self): # create model and setup callbacks self.model = Model() self.model.spyeworks.addCallback(self.updatePlayerOnline) self.model.spyeworks.currentList.addCallback(self.updateCurrentList) self.model.sensorstate.addCallback(self.updateSensorState) # create variables for timers self.activeTimer = Timer(1, self.restartFunc, ()) #self.playIdleList=False # update variables with data from model self.updatePlayerOnline(self.model.spyeworks.get()) self.updateSensorState(self.model.sensorstate.get()) ################################# ### Methods for printing to the console ################################# # updates the player online status in the view def updatePlayerOnline(self, value): print("Player is " + value) pass # updates the current list status in the view def updateCurrentList(self, value): print("Current List is " + value) pass # dummy function for passing to timer thread def dummyFunc(self): pass # restart function for passing to timer thread def restartFunc(self): if self.model.sensorstate.get() == 'On': # sensor is active, restart active list self.startActiveList() # handles updates to the sensor status def updateSensorState(self, value): # updates the sensor status in the view print("Sensor is " + value) # if the sensor is activated if value == "On": # if the active timer is not active if self.activeTimer.isAlive() == False: self.startActiveList() def startActiveList(self): # play the active list self.model.spyeworks.playActive() # start the active list timer self.activeTimer = Timer(int(self.model.activedelay.get()), self.restartFunc, ()) self.activeTimer.start()
class LCD_SM(StateMachine): def __init__(self, cad): # Initial state self.cad = cad self.parser = ConfigParser() self.configFile = '/home/pi/projects/PiFace/config.ini' self.parser.read(self.configFile) # Static variable initialization: LCD_SM.waiting = Waiting(cad, self) LCD_SM.menu = Menu(cad, self) LCD_SM.sections = Sections(cad, self) LCD_SM.options = Options(cad, self) LCD_SM.editing = Editing(cad, self) LCD_SM.scanning = Scanning(cad, self) StateMachine.__init__(self, LCD_SM.waiting) self.currentState = self.currentState.run() #Timer used to turn off display when inactive for self.t = Timer(INACTIVE_TIME, self.hibernate) def run(self, event): self.currentState = self.currentState[0].next(event.pin_num) #If a button has been pressed the program will be directed #here. Cancel the current timer and start a new timer if self.t.isAlive(): self.t.cancel() self.t.join() if not self.t.isAlive(): self.t = Timer(INACTIVE_TIME, self.hibernate) self.t.start() #The Waiting.run state does not require any arguments if (len(self.currentState)>1): self.currentState[0].run(self.currentState[1]) else: self.currentState[0].run() def hibernate(self): #If inactive put the display in waiting state self.currentState = LCD_SM.waiting.run() self.t.cancel()
class PandaPlayer(xbmc.Player): def __init__(self, core=None, panda=None): #xbmc.Player.__init__(self, xbmc.PLAYER_CORE_MPLAYER) #xbmc.Player.__init__(self, xbmc.PLAYER_CORE_AUTO) xbmc.Player.__init__(self) self.panda = panda self.timer = None def playSong(self, item): self.play(item[0], item[1]) def play(self, url, item): xbmc.Player(xbmc.PLAYER_CORE_MPLAYER).play(url, item) #xbmc.Player(xbmc.PLAYER_CORE_AUTO).play(url, item) def onPlayBackStarted(self): print "PANDORA: onPlayBackStarted: %s" % self.getPlayingFile() if self.panda.playing: '''if not "pandora.com" in self.getPlayingFile(): self.panda.playing = False self.panda.quit() else: #Show Visualization (disappears after each song...)''' xbmc.executebuiltin("ActivateWindow( 12006 )") def onPlayBackEnded(self): print "PANDORA: onPlayBackEnded" self.stop() print "PANDORA: playing = %s" % self.panda.playing if self.timer and self.timer.isAlive(): self.timer.cancel() if self.panda.skip: self.panda.skip = False if self.panda.playing: self.timer = Timer(0.5, self.panda.playNextSong) self.timer.start() def onPlayBackStopped(self): print "PANDORA: onPlayBackStopped" self.stop() print "PANDORA: playing = %s" % self.panda.playing if self.timer and self.timer.isAlive(): self.timer.cancel() #catch for if this was actually a skip if self.panda.playing and self.panda.skip: self.panda.skip = False self.timer = Timer(0.5,self.panda.playNextSong) self.timer.start() else: if xbmc.getCondVisibility('Skin.HasSetting(PandoraVis)'): xbmc.executebuiltin('Skin.Reset(PandoraVis)') self.panda.Quit()
def battery_charging_monitor(event): battery_charging_monitor.log.debug("Battery charging monitor: {}: start".format(event.itemState)) global charger_timer if items["Outlet9"] == ON and event.itemState <= DecimalType(8) and event.oldItemState <= DecimalType(8): if charger_timer is None or not charger_timer.isAlive(): charger_timer = Timer(600, lambda: events.sendCommand("Outlet9","OFF"))# 600 seconds = 5 minutes charger_timer.start() battery_charging_monitor.log.info("Battery charging monitor: Started battery charging turn off timer: Outlet9_Power=[{}], oldItemState=[{}]".format(event.itemState, event.oldItemState)) elif charger_timer is not None and charger_timer.isAlive(): charger_timer.stop() battery_charging_monitor.log.info("Battery charging monitor: Canceled battery charging turn off timer: Outlet9_Power=[{}], oldItemState=[{}]".format(event.itemState, event.oldItemState))
class PandaPlayer( xbmc.Player ): def __init__( self, core=None, panda=None ): log.debug( "PandaPlayer.__init__( CORE, PANDA )" ) xbmc.Player.__init__( self ) self.panda = panda self.timer = None self.playNextSong_delay = 0.5 log.debug( "PandaPlayer.__init__ :: end" ) def playSong( self, item ): log.debug( "PandaPlayer.playSong()" ) log.debug( "PandaPlayer.playSong: item[url] %s" % item[0] ) log.debug( "PandaPlayer.playSong: item[item] %s" % item[1] ) self.play( item[0], item[1] ) log.debug( "PandaPlayer.playSong() :: end" ) def onPlayBackStarted( self ): try: log.debug( "PandaPlayer.onPlayBackStarted: %s" %self.getPlayingFile() ) except: pass log.debug( "PandaPlayer.onPlayBackStarted :: end" ) def onPlayBackEnded( self ): log.debug( "PandaPlayer.onPlayBackEnded()" ) self.stop() log.debug( "playing = %s" %self.panda.playing ) if self.timer and self.timer.isAlive(): self.timer.cancel() if self.panda.skip: self.panda.skip = False if self.panda.playing: self.timer = Timer( self.playNextSong_delay, self.panda.playNextSong ) self.timer.start() log.debug( "PandaPlayer.onPlayBackEnded() :: end" ) def onPlayBackStopped( self ): log.debug( "PandaPlayer.onPlayBackStopped()" ) self.stop() log.debug( "playing = %s" %self.panda.playing ) if self.timer and self.timer.isAlive(): self.timer.cancel() if self.panda.playing and self.panda.skip: self.panda.skip = False self.timer = Timer( self.playNextSong_delay, self.panda.playNextSong ) self.timer.start() else: if xbmc.getCondVisibility('Skin.HasSetting(PandoraVis)'): # show UI xbmc.executebuiltin('Skin.Reset(PandoraVis)') self.panda.stop() log.debug( "PandaPlayer.onPlayBackStopped() :: end" )
class PandaPlayer(xbmc.Player): def __init__(self, core=None, panda=None): #xbmc.Player.__init__(self, xbmc.PLAYER_CORE_MPLAYER) #xbmc.Player.__init__(self, xbmc.PLAYER_CORE_AUTO) xbmc.Player.__init__(self) self.panda = panda self.timer = None def playSong(self, item): self.play(item[0], item[1]) def play(self, url, item): xbmc.Player(xbmc.PLAYER_CORE_MPLAYER).play(url, item) #xbmc.Player(xbmc.PLAYER_CORE_AUTO).play(url, item) def onPlayBackStarted(self): print "PANDORA: onPlayBackStarted: %s" % self.getPlayingFile() if self.panda.playing: '''if not "pandora.com" in self.getPlayingFile(): self.panda.playing = False self.panda.quit() else: #Show Visualization (disappears after each song...)''' xbmc.executebuiltin("ActivateWindow( 12006 )") def onPlayBackEnded(self): print "PANDORA: onPlayBackEnded" self.stop() print "PANDORA: playing = %s" % self.panda.playing if self.timer and self.timer.isAlive(): self.timer.cancel() if self.panda.skip: self.panda.skip = False if self.panda.playing: self.timer = Timer(0.5, self.panda.playNextSong) self.timer.start() def onPlayBackStopped(self): print "PANDORA: onPlayBackStopped" self.stop() print "PANDORA: playing = %s" % self.panda.playing if self.timer and self.timer.isAlive(): self.timer.cancel() #catch for if this was actually a skip if self.panda.playing and self.panda.skip: self.panda.skip = False self.timer = Timer(0.5, self.panda.playNextSong) self.timer.start() else: if xbmc.getCondVisibility('Skin.HasSetting(PandoraVis)'): xbmc.executebuiltin('Skin.Reset(PandoraVis)') self.panda.Quit()
def runCommandPopenCommunicate(command, shell_True, timeout_sec_None, print_comand_True): run_successfully = False if not isinstance(command, str): command = ' '.join(command) command = shlex.split(command) if print_comand_True: print('Running: ' + ' '.join(command)) if shell_True: command = ' '.join(command) proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) else: proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) not_killed_by_timer = True if timeout_sec_None is None: stdout, stderr = proc.communicate() else: time_counter = Timer(timeout_sec_None, kill_subprocess_Popen, args=( proc, command, )) time_counter.start() stdout, stderr = proc.communicate() time_counter.cancel() not_killed_by_timer = time_counter.isAlive() stdout = stdout.decode("utf-8") stderr = stderr.decode("utf-8") if proc.returncode == 0: run_successfully = True else: if not print_comand_True and not_killed_by_timer: print('Running: ' + str(command)) if len(stdout) > 0: print('STDOUT') print(stdout) if len(stderr) > 0: print('STDERR') print(stderr) return run_successfully, stdout, stderr
def exec_with_timeout(self, cmd, timeout): proc = subprocess.Popen( cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE # Unused for now ) timer = Timer(timeout, proc.kill) try: timer.start() stdout, _ = proc.communicate() finally: if timer.isAlive(): timer.cancel() return stdout
def runQuery(txn, sql, columns, dbapi, timeout): """ execute a sql query. @param txn: database cursor @type txn: dbapi.cursor or adbapi.Transaction @param sql: sql operation @type sql: string @param columns: columns to return @type columns: list @param dbapi: dbapi module to use @type dbapi: module @param timeout: timeout in seconds @type timeout: int """ def _timeout(txn): if isinstance(txn, dbapi.Transaction): cursor = txn._cursor connection = txn._connection else: cursor = txn connection = getattr(cursor, 'connection', lambda:None)() if hasattr(connection, 'cancel'): connection.cancel() cursor.close() def _convert(val, type): if val is None: if type == dbapi.STRING: return '' if type == dbapi.NUMBER: return 0 return None if val and type == dbapi.NUMBER: return float(val) if type == dbapi.STRING: return str(val).strip() return val res = [] t = Timer(timeout, _timeout, txn) t.start() try: for q in re.split('[ \n]go[ \n]|;[ \n]', sql, re.I): if not q.strip(): continue txn.execute(q.strip()) except Exception, ex: if t.isAlive(): t.cancel() else: ex = TimeoutError('Query Timeout') raise ex
class PandaPlayer( xbmc.Player ): def __init__( self, core=None, panda=None ): xbmc.Player.__init__( self, xbmc.PLAYER_CORE_MPLAYER ) self.panda = panda self.timer = None def playSong( self, item ): print "PANDORA: Item 0 %s" % item[0] print "PANDORA: Item 1 %s" % item[1] self.play( item[0], item[1] ) def play( self, url, item ): xbmc.Player(xbmc.PLAYER_CORE_MPLAYER).play( url, item ) def onPlayBackStarted( self ): print "PANDORA: onPlayBackStarted: %s" %self.getPlayingFile() if self.panda.playing: if not "pandora.com" in self.getPlayingFile(): if not "p-cdn.com" in self.getPlayingFile(): self.panda.playing = False self.panda.quit() else: #Show Visualization (disappears after each song...) xbmc.executebuiltin( "ActivateWindow( 12006 )" ) def onPlayBackEnded( self ): print "PANDORA: onPlayBackEnded" self.stop() print "PANDORA: playing = %s" %self.panda.playing if self.timer and self.timer.isAlive(): self.timer.cancel() if self.panda.skip: self.panda.skip = False if self.panda.playing: self.timer = Timer( 0.5, self.panda.playNextSong ) self.timer.start() def onPlayBackStopped( self ): print "PANDORA: onPlayBackStopped" self.stop() print "PANDORA: playing = %s" %self.panda.playing if self.timer and self.timer.isAlive(): self.timer.cancel() if self.panda.playing: if self.panda.skip: self.panda.skip = False self.timer = Timer( 0.5, self.panda.playNextSong ) self.timer.start()
def main(stdscr): drawScreen(stdscr) stdscr.nodelay(True) t = Timer(5.0, drawSongs,args=[stdscr,]) t.start() fullScreenTimer=Timer(60.0, drawScreen,args=[stdscr,]) fullScreenTimer.start() x='blank' while x!='qq': try: if x!='blank': x=str(x) + str(stdscr.getkey(4,16)) selectedSongNumber=int(x)-1 if selectedSongNumber<100: addSong(stdscr,selectedSongNumber) x='blank' else: x=stdscr.getkey(4,15) except: if t.isAlive() == False: # logging.debug("Exception getting key\n") # logging.debug("t is alive: " + str(t.isAlive()) + "\n") t = Timer(7.0, drawSongs,args=[stdscr,]) t.start() # after 5 seconds if fullScreenTimer.isAlive()==False: fullScreenTimer=Timer(60.0, drawScreen,args=[stdscr,]) fullScreenTimer.start() pass stdscr.keypad(0);
def parseCard(self): string = {'string': ''} def cancelString(totalString): totalString['string'] = '' timeout = Timer(2.5, cancelString, [string]) while True: buffer = self.fp.read(8) for c in bytearray(buffer): if c > 29 and c < 41: if not timeout.isAlive(): timeout = Timer(2.5, cancelString, [string]) timeout.start() if c == 40: if timeout.isAlive(): timeout.cancel() return string['string'] else: if c == 39: string['string'] = string['string'] + '0' else: string['string'] = string['string'] + \ chr(c+19)
class PandaPlayer(xbmc.Player): def __init__(self, core=None, panda=None): xbmc.Player.__init__(self, xbmc.PLAYER_CORE_MPLAYER) self.panda = panda self.timer = None def playSong(self, item): print "PANDORA: Item 0 %s" % item[0] print "PANDORA: Item 1 %s" % item[1] self.play(item[0], item[1]) def play(self, url, item): xbmc.Player(xbmc.PLAYER_CORE_MPLAYER).play(url, item) def onPlayBackStarted(self): print "PANDORA: onPlayBackStarted: %s" % self.getPlayingFile() if self.panda.playing: if not "pandora.com" in self.getPlayingFile(): if not "p-cdn.com" in self.getPlayingFile(): self.panda.playing = False self.panda.quit() else: #Show Visualization (disappears after each song...) xbmc.executebuiltin("ActivateWindow( 12006 )") def onPlayBackEnded(self): print "PANDORA: onPlayBackEnded" self.stop() print "PANDORA: playing = %s" % self.panda.playing if self.timer and self.timer.isAlive(): self.timer.cancel() if self.panda.skip: self.panda.skip = False if self.panda.playing: self.timer = Timer(0.5, self.panda.playNextSong) self.timer.start() def onPlayBackStopped(self): print "PANDORA: onPlayBackStopped" self.stop() print "PANDORA: playing = %s" % self.panda.playing if self.timer and self.timer.isAlive(): self.timer.cancel() if self.panda.playing: if self.panda.skip: self.panda.skip = False self.timer = Timer(0.5, self.panda.playNextSong) self.timer.start()
class Player: def __init__(self, state): state.addObserver(self) self.currClip = None self.timer = None self.output = Output() self.state = state self.audioFile = None self.converter = None self.mp3file = None def update(self, observable, *args): # print "player: updates" if observable.currClip != self.currClip: self.stop() self.currClip = observable.currClip self.loadAudioFile() if observable.isPaused and self.output.isPlaying: self.stop() if not observable.isPaused and not self.output.isPlaying: self.play() def play(self): # print "is playing" self.timer = Timer(self.currClip[1] - float(self.audioFile.pos), self.atEndOfFile) self.output.play(self.audioFile, self.converter) self.timer.start() def stop(self): if self.timer and self.timer.isAlive(): self.timer.cancel() self.output.stop() def loadAudioFile(self): if self.mp3file != self.currClip[2]: self.mp3file = self.currClip[2] self.audioFile = AudioFile(self.mp3file) self.audioFile.pos = self.currClip[0] self.converter = Converter(self.audioFile, self.output) def atEndOfFile(self): self.output.stop() self.state.skip()
class PeriodicTimer(object): def __init__(self, frequency=60, *args, **kwargs): self.is_stopped = Event() self.is_stopped.clear() self.interval = frequency self.timer = Timer(self.frequency, self._check_for_event, ()) self.timer.daemon = True @property def interval(self): return self.frequency @interval.setter def interval(self, frequency): self.frequency = frequency self.stop() self.timer = Timer(self.frequency, self._check_for_event, ()) return self.frequency def action(self, action, *action_args, **kwargs): self._action = action self._action_args = action_args self._action_kwargs = kwargs def start(self): self.is_stopped.clear() if not self.timer.isAlive(): self.timer.start() def stop(self): self.is_stopped.set() def _check_for_event(self): while not self.is_stopped.isSet(): if self.is_stopped.isSet(): break if self._action: self._action(*self._action_args, **self._action_kwargs) else: self.stop() # Handle not so graceful shutdown try: self.is_stopped.wait(self.frequency) except: pass
def execute(command, args=[], cwd='.', shell=False, timeout=Timeouts.DEFAULT_POPEN_TIMEOUT): """ Execute command in cwd. :param command: str :param args: [] :param cwd: filepath :param shell: True/False :param timeout: float :return: int, [], [] """ try: logger.debug("Host Command: {} {}".format(command, args)) process = subprocess.Popen([command] + args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=cwd, shell=shell) try: # timer is created to kill the process after the timeout timer = Timer(float(timeout), process.kill) timer.start() output, error = process.communicate() if sys.version_info[0] == 3: output = output.decode('utf-8') error = error.decode('utf-8') finally: # If the timer is alive, that implies process exited within the timeout; # Hence stopping the timer task; if timer.isAlive(): timer.cancel() else: logger.error("Timer expired for the process. Process didn't \ finish within the given timeout of %f" % (timeout)) return_code = process.returncode logger.debug("Result Code (%d): stdout: (%s) stderr: (%s)" % (return_code, output, error)) return return_code, __format_output(output), __format_output(error) except OSError as error: return -1, [], __format_output(str(error))
def handle_input_event(event): logger.info("hardware: event detected at pin number " + str(event.pin_num)) #wait if input state persists for at least 0.2s, otherwise dismiss event unbounce_timer = Timer( interval=0.2, #time in seconds function=state_handler.handle_event, args=[states[event.pin_num]]) unbounce_timer.start() while unbounce_timer.isAlive(): #although event listeners receive rising edge event (-> voltage), # input pin value is 0 afterwards (why?) if (pfd.input_pins[event.pin_num].value == event.direction): logger.warning("hardware: event dismissed, it was a faulty " + "voltage drop") unbounce_timer.cancel() break
class PerpetualTimer: def __init__(self, t, h_function): self.t = t self.hFunction = h_function self.thread = Timer(self.t, self.handle_function) def handle_function(self): self.hFunction() self.thread = Timer(self.t, self.handle_function) self.thread.start() def start(self): self.thread.start() def cancel(self): self.thread.cancel() def is_alive(self): return self.thread.isAlive()
def run_module(self, port): t = Timer(5.0, self.__check_module_state) t.start() vectors = self._get_default_vector2() if not vectors: vectors = self.vectors.get_vectors_by_interpreters(self.modhandler.loaded_shells) for vector in vectors: self.last_vector = vector.name self.__execute_payload(vector, [port]) if t.isAlive(): t.cancel() if not self.done: self.last_vector = None self.mprint("[%s] No backdoor method worked. Assure port is not busy." % (self.name))
def run_module(self, port): t = Timer(5.0, self.__check_module_state) t.start() vectors = self._get_default_vector2() if not vectors: vectors = self.vectors.get_vectors_by_interpreters(self.modhandler.loaded_shells) for vector in vectors: self.last_vector = vector.name self.__execute_payload(vector, [port]) if t.isAlive(): t.cancel() if not self.done: self.last_vector = None self.mprint("[%s] No vector works. Check port '%i' availability and privileges" % (self.name, port))
def run_module(self, host, port): t = Timer(5.0, self.__check_module_state) t.start() vectors = self._get_default_vector2() if not vectors: vectors = self.vectors.get_vectors_by_interpreters(self.modhandler.loaded_shells) for vector in vectors: self.last_vector = vector.name self.__execute_payload(vector, [host, port]) if t.isAlive(): t.cancel() if not self.done: self.last_vector = None self.mprint("[%s] No reverse backdoor method worked. Assure remote port is" % (self.name)) self.mprint("[%s] listening using commands like \'nc -v -l -p <port>\'" % (self.name))
class timer_message_mqtt(Thread): def __init__(self, duree, fonction, args=[], kwargs={}): Thread.__init__(self) self.duree = duree self.fonction = fonction self.args = args self.kwargs = kwargs self.encore = True # pour permettre l'arret a la demande def run(self): while self.encore: self.timer = Timer(self.duree, self.fonction, self.args, self.kwargs) self.timer.setDaemon(True) self.timer.start() self.timer.join() def stop(self): self.encore = False # pour empecher un nouveau lancement de Timer et terminer le thread if self.timer.isAlive(): self.timer.cancel() # pour terminer une eventuelle attente en cours de Timer
def runWithArguments(self, arguments, timeout=10): """ Execute test script with arguments. """ test_file = os.path.join( os.path.dirname(__file__), 'helper_sys_argv.py') command = [sys.executable, test_file] command.extend(arguments) proc = subprocess.Popen( command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, ) timer = Timer(timeout, lambda p: p.kill(), [proc]) timer.start() stdout, stderr = proc.communicate() timer.cancel() timer.join(timeout=10) if timer.isAlive(): raise AssertionError('Timeout thread is still alive.') return stdout, stderr
def run_module(self, port): t = Timer(5.0, self.__check_module_state) t.start() vectors = self._get_default_vector2() if not vectors: vectors = self.vectors.get_vectors_by_interpreters( self.modhandler.loaded_shells) for vector in vectors: self.last_vector = vector.name self.__execute_payload(vector, [port]) if t.isAlive(): t.cancel() if not self.done: self.last_vector = None self.mprint( "[%s] No vector works. Check port '%i' availability and privileges" % (self.name, port))
def run_rng(self, rng_path): """ Run the RNG through JingTrang :param rng_path: Path to the RelaxNG file to run against the XML to test """ test = subprocess.Popen( ["java", "-Duser.country=US", "-Duser.language=en", "-jar", TESTUnit.JING, rng_path, self.path], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False ) out = [] error = [] timer = Timer(self.timeout, test.kill) try: timer.start() out, error = test.communicate() except Exception as E: self.error(E) yield False pass finally: if not timer.isAlive(): self.log("Timeout on RelaxNG") yield False timer.cancel() pass timer.cancel() # This is to deal with Travis printing a message about the _JAVA_OPTIONS when a java command is run # Travis printing this command resulted in this test not passing out = '\n'.join([x for x in out.decode().split('\n') if '_JAVA_OPTIONS' not in x]).encode() error = '\n'.join([x for x in error.decode().split('\n') if '_JAVA_OPTIONS' not in x]).encode() if len(out) > 0: for issue in TESTUnit.rng_logs(out): self.log(issue) self.dtd_errors.append(issue) yield len(out) == 0 and len(error) == 0
def run_module(self, port): t = Timer(5.0, self.__check_module_state) t.start() vectors = self._get_default_vector2() if not vectors: vectors = self.vectors.get_vectors_by_interpreters( self.modhandler.loaded_shells) for vector in vectors: self.last_vector = vector.name self.__execute_payload(vector, [port]) if t.isAlive(): t.cancel() if not self.done: self.last_vector = None self.mprint( "[%s] No backdoor method worked. Assure port is not busy." % (self.name))
def runCommandPopenCommunicate(command, shell_True, timeout_sec_None, print_comand_True): run_successfully = False if not isinstance(command, basestring): command = ' '.join(command) command = shlex.split(command) if print_comand_True: print 'Running: ' + ' '.join(command) if shell_True: command = ' '.join(command) proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) else: proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) not_killed_by_timer = True if timeout_sec_None is None: stdout, stderr = proc.communicate() else: timer = Timer(timeout_sec_None, kill_subprocess_Popen, args=(proc, command,)) timer.start() stdout, stderr = proc.communicate() timer.cancel() not_killed_by_timer = timer.isAlive() if proc.returncode == 0: run_successfully = True else: if not print_comand_True and not_killed_by_timer: print 'Running: ' + str(command) if len(stdout) > 0: print 'STDOUT' print stdout.decode("utf-8") if len(stderr) > 0: print 'STDERR' print stderr.decode("utf-8") return run_successfully, stdout, stderr
def main(): """The application starts the UI on X and wait at least 60 seconds a choice. Passed 60 seconds it proceeds with default choice. Args: --debug (optional) : save on file the stdout/stderr of choiced application. """ # command line argument to enable logging facility if len(sys.argv) > 1 and sys.argv[1] == '--debug': logging.basicConfig(filename='gui_execution.log', level=logging.DEBUG, format='%(asctime)s - %(process)d - %(levelname)s - %(message)s') logging.info("Enable save on file std output/error.") gui = RaspbianWelcomeGui(True) else: gui = RaspbianWelcomeGui() # separate thread that wait 60 seconds # after timeout it starts kodi as default option timer = Timer(60, lambda: [logging.debug('Timeout occurs'), gui.click(session_commands[default_session])]) timer.daemon = True try: # start timer timer.start() gui.handle_event() finally: if timer.isAlive(): logging.debug('After exit mainloop Timer is alive so cancel it') timer.cancel() else: logging.debug('After exit mainloop Timer is dead') os._exit(0)
class EmbeddedLog(object): """ EmbeddedLog implementation """ def __init__(self, device): """ Constructor :type device: object :param device: instance of the device """ self._dut_instance = device self._current_time = datetime.now().strftime("%Y-%m-%d_%Hh%M.%S") # Device general parameters self._wait_btwn_cmd = self._dut_instance.get_config( "waitBetweenCmd", 5, float) self._uecmd_default_timeout = self._dut_instance.get_config( "defaultTimeout", 50, int) # This option will force to retrieve all embedded logs, to be sure to have crash logs, ap logs ... # on critical failures retrieve_device_log_on_critical = self._dut_instance.get_config( "retrieveDeviceLogOnCriticalFailure", "False", "str_to_bool") # Application log parameters self._application_log_enable = self._dut_instance.get_config( "retrieveApplicationLog", "True", "str_to_bool") or retrieve_device_log_on_critical device_name = self._dut_instance.whoami().get("device", "") if not device_name: error_msg = "Device name cannot be found from device instance!" raise AcsConfigException(AcsConfigException.INSTANTIATION_ERROR, error_msg) report_tree = self._dut_instance.get_report_tree() # Create everything application logging requires only if required if self._application_log_enable: self._clean_application_logs = self._dut_instance.get_config( "cleanApplicationLog", "True", "str_to_bool") or retrieve_device_log_on_critical self._application_log_options = self._dut_instance.get_config( "applicationLogOptions", "pull_timer:60,aplog_pull_timeout:60,log_location:/logs", "str_to_dict") self._application_log_folder = report_tree.get_subfolder_path( subfolder_name=LOG_SUBFOLDERS[0], device_name=device_name) self._aplog_reg_device = re.compile(APLOG_PATTERN_DEVICE) self._aplog_reg_host = re.compile(APLOG_PATTERN_HOST) self._aplog_group_id = 1 self.__aplog_timer = None self.__is_application_logger_started = False # Modem log parameters self._modem_log_enable = self._dut_instance.get_config( "retrieveModemTrace", "False", "str_to_bool") # Create everything modem logging requires only if required if self._modem_log_enable: self._clean_modem_logs = self._dut_instance.get_config( "cleanModemTrace", "False", "str_to_bool") self._modem_trace_options = self._dut_instance.get_config( "modemTraceOptions", "hsi_speed:h,trace_level:2,trace_location:emmc,file_size_option:1,pull_timeout:60", "str_to_dict") self._modem_log_folder = report_tree.get_subfolder_path( subfolder_name=LOG_SUBFOLDERS[1], device_name=device_name) self._modem_api = self._dut_instance.get_uecmd("Modem") self.__is_modem_logger_started = False # PTI log parameters self._pti_log_enable = self._dut_instance.get_config( "retrievePTITrace", "False", "str_to_bool") # Create everything PTI logging requires only if required if self._pti_log_enable: self._pti_probe = self._dut_instance.get_config("PTIProbe", "FIDO") self._pti_cmdline = self._dut_instance.get_config( "enableAplogptiCmdLine", "adb shell setprop persist.service.aplogpti.enable 1") self._pti_log_folder = report_tree.get_subfolder_path( subfolder_name=LOG_SUBFOLDERS[2], device_name=device_name) self._pti_log = None self._pti_log_file = None self._total_pti_log = 0 self.__init_pti_log() # UART (Serial) log parameters # It is possible to log on several serial port at the same time # For each port, the user can decide to listen to it or not: # Value of retrieveSerialTrace is an ordered list of booleans separated by ; self._serial_log_enable_list = self._dut_instance.get_config( "retrieveSerialTrace", "False").replace(" ", "").split(";") self._num_of_serial_confs = len(self._serial_log_enable_list) # Value of serialPort is an ordered list of string separated by ; self._serial_port_list = self._dut_instance.get_config( "serialPort", "COM1").replace(" ", "").split(";") num_of_serial_port_confs = len(self._serial_port_list) # Value of serialBaudRate is an ordered list of integers separated by ; self._serial_baudrate_list = self._dut_instance.get_config( "serialBaudRate", "115200").replace(" ", "").split(";") num_of_serial_baud_rate_confs = len(self._serial_baudrate_list) # Value of serialHdwFlowControl is an ordered list of integers separated by ; self._serial_hw_flow_control_list = self._dut_instance.get_config( "serialHdwFlowControl", "False").replace(" ", "").split(";") num_of_serial_hw_control_confs = len(self._serial_hw_flow_control_list) # Check that confs are complete, no element are missing if not all(x == self._num_of_serial_confs for x in (num_of_serial_port_confs, num_of_serial_baud_rate_confs, num_of_serial_hw_control_confs)): raise AcsConfigException(AcsConfigException.INVALID_BENCH_CONFIG, "Missing serial log information") # Create everything serial (UART) logging requires only if required, for all ports conf_id = 0 self._serial_confs = {} is_serial_conf_enable = False # Add all serial configuration to a same dictionary while conf_id < self._num_of_serial_confs: self._serial_confs[(ENABLE + str(conf_id))] = str_to_bool( self._serial_log_enable_list[conf_id]) if self._serial_confs[(ENABLE + str(conf_id))]: is_serial_conf_enable = True if self._serial_port_list[conf_id] is not "": self._serial_confs[ PORT + str(conf_id)] = self._serial_port_list[conf_id] else: self._serial_confs[PORT + str(conf_id)] = "COM1" if self._serial_baudrate_list[conf_id].isdigit(): self._serial_confs[BAUDRATE + str(conf_id)] = int( self._serial_baudrate_list[conf_id]) else: self._serial_confs[BAUDRATE + str(conf_id)] = 115200 self._serial_confs[HWCTRLFLOW + str(conf_id)] = str_to_bool( self._serial_hw_flow_control_list[conf_id]) conf_id += 1 # Create folder for SERIAL only if required if is_serial_conf_enable: self._serial_log_folder = report_tree.get_subfolder_path( subfolder_name=LOG_SUBFOLDERS[3], device_name=device_name) self.__init_serial_log() # Callback dictionary self.__start_log = { "MODEM": self.__start_modem_log, "APPLICATION": self.__start_application_log, "PTI": self.__start_pti_log, "SERIAL": self.__start_serial_log } self.__stop_log = { "MODEM": self.__stop_modem_log, "APPLICATION": self.__stop_application_log, "PTI": self.__stop_pti_log, "SERIAL": self.__stop_serial_log } self.__retrieve_log = { "MODEM": self.__retrieve_modem_log, "APPLICATION": self.__retrieve_application_log } self.__erase_log = {"APPLICATION": self.__erase_application_log} def __init_pti_log(self): """ Depends on device config Init logging capabilities: PTI, LTB """ from acs.Device.DeviceLogger.MptaLogger.MptaLogger import MptaLogger self._pti_log = MptaLogger(self._pti_probe) self._pti_log_file = os.path.join( self._pti_log_folder, str(self._dut_instance.get_phone_model() + "_" + self._current_time)) def __init_serial_log(self): """ Constructor of the logger """ from acs.Device.DeviceLogger.SerialLogger.SerialLogger import SerialLogger conf_id = 0 while conf_id < self._num_of_serial_confs: serial_logger = None if self._serial_confs[(ENABLE + str(conf_id))]: self._serial_confs[PORT_NAME + str(conf_id)] = self._serial_confs[ PORT + str(conf_id)].replace( os.sep, "_") serial_logger = SerialLogger(self) self._serial_confs[SERIAL_LOG_FILE + str(conf_id)] = \ os.path.join(self._serial_log_folder, str(self._dut_instance.get_phone_model() + "_" + self._current_time + "_SERIAL_" + self._serial_confs[PORT_NAME + str(conf_id)] + ".log")) serial_logger.set_output_file( self._serial_confs[SERIAL_LOG_FILE + str(conf_id)]) if serial_logger.configure( com_port=self._serial_confs[PORT + str(conf_id)], baudrate=self._serial_confs[BAUDRATE + str(conf_id)], hdw_flow_control=self._serial_confs[ HWCTRLFLOW + str(conf_id)]) != Global.SUCCESS: serial_logger = None else: self._serial_confs[SERIAL_LOGGER + str(conf_id)] = serial_logger conf_id += 1 def __start_application_log_timer(self, timer=None): """ Start the timer to retrieve application logs from the device """ try: # Start timer to retrieve application log if timer is None: aplog_timer = int(self._application_log_options["pull_timer"]) else: aplog_timer = timer self.__aplog_timer = Timer(aplog_timer, self.__retrieve_application_log, [True]) self.__aplog_timer.name = "EmbeddedLog:ApplicationLogTimer" self.__aplog_timer.daemon = True self.__aplog_timer.start() self.__is_application_logger_started = True except Exception as ex: # pylint: disable=W0703 self._dut_instance.get_logger().debug( "Unable to launch aplog timer : %s" % (str(ex), )) def __start_application_log(self): """ Start the application log for the device. Usually call after a first successful connection to the device. """ if self._application_log_enable and not self.__is_application_logger_started: if self._dut_instance.get_config("cleanLogcat", "True", "str_to_bool"): self.__erase_application_log() self._dut_instance.get_logger().debug( "Starting application log...") if self.__aplog_timer is not None: try: if self.__aplog_timer.isAlive(): self.__aplog_timer.cancel() except Exception as ex: # pylint: disable=W0703 self._dut_instance.get_logger().debug( "Unable to initialize aplog timer : %s" % (str(ex), )) finally: self.__aplog_timer = None # Start the application log timer self.__start_application_log_timer(1) aplog_timer = int(self._application_log_options["pull_timer"]) self._dut_instance.get_logger().debug( "Application log started: retrieving aplog every %d s" % (aplog_timer, )) def __copy_aplog_files_on_host(self): """ Pull aplogs from device to host as temporary file. Those temporary files will be renamed into their final name at the end of the campaign """ # Get aplog options pull_timeout = int(self._application_log_options["aplog_pull_timeout"]) aplog_folder = self._application_log_options["log_location"] try: # Create temporary folder (if needed) to store aplogs tmp_aplog_folder = os.path.join(self._application_log_folder, TMP_FOLDER) if not os.path.isdir(tmp_aplog_folder): os.makedirs(tmp_aplog_folder) # Always retrieve current aplog file self._dut_instance.pull( remotepath="{0}/aplog".format(aplog_folder), localpath=self._application_log_folder, timeout=pull_timeout, silent_mode=True, force_execution=True) # List all generated aplog # This command will return a list of aplog files with its unique id # obtained by checksum algorithm # ls aplog.* | xargs cksum # 2893497734 5120005 aplog.1 # 1797278614 5120010 aplog.2 # 1086330118 5120066 aplog.3 # 3387085104 5120071 aplog.4 # 1017804844 5120021 aplog.5 # 1304156047 5120280 aplog.6 # 3117525335 5120212 aplog.7 # checksum file size file name output_cmd = self._dut_instance.run_cmd( cmd="adb shell ls %s/aplog.* | xargs cksum" % (aplog_folder, ), timeout=self._uecmd_default_timeout, silent_mode=True, force_execution=True) if output_cmd[ 0] != Global.FAILURE and "No such file" not in output_cmd[ 1]: # List aplog checksum on host aplog_checksum_list_on_host = [ self._aplog_reg_host.findall(x)[0][2] for x in os.listdir(tmp_aplog_folder) if self._aplog_reg_host.findall(x) ] # List aplog files on device # It is a list of tuple of 3 elements : aplog checksum, aplog size and aplog index aplog_files_on_device = [ tuple(self._aplog_reg_device.findall(x)[0]) for x in output_cmd[1].splitlines() if self._aplog_reg_device.findall(x) ] # Pull files which are not on host aplog_file_pulled = False for aplog_checksum, _, aplog_index in aplog_files_on_device: # If the file does not exists we pull it if aplog_checksum not in aplog_checksum_list_on_host: remote_path = "{0}/aplog.{1}".format( aplog_folder, aplog_index) aplog_file_to_pull = os.path.join( tmp_aplog_folder, "{0}.aplog.{1}.{2}".format(self._aplog_group_id, aplog_index, aplog_checksum)) self._dut_instance.pull(remotepath=remote_path, localpath=aplog_file_to_pull, timeout=pull_timeout, silent_mode=True) aplog_file_pulled = True if aplog_file_pulled: self._aplog_group_id += 1 except DeviceException as device_exception: error_msg = "Error when retrieving aplog: %s" % ( device_exception.get_error_message(), ) self._dut_instance.get_logger().error(error_msg) def __retrieve_application_log(self, relaunch_timer=False): """ Fetch application logs from devices, if any. """ if self._application_log_enable: try: if self._dut_instance.is_available(): self.__copy_aplog_files_on_host() else: warning_msg = "Retrieving aplog not possible, no adb connection to the device" self._dut_instance.get_logger().warning(warning_msg) # Relaunch timer if needed if self.__is_application_logger_started and relaunch_timer: self.__start_application_log_timer() elif self.__aplog_timer and self.__aplog_timer.isAlive(): self.__aplog_timer.cancel() self.__aplog_timer = None except (KeyboardInterrupt, SystemExit): # Skip pulling and stop properly the application log mechanism self._dut_instance.get_logger().debug( "Received ctrl-c when retrieving aplog !") self.__stop_application_log() raise def __rename_tmp_application_log(self): """ Rename aplog located in tmp folder into final name(i.e: aplog.1, aplog.2 ...) """ # Rename aplog files located in temporary folder tmp_aplog_folder = os.path.join(self._application_log_folder, TMP_FOLDER) if os.path.exists(tmp_aplog_folder): try: # List and sort application logs aplog_index = 1 for aplog_group_id in range(self._aplog_group_id, 0, -1): # Greater group id contains more recent aplog files aplog_files = sorted([ x for x in os.listdir(tmp_aplog_folder) if x.find("{0}.aplog.".format(aplog_group_id)) != -1 ]) for aplog_file in aplog_files: shutil.copy( os.path.join(tmp_aplog_folder, aplog_file), os.path.join(self._application_log_folder, "aplog.{0}".format(aplog_index))) aplog_index += 1 # Remove temporary folder shutil.rmtree(tmp_aplog_folder, ignore_errors=True) except (shutil.Error, _SysErrors) as shutil_error: self._dut_instance.get_logger().debug( "Error when formatting aplog: %s" % (str(shutil_error), )) else: self._dut_instance.get_logger().debug("No aplog retrieved") def __stop_application_log(self): """ Stop and format the application log of the device. """ if self._application_log_enable and self.__is_application_logger_started: self._dut_instance.get_logger().debug( "Stopping application log...") # Disable timer to retrieve application logs try: if self.__aplog_timer and self.__aplog_timer.isAlive(): self.__aplog_timer.cancel() self.__aplog_timer = None except Exception as ex: # pylint: disable=W0703 self._dut_instance.get_logger().debug( "Error occurred when cancelling aplog timer : %s" % (str(ex), )) # Retrieve application one last time before stopping self.__retrieve_application_log() self.__is_application_logger_started = False self._dut_instance.get_logger().debug("Application log stopped") def __erase_application_log(self): """ Erase the application log of the device. """ # Clean application logs if self._application_log_enable: # Rename application logs self.__rename_tmp_application_log() if self._clean_application_logs: self._dut_instance.get_logger().debug( "Erasing application log...") if self._dut_instance.is_available(): aplog_folder = self._application_log_options[ "log_location"] self._dut_instance.run_cmd("adb shell rm %s/aplog.*" % (aplog_folder, ), self._uecmd_default_timeout, silent_mode=True) self._dut_instance.get_logger().debug( "Application log erased") else: warning_msg = "Erasing aplog not possible, no adb connection to the device" self._dut_instance.get_logger().warning(warning_msg) def __start_modem_log(self): """ Starts the modem log for the device. Usually call after a first successful connection to the device. """ if self._modem_log_enable and not self.__is_modem_logger_started: self._dut_instance.get_logger().debug("Starting modem log...") if self._dut_instance.is_available(): # Retrieve modem trace option hsi_speed = self._modem_trace_options["hsi_speed"] trace_level = self._modem_trace_options["trace_level"] trace_location = self._modem_trace_options["trace_location"] file_size_option = self._modem_trace_options[ "file_size_option"] # Configure Modem Trace self._modem_api.configure_modem_trace(hsi_speed, int(trace_level)) # Wait a while time.sleep(self._wait_btwn_cmd) # Reboot phone self._dut_instance.reboot(wait_for_transition=True) # Activate trace_modem if trace_location.lower() == "sdcard": trace_location = BPLOG_LOC.SDCARD # pylint:disable=E1101 else: trace_location = BPLOG_LOC.EMMC # pylint:disable=E1101 self._modem_api.activate_modem_trace(trace_location, int(file_size_option)) self.__is_modem_logger_started = True self._dut_instance.get_logger().debug("Modem log started") else: warning_msg = "Starting modem log not possible, no adb connection to the device" self._dut_instance.get_logger().warning(warning_msg) def __retrieve_modem_log(self): """ Fetch modem logs from the device, if any. """ if self._modem_log_enable and self.__is_modem_logger_started: if self._dut_instance.is_available(): # Retrieve pull timeout pull_timeout = int(self._modem_trace_options["pull_timeout"]) trace_location = self._modem_trace_options["trace_location"] # Retrieve modem logs list if trace_location.lower() == "sdcard": modem_log_path = "/sdcard/logs" else: modem_log_path = "/logs" output_cmd = self._dut_instance.run_cmd( "adb shell ls %s/bplog*" % (modem_log_path, ), self._uecmd_default_timeout, silent_mode=True) if output_cmd[ 0] == Global.FAILURE or "No such file" in output_cmd[1]: self._dut_instance.get_logger().debug( "Error retrieving modem log: %s" % (output_cmd[1], )) else: # Fetch all modem logs list_files = [ str(x).strip() for x in output_cmd[1].splitlines() ] for modem_log_filename in list_files: try: self._dut_instance.get_logger().debug( "Retrieving modem log...") self._dut_instance.pull(modem_log_filename, self._modem_log_folder, pull_timeout) self._dut_instance.get_logger().debug( "Modem log retrieved") except (KeyboardInterrupt, SystemExit): # Stop properly the modem log mechanism self._dut_instance.get_logger().debug( "Received ctrl-c when retrieving modem log !") self.__stop_modem_log() raise except DeviceException as device_exception: self._dut_instance.get_logger().debug( "Error when retrieving modem log: %s" % (device_exception.get_error_message(), )) break else: warning_msg = "Retrieving modem log not possible, no adb connection to the device" self._dut_instance.get_logger().warning(warning_msg) def __stop_modem_log(self): """ Stop the modem log of the device. Clean logs if needed """ if self._modem_log_enable and self.__is_modem_logger_started: self._dut_instance.get_logger().debug("Stopping modem log...") if self._dut_instance.is_available(): # stopping service self._modem_api.deactivate_modem_trace() # stop tracing self._modem_api.configure_modem_trace("u", 0) # Clean modem logs if self._clean_modem_logs: trace_location = self._modem_trace_options[ "trace_location"] if trace_location.lower() == "sdcard": modem_log_path = "/sdcard/logs" else: modem_log_path = "/logs" self._dut_instance.run_cmd("adb shell rm %s/bplog*" % (modem_log_path, ), self._uecmd_default_timeout, silent_mode=True) self.__is_modem_logger_started = False self._dut_instance.get_logger().debug("Modem log stopped") else: warning_msg = "Stopping modem log not possible, no adb connection to the device" self._dut_instance.get_logger().warning(warning_msg) def __start_pti_log(self): """ Starts the pti log for the device. The probe can be either PTI or LTB. """ if self._pti_log_enable and self._pti_log: self._dut_instance.get_logger().debug("Starting PTI log...") self._total_pti_log += 1 # pylint: disable=W0703 # Agree to catch and report all exceptions # while starting pti logger try: self._pti_log.set_output_file(self._pti_log_file + "_PTI_" + str(self._total_pti_log)) self._pti_log.start() self._dut_instance.get_logger().debug("PTI log started") except Exception as exception: self._dut_instance.get_logger().error( "Exception when starting PTI log [%s]", str(exception)) def __stop_pti_log(self): """ Stops the pti log for the device. """ if self._pti_log_enable and self._pti_log: self._dut_instance.get_logger().debug("Stopping PTI log...") # pylint: disable=W0703 # Agree to catch and report all exceptions # while disconnecting the pti try: self._pti_log.stop() self._dut_instance.get_logger().debug("PTI log stopped") except Exception as exception: self._dut_instance.get_logger().error( "Exception when stopping PTI log [%s]", str(exception)) def __start_serial_log(self): """ Starts the serial (UART) log for the device. """ conf_id = 0 while conf_id < self._num_of_serial_confs: if self._serial_confs[ (ENABLE + str(conf_id))] and self._serial_confs[SERIAL_LOGGER + str(conf_id)]: self._dut_instance.get_logger().debug( "Starting SERIAL (UART) log on port %s ...", self._serial_confs[PORT_NAME + str(conf_id)]) try: if self._serial_confs[ SERIAL_LOGGER + str(conf_id)].start() == Global.SUCCESS: self._dut_instance.get_logger().debug( "SERIAL (UART) log started on port %s", self._serial_confs[PORT_NAME + str(conf_id)]) except Exception as exception: self._dut_instance.get_logger().error( "Exception when starting SERIAL (UART) log on port %s [%s]", self._serial_confs[PORT_NAME + str(conf_id)], str(exception)) conf_id += 1 def __stop_serial_log(self): """ Stops the serial (UART) log for the device. """ conf_id = 0 while conf_id < self._num_of_serial_confs: if self._serial_confs[ (ENABLE + str(conf_id))] and self._serial_confs[SERIAL_LOGGER + str(conf_id)]: self._dut_instance.get_logger().debug( "Stopping SERIAL (UART) log on port %s ...", self._serial_confs[PORT_NAME + str(conf_id)]) try: self._serial_confs[SERIAL_LOGGER + str(conf_id)].stop() self._dut_instance.get_logger().debug( "SERIAL (UART) log stopped on port %s", self._serial_confs[PORT_NAME + str(conf_id)]) except Exception as exception: self._dut_instance.get_logger().error( "Exception when stopping SERIAL (UART) log on port % [%s]", self._serial_confs[PORT_NAME + str(conf_id)], str(exception)) conf_id += 1 def start(self, log_type): """ Start embedded logs :type log_type: string :param log_type: Log to retrieve Possible values: MODEM, APPLICATION, PTI, SERIAL """ dict_key = str(log_type).upper() if dict_key not in self.__start_log: self._dut_instance.get_logger().debug( "Unable to start %s logs ! Feature not implemented." % (str(log_type), )) else: # Call method from callback dictionary self.__start_log[dict_key]() def stop(self, log_type): """ Stop embedded logs :type log_type: string :param log_type: Log to retrieve Possible values: MODEM, APPLICATION, PTI, SERIAL """ dict_key = str(log_type).upper() if dict_key not in self.__stop_log: self._dut_instance.get_logger().debug( "Unable to stop %s logs ! Feature not implemented." % (str(log_type), )) else: # Call method from callback dictionary self.__stop_log[dict_key]() def retrieve_log(self, log_type): """ Retrieve embedded logs :type log_type: string :param log_type: Log to retrieve Possible values: MODEM, APPLICATION """ dict_key = str(log_type).upper() if dict_key not in self.__retrieve_log: self._dut_instance.get_logger().debug( "Unable to retrieve %s logs ! Feature not implemented." % (str(log_type), )) else: # Call method from callback dictionary self.__retrieve_log[dict_key]() def erase_log(self, log_type): """ Erase embedded logs :type log_type: string :param log_type: Log to erase Possible values: APPLICATION """ dict_key = str(log_type).upper() if dict_key not in self.__erase_log: self._dut_instance.get_logger().debug( "Unable to erase %s logs ! Feature not implemented." % (str(log_type), )) else: # Call method from callback dictionary self.__erase_log[dict_key]() def enable_log_on_pti(self): """ Enable pti log """ if self._pti_log_enable: self._dut_instance.get_logger().info( "Forwarding logcat log to PTI log ...") if self._dut_instance.is_available(): self._dut_instance.run_cmd(self._pti_cmdline, 1) self._dut_instance.get_logger().info( "Logcat log forwarded to PTI log") else: warning_msg = "Forwarding logcat log on PTI not possible, no adb connection to the device" self._dut_instance.get_logger().warning(warning_msg)
import signal i = 0 def say(word): global i i += 1 if i < 5: printsay( word, i, ) t.run() else: t.cancel() def printsay(word, i): time.sleep(10) print word, i t = Timer(1, say, ['hello']) t.start() while t.isAlive(): print('waiting for thread to close') t.join(1) print 'testing', t.isAlive() print 'khatam'
class Delta(BaseClient): def __init__(self, account, channels, product_id, callback=None): super().__init__() self.logger = logging.getLogger(__name__) self.timer = Timer(40.0, self.ping) self.api_key = account['api_key'] self.api_secret = account['api_secret'] if account['chain'] == 'testnet': self.delta_base_url = "https://testnet-api.delta.exchange" self.ws_endpoint = "wss://testnet-api.delta.exchange:2096" elif account['chain'] == 'mainnet': self.delta_base_url = "https://api.delta.exchange" self.ws_endpoint = "wss://api.delta.exchange:2096" elif account['chain'] == 'devnet': self.delta_base_url = "https://devnet-api.delta.exchange" self.ws_endpoint = "wss://devnet-api.delta.exchange:2096" else: raise Exception('InvalidDeltaChain') self.channels = channels self.product_id = product_id self.callback = callback self.callbacks = {} self.data = { 'positions': {}, 'mark_price': {}, 'l2_orderbook': [], 'spot_price': {} } self.last_seen = {'positions': {}, 'mark_price': {}, 'spot_price': {}} self.exited = True self.delta_client = self.connect_rc() self.product = self.get_product(product_id) self.contract_size = round_price_by_tick_size( self.product['contract_value'], self.product['contract_value']) self.tick_size = round_price_by_tick_size(self.product['tick_size'], self.product['tick_size']) self.isInverse = self.product['product_type'] == 'inverse_future' self.isQuanto = self.product['is_quanto'] self.symbol = self.product['symbol'] if self.channels: self.connect() def __auth(self): if not self.ws: raise Exception('Need to establish a socket connection first') method = 'GET' timestamp = get_time_stamp() path = '/live' signature_data = method + timestamp + path signature = generate_signature(self.api_secret, signature_data) self.ws.send( json.dumps({ "type": "auth", "payload": { "api-key": self.api_key, "signature": signature, "timestamp": timestamp } })) def __on_error(self, error): self.logger.info(error) def __on_open(self): self.logger.info("Delta Websocket Opened.") def __on_close(self): self.logger.info('Delta Websocket Closed.') if not self.exited: self.reconnect() def __on_message(self, message): self.__restart_ping_timer() try: message = json.loads(message) if 'type' in message: event = message['type'] if event == 'l2_orderbook': self.orderbook_updates(message) elif event in [ 'fill', 'self_trade', 'pnl', 'liquidation', 'adl', 'stop_trigger', 'stop_cancel' ]: if 'trading_notifications' in self.callbacks: self.callbacks['trading_notifications'](message) elif event == 'positions': self.position_updates(message) elif event == 'mark_price': self.mark_price_update(message) elif event == 'spot_price': self.spot_price_update(message) else: pass elif 'message' in message: self.logger.info(message['message']) except Exception: pass def __restart_ping_timer(self): if self.timer.isAlive(): self.timer.cancel() self.timer = Timer(40.0, self.ping) self.timer.start() def connect_rc(self): delta_client = DeltaRestClient(base_url=self.delta_base_url, api_key=self.api_key, api_secret=self.api_secret) return delta_client def connect(self): self.ws = websocket.WebSocketApp(self.ws_endpoint, on_message=self.__on_message, on_close=self.__on_close, on_open=self.__on_open, on_error=self.__on_error) self.wst = threading.Thread(name='Delta Websocket', target=lambda: self.ws.run_forever( ping_interval=30, ping_timeout=10)) self.wst.daemon = True self.wst.start() conn_timeout = 5 while not self.ws.sock or not self.ws.sock.connected and conn_timeout: sleep(1) conn_timeout -= 1 if not conn_timeout: self.logger.debug( "Couldn't establish connetion with Delta websocket") else: self.exited = False if self.api_key and self.api_secret: self.__auth() sleep(2) for channel_name in self.channels: self.subscribeChannel(channel_name, self.symbol, self.callback) def is_thread_alive(self): return (self.wst and self.wst.is_alive()) def disconnect(self): self.exited = True self.timer.cancel() self.ws.close() self.callbacks.clear() # self.wst.join() self.logger.info("Delta Websocket Disconnected.") def reconnect(self): self.disconnect() self.connect() def isConnected(self): return self.ws.sock and self.ws.sock.connected def ping(self): self.ws.send(json.dumps({"type": "ping"})) def orderbook_updates(self, result): orderbooks = list( filter(lambda x: x['symbol'] == result['symbol'], self.data['l2_orderbook'])) if orderbooks: orderbook = orderbooks[0] orderbook['last_seen'] = time.time() orderbook['bids'] = result['buy'] orderbook['asks'] = result['sell'] else: orderbook = { 'last_seen': time.time(), 'symbol': result['symbol'], 'asks': result['buy'], 'bids': result['sell'] } self.data['l2_orderbook'].append(orderbook) def position_updates(self, result): product_id = result['product_id'] position = get_position(entry_price=round_price_by_tick_size( result['entry_price'], self.tick_size), size=int(result['size'])) position['margin'] = Decimal(result['margin']) position['liquidation_price'] = Decimal(result['liquidation_price']) self.data['positions'][product_id] = position self.last_seen['positions'][product_id] = time.time() def mark_price_update(self, result): product_id = result['product_id'] self.data['mark_price'][product_id] = Decimal(result['price']) self.last_seen['mark_price'][product_id] = time.time() def spot_price_update(self, result): spot_symbol = result['symbol'] self.data['spot_price'][spot_symbol] = Decimal(result['price']) self.last_seen['spot_price'][spot_symbol] = time.time() """ ******* SOCKET METHODS ******* """ def subscribeChannel(self, channel_name, symbol, callback=None): self.ws.send( json.dumps({ "type": "subscribe", "payload": { "channels": [{ "name": channel_name, "symbols": [symbol] }] } })) self.callbacks[channel_name] = callback def unsubscribeChannel(self, channel_name): self.ws.send( json.dumps({ "type": "unsubscribe", "channels": [{ "name": channel_name, "symbols": [self.symbol] }] })) if channel_name in self.callbacks: del self.callbacks[channel_name] def market_depth(self, symbol): if self.isConnected() and 'l2_orderbook' in self.data: orderbooks = list( filter(lambda x: x['symbol'] == symbol, self.data['l2_orderbook'])) if orderbooks and time.time() - orderbooks[0]['last_seen'] < 2: asks = list( map( lambda x: { 'price': round_price_by_tick_size(x['limit_price'], self. tick_size), 'size': int(x['size']) }, orderbooks[0]['asks'])) bids = list( map( lambda x: { 'price': round_price_by_tick_size(x['limit_price'], self. tick_size), 'size': int(x['size']) }, orderbooks[0]['bids'])) bids = list( filter(lambda x: Decimal(x['price']) > self.tick_size, bids)) asks.sort(key=lambda x: x['price']) bids.sort(key=lambda x: x['price'], reverse=True) return {'asks': asks, 'bids': bids} return self.getorderbook(self.product_id) def mark_price(self, product_id): if self.isConnected() and 'mark_price' in self.data: if product_id in self.data['mark_price'] and time.time( ) - self.last_seen['mark_price'][product_id] < 15: return self.data['mark_price'][product_id] return self.get_mark_price(product_id) def spot_price(self, spot_symbol, product_id): if self.isConnected() and 'spot_price' in self.data: if spot_symbol in self.data['spot_price'] and time.time( ) - self.last_seen['spot_price'][spot_symbol] < 15: return self.data['spot_price'][spot_symbol] return self.get_spot_price(product_id) def position(self, product_id): # if self.isConnected(): # if product_id in self.data['positions'] and time.time() - self.last_seen['positions'][product_id] < 15: # return self.data['positions'][product_id] # Get position from rest and set locally position = self.get_position_over_rest(product_id) self.data['positions'][product_id] = position self.last_seen['positions'][product_id] = time.time() return position """ ******* HTTP METHODS ******* """ @handle_requests_exceptions(client='Delta') def getorderbook(self, product_id): orderbook = self.delta_client.get_L2_orders(product_id, auth=True) asks = list( map( lambda x: { 'price': round_price_by_tick_size(x['price'], self. tick_size), 'size': int(x['size']) }, orderbook['sell_book'])) bids = list( map( lambda x: { 'price': round_price_by_tick_size(x['price'], self. tick_size), 'size': int(x['size']) }, orderbook['buy_book'])) asks.sort(key=lambda x: x['price']) bids.sort(key=lambda x: x['price'], reverse=True) return {'asks': asks, 'bids': bids} @handle_requests_exceptions(client='Delta') def get_mark_price(self, product_id): mark_price = self.delta_client.get_mark_price(product_id, auth=True) return Decimal(mark_price) @handle_requests_exceptions(client='Delta') def get_spot_price(self, product_id): orderbook = self.delta_client.get_L2_orders(product_id, auth=True) spot_price = orderbook['spot_price'] return Decimal(spot_price) @handle_requests_exceptions(client='Delta') def get_position_over_rest(self, product_id): positions = self.delta_client.get_position(product_id) if positions and int(positions['size']) != 0: position = get_position(entry_price=round_price_by_tick_size( positions['entry_price'], self.tick_size), size=int(positions['size'])) position['margin'] = Decimal(positions['margin']) position['liquidation_price'] = Decimal( positions['liquidation_price']) return position else: # RETURN EMPTY POSITION ONLY IF NO OPEN POSITIONS IS FOUND position = get_position() position['margin'] = 0 position['liquidation_price'] = None self.logger.info(position) return position @handle_requests_exceptions(client='Delta') def addPositionMargin(self, product_id, delta_margin): return self.delta_client.change_position_margin( product_id, delta_margin) @handle_requests_exceptions(client='Delta') def funds(self): response = self.delta_client.get_wallet( self.product['settling_asset']['id']) return Decimal(response['balance']) - Decimal( response['position_margin']) @handle_requests_exceptions(client='Delta') def available_funds(self): response = self.delta_client.get_wallet( self.product['settling_asset']['id']) return Decimal(response['balance']) - Decimal( response['position_margin']) - Decimal( response['order_margin']) - Decimal(response['commission']) @handle_requests_exceptions(client='Delta') def get_product(self, product_id): response = self.delta_client.request("GET", "products", auth=True) response = response.json() products = list(filter(lambda x: x['id'] == product_id, response)) return products[0] if len(products) > 0 else None @handle_requests_exceptions(client='Delta') def get_open_orders(self, product_id, state=OrderState.OPEN, page_num=1, page_size=500): query = { 'product_id': product_id, 'state': state.value, 'page_num': page_num, 'page_size': page_size } if state.value == 'pending': query['stop_order_type'] = 'stop_loss_order' response = self.delta_client.get_orders(query) return response @handle_requests_exceptions(client='Delta') def get_ticker(self, product_id): response = self.delta_client.get_ticker(product_id) return response @handle_requests_exceptions(client='Delta') def set_leverage(self, product_id, leverage): response = self.delta_client.set_leverage(product_id=product_id, leverage=leverage) return response def market_order(self, size, product_id): side = 'buy' if size > 0 else 'sell' if size != 0: order = { 'size': abs(size), 'side': side, 'order_type': 'market_order', 'product_id': product_id } self.place_order(order) @handle_requests_exceptions(client='Delta') def place_order(self, order): response = self.delta_client.create_order(order) if order['order_type'] == 'market_order' and response[ 'unfilled_size'] != 0: self.logger.info('Delta exception: %s' % str(response)) raise custom_exceptions.PlaceOrderError('Delta') else: return response @handle_requests_exceptions(client='Delta') def create_stop_order(self, product_id, size, stop_price): side = 'buy' if size > 0 else 'sell' response = self.delta_client.place_stop_order( product_id=product_id, size=abs(int(size)), side=side, stop_price=str(stop_price), order_type=OrderType.MARKET) return response @handle_requests_exceptions(client='Delta') def edit_order(self, product_id, order_id, size, stop_price): order = { 'id': order_id, 'product_id': product_id, 'size': size, 'stop_price': str(stop_price) } response = self.delta_client.request("PUT", "orders", order) return response @handle_requests_exceptions(client='Delta') def batch_create(self, product_id, orders): create_batches = self.slice_orders(orders) for create_order in create_batches: self.delta_client.batch_create(product_id, list(create_order)) self.logger.info('Created orders: %s, batch size: %s' % (len(orders), len(create_batches))) @handle_requests_exceptions(client='Delta') def batch_cancel(self, product_id, orders): delete_batches = self.slice_orders(orders) for delete_order in delete_batches: self.delta_client.batch_cancel( self.product_id, list(map(cancel_order_format, list(delete_order)))) self.logger.info('Deleted orders: %s, batch size: %s' % (len(orders), len(delete_batches))) @handle_requests_exceptions(client='Delta') def batch_edit(self, product_id, orders): edit_batches = self.slice_orders(orders, size=20) for edit_order in edit_batches: self.delta_client.batch_edit(product_id, list(edit_order)) self.logger.info('Edited orders: %s' % (len(orders))) def slice_orders(self, orders, size=5): orders = iter(orders) return list(iter(lambda: tuple(islice(orders, size)), ()))
class App(object): def __init__(self, config, model, logw): self.logw = lambda msg: logw(basename(__file__) + ": " + str(msg)) self.model = model self.csignal = CSignal() self.config = config self.bindings = self.config.get_key_bindings() self.tasklists = [] self.current_tasklist = None self.tasks = [] self.mbody = None self.help_visible = False self.tasks_visible = False self.build(init=True) self.setup_signals() self.dirty = False self.timer = Timer(2, self.on_timer) self.idle_time = 0 self.show_completed = False def build_body(self): self.filler = urwid.Filler(self.listbox, valign="middle", height=("relative", 100), top=2, min_height=10) if self.mbody is not None: self.mbody.original_widget = self.filler else: # TODO: check logic self.mbody = urwid.AttrMap( urwid.Filler(self.listbox, valign="middle", height=("relative", 100), top=2, min_height=10), "body" ) def build_tasks(self): self.walker = urwid.SimpleListWalker(self.tasks) self.listbox = urwid.ListBox(self.walker) self.update_header("Tasklist: " + self.current_tasklist_title) self.build_body() self.tasks_visible = True def build(self, init=False): self.tasklists = self.model.tasklists() self.listbox = ListWidget(u" Please select a list", self.tasklists, self.on_list_select) self.build_body() if init: self.set_top(self.mbody) self.build_header("Sandook(ToDo)") self.build_footer(self.config.get_help_text()) self.frame = urwid.Frame(self.top, header=self.header, footer=self.footer) else: self.update_footer("Status:") self.update_header("Sandook(ToDo)") self.tasks_visible = False def build_edit_tasklist(self, sel_tasklist): qbox = QuestionBoxW( question=u"Rename TaskList", edit_text=sel_tasklist[1], button_text="Edit", callback=self.on_sel_tasklist_edit, cancel_button_text="Cancel", cancel_callback=self.on_tasklist_edit_cancel, ) self.mbody.original_widget = qbox def build_add_tasklist(self): qbox = QuestionBoxW( question=u"New TaskList name", edit_text=" ", button_text="Add", callback=self.on_tasklist_add, cancel_button_text="Cancel", cancel_callback=self.on_tasklist_add_cancel, ) self.mbody.original_widget = qbox def build_delete_tasklist(self, sel_tasklist): qbox = QuestionBoxW( question=u"Are you sure, you want to delete the tasklist '%s'? This will also delete" u"all the tasks." % sel_tasklist[1], edit_text="", button_text="Yes", callback=self.on_tasklist_delete, cancel_button_text="No", cancel_callback=self.on_tasklist_delete_cancel, ) self.mbody.original_widget = qbox def build_delete_task(self): sel_task = self.listbox.focus self._original_w = self.mbody.original_widget qbox = QuestionBoxW( question=u"Are you sure, you want to delete the task: '%s'?" u" You can also mark it as complete" % sel_task.title, edit_text="", button_text="Yes", callback=self.on_task_delete, cancel_button_text="No", cancel_callback=self.on_task_delete_cancel, ) self.mbody.original_widget = qbox def build_help_dialog(self): self._original_w = self.mbody.original_widget helpd = HelpDialog(config=self.config, callback=self.on_help_exit) self.mbody.original_widget = helpd self.help_visible = True def build_header(self, header): self.header = urwid.AttrMap( urwid.Pile([urwid.Text(("header", header), align="center", wrap="any"), urwid.Divider(div_char=".")]), "header", ) def build_footer(self, help_text): self.footer = urwid.AttrMap( urwid.Columns( [ urwid.Padding(urwid.Text(u"Status"), width=("relative", 70), align="left"), urwid.Padding( urwid.Text(u"For Commands, press : %s " % help_text), width="pack", min_width=len(help_text) + 24, align="right", ), ], dividechars=2, focus_column=0, ), "footer", ) def update_header(self, text): # This needs to be modified if footer structure is changed. self.header.original_widget.contents[0][0].set_text(text) def update_footer(self, text, trim=True): # This needs to be modified if footer structure is changed. ftext = text[:25] + "..." if (len(text) > 28 and trim) else text self.footer.original_widget.contents[0][0].original_widget.set_text(ftext) if not trim: # Indicates an error has occured. self.footer.set_attr_map({None: "error"}) def set_top(self, widget): self.top = urwid.Overlay( widget, urwid.SolidFill(" "), align="center", width=("relative", 95), valign="middle", height=("relative", 95), min_width=20, min_height=10, ) def setup_signals(self): urwid.connect_signal(self.csignal, "task_complete", self.on_task_completed) urwid.connect_signal(self.csignal, "add_item", self.on_add_item) urwid.connect_signal(self.csignal, "move_up", self.on_move_up) urwid.connect_signal(self.csignal, "move_down", self.on_move_down) urwid.connect_signal(self.csignal, "input_recv", self.on_input_recv) urwid.connect_signal(self.csignal, "show_tasklists", self.on_show_tasklists) urwid.connect_signal(self.csignal, "toggle_complete", self.on_toggle_complete) urwid.connect_signal(self.csignal, "delete_task", self.build_delete_task) def exit_program(self, button): self.cancel_timer_thread() self.save() urwid.ExitMainLoop() def on_show_tasklists(self): self.save() self.build() def on_toggle_complete(self): self.show_completed = not self.show_completed self.tasks = [] self.build_taskwidgets() def on_help_exit(self, button): self.help_visible = False self.mbody.original_widget = self._original_w def on_tasklist_add(self, button, arg): self.logw("Add tasklist " + arg) self.model.add_tasklist(title=arg) self.build() def on_tasklist_add_cancel(self, button, arg): self.logw("Cancel tasklist add") self.build() def on_tasklist_edit_cancel(self, button, arg): self.logw("Cancel tasklist edit") self.build() def on_tasklist_delete(self, button, arg): self.logw("Delete tasklist " + str(self.sel_tasklist_edit[1])) self.model.delete_tasklist(self.sel_tasklist_edit[0]) self.build() def on_task_delete(self, button, arg): self.logw("Delete task* " + str(self.listbox.focus.title)) self.logw("Delete task* : " + str(self.listbox.focus.taskid) + " " + self.current_tasklist) self.model.delete_task(self.current_tasklist, self.listbox.focus.taskid) self.tasks = [] self.build_taskwidgets() def on_tasklist_delete_cancel(self, button, arg): self.logw("Cancel tasklist delete") self.build() def on_task_delete_cancel(self, button, arg): self.logw("Cancel task delete") self.mbody.original_widget = self._original_w def on_task_enter(self, button, arg): curr_task = self.listbox.focus self.model.update_task(self.current_tasklist, curr_task.taskid, title=curr_task.title) self.update_footer("Saved: %s" % arg[1]) def keystroke(self, key): self.logw("Pres: keypress %s " % key) if key in self.bindings["show_help"] and not self.help_visible: self.build_help_dialog() elif self.tasks_visible: self.listbox.focus.keypress(None, key) else: if key in self.bindings["tasklist_edit"] and not self.help_visible: self.sel_tasklist_edit = self.listbox.focus_tasklist_id() self.logw(key + str(self.sel_tasklist_edit[0:2])) self.build_edit_tasklist(self.sel_tasklist_edit) elif key in self.bindings["tasklist_add"] and not self.help_visible: self.build_add_tasklist() elif key in self.bindings["tasklist_delete"] and not self.help_visible: self.sel_tasklist_edit = self.listbox.focus_tasklist_id() self.logw(key + str(self.sel_tasklist_edit[0:2])) self.build_delete_tasklist(self.sel_tasklist_edit) def on_sel_tasklist_edit(self, button, arg): self.logw("On_Sel_tasklist_edit " + arg) self.model.update_tasklist(self.sel_tasklist_edit[0], title=arg) self.save() self.build() def on_list_select(self, button, arg): self.current_tasklist = arg[0] self.current_tasklist_title = arg[1] self.logw("Select tasklist %s :" % arg) self.tasks = [] self.build_taskwidgets() def build_taskwidgets(self): ret_tasks = self.model.tasks(self.current_tasklist) for item in ret_tasks: self.logw(item) comp = False if item[3] == "" else True item_list = list(item) item_list[3] = comp self.logw(" show_completed %s, comp %s:: " % (str(self.show_completed), str(comp))) if not (self.show_completed and comp): self.logw(" adding Task:: " + str(item_list)) self.tasks.append( TaskWidget( task=item_list, callback=self.on_task_enter, change_callback=self.on_input_recv, signal=self.csignal, bindings=self.bindings, logw=self.logw, ) ) # Check is self.tasks is empty. If yes, create one if len(self.tasks) == 0: self.on_add_item() self.build_tasks() def on_timer(self): self.idle_time += 1 if self.idle_time > 4: self.idle_time = 0 self.logw("save() called") self.save() else: self.timer = Timer(2, self.on_timer) self.timer.start() def on_input_recv(self, button, arg): self.logw("Pres : input received, clearing dirty data\n") self.dirty = False self.idle_time = 0 if hasattr(self, "timer"): self.logw("thread exists") if self.timer.isAlive(): self.logw("Thread is active, cancel thread") self.timer.cancel() self.timer = Timer(2, self.on_timer) self.timer.start() self.logw("thread started:") def save(self): if not self.dirty: self.logw("save this shit!!") if self.tasks_visible: for twidget in self.tasks: self.logw("Test save count (tasks) " + str(twidget.values())) tt = twidget.values() task_title = tt[1] comp = "x" if not tt[3] else utils.now() # Only save tasks which are not empty if len(task_title) > 0: self.model.update_task( self.current_tasklist, tt[0], title=task_title, position=tt[2], completed=comp ) self.model.save() self.dirty = False else: self.logw("Dirty data exists: CANNOT *save* this shit!!") def on_move_up(self): pos = self.listbox.focus_position if (pos - 1) < 0: return self.logw("Pres: Move up item") tw = self.walker.pop(pos) pos -= 1 self.walker.insert(pos, tw) self.listbox.focus_position = pos def on_move_down(self): pos = self.listbox.focus_position if (pos + 1) > len(self.walker.positions()) - 1: return self.logw("Pres: Move down item") tw = self.walker.pop(pos) pos += 1 self.walker.insert(pos, tw) self.listbox.focus_position = pos def on_add_item(self): if not self.dirty: self.logw("Add new item") nt = self.model.add_task(self.current_tasklist, title="") comp = False if nt.completed == "" else True tw = TaskWidget( [nt.id, nt.title, nt.position, comp], callback=self.on_task_enter, change_callback=self.on_input_recv, signal=self.csignal, bindings=self.bindings, logw=self.logw, ) self.tasks.append(tw) pos = self.listbox.focus_position + 1 self.walker.insert(pos, tw) self.listbox.focus_position = pos self.dirty = True else: self.logw("Dirty data exists: Cannot add new item") def on_task_completed(self): taskid = self.listbox.focus.taskid completed = self.listbox.focus.values()[3] self.logw( " Received Signal, Will mark the task %s as complete: %s" % (self.listbox.focus.taskid, str(completed)) ) comp_val = "x" if not completed else utils.now() self.model.update_task(self.current_tasklist, taskid, completed=comp_val) update_text = "complete" if completed else "uncomplete" self.update_footer('Task "%s" marked as %s' % (self.listbox.focus.title, update_text)) def cancel_timer_thread(self): if self.timer and self.timer.isAlive(): self.timer.cancel() def main(self): try: self.loop = urwid.MainLoop( self.frame, palette=self.config.get_palette(), unhandled_input=self.keystroke, handle_mouse=False ) self.loop.screen.set_terminal_properties(256) self.loop.run() except (KeyboardInterrupt, SystemExit): self.exit_program(None)
class PandaPlayer( xbmc.Player ): def __init__( self, core=None, panda=None ): xbmc.Player.__init__( self ) self.panda = panda self.timer = None self.playNextSong_delay = 0.5 def playSong( self, item ): log( "playSong: item[url] %s" % item[0], xbmc.LOGDEBUG ) log( "playSong: item[item] %s" % item[1], xbmc.LOGDEBUG ) self.play( item[0], item[1] ) def play( self, url, item ): # override play() to force use of PLAYER_CORE_MPLAYER xbmc.Player( xbmc.PLAYER_CORE_MPLAYER ).play( url, item ) # NOTE: using PLAYER_CORE_MPLAYER is necessary to play .mp4 streams (low & medium quality from Pandora) # ... unfortunately, using "xbmc.Player([core]) is deprecated [ see URLref: http://forum.xbmc.org/showthread.php?tid=173887&pid=1516662#pid1516662 ] # ... and it may be removed from Gotham [ see URLref: https://github.com/xbmc/xbmc/pull/1427 ] # ToDO: discuss with the XBMC Team what the solution to this problem would be def onPlayBackStarted( self ): log( "onPlayBackStarted: %s" %self.getPlayingFile(), xbmc.LOGDEBUG ) if self.panda.playing: # ToDO: ? remove checks for pandora.com / p-cdn.com (are they needed? could be a maintainence headache if the cdn changes...) if not "pandora.com" in self.getPlayingFile(): if not "p-cdn.com" in self.getPlayingFile(): self.panda.playing = False self.panda.quit() else: # show visualization (disappears after each song...) xbmc.executebuiltin( "ActivateWindow( 12006 )" ) def onPlayBackEnded( self ): log( "onPlayBackEnded", xbmc.LOGDEBUG ) self.stop() log( "playing = %s" %self.panda.playing, xbmc.LOGDEBUG ) if self.timer and self.timer.isAlive(): self.timer.cancel() if self.panda.skip: self.panda.skip = False if self.panda.playing: self.timer = Timer( self.playNextSong_delay, self.panda.playNextSong ) self.timer.start() def onPlayBackStopped( self ): log( "onPlayBackStopped", xbmc.LOGDEBUG ) self.stop() log( "playing = %s" %self.panda.playing, xbmc.LOGDEBUG ) if self.timer and self.timer.isAlive(): self.timer.cancel() if self.panda.playing and self.panda.skip: self.panda.skip = False self.timer = Timer( self.playNextSong_delay, self.panda.playNextSong ) self.timer.start() else: if xbmc.getCondVisibility('Skin.HasSetting(PandoraVis)'): # turn off visualization xbmc.executebuiltin('Skin.Reset(PandoraVis)') self.panda.stop()
else: # Prepare SQL query to INSERT a record into the database. twitter.userToMysql(followers,db) print "user not in database " + followers.screen_name # Start a timer to limit followers per day if followerCount == followerResetCount: print ("Followed the maximum number of people for this " "session timing out") followerTimer = Timer(followerTimerLength,timeout) followerTimer.start() followerCount = 0 # Check if the timer is running for follower reset if not followerTimer.isAlive(): # check to make sure they have followers if followers.friends_count > 0: if float(followers.followers_count)/float(followers.friends_count) >= 0.2: followers.follow() twitter.updateFollowing(followers.id,1,db) print 'Now following ' + followers.screen_name followerCount += 1 currentlyFollowingcount += 1 # Check if my friend count is too high if currentlyFollowingcount >= maxFollowers: # # Recrawl all people following me update the follower flag in db # for follower in tweepy.Cursor(twitter.api.followers).items(): # print "recrawling followers"
class Spider(object): logger = logging.getLogger("spider.mainthread") def __init__(self,strategy=Strategy()): monkey.patch_all() self.strategy = strategy self.queue = queue.Queue() self.urltable = UrlTable(strategy.max_count) self.pool = pool.Pool(strategy.concurrency) self.greenlet_finished = event.Event() self._stop = event.Event() def setRootUrl(self,url): if isinstance(url,basestring): url = UrlObj(url) self.root = url self.put(self.root) def put(self, url): if url not in self.urltable: self.queue.put(url) def run(self): self.timer = Timer(self.strategy.time, self.stop) self.timer.start() self.logger.info("spider '%s' begin running",self.root) while not self.stopped() and self.timer.isAlive(): for greenlet in list(self.pool): if greenlet.dead: self.pool.discard(greenlet) try: url = self.queue.get_nowait() except queue.Empty: if self.pool.free_count() != self.pool.size: self.greenlet_finished.wait() self.greenlet_finished.clear() continue else: self.stop() greenlet = Handler(url, self) self.pool.start(greenlet) def stopped(self): return self._stop.is_set() def stop(self): self.logger.info("spider '%s' finished. fetch total (%d) urls",self.root,len(self.urltable)) self.timer.cancel() self._stop.set() self.pool.join() self.queue.put(StopIteration) return def dump(self): import StringIO out = StringIO.StringIO() for url in self.urltable: try: print >> out ,url except: continue return out.getvalue()
class CylonAlarm(): def __init__(self,domain_id,config,on_state_actions,hardware): self.hardware=hardware self.domain_id = domain_id self.config = config self.state = "" for zone in config["connections"]["zones"]: for domain in zone["domain"]: if domain == self.domain_id: self.hardware.addSensorEvent(zone["pin"],self.movement) self.activation_timer = Timer(0, self.dummy) self.alarm_delay_timer = Timer(0, self.dummy) self.alarm_duration_timer = Timer(0, self.dummy) self.action_thread=Action(on_state_actions,self.hardware) self.sendsms = SendSMS(self.domain_id,self.config) print("\n"+print_time()+" [Domain "+str(self.domain_id)+"] \033[92mRunning...\033[0m Press Ctrl+C to exit") self.deactivate() # todo # just a workaround to be able to call activation_timer.cancel() without being really started def dummy(self): pass def is_deactivated(self): if self.state == "deactivated": return True else: return False def actdeact(self): if self.is_deactivated(): self.sactivate() else: self.deactivate() def sactivate(self): self.action_thread.thread_stop() print(print_time()+" [Domain "+str(self.domain_id)+"] Activating in "+str(self.config["settings"]["activation_wait"])+" seconds...") self.state = "activating" self.activation_timer = Timer(self.config["settings"]["activation_wait"],self.start_sensing) self.activation_timer.start() self.action_thread.new_state_set(self.state) def deactivate(self): self.action_thread.thread_stop() self.activation_timer.cancel() self.alarm_delay_timer.cancel() self.alarm_duration_timer.cancel() self.stop_sensing() self.state = "deactivated" print(print_time()+" [Domain "+str(self.domain_id)+"] \033[93mDeactivated\033[0m") self.action_thread.new_state_set(self.state) def check_all_zones_state(self): for zone in self.config["connections"]["zones"]: for domain in zone["domain"]: if domain == self.domain_id and self.hardware.getSensorInAfterDelay(zone["pin"]): return zone["pin"] return 0 def start_sensing(self): self.action_thread.thread_stop() self.state = "activated" print(print_time()+" [Domain "+str(self.domain_id)+"] \033[93mActivated\033[0m") self.action_thread.new_state_set(self.state) # if a zone is already opened (workaround) active_zone = self.check_all_zones_state() if active_zone: self.movement(active_zone) def stop_sensing(self): self.action_thread.thread_stop() def movement(self,channel): pass_check = self.hardware.getSensorInAfterDelay(channel) and self.state=="activated" and not self.state=="alarming" and not self.alarm_delay_timer.isAlive() if pass_check: self.action_thread.thread_stop() self.state = "movement" print(print_time()+" [Domain "+str(self.domain_id)+"]\033[1;97m Movement detected (>500ms), you have "+str(self.config["settings"]["alarm_delay"])+" seconds...\033[0m") self.alarm_delay_timer = Timer(self.config["settings"]["alarm_delay"],self.sound_the_alarm) self.alarm_delay_timer.start() self.stop_sensing() self.action_thread.new_state_set(self.state) def sound_the_alarm(self): self.action_thread.thread_stop() for siren in self.config["connections"]["sirens"]: for domain in siren["domain"]: if domain == self.domain_id: self.hardware.high(siren["pin"]) self.state = "alarming" print("\033[91m"+print_time()+" [Domain "+str(self.domain_id)+"] <<< ALERT >>>\033[0m") self.sendsms.thread_start() self.alarm_duration_timer = Timer(self.config["settings"]["alarm_duration"],self.stop_the_alarm) self.alarm_duration_timer.start() self.action_thread.new_state_set("activated") # fix it def stop_the_alarm(self): for siren in self.config["connections"]["sirens"]: for domain in siren["domain"]: if domain == self.domain_id: self.hardware.low(siren["pin"]) print(print_time()+" [Domain "+str(self.domain_id)+"]\033[1;97m Alarm stopped\033[0m") def __exit__(self): self.activation_timer.cancel() self.alarm_delay_timer.cancel() self.alarm_duration_timer.cancel() try: self.activation_timer.join() self.alarm_delay_timer.join() self.alarm_duration_timer.join() except: pass self.action_thread.thread_stop() self.sendsms.thread_stop()
class AppEventManager(object): """ This class manages all events to send during app running """ def __init__(self, device, app, event_policy, event_count, event_interval, event_duration): """ construct a new AppEventManager instance :param device: instance of Device :param app: instance of App :param event_policy: policy of generating events, string :return: """ self.logger = logging.getLogger('AppEventManager') self.enabled = True self.device = device self.app = app self.policy = event_policy self.events = [] self.event_factory = None self.event_count = event_count self.event_interval = event_interval self.event_duration = event_duration self.monkey = None self.timer = None if not self.event_count or self.event_count is None: self.event_count = 100 if not self.policy or self.policy is None: self.policy = POLICY_NONE if not self.event_interval or self.event_interval is None: self.event_interval = 2 if self.policy == POLICY_NONE: self.event_factory = None elif self.policy == POLICY_MONKEY: self.event_factory = None elif self.policy == POLICY_RANDOM: self.event_factory = RandomEventFactory(device, app) elif self.policy == POLICY_STATIC: self.event_factory = StaticEventFactory(device, app) elif self.policy == POLICY_DYNAMIC: self.event_factory = DynamicEventFactory(device, app) elif self.policy == POLICY_STATE_RECORDER: self.event_factory = StateRecorderFactory(device, app) elif self.policy == POLICY_MANUAL: self.event_factory = ManualEventFactory(device, app) else: self.event_factory = FileEventFactory(device, app, self.policy) def add_event(self, event): """ add one event to the event list :param event: the event to be added, should be subclass of AppEvent :return: """ if event is None: return self.events.append(event) self.device.send_event(event) def dump(self): """ dump the event information to files :return: """ event_log_file = open(os.path.join(self.device.output_dir, "droidbot_event.json"), "w") event_array = [] for event in self.events: event_array.append(event.to_dict()) json.dump(event_array, event_log_file) event_log_file.close() if self.event_factory is not None: self.event_factory.dump() # def on_state_update(self, old_state, new_state): # """ # callback method invoked by AppstateMonitor # :param old_state: origin state of App # :param new_state: new state of App # :return: # """ # def set_event_factory(self, event_factory): """ set event factory of the app :param event_factory: the factory used to generate events :return: """ self.event_factory = event_factory def start(self): """ start sending event """ self.logger.info("start sending events, policy is %s" % self.policy) if self.event_duration: self.timer = Timer(self.event_duration, self.stop) self.timer.start() try: if self.event_factory is not None: self.event_factory.start(self) elif self.policy == POLICY_MONKEY: throttle = self.event_interval * 1000 monkey_cmd = "adb shell monkey %s --throttle %d -v %d" % ( ("" if self.app.get_package_name() is None else "-p " + (self.app.get_package_name())), throttle, self.event_count) self.monkey = subprocess.Popen(monkey_cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE) while self.enabled: time.sleep(1) elif self.policy == POLICY_NONE: self.device.start_app(self.app) while True: input = raw_input("press ENTER to save current state, type q to exit...") if input.startswith('q'): break state = self.device.get_current_state() if state is not None: state.save2dir() except KeyboardInterrupt: pass self.stop() self.dump() self.logger.info("finish sending events, saved to droidbot_event.json") def stop(self): """ stop sending event """ if self.monkey: self.monkey.terminate() self.monkey = None if self.timer and self.timer.isAlive(): self.timer.cancel() self.timer = None self.enabled = False
def main(argv=None): # IGNORE:C0111 '''Command line options.''' if argv is None: argv = sys.argv else: sys.argv.extend(argv) program_version_message = __version__ program_license = '''tvb -- TV Bridge Created by Juncheng Chen on %s. Copyright 2015 organization_name. All rights reserved. Licensed under the Apache License 2.0 http://www.apache.org/licenses/LICENSE-2.0 Distributed on an "AS IS" basis without warranties or conditions of any kind, either express or implied. USAGE ''' % (__date__) try: parser = ArgumentParser(description=program_license, formatter_class=RawDescriptionHelpFormatter) parser.add_argument('-d', dest="devices", help=u"device ip address or name", metavar="ip_address", nargs='*') parser.add_argument('-c', dest="commands", choices=support_commands, help=u"collection commands, default %(default)s", default=default_commands, nargs='*') parser.add_argument('-l', '--log', dest="log_dir", help=u"specify the file path where to store logs", default=os.path.abspath('.'), metavar="log path", nargs='?') parser.add_argument('-t', '--time', dest="time", type=float, help=u"execution time, unit(minutes)", default=-1, metavar="minutes", nargs='?') parser.add_argument('-i', '--interval', dest="interval", type=int, help=u"time interval of command, unit(seconds), default %(default)s seconds", default=10, metavar="seconds", nargs='?') parser.add_argument('-p', '--process', dest="process_names", help=u"specify process names to generate line chart", metavar="process names", nargs='*') parser.add_argument('-m', '--monkey', dest="monkey", help=u"monkey will only allow the system to visit activities within those packages", metavar="packages", nargs='*') parser.add_argument('-b', '--blacklist', dest="blacklist", help=u"monkey will not allow the system to visit activities within those packages", metavar="packages", nargs='+') parser.add_argument('-s', '--script', dest="script", help=u"monkey will repeat run according the script", metavar="script_path", nargs='?') parser.add_argument('-o', '--throttle', dest="throttle", type=int, help=u"inserts a fixed delay between monkey events, unit(millisecond), default %(default)s millisecond", default=500, metavar="millisecond", nargs='?') parser.add_argument('--pct-touch', dest="pct-touch", type=int, help=u"adjust percentage of touch events.", metavar="percentage", nargs='?') parser.add_argument('--pct-motion', dest="pct-motion", type=int, help=u"adjust percentage of motion events.", metavar="percentage", nargs='?') parser.add_argument('--pct-trackball', dest="pct-trackball", type=int, help=u"adjust percentage of trackball events, default 5", metavar="percent", nargs='?') parser.add_argument('--pct-nav', dest="pct-nav", type=int, help=u'adjust percentage of "basic" navigation events, default 55', metavar="percent", nargs='?') parser.add_argument('--pct-majornav', dest="pct-majornav", type=int, help=u'adjust percentage of "major" navigation events, default 15', metavar="percent", nargs='?') parser.add_argument('--pct-syskeys', dest="pct-syskeys", type=int, help=u'adjust percentage of "system" key events, default 15', metavar="percent", nargs='?') parser.add_argument('--pct-appswitch', dest="pct-appswitch", type=int, help=u'adjust percentage of activity launches, default 9', metavar="percent", nargs='?') parser.add_argument('--pct-anyevent', dest="pct-anyevent", type=int, help=u'adjust percentage of other types of events, default 1', metavar="percent", nargs='?') parser.add_argument("-r", "--report", dest="report", help=u"regenerate excel report", metavar="report path", nargs='?') parser.add_argument("-v", "--verbose", dest="verbose", action="count", default=0, help=u"verbose level") parser.add_argument('-V', '--version', action='version', help=u"show version and exit", version=program_version_message) # Process arguments args = parser.parse_args() logging.basicConfig(level=logging.INFO if args.verbose == 0 else logging.DEBUG, format='%(asctime)s %(levelname)-5s %(message)s', datefmt='%y-%m-%d %H:%M:%S') logger.debug(args) if not args.devices and not args.report: raise CLIError("no operation") if args.report: logger.info('report dir %s' % args.report) Report(args.report, args.process_names) return 0 if args.monkey is not None: args.commands.insert(0, 'monkey') if not args.process_names: args.process_names = args.monkey if args.blacklist: args.commands.insert(0, 'blacklist') if args.script: args.commands.insert(0, 'script') config = Config(args) total_time = args.time * 60 if args.time > 0 else None timeout = len(config.commands) * 10 def watchdog(): logger.error('watchdog %s seconds timeout' % timeout) for command in config.commands: command.kill() logger.info('start collection') try: while total_time is None or total_time > 0: timer = Timer(timeout, watchdog) timer.setDaemon(True) timer.start() logger.debug('watchdog start') before = time() for command in config.commands: command.execute() if timer.isAlive(): timer.cancel() logger.debug('watchdog cancel') timer.join() if total_time is not None: total_time -= args.interval logger.debug("remain time: %s sec." % total_time) delta = time() - before if delta < args.interval: sleep(args.interval - delta) except KeyboardInterrupt: logger.info('KeyboardInterrupt') for command in config.commands: command.clean() if config.last_commads: logger.info('please wait a moment') for command in config.last_commads: command.execute() logger.info('collection finish') Report(config.log_dir, args.process_names) logger.info('finish') except KeyboardInterrupt: return 0 except Exception, e: if DEBUG: raise(e) sys.stderr.write(str(e) + "\n") sys.stderr.write(" for help use --help") return 2
room_dict = db_inst.room_id2desc db_inst.transfor_absolute_time() except Exception: log_msg = 'Something wrong with the database when try transforing absolute time !!!' log_handler.error(log_msg) return ERR pointer = 0 timer = Timer(THRESHOLD_LOAD_CYCLE, timer_work) while not stopEvent.isSet(): dbIsReady = MssqlConnection.test_connection() if dbIsReady == SUC: timer = Timer(THRESHOLD_LOAD_CYCLE, timer_work) timer.setName(THREAD_POLICY) timer.start() while timer.isAlive(): sleep(0.1) else: log_msg = 'Something wrong with the database, system will reconnect in %d seconds !!!' %db_reconnect_cycle[pointer] log_handler.error(log_msg) sleep(db_reconnect_cycle[pointer]) pointer = (pointer + 1) % len(db_reconnect_cycle) timer.cancel() log_msg = 'Load Threshold Thread shutdown and cleaned! ' log_handler.work(log_msg) # log_manager.add_work_log(log_msg, sys._getframe().f_code.co_name) print '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>' exit()
#create timer time_thread = Timer(period+timeout,LoadDriver.CollectResults) terminator_thread = Timer(period,LoadDriver._terminator) time_thread.start() terminator_thread.start() #launch host-testers for host in hosts: tr = LoadDriver.Spawner(host[0],host[1],rate,period,timeout) tr.start() #wait for the results to come in time_thread.join(period+timeout+1) #check if the result-gathering is successful if time_thread.isAlive(): print "Error in timer thread!" sys.exit(1) #print the response times (in seconds) and the number of attempts print "-----------------" print "Response Times:" print LoadDriver.CollectionResults[0] print "-----------------" print "Number of attempts:" print LoadDriver.CollectionResults[1] print "-----------------" print "Average Response Time:" print sum(LoadDriver.CollectionResults[0])/len(LoadDriver.CollectionResults[0])
webcam_process.parent_connection.send(round(time.clock(),4)) webcam_process.parent_connection.send(trial_path) webcam_process.start() while not gui.stop_event : gui.update() # Checks for runtime exceptions encountered in the child processes if flower_control_process.exception_occurred() : root_logger.critical('exception occurred!') break if webcam_process.exception_occurred() : root_logger.critical('exception occurred!') break root_logger.info('terminating GUI') gui.stop() flower_control_process.exit_event.set() webcam_process.exit_event.set() comment_file = trial_path + "/comments.txt" webcam_process.join() flower_control_process.join() if t.isAlive(): t.cancel() #write_comment(comment_file)
class Controller: def __init__(self, root): # create modle and setup callbacks self.model = Model() self.model.spyeworks.addCallback(self.updatePlayerOnline) self.model.spyeworks.currentList.addCallback(self.updateCurrentList) self.model.spyeworks.allLists.addCallback(self.updateAllLists) self.model.ipaddy.addCallback(self.updateIP) self.model.filepath.addCallback(self.updateFilepath) self.model.active.addCallback(self.updateActive) self.model.idle.addCallback(self.updateIdle) self.model.sensorstate.addCallback(self.updateSensorState) self.model.activedelaytime.addCallback(self.updateActiveDelayTime) self.model.idledelaytime.addCallback(self.updateIdleDelayTime) # create variables for tracking checkboxs and timers self.ActiveList=tk.StringVar() self.ActiveList.set(self.model.activelist.get()) self.IdleList=tk.StringVar() self.IdleList.set(self.model.idlelist.get()) self.SensorEnable=tk.StringVar() self.SensorEnable.set(self.model.sensorenable.get()) self.activeTimer=Timer(1, self.dummyFunc, ()) self.idleTimer=Timer(1, self.dummyFunc, ()) self.playIdleList=False # create main view and link edit btns to funcs self.view = View(root) self.view.editIPButton.config(command=self.editIP) self.view.editFilepathButton.config(command=self.editFilepath) self.view.editActiveButton.config(command=self.editActive) self.view.editIdleButton.config(command=self.editIdle) self.view.SensorEnable.config(variable=self.SensorEnable,command=self.updateSensorEnable) self.view.ActiveListCheck.config(variable=self.ActiveList,command=self.updateActiveList) self.view.editActiveDelayTimeButton.config(command=self.editActiveDelayTime) self.view.IdleListCheck.config(variable=self.IdleList,command=self.updateIdleList) self.view.editIdleDelayTimeButton.config(command=self.editIdleDelayTime) # update variables with data from model self.updatePlayerOnline(self.model.spyeworks.get()) self.updateCurrentList(self.model.spyeworks.currentList.get()) self.updateAllLists(self.model.spyeworks.allLists.get()) self.updateIP(self.model.ipaddy.get()) self.updateFilepath(self.model.filepath.get()) self.updateActive(self.model.active.get()) self.updateIdle(self.model.idle.get()) self.updateSensorState(self.model.sensorstate.get()) self.updateActiveDelayTime(self.model.activedelaytime.get()) self.updateIdleDelayTime(self.model.idledelaytime.get()) ################################################################# ### Methods for launching the changer popup for changing settings ################################################################# # launches the popup for editing the players ip address def editIP(self): self.editIP = IPChangerWidget(self.view,self,"Set IP Address","Current IP", "New IP") self.editIP.curractual.config(text=self.model.ipaddy.get()) # launches the popup for editing the filepath def editFilepath(self): self.editFilepath = FilepathChangerWidget(self.view,self,"Set Filepath","Current Filepath", "New Filepath") self.editFilepath.curractual.config(text=self.model.filepath.get()) # launches the popup for editing the active list def editActive(self): self.editActive = ActiveChangerWidget(self.view,self,"Set Active List","Current Active List", "New Active List") self.editActive.curractual.config(text=self.model.active.get()) # launches the popup for editing the idle list def editIdle(self): self.editIdle = IdleChangerWidget(self.view,self,"Set Idle List","Current Idle List", "New Idle List") self.editIdle.curractual.config(text=self.model.idle.get()) # launches the popup for editing the active delay time def editActiveDelayTime(self): self.editActiveDelayTime = ActiveDelayTimeChangerWidget(self.view,self,"Set Active Delay Time","Current Active Delay Time", "New Active Delay Time") self.editActiveDelayTime.curractual.config(text=self.model.activedelaytime.get()) # launches the popup for editing the idle delay time def editIdleDelayTime(self): self.editIdleDelayTime = IdleDelayTimeChangerWidget(self.view,self,"Set Idle Delay Time","Current Idle Delay Time", "New Idle Delay Time") self.editIdleDelayTime.curractual.config(text=self.model.idledelaytime.get()) ################################## ### Methods for updating the model ################################## # sets the new ip address returned from the validate ip function def newIP(self, value): self.model.SetIP(value) # sets the new filepath returned from the validate filepath function def newFilepath(self, value): self.model.SetFilepath(value) # sets the new active list returned from the validate active function def newActive(self, value): self.model.SetActive(value) # sets the new idle list returned from the validate idle function def newIdle(self, value): self.model.SetIdle(value) # sets the new idle list returned from the validate idle function def newActiveDelayTime(self, value): self.model.SetActiveDelayTime(value) # sets the new idle list returned from the validate idle function def newIdleDelayTime(self, value): self.model.SetIdleDelayTime(value) ################################# ### Methods for updating the view ################################# # updates the player online status in the view def updatePlayerOnline(self, value): self.view.updateOnline(value) # updates the current list status in the view def updateCurrentList(self, value): self.view.updateCurrentList(value) # updates the current list status in the view def updateAllLists(self, value): self.view.updateAllLists(value) # updates the ip address in the view def updateIP(self, value): self.view.updateIP(value) # updates the filepath in the view def updateFilepath(self, value): self.view.updateFilepath(value) # updates the active list in the view def updateActive(self, value): self.view.updateActive(value) # updates the idle list in the view def updateIdle(self, value): self.view.updateIdle(value) # updates the sensor status in the view def updateSensorEnable(self): self.model.SetSensorEnable(self.SensorEnable.get()) # if the sensor has been disabled, cancel any active timers if self.SensorEnable.get()=="F": if self.activeTimer.isAlive(): self.activeTimer.cancel() if self.idleTimer.isAlive(): self.idleTimer.cancel() # dummy function for passing to timer thread def dummyFunc(self): pass # handles updates to the sensor status def updateSensorState(self, value): # updates the sensor status in the view self.view.updateSensor(value) # sensor effects # if sensor is enabled if self.SensorEnable.get()=="T": # if the sensor is activated if value=="On": # if the idle timer is active, cancel it if self.idleTimer.isAlive()==True: self.idleTimer.cancel() # if the active timer is on and the active list is enabled, restart the active timer if self.activeTimer.isAlive()==True and self.model.activelist.get()=="T": self.activeTimer.cancel() self.activeTimer=Timer(int(self.model.activedelaytime.get()), self.activeListTimer, ()) self.activeTimer.start() else: self.model.spyeworks.playActive() self.activeTimer=Timer(int(self.model.activedelaytime.get()), self.activeListTimer, ()) self.activeTimer.start() # if the sensor is inactive and the idle list is enabled elif value=="Off" and self.model.idlelist.get()=="T": # if the idle timer is going (it shouldn't be, but just in case) if self.idleTimer.isAlive()==True: self.idleTimer.cancel() # if the active list timer is running and the active list is enabled if self.activeTimer.isAlive()==True and self.model.activelist.get()=="T": self.playIdleList=True # if the active timer is not running or the active list isn't enabled else: self.idleTimer=Timer(int(self.model.idledelaytime.get()), self.model.spyeworks.playIdle, ()) self.idleTimer.start() # plays idle list when active list is finished if called for def activeListTimer(self): if self.playIdleList==True and self.model.sensorstate.get()=="Off" and self.model.idlelist.get()=="T": self.idleTimer=Timer(int(self.model.idledelaytime.get()), self.model.spyeworks.playIdle, ()) self.idleTimer.start() self.playIdleList=False # updates the active delay in the view def updateActiveList(self): self.model.SetActiveList(self.ActiveList.get()) # updates the active delay time in the view def updateActiveDelayTime(self, value): self.view.updateActiveDelayTime(value) # updates the idle delay in the view def updateIdleList(self): self.model.SetIdleList(self.IdleList.get()) # updates the idle delay time in the view def updateIdleDelayTime(self, value): self.view.updateIdleDelayTime(value)
class StateMachineModel: def __init__(self, parent, interface, collaborator_name): self.interface = interface self.parent = parent self.collaborator_name = collaborator_name self.task_name = None self.commit_time_limit = 120.0 self.subTask = {} self.currentSubTask = str() self.initialized = False self.currentSubTaskState = None self.commit_timer = Timer(0, self.void) self.interface_completion = False def INACTIVE(self): self.subTask = {} self.currentSubTask = str() self.parent.adapter.begin_gather() self.interface.set_value("INACTIVE") self.parent.adapter.complete_gather() def PREPARING(self): self.parent.adapter.begin_gather() self.interface.set_value("PREPARING") self.parent.adapter.complete_gather() def COMMITTED(self): self.parent.adapter.begin_gather() self.interface.set_value("COMMITTED") self.parent.adapter.complete_gather() self.timer_commit() def timer_commit(self): if not self.commit_time_limit: return self.no_commit() self.commit_timer = Timer(self.commit_time_limit, self.no_commit) self.commit_timer.daemon = True self.commit_timer.start() def committed(self): #creates 'self.subtasks' list for device specific tasks self.subTask_collab = False collaborator = False self.ordered_tasks = [] self.subTasks = [] self.task_coordinator = self.parent.master_tasks[ self.parent.master_uuid]['coordinator'].keys()[0] master_task = self.parent.master_tasks[self.parent.master_uuid] for key, val in master_task['coordinator'][ self.task_coordinator]['SubTask'].iteritems(): if val: self.ordered_tasks.append([val[4], key, val]) self.ordered_tasks.sort() for i, z in enumerate(self.ordered_tasks): key = z[1] val = z[2] if val: if self.collaborator_name in val[2]: #val[2] is the list of collaborators involved in the lowest level task coordinated by key self.subTask_collab = True elif self.collaborator_name == key: collaborator = True elif not collaborator and key == self.task_coordinator: continue elif collaborator and key != self.collaborator_name: continue if (collaborator and key == self.collaborator_name) or self.subTask_collab: #top level tasks self.subTask[val[0]] = subTask.subTask( parent=self.parent, interface=interface, master_task_uuid=self.parent.master_uuid, collaborators=None, taskName=val[0]) self.subTask[val[0]].superstate.create() if val[2]: if val[0] in master_task['collaborators'][ self.collaborator_name]['SubTask']: self.subtask_collaborator_name = self.collaborator_name elif val[2]: self.subtask_collaborator_name = val[2] else: self.subtask_collaborator_name = None continue currentSubTaskList = master_task['collaborators'][ self.subtask_collaborator_name]['SubTask'][ val[0]] for i, x in enumerate(currentSubTaskList): if x[4]: #low level tasks; interfaces only self.subTask[x[1]] = subTask.subTask( parent=self.parent, interface=interface, master_task_uuid=self.subTask[ val[0]].superstate.task_uuid, collaborators=None, taskName=x[1]) self.subTask[x[1]].superstate.create() self.subTasks.append( [val[0], val[3], currentSubTaskList, key]) if self.subTasks: self.currentSubTask = self.subTasks[0][0] self.currentSubTaskType = self.subTasks[0][1] self.currentSubTaskList = self.subTasks[0][2] self.collaborator_key = self.subTasks[0][3] self.activate = False def current_subtask_check(self, source=None, comp=None, name=None, value=None, code=None, text=None): #event handling once committed if (value == "ACTIVE" and self.currentSubTaskState == "ACTIVE" and name.split('_')[-1] in str(self.ordered_tasks)): #if an untimely event received; send for retry if self.currentSubTaskType not in name: return "RETRY" else: pass if self.currentSubTaskState != 'ACTIVE' or self.currentSubTaskType in name: #top level task management; activation and completion if self.currentSubTask in self.subTask and self.currentSubTaskType in name: self.subTask[self.currentSubTask].superstate.event( source, comp, name, value, code, text) if value.lower() == 'active': self.currentSubTaskState = value elif (self.currentSubTaskList and (self.currentSubTaskList[-1][2] == 'COMPLETE' or not self.currentSubTaskList[-1][4]) and not self.subTask_collab): if (self.subTask[self.currentSubTask].superstate.state == 'removed' and code in self.parent.master_tasks): self.parent.master_tasks[code]['coordinator'][ self.task_coordinator]['SubTask'][ self.collaborator_key][1] = 'COMPLETE' if self.currentSubTask == self.ordered_tasks[-1][ 2][0]: self.completed() if self.currentSubTaskList and self.currentSubTaskState == "ACTIVE" and not self.activate: #low level task management for i, x in enumerate(self.currentSubTaskList): if x[4] and self.currentSubTaskList[i][2] == 'READY': if x[1] == name.split('_')[-1]: self.subTask[x[1]].superstate.event( source, comp, name, value, code, text) if self.subTask[x[ 1]].superstate.state == "removed" and code in self.parent.master_tasks: self.parent.master_tasks[code]['collaborators'][ self.subtask_collaborator_name]['SubTask'][ self.currentSubTask][i][2] = 'COMPLETE' self.currentSubTaskList[i][2] = 'COMPLETE' else: break elif (not x[4] and self.collaborator_name == self.subtask_collaborator_name and not self.currentSubTaskList[i][2]): #internal tasks management; activation and completion self.activate = True self.currentSubTaskList[i][2] = 'COMPLETE' self.parent.event(self.collaborator_name, 'internal_event', x[1], 'ACTIVATE', None, self.collaborator_key) self.parent.master_tasks[code]['collaborators'][ self.collaborator_name]['SubTask'][ self.currentSubTask][i][2] = 'COMPLETE' self.activate = False elif x[4] and not self.currentSubTaskList[i][ 2] and self.subTask_collab: self.currentSubTaskList[i][2] = 'READY' self.parent.event(self.collaborator_name, 'interface_initialization', 'SubTask_' + x[1], 'IDLE') break elif x[4] and not self.currentSubTaskList[i][ 2] and not self.subTask_collab: self.currentSubTaskList[i][2] = 'READY' break if self.subTask_collab and self.currentSubTaskList[-1][ 2] == 'COMPLETE' and not self.interface_completion: #top level task completion for low level task collaborators self.subTask[self.currentSubTask].superstate.success() self.interface_completion = True self.parent.event(self.collaborator_name, 'interface_completion', self.currentSubTaskType, 'COMPLETE') self.interface_completion = False self.parent.master_tasks[self.parent.master_uuid][ 'coordinator'][self.task_coordinator]['SubTask'][ self.collaborator_key][1] = 'COMPLETE' if self.subTasks[-1][0] == self.currentSubTask: self.parent.master_tasks[ self.parent.master_uuid]['collaborators'][ self.collaborator_name]['state'][2] = 'COMPLETE' self.completed() self.parent.IDLE() else: next = False for subTask in self.subTasks: if subTask[0] == self.currentSubTask: next = True elif next: self.currentSubTask = subTask[0] self.currentSubTaskType = subTask[1] self.currentSubTaskList = subTask[2] self.collaborator_key = subTask[3] self.currentSubTaskState = None self.parent.IDLE() break def void(): pass def event(self, source, comp, name, value, code=None, text=None): try: #collaboration binding related event handling if (comp == 'Coordinator' and name == 'binding_state' and value.lower() == 'inactive' and self.interface.value().lower() == 'committed'): if self.currentSubTask: self.subTask[self.currentSubTask].superstate.success() self.completed() self.parent.COMPLETED() elif comp == 'Coordinator' and name == 'binding_state' and value.lower( ) == 'preparing': self.parent.master_tasks[code[0]] = code[1] self.task_created() elif comp == 'Coordinator' and name == 'binding_state': if value.lower() == 'committing': self.commit() elif value.lower( ) == 'committed' and text in self.parent.master_tasks[ code]['coordinator']: self.parent.master_tasks[code]['coordinator'][text][ 'state'][2] = value if self.commit_timer.isAlive(): self.commit_timer.cancel() self.committed() #interfaces related event handling elif 'SubTask' in name and self.interface.value().lower( ) == 'committed': if self.currentSubTask: try: if name.split('_')[-1] in str(self.subTasks): activate = self.current_subtask_check( source, comp, name, value, code, text) if activate == "RETRY": print( "Retrying task activation in 0.5 sec") time.sleep(0.5) self.parent.event(source, comp, name, value, code, text) except Exception as e: "Retrying in 0.500 sec" time.sleep(0.500) self.parent.event(source, comp, name, value, code, text) elif self.subTask: for k, v in self.parent.master_tasks[ self.parent.master_uuid]['coordinator'][ self. task_coordinator]['SubTask'].iteritems(): if v and name.split( '_')[-1] in v[3] and v[0] in self.subTask: try: self.current_subtask_check( source, comp, name, value, code, text) except Exception as e: print(e) print("Retrying in 0.5 sec") time.sleep(0.5) self.parent.event(source, comp, name, value, code, text) self.currentSubTaskState = value.lower() elif v and name.split('_')[-1] in v[3] and v[ 0] not in self.subTask: self.parent.event(source, comp, name.split('_')[-1], value, code, text) else: collab = None if k == self.collaborator_name: if v: collab = v[2] if collab and self.parent.master_tasks[ self.parent. master_uuid]['collaborators'][collab][ 'SubTask'][self.task_name]: for t in self.parent.master_tasks[ self.parent.master_uuid][ 'collaborators'][collab][ 'SubTask'][self.task_name]: if name.split('_')[-1] == t[1]: self.parent.event( source, comp, name.split('_')[-1], value, code, text) break except Exception as e: print("Error processing collaborator event:") print(e) print("Retrying in 1 sec") time.sleep(1)
class Controller: def __init__(self): # create model and setup callbacks self.model = Model() self.model.spyeworks.addCallback(self.updatePlayerOnline) self.model.spyeworks.currentList.addCallback(self.updateCurrentList) self.model.sensorstate.addCallback(self.updateSensorState) # create variables for timers self.activeTimer=Timer(1, self.dummyFunc, ()) self.idleTimer=Timer(1, self.dummyFunc, ()) self.playIdleList=False # update variables with data from model self.updatePlayerOnline(self.model.spyeworks.get()) self.updateSensorState(self.model.sensorstate.get()) ################################# ### Methods for printing to the console ################################# # updates the player online status in the view def updatePlayerOnline(self, value): #print("Player is "+value) pass # updates the current list status in the view def updateCurrentList(self, value): #print("Current List is "+value) pass # dummy function for passing to timer thread def dummyFunc(self): pass # handles updates to the sensor status def updateSensorState(self, value): # updates the sensor status in the view #print("Sensor is "+value) # if the sensor is activated if value=="On": # if the idle timer is active, cancel it if self.idleTimer.isAlive()==True: self.idleTimer.cancel() # if the idle list is playing, play the active list if self.model.spyeworks.currentList.get()==self.model.idle.get(): self.model.spyeworks.playActive() # if the sensor is inactive and the idle list is enabled elif value=="Off" and self.model.idlelist.get()=="T": # if the idle timer is going (it shouldn't be, but just in case) if self.idleTimer.isAlive()==True: self.idleTimer.cancel() # start the idle list timer self.idleTimer=Timer(int(self.model.idledelaytime.get()), self.model.spyeworks.playIdle, ()) self.idleTimer.start() # plays idle list when active list is finished if called for def activeListTimer(self): if self.playIdleList==True and self.model.sensorstate.get()=="Off" and self.model.idlelist.get()=="T": self.idleTimer=Timer(int(self.model.idledelaytime.get()), self.model.spyeworks.playIdle, ()) self.idleTimer.start() self.playIdleList=False
class Dealer: COMMAND_OPTIONS = "cmd-opts" CLIENT_RESP_SELECT_ACTION = "player-act" def __init__(self, play_round): self.__my_play_round = play_round self.__timer_wait_player = None self.__default_player_action = None self.__player_accept_command = None self.__select_action_publish_players = None self.__sent_action_group = None self.__play_cards_process = [] def get_play_cards_process(self): return self.__play_cards_process def get_previous_played_cards(self): if len(self.__play_cards_process) > 0: return self.__play_cards_process[-1] else: return None, None def record_player_cards_history(self, player, cards): self.__play_cards_process.append((player, cards)) def send_player_action_group(self, player, act_group, select_publish_players): self.__player_accept_command = player self.__select_action_publish_players = select_publish_players if player and act_group: self.__sent_action_group = act_group self.__default_player_action = act_group.get_default_action() self.start_timer_to_wait_player_response(act_group.get_select_timeout()) cmd_obj = {"cmd": Dealer.COMMAND_OPTIONS, "opts": act_group.to_json_object()} player.send_server_command(cmd_obj) def start_timer_to_wait_player_response(self, timeout_seconds): self.__timer_wait_player = Timer(timeout_seconds, self.select_default_action) self.__timer_wait_player.start() def select_default_action(self): player = self.__player_accept_command act = self.__default_player_action if act: self.__my_play_round.process_player_select_action(player, act.get_act_id()) def process_player_select_action_of_id(self, player, action_id): action = self.get_action_by_id(action_id) if action: self.process_player_select_action(player, action) def process_player_select_action(self, player, action): self.reset_timer() if self.__select_action_publish_players: resp = {"resp": Dealer.CLIENT_RESP_SELECT_ACTION, "sel-act": action.to_broadcast_json_object()} for p in self.__select_action_publish_players: p.send_server_command(resp) def get_action_by_id(self, action_id): if self.__sent_action_group: return self.__sent_action_group.get_action_by_id(action_id) else: return None def reset_timer(self): if self.__default_player_action: self.__default_player_action = None if self.__timer_wait_player: if self.__timer_wait_player.isAlive(): self.__timer_wait_player.cancel() self.__timer_wait_player = None
class AppEventManager(object): """ This class manages all events to send during app running """ def __init__(self, device, app, event_duration = None): """ construct a new AppEventManager instance :param device: instance of Device :param app: instance of App :return: """ self.logger = logging.getLogger("AppEventManager") self.enabled = True self.device = device self.app = app self.policy = "dynamic" self.events = [] self.event_count = 1000 self.event_interval = 1 self.event_duration = event_duration self.monkey = None self.timer = None self.event_factory = DynamicEventFactory(device, app) def add_event(self, event): """ add one event to the event list :param event: the event to be added, should be subclass of AppEvent :return: """ if event is None: return self.events.append(event) self.device.send_event(event) def dump(self): """ dump the event information to files :return: """ event_log_file = open(os.path.join(self.device.output_dir, "droidbot_event.json"), "w") event_array = [] for event in self.events: event_array.append(event.to_dict()) json.dump(event_array, event_log_file) event_log_file.close() if self.event_factory is not None: self.event_factory.dump() def set_event_factory(self, event_factory): """ set event factory of the app :param event_factory: the factory used to generate events :return: """ self.event_factory = event_factory def start(self): """ start sending event """ self.logger.info("Start sending events, policy is %s" % self.policy) if self.event_duration: self.timer = Timer(self.event_duration, self.stop) self.timer.start() try: if self.event_factory is not None: self.event_factory.start(self) except KeyboardInterrupt: pass self.stop() self.dump() self.logger.info("Finish sending events, saved to droidbot_event.json") def stop(self): """ stop sending event """ if self.monkey: self.monkey.terminate() self.monkey = None if self.timer and self.timer.isAlive(): self.timer.cancel() self.timer = None self.enabled = False
class DroidBot(object): """ The main class of droidbot """ # this is a single instance class instance = None def __init__(self, app_path=None, device_serial=None, is_emulator=False, output_dir=None, env_policy=None, policy_name=None, random_input=False, script_path=None, event_count=None, event_interval=None, timeout=None, keep_app=None, keep_env=False, cv_mode=False, debug_mode=False, profiling_method=None, grant_perm=False, enable_accessibility_hard=False, master=None, humanoid=None, ignore_ad=False, replay_output=None, epsilon=None, acv=None, main_activity=None, scroll_full_down_y=None): """ initiate droidbot with configurations :return: """ logging.basicConfig( level=logging.DEBUG if debug_mode else logging.INFO) self.logger = logging.getLogger('DroidBot') DroidBot.instance = self self.output_dir = output_dir if output_dir is not None: if not os.path.isdir(output_dir): os.makedirs(output_dir) ''' html_index_path = pkg_resources.resource_filename("droidbot", "resources/index.html") stylesheets_path = pkg_resources.resource_filename("droidbot", "resources/stylesheets") target_stylesheets_dir = os.path.join(output_dir, "stylesheets") if os.path.exists(target_stylesheets_dir): shutil.rmtree(target_stylesheets_dir) shutil.copy(html_index_path, output_dir) shutil.copytree(stylesheets_path, target_stylesheets_dir) ''' self.timeout = timeout self.timer = None self.keep_env = keep_env self.keep_app = keep_app self.device = None self.app = None self.droidbox = None self.env_manager = None self.input_manager = None self.enable_accessibility_hard = enable_accessibility_hard self.humanoid = humanoid self.ignore_ad = ignore_ad self.replay_output = replay_output self.acv = acv self.enabled = True try: self.device = Device( device_serial=device_serial, is_emulator=is_emulator, output_dir=self.output_dir, cv_mode=cv_mode, grant_perm=grant_perm, enable_accessibility_hard=self.enable_accessibility_hard, humanoid=self.humanoid, ignore_ad=ignore_ad) self.app = App(app_path, output_dir=self.output_dir) if main_activity is not None: self.app.main_activity = main_activity self.env_manager = AppEnvManager(device=self.device, app=self.app, env_policy=env_policy) self.input_manager = InputManager( device=self.device, app=self.app, policy_name=policy_name, epsilon=epsilon, random_input=random_input, event_count=event_count, event_interval=event_interval, script_path=script_path, profiling_method=profiling_method, master=master, replay_output=replay_output, scroll_full_down_y=scroll_full_down_y) except Exception: import traceback traceback.print_exc() self.stop() sys.exit(-1) @staticmethod def get_instance(): if DroidBot.instance is None: print("Error: DroidBot is not initiated!") sys.exit(-1) return DroidBot.instance def start(self): """ start interacting :return: """ if not self.enabled: return self.logger.info("Starting DroidBot") try: if self.timeout > 0: self.timer = Timer(self.timeout, self.stop) self.timer.start() self.device.set_up() if not self.enabled: return self.device.connect() if not self.enabled: return self.device.install_app(self.app) if not self.enabled: return self.env_manager.deploy() if not self.enabled: return if self.droidbox is not None: self.droidbox.set_apk(self.app.app_path) self.droidbox.start_unblocked() self.input_manager.start() self.droidbox.stop() self.droidbox.get_output() else: if self.acv is not None: self.acv.start_to_instrument_apk(self.app.package_name) self.input_manager.start() except KeyboardInterrupt: self.logger.info("Keyboard interrupt.") pass except Exception: import traceback traceback.print_exc() self.stop() sys.exit(-1) self.stop() ''' if self.output_dir is not None and isinstance(self.input_manager.policy, DataLossPolicy): dirs = ["/stylesheets", "/views"] files = ["/index.html"] self.delete_unused_sources(dirs, files) ''' self.logger.info("DroidBot Stopped") def stop(self): self.enabled = False if self.acv: self.acv.stop_to_instrument_apk(self.app.package_name) if self.timer and self.timer.isAlive(): self.timer.cancel() if self.env_manager: self.env_manager.stop() if self.input_manager: self.input_manager.stop() if self.droidbox: self.droidbox.stop() if self.device: self.device.disconnect() if not self.keep_env: self.device.tear_down() if not self.keep_app: self.device.uninstall_app(self.app) if hasattr(self.input_manager.policy, "master") and \ self.input_manager.policy.master: import xmlrpc.client proxy = xmlrpc.client.ServerProxy(self.input_manager.policy.master) proxy.stop_worker(self.device.serial) def delete_unused_sources(self, dir_list, file_list): import shutil import os for dir_name in dir_list: shutil.rmtree(self.output_dir + dir_name, ignore_errors=True) for file_name in file_list: os.remove(self.output_dir + file_name)
class Spider(object): def __init__(self): monkey.patch_all() self.queue = queue.Queue() self.pool = pool.Pool(int(CoreConfigure().get_config_section_map("spider")['concurrency'])) self.url_table = UrlTable() self.timer = Timer(int(CoreConfigure().get_config_section_map("spider")['timeout']), self.stop) self._stop = event.Event() self.greenlet_finished = event.Event() self.root = None # url_object self.initialize_db() def initialize_db(self): host = CoreConfigure().get_config_section_map('db')['host'] port = CoreConfigure().get_config_section_map('db')['port'] db_name = CoreConfigure().get_config_section_map('db')['database'] collect_name = CoreConfigure().get_config_section_map('db')['collection'] self.db_cli = MongoClient(host, int(port)) # mongodb collection self.collection = self.db_cli[db_name][collect_name] def set_root(self, url): if isinstance(url, basestring): url = UrlObj(url, type = 0) self.root = url self.push_task(self.root) self.url_table.insert(self.root) def push_task(self, url): # type(url) is UrlObj if url not in self.url_table: self.queue.put(url) def run(self, url = None): begin = time.time() if url is None: # read from configure file for default value url = CoreConfigure().get_config_section_map('content')['root_url'] self.set_root(url) self.timer.start() logger.info("spider begin crawl") while not self.stopped() and self.timer.isAlive(): for greenlet in list(self.pool): if greenlet.dead: self.pool.discard(greenlet) try: url = self.queue.get_nowait() except queue.Empty: if self.pool.free_count() != self.pool.size: # wait until one greenlet finish to flash queue self.greenlet_finished.wait() self.greenlet_finished.clear() continue else: self.stop() greenlet = Handler(url, self) self.pool.start(greenlet) logger.info("total time elapsed %0.2f" % (time.time() - begin)) def stopped(self): return self._stop.is_set() def stop(self): logger.info("spider finish, totally catched (%d) urls" % len(self.url_table)) self.timer.cancel() self._stop.set() self.pool.join() self.queue.put(StopIteration)
def clientThread(connectionSocket, addr): peer = None global peer_num room_name = None try: print("Thread Client Entering Now...") host, sock = addr msg = connectionSocket.recv(1024).decode().split() room_name = msg[0] peer_port = msg[1] notif_port = msg[2] peer_num += 1 peer = host + "," + peer_port + "," + notif_port print(peer) if room_name == "___newroom___" or not room_name in room_list_dict: room_name = unique_name() while room_name in room_list_dict: room_name = unique_name() room_list_dict[room_name] = [peer] connectionSocket.send( ("___newroom___ " + room_name + " " + peer).encode()) else: peers_list = room_list_dict[room_name] peers_list.insert(0, peer) to_send = " ".join(peers_list) connectionSocket.send(to_send.encode()) room_list_dict[room_name] = peers_list print(peers_list) while True: global code_num # print("inside while loop") msg = connectionSocket.recv(1024).decode() if msg == "___end___": room_list_dict[room_name].remove(peer) print(room_list_dict[room_name]) break fname = str(uuid.uuid4()) f = open(fname, 'w') if msg[0:7] == "python3": cmd = 'python3 ' + fname elif msg[0:7] == "python2": cmd = 'python ' + fname elif msg[0:7].lower() == "haskell": cmd = 'runhaskell ' + fname else: connectionSocket.send("Unknown language".encode()) continue code_socket = socket(AF_INET, SOCK_STREAM) # reuse port code_socket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) code_socket.bind((config.server_addr, base_code_port + code_num)) code_socket.listen(15) connectionSocket.send((config.server_addr + " " + str(base_code_port + code_num)).encode()) code_num += 1 code_connection, code_host = code_socket.accept() f.write(msg[8:]) while True: data = code_connection.recv(1024).decode() if (data[-9:] == "___EOF___" or not data or data == '' or len(data) <= 0): f.write(data[0:-9]) break else: f.write(data) f.close() try: output = "" p = Popen("exec " + cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True) timer = Timer(10.0, lambda x: x.kill(), [p]) try: timer.start() output = p.stdout.read() except Exception as e: print(e) finally: if (not timer.isAlive()): output = "Execution canceled. Process took too long.\n".encode( ) timer.cancel() if (output.decode() == "" or output.decode() == None): output = "[No output]\n".encode() except: output = "Unexpected error\n".encode() i = 0 output = output.decode().replace(fname, "Your program").encode() while True: data = output[i * 1024:(i + 1) * 1024] i += 1 if (not data or data == '' or len(data) <= 0): break else: try: code_connection.send(data) except: code_connection.send("Unexpected error\n".encode()) code_connection.close() os.remove(fname) except OSError as e: try: os.remove(fname) except: pass print("Socket error:", e)
class FTPMirror(Thread): def __init__(self, id, urls): Thread.__init__(self) self.abort = False self.urls = urls self.id = id self.speed = 0 self.start() def log(self, msg): logger.debug(msg) log = DownloadLog(download_id_id=self.id, message=msg) for i in range(0, 1): try: log.save() return except OperationalError: connection.close() pass def cleanup(self): # Cleanup for c in self.m.handles: if c.fp is not None: common.close_file(c.fp) c.fp = None c.close() self.m.close() def add_url_queue(self, url): size = url[1] urlpath = url[0].strip().rstrip("/") ftpurl = "ftp://%s:%s@%s:%s/%s" % (ftpmanager.FTP_USER, ftpmanager.FTP_PASS, ftpmanager.FTP_IP, ftpmanager.FTP_PORT, urlpath) ftpurl = ftpurl.encode("UTF-8") basepath = "" for part in urlpath.split(os.sep)[3:]: basepath = os.path.join(basepath, part) url_savepath = os.path.join(self.savepath, basepath).rstrip("/") if os.path.isfile(url_savepath): #compare sizes local_size = os.path.getsize(url_savepath) if local_size > 0 and local_size == size: #Already downloaded, skip return #make local folders if they dont exist try: os.makedirs(os.path.split(url_savepath)[0]) except OSError as e: if e.errno == 17: pass else: raise(e) if re.match(".+\.sfv$", urlpath): #download first self.priority.append((ftpurl, url_savepath, size, 0)) else: #add it to the queue self.queue.append((ftpurl, url_savepath, size, 0)) def create_curls(self): self.m.handles = [] for i in range(settings.THREADS_PER_DOWNLOAD): c = pycurl.Curl() c.fp = None c.thread_num = i c.setopt(pycurl.FOLLOWLOCATION, 1) c.setopt(pycurl.MAXREDIRS, 5) c.setopt(pycurl.NOSIGNAL, 1) #c.setopt(pycurl.VERBOSE, 1) c.setopt(pycurl.FTP_SSL, pycurl.FTPSSL_ALL) c.setopt(pycurl.SSL_VERIFYPEER, 0) c.setopt(pycurl.SSL_VERIFYHOST, 0) #TIMER FUNCTIONS #c.setopt(pycurl.LOW_SPEED_LIMIT, ftpmanager.FTP_TIMEOUT_MIN_SPEED) #c.setopt(pycurl.LOW_SPEED_TIME, ftpmanager.FTP_TIMEOUT_WAIT_DOWNLOAD) #c.setopt(pycurl.TIMEOUT, 3600) c.checker = FTPChecker(c.thread_num) c.setopt(c.NOPROGRESS, 0) c.setopt(c.PROGRESSFUNCTION, c.checker.progress_check) #PRET SUPPORT c.setopt(188, 1) self.m.handles.append(c) def get_next_queue(self): #lets find the next one we want to process pop_key = None now = datetime.now() for idx, queue_item in enumerate(self.queue): if queue_item[3] == 0: pop_key = idx break else: seconds = (now-queue_item[3]).total_seconds() if seconds > 30: #lets use this one pop_key = idx break return pop_key def add_file_curl(self, curl, url, filename, remote_size): curl.checker.reset() self.log("Remote file %s size: %s" % (filename, remote_size)) short_filename = os.path.basename(filename) local_size = 0 f = None if os.path.isfile(filename): #compare sizes local_size = os.path.getsize(filename) if local_size == 0: #try again self.log("Will download %s (%s bytes)" % (short_filename, remote_size)) f = common.open_file(filename, "wb") if local_size > remote_size: f = common.open_file(filename, "wb") #lets retry if filename in self.failed_list: #what count are we at count = self.failed_list.get(curl.filename) if count >= 2: self.log("Local size was bigger then remote, max reties reached, setting to failed %s" % short_filename) return else: self.failed_list[filename] += 1 else: self.failed_list[filename] = 1 self.log("Strange, local size was bigger then remote, re-downloading %s" % short_filename) f = common.open_file(filename, "wb") else: #lets resume self.log("Partial download %s (%s bytes), lets resume from %s bytes" % (short_filename, local_size, local_size)) f = common.open_file(filename, "ab") else: self.log("Will download %s (%s bytes)" % (short_filename, remote_size)) f = common.open_file(filename, "wb") curl.setopt(pycurl.RESUME_FROM, local_size) curl.fp = f curl.setopt(pycurl.URL, url) curl.setopt(pycurl.WRITEDATA, curl.fp) self.m.add_handle(curl) # store some info curl.filename = filename curl.remote_size = remote_size curl.url = url def get_save_path(self): dlitem = DownloadItem.objects.get(id=self.id) return dlitem.localpath def update_dlitem(self, id, message=None, failed=False): try: dlitem = DownloadItem.objects.get(id=id) if message: logger.info(message) dlitem.message = message dlitem.log(message) if failed: dlitem.retries += 1 dlitem.save() except: pass def run(self): try: max_speed_kb = settings.MAX_SPEED_PER_DOWNLOAD speed_delay = 0 self.savepath = self.get_save_path() self.log("Starting Download") update_interval = 1 self.timer = Timer(update_interval, dummy_timer_fn) self.timer.start() self.m = pycurl.CurlMulti() # Make a queue with (url, filename) tuples self.queue = [] self.priority = [] try: for url in self.urls: self.add_url_queue(url) except Exception as e: logger.exception(e) self.log("Error: %s" % str(e)) self.update_dlitem(self.id, message=str(e)) return self.queue = self.priority + self.queue # Pre-allocate a list of curl objects self.create_curls() num_urls = len(self.queue) self.failed_list = {} # Main loop freelist = self.m.handles[:] num_processed = 0 while num_processed < num_urls: # If there is an url to process and a free curl object, add to multi stack while self.queue and freelist: key = self.get_next_queue() if None is key: #didn't find any to process.. are we still downloading an item?? if len(freelist) == settings.THREADS_PER_DOWNLOAD: logger.debug("Sleeping 0.5 secs") time.sleep(0.5) break url, filename, remote_size, ___ = self.queue.pop(key) try: self.add_file_curl(freelist.pop(), url, filename, remote_size) except Exception as e: logger.exception(e) self.log(str(e)) self.update_dlitem(self.id, message=str(e)) self.cleanup() return # Run the internal curl state machine for the multi stack while 1: ret, num_handles = self.m.perform() if ret != pycurl.E_CALL_MULTI_PERFORM: break # Check for curl objects which have terminated, and add them to the freelist while 1: num_q, ok_list, err_list = self.m.info_read() for c in ok_list: self.log("Closing file %s" % c.fp) common.close_file(c.fp) #Did we download the file properly??? success = False for x in range(0, 4): local_size = os.path.getsize(c.filename) if local_size == c.remote_size: success = True break logger.debug("sleeping 3 seconds") time.sleep(3) if success: self.log("CURL Thread: %s Success:%s" % (c.thread_num, os.path.basename(c.filename))) else: self.failed_list[c.filename] = 1 self.queue.append((c.url, c.filename, c.remote_size, datetime.now())) num_processed -= 1 self.log("CURL Thread: %s Failure: %s as it Didnt download properly, the local (%s) size does not match the remote size (%s), lets retry" % (c.thread_num, os.path.basename(c.filename), local_size, c.remote_size)) c.fp = None freelist.append(c) self.m.remove_handle(c) for c, errno, errmsg in err_list: common.close_file(c.fp) c.fp = None self.m.remove_handle(c) #should we retry? if errno in retry_on_errors: if errmsg == "Callback aborted": errmsg = "No data received for %s seconds" % ftpmanager.FTP_TIMEOUT_TRANSFER msg = "CURL Thread %s: Unlimited retrying: %s %s %s" % (c.thread_num, os.path.basename(c.filename), errno, errmsg) self.queue.append((c.url, c.filename, c.remote_size, datetime.now())) num_processed -= 1 else: if c.filename in self.failed_list: #what count are we at count = self.failed_list.get(c.filename) if count >= 3: msg = "CURL Thread %s: Failed: %s %s %s" % (c.thread_num, os.path.basename(c.filename), errno, errmsg) last_err = errmsg else: self.failed_list[c.filename] += 1 #retry self.queue.append((c.url, c.filename, c.remote_size, datetime.now())) msg = "CURL Thread %s: Retrying: %s %s %s" % (c.thread_num, os.path.basename(c.filename), errno, errmsg) num_processed -= 1 else: #lets retry self.failed_list[c.filename] = 1 self.queue.append((c.url, c.filename, c.remote_size, datetime.now())) msg = "CURL Thread %s: Retrying: %s %s %s" % (c.thread_num, os.path.basename(c.filename), errno, errmsg) num_processed -= 1 self.log(msg) freelist.append(c) num_processed = num_processed + len(ok_list) + len(err_list) if num_q == 0: break #lets do checks if not self.timer.isAlive(): if self.abort: self.cleanup() return #Lets get speed self.speed = 0 for handle in self.m.handles: try: self.speed = self.speed + handle.getinfo(pycurl.SPEED_DOWNLOAD) except: pass current_speed_kb = self.speed / 1024 #Do we need to throttle the speed? if max_speed_kb > 0: #Are we over our limit?? if current_speed_kb > max_speed_kb: #Throttle down over_by = current_speed_kb - max_speed_kb if over_by > 5: delay_by = over_by / 2000 speed_delay += delay_by #Are we under the limit if current_speed_kb < max_speed_kb: if speed_delay > 0: #Throttle up under_by = max_speed_kb - current_speed_kb delay_by = under_by / 2000 if under_by > 5: speed_delay -= delay_by if speed_delay < 0: speed_delay = 0 #Lets restart the timer for updates.. self.timer = Timer(update_interval, dummy_timer_fn) self.timer.start() # We just call select() to sleep until some more data is available. if speed_delay > 0: time.sleep(speed_delay) self.m.select(1.0) except Exception as e: logger.exception(e) self.update_dlitem(self.id, message=str(e)) finally: # Cleanup self.cleanup()