#!/usr/bin/env python3 import os import time import subprocess import syslog #in minutes SERVER_WAIT_TIME=2 CLIENT_WAIT_TIME=1 DEBUG=True class Shutdowner: def __init__(self): self.init_var() self.current_flavour=self.what_am_i() self.shutdown(self.current_flavour) #def __init__ def dprint(self,msg): msg="[lliurex-shutdowner] %s"%msg if DEBUG: print(msg) syslog.syslog(msg) #def dprint def init_var(self): self.lliurex_flavour={} self.lliurex_flavour["server"]=False self.shutdown_function={} self.shutdown_function["server"]=self.server_shutdown #def init_var def what_am_i(self): if os.system("lliurex-version -x server")==0: self.lliurex_flavour["server"]=True if self.lliurex_flavour["server"]: return "server" return "unknown" #def what_am_i def shutdown(self,flavour): return self.shutdown_function[flavour]() #def shutdown def server_shutdown(self): self.dprint("Server shutdown") # server always handles shutdown dialogs for thin clients self.standalone_shutdown(SERVER_WAIT_TIME) #def server_shutdown def unknown_shutdown(self): self.dprint("Unknown shutdown") self.dprint("Calling basic_shutdown...") self.basic_shutdown() #def unknown_shutdown def standalone_shutdown(self,wait_time=CLIENT_WAIT_TIME): self.dprint("Standalone shutdown") standalone=subprocess.Popen(["who | grep tty"],shell=True,stdout=subprocess.PIPE).communicate()[0] standalone=standalone.decode("utf-8") displays=set() for item in standalone.strip("\n").split("\n"): display=None x=item.split(" ") user=x[0] display=x[-1] if "(" not in display or ")" not in display or ":" not in display: continue display=display.lstrip("(").rstrip(")") command="su -c 'DISPLAY=%s /usr/sbin/shutdown-lliurex-dialog.py %s' %s &"%(display,wait_time,user) self.dprint("\t%s"%command) os.system(command) self.dprint("Calling basic_shutdown...") self.basic_shutdown(wait_time) #def standalone_shutdown def basic_shutdown(self,wait_time=CLIENT_WAIT_TIME): self.dprint("Basic shutdown") command="/sbin/shutdown -h +%s"%wait_time self.dprint("\t%s"%command) os.system(command) #def basic_shutdown #class Shutdowner if __name__=="__main__": sd=Shutdowner()