def isConnectionOn(): retcode, output = callExt('ping -c 1 goole.com') if retcode == 0: return True for i in range(2): sleep(3) retcode, output = callExt('ping -c 1 goole.com') if retcode == 0: return True return False
def shutdown(minutes_to_delay): shutdownCmd = 'shutdown -h +%d' % (minutes_to_delay) retcode, output = callExt(shutdownCmd) if len(output) > 0: #print the output of the external command for outLine in output.splitlines(): logAppend("callExt: {0}".format(outLine))
def suspend(suspendToMemory, waitSeconds, onResume=None): if waitSeconds <= 0: return sync_with_cloud(300) #syncDiskWithMemory() suspendStartTime = time() if suspendToMemory: suspendCmd = 'rtcwake -l -m mem -s %d' % (waitSeconds) if onResume is not None: suspendCmd = '{0} && {1}'.format(suspendCmd, onResume) try: retcode, output = callExt(suspendCmd) if len(output) > 0: #print the output of the external command for outLine in output.splitlines(): logAppend("callExt: {0}".format(outLine)) if retcode < 0: raise SuspendError("rtc", "suspend: rtcwake was terminated by signal {0}".format(-retcode)) if retcode > 0: raise SuspendError("rtc", "suspend: rtcwake returned error code {0}".format(retcode)) except ShellError as e: raise SuspendError("OSError", "suspend: rtcwake execution failed: {0}".format(e)) else: sleep(waitSeconds) # Resume from suspend if time() - suspendStartTime < waitSeconds: # Resume from suspend was not caused by the rtc, such as power button or keyboard return False return True
def syncDiskWithMemory(): print 'Sync Disk With Memory' try: retcode, output = callExt('sync') if len(output) > 0: #print the output of the external command for outLine in output.splitlines(): logAppend("sync: {0}".format(outLine)) if retcode < 0: raise SuspendError("sync", "sync was terminated with failure code {0}".format(-retcode)) except ShellError as e: raise SuspendError("OSError", "sync execution failed: {0}".format(e))
def check_and_reset_network_connection(): '''Require DNS servers setting. See: http://askubuntu.com/a/465759 ''' print 'Check network connection' if not isConnectionOn(): logAppend('network:reset connection') # Require root permissions # See: http://ubuntuforums.org/showthread.php?t=1829796 retcode, output = callExt('service network-manager restart') if len(output) > 0: #print the output of the external command for outLine in output.splitlines(): logAppend("{0}".format(outLine)) if not isConnectionOn(): # The connection is lost # raise CloudError("network", "ping error") logAppend('network:ping error')
def screenPowerSave(on): if on: callExt('vbetool dpms off') else: callExt('vbetool dpms on')