import os import subprocess import threading import xmlrpclib import cups IOTA_RESPONSE_NOT_READY=-1 IOTA_RESPONSE_NO_SUCH_JOB=-2 IOTA_GUI="/usr/bin/printa-gui" class Printa: def __init__(self): self.jobs={} self.display={} #def init def add_job(self,ip,job): #ip is an autocompleted parameter. contains client IP ret=-1 if type(job) != type({}): return ret ret-=1 for key in ["job","user","msg_type","time"]: if key not in job: return ret ret-=1 display=self.get_display(job["user"],ip) if display==None: #remove job from CUPS here return ret SYSTEM_LANGUAGE_FILE="/etc/default/locale" USER_LANGUAGE_FILE="/home/%s/.pam_environment"%job["user"] lang="" if os.path.exists(USER_LANGUAGE_FILE): f=open(USER_LANGUAGE_FILE) lines=f.readlines() f.close() for line in lines: if "LANG=" in line: lang=line.strip("\n") lang=lang.replace('"',"") lang=lang.replace("'","") if lang=="" and os.path.exists(SYSTEM_LANGUAGE_FILE): f=open(SYSTEM_LANGUAGE_FILE) lines=f.readlines() f.close() for line in lines: if "LANG=" in line: lang=line.strip("\n") lang=lang.replace('"',"") lang=lang.replace("'","") cmd="%s %s %s"%(lang,IOTA_GUI,job["job"]) self.jobs[job["job"]]=job return self.callback(ip,job["user"],cmd) #def add_job def remove_job(self,job_id): try: self.cups_conn=cups.Connection() self.cups_conn.cancelJob(int(job_id)) return True except: return False #def remove_job def get_job(self,job): if job in self.jobs: return self.jobs[job] return -1 def get_job_response(self,job): #IOTA_RESPONSE_NOT_READY=-1 #IOTA_RESPONSE_NO_SUCH_JOB=-2 if job in self.jobs: if "response" in self.jobs[job]: return self.jobs[job]["response"] else: return IOTA_RESPONSE_NOT_READY return IOTA_RESPONSE_NO_SUCH_JOB def set_response(self,user_info,job,ret): try: self.jobs[job]["response"]=ret if ret==1: self.jobs[job]["status"]="completed" if ret==0: self.jobs[job]["status"]="cancelled" user_info=pwd.getpwnam(self.jobs[job]["user"]) user_uid=str(user_info.pw_uid) except: return False return True #def set_response def register_display(self,user,display): display=display.split(":") if len(display)>1: display=":"+display[1] else: display=":"+display[0] self.display[user]=display return True #def register_display def get_display(self,user,ip): p=subprocess.Popen(["who"],stdout=subprocess.PIPE) output=p.communicate()[0].split("\n") displays=[] gtty_found=False for line in output: if user in line: if "tty" in line: tmp=line.split("tty")[1][:1] tmp=int(tmp) if tmp>=7: gtty_found=True line=line.split(" ")[-1] if "(" in line and ")" in line and ":" in line: displays.append(line[1:len(line)-1]) for display in displays: if ip in display: return display if len(displays)>0: return displays[0] if len(displays)==0 and gtty_found: return ":0" if user in self.display: return self.display[user] return None #def get_display def callback(self,ip,user,cmd,force_display=None): if force_display==None: try: display=self.get_display(user,ip) except Exception as e: raise e if display==None: #remove job from CUPS here display=self.get_display("cless",ip) #return -1 else: display=force_display display=ip+force_display objects["GuiLauncherManager"].execute("",user,cmd) ''' cmd="DISPLAY='%s' su -c '%s' %s"%(display,cmd,user) print cmd f=open("/tmp/callback","w") f.write(cmd) f.write("\n") f.close() t=threading.Thread(target=self._exec,args=(cmd,)) t.daemon=True t.start() ''' return 0 #def callback def _exec(self,cmd): os.system(cmd) #def _exec def get_controlled_printers(self): printers=self.get_printers() ret={} for printer_name in printers: if "printa:" in printers[printer_name]["device-uri"]: ret[printer_name]=printers[printer_name]["device-uri"] return ret #def get_controlled_printers def get_non_controlled_printers(self): printers=self.get_printers() ret={} for printer_name in printers: if "printa:" not in printers[printer_name]["device-uri"]: ret[printer_name]=printers[printer_name]["device-uri"] return ret #def get_non_controlled_printers def get_printers(self): printers={} try: con=cups.Connection() printers=con.getPrinters() return printers except Expcetion as e: print e return printers #def get_printers def enable_control(self,device_name): if device_name not in self.get_controlled_printers(): current_devices=self.get_non_controlled_printers() if device_name in current_devices: try: current_uri=current_devices[device_name] uri="printa:"+current_uri con=cups.Connection() con.setPrinterDevice(device_name,uri) return True except Exception as e: print e return False #def enable_control def disable_control(self,device_name): if device_name not in self.get_non_controlled_printers(): current_devices=self.get_controlled_printers() if device_name in current_devices: try: current_uri=current_devices[device_name] uri=current_uri.strip("printa:") con=cups.Connection() con.setPrinterDevice(device_name,uri) return True except Exception as e: print e return False #def disable_control #class Iota if __name__=="__main__": i=Iota() print i.get_display("cless","")