#!/usr/bin/python import sys import syslog import os import time import xmlrpclib import subprocess import pwd LOG_FILE="/tmp/pepito" CUPS_DIR="/var/spool/cups/" tmp=xmlrpclib.ServerProxy("https://localhost:9779") try: url=objects["VariablesManager"].get_variable("PRINTACLIENTCONFIG")["server"] except: url="localhost" SERVER_URL="https://%s:9779"%url TOTAL_WAIT_TIME=60 SLEEP_TIME=5 DEBUG=True def get_pages(job_id): for f in os.listdir(CUPS_DIR): if job_id+"-" in f: file_path=CUPS_DIR+f p=subprocess.Popen(["file",file_path],stdout=subprocess.PIPE) output=p.communicate()[0].strip("\n") if "PostScript" in output: os.system("ps2pdf %s /tmp/.%s"%(file_path,f)) file_path="/tmp/."+f p=subprocess.Popen(["pdfinfo",file_path],stdout=subprocess.PIPE) output=p.communicate()[0] output=output.split("\n") for line in output: if "Pages:" in line: return int(line.split(" ")[-1]) return -1 #def get_pages def debug(info,data): if DEBUG: try: syslog.syslog("[PRINTA] "+str(info)+":") syslog.syslog(str(data)) except Exception as e: syslog.syslog("[ERROR] " + str(e)) #def debug def prepare_backend(device_uri): try: tmp=device_uri.split(":")[1] tmp="/usr/lib/cups/backend/"+tmp except Exception as e: tmp=str(e) return tmp #def prepare_backend def prepare_args(args): try: ret="" for arg in args: ret+="'" ret+=arg ret+="' " except Exception as e: debug("prepare_args exception",str(e)) return "" #def prepare_args def fix_device_uri(): uri=os.environ["DEVICE_URI"].replace("printa:","") os.environ["DEVICE_URI"]=uri return uri #fix device_uri def get_n4d_key(): f=open("/etc/n4d/key") key=f.readline().strip("\n") f.close() return key def update_quota(user,pages): try: server=xmlrpclib.ServerProxy(SERVER_URL) ret=server.substract_user_quota(get_n4d_key(),"PrintaServer",user,pages) debug("user_quota after",(ret)) except Exception as e: debug("error update_quota",e) #def update_quota def create_job_info(): job=sys.argv[1] user=sys.argv[2] copies=int(sys.argv[4]) job_name=sys.argv[3] printer=os.environ["DEVICE_URI"] d={} d["job"]=job d["job_name"]=job_name d["user"]=user d["printer_uri"]=printer d["printer"]=os.environ["PRINTER_INFO"] d["time"]=time.time() d["msg_type"]=0 try: d["pages"]=get_pages(job)*copies except: d["pages"]=-1 return d if __name__=="__main__": if len(sys.argv)>5: n4d_key=get_n4d_key() try: debug("BEGIN","=====================") debug("sys.argv",sys.argv) backend=prepare_backend(os.environ["DEVICE_URI"]) debug("backend",backend) debug("END","====================\n") args=[fix_device_uri()]+sys.argv[1:] debug("Parameters",args) job_info=create_job_info() debug("job_info",job_info) fix_device_uri() server=xmlrpclib.ServerProxy(SERVER_URL) client=xmlrpclib.ServerProxy("https://localhost:9779") job_info["status"]=0 ok_status=True ret=client.add_job(n4d_key,"Printa","",job_info) debug("add_job",ret) if ret==0 and ok_status: ok=False for x in range(TOTAL_WAIT_TIME/SLEEP_TIME): r=client.get_job_response("","Printa",job_info["job"]) if type(r)==type(1): if r < 0: debug("Sleeping...",5) time.sleep(5) continue else: if r==1: ok=True debug("got_ok_response","") break else: ok=False debug("got_cancel_response","") break if ok: debug("Printing...","") newpid = os.fork() if newpid == 0: os.execv(backend,args) sys.exit(1) os.wait() else: debug("Cancelling...","") ret=client.remove_job(n4d_key,"Printa",job_info["job"]) else: ret=client.remove_job(n4d_key,"Printa",job_info["job"]) sys.exit(0) except Exception as e: debug("ERROR",e)