~ronalde/schoolcups/trunk

« back to all changes in this revision

Viewing changes to trunk/copyjob

  • Committer: Ronald van Engelen
  • Date: 2008-12-21 11:52:14 UTC
  • Revision ID: ubuntu-users@lacocina.nl-20081221115214-827dmp1z7f7enait
initial version

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /usr/bin/env python
 
2
 
 
3
# holdjob - printcontrol cups backend
 
4
# Copyright (C) 2007  Wim van Schijndel <wvschijndel@hooghuislyceum.nl>
 
5
#
 
6
# This program is free software; you can redistribute it and/or modify
 
7
# it under the terms of the GNU General Public License as published by
 
8
# the Free Software Foundation; either version 2 of the License, or
 
9
# (at your option) any later version.
 
10
#
 
11
# This program is distributed in the hope that it will be useful,
 
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
# GNU General Public License for more details.
 
15
#
 
16
# You should have received a copy of the GNU General Public License
 
17
# along with this program; if not, write to the Free Software
 
18
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
19
 
 
20
import sys
 
21
import os.path, os
 
22
import cups
 
23
from storm.locals import *
 
24
 
 
25
# reading configuration file
 
26
config={}
 
27
filepointer = open ("/opt/asq/asq.conf" ,"r")
 
28
for line in filepointer:
 
29
        if line.replace(" ","")[0]!="#":
 
30
                line = line.split()
 
31
                cnf = []
 
32
                for arg in line:
 
33
                        if len(arg)>0:
 
34
                                cnf.append(arg)
 
35
                if len(cnf)>0:
 
36
                        try:
 
37
                                config[cnf[0]] = cnf[1]
 
38
                        except:
 
39
                                config[cnf[0]] = ""
 
40
 
 
41
# setting cups constants
 
42
CUPS_BACKEND_OK = 0 # Job completed successfully
 
43
CUPS_BACKEND_FAILED = 1 # Job failed, use error-policy
 
44
CUPS_BACKEND_AUTH_REQUIRED = 2 # Job failed, authentication required
 
45
CUPS_BACKEND_HOLD = 3 # Job failed, hold job
 
46
CUPS_BACKEND_STOP = 4 #  Job failed, stop queue
 
47
CUPS_BACKEND_CANCEL = 5 # Job failed, cancel job
 
48
 
 
49
# defining database classes
 
50
class dbjob(object):
 
51
        __storm_table__ = "jobs"
 
52
        jobs_id = Int(primary=True)
 
53
        job_id = Int()
 
54
        job_options = Unicode()
 
55
        job_name = Unicode()
 
56
        job_printer = Unicode()
 
57
        job_priority = Unicode()
 
58
        time_at_processing = Int()
 
59
        job_originating_user_name = Unicode()
 
60
        job_k_octets = Int()
 
61
        job_media_sheets_completed  = Int()
 
62
        time_at_creation = Int()
 
63
        job_state = Int()
 
64
        job_pages = Int()
 
65
        job_data_file = Unicode()
 
66
        job_thumb_file = Unicode()
 
67
 
 
68
# setting callback function for cups authentication
 
69
def callback ():
 
70
        return config["cups_admin_pass"]
 
71
 
 
72
# translate cups printeruri to printername
 
73
def UriToPrinter(uri):
 
74
        return uri[uri.rfind("/")+1:len(uri)]
 
75
 
 
76
# main program
 
77
def main():
 
78
        # only 1 argument so display backend info to cups
 
79
        if len(sys.argv)==1:
 
80
                sys.stdout.write("direct %s:// \"Unknown\" \"CopyJob\"\n" % os.path.basename(sys.argv[0]))
 
81
                sys.stdout.flush()
 
82
                sys.exit(CUPS_BACKEND_OK)
 
83
        # to many or to little arguments so display usage
 
84
        #elif len(sys.argv) not in (5,6):
 
85
        #       sys.stdout.write("Usage: %s job-id user title copies options [file]\n" % os.path.basename(sys.argv[0]))
 
86
        #       sys.stdout.flush()
 
87
        #       sys.exit(CUPS_BACKEND_CANCEL)
 
88
        # getting job from cups so process
 
89
        else:
 
90
                # create cups info list with given arguments
 
91
                cupsjobinfo={}
 
92
                cupsjobinfo["job_id"]=int(sys.argv[1])
 
93
                cupsjobinfo["job_options"]=sys.argv[5]
 
94
                # setting cups admin user and password
 
95
                cups.setUser(config["cups_admin_user"])
 
96
                cups.setPasswordCB (callback)
 
97
                # make connection with cups and get active jobs
 
98
                cc = cups.Connection()
 
99
                cupsjobs = cc.getJobs()
 
100
                # connect to sql database
 
101
                database = create_database("mysql://%s:%s@%s:%s/%s" % (config["sql_user"],config["sql_pass"],config["sql_host"],config["sql_port"],config["sql_database"]))
 
102
                store = Store(database)
 
103
                # append cups info list with job info from cups
 
104
                cupsjobinfo["job_name"] = cupsjobs[cupsjobinfo["job_id"]]["job-name"]
 
105
                cupsjobinfo["job_printer"] = UriToPrinter(cupsjobs[cupsjobinfo["job_id"]]["job-printer-uri"])
 
106
                cupsjobinfo["job_originating_user_name"] = cupsjobs[cupsjobinfo["job_id"]]["job-originating-user-name"]
 
107
                # append cups info list with postscriptfile location
 
108
                if len(str(cupsjobinfo["job_id"])) < 5 and str(cupsjobinfo["job_id"]).isdigit() :
 
109
                        cupsjobinfo["job_data_file"] = "d%05i-001" % int(cupsjobinfo["job_id"])
 
110
                else :
 
111
                        cupsjobinfo["job_data_file"] = "d%s-001" % cupsjobinfo["job_id"]
 
112
                cupsjobinfo["job_data_file"] = os.path.join(config["cups_spool_dir"], cupsjobinfo["job_data_file"])
 
113
                # create a copy in the holding queue
 
114
                cupsjobinfo["job_id"] = cc.printTestPage("%s_holding" % (cupsjobinfo["job_printer"]),cupsjobinfo["job_data_file"],cupsjobinfo["job_name"],"application/postscript",cupsjobinfo["job_originating_user_name"])
 
115
                # get info from cups job copy
 
116
                cupsjobs = cc.getJobs()
 
117
                # changing and adding info from cubs job copy
 
118
                cupsjobinfo["job_name"] = cupsjobs[cupsjobinfo["job_id"]]["job-name"]
 
119
                cupsjobinfo["job_printer"] = UriToPrinter(cupsjobs[cupsjobinfo["job_id"]]["job-printer-uri"])
 
120
                cupsjobinfo["job_originating_user_name"] = cupsjobs[cupsjobinfo["job_id"]]["job-originating-user-name"]
 
121
                # append cups info list
 
122
                if len(str(cupsjobinfo["job_id"])) < 5 and str(cupsjobinfo["job_id"]).isdigit() :
 
123
                        cupsjobinfo["job_data_file"] = "d%05i-001" % int(cupsjobinfo["job_id"])
 
124
                else :
 
125
                        cupsjobinfo["job_data_file"] = "d%s-001" % cupsjobinfo["job_id"]
 
126
                cupsjobinfo["job_data_file"] = os.path.join(config["cups_spool_dir"], cupsjobinfo["job_data_file"])
 
127
                cupsjobinfo["time_at_creation"] = int(cupsjobs[cupsjobinfo["job_id"]]["time-at-creation"])
 
128
                cupsjobinfo["job_state"] = cupsjobs[cupsjobinfo["job_id"]]["job-state"]
 
129
                cupsjobinfo["job_media_sheets_completed"] = cupsjobs[cupsjobinfo["job_id"]]["job-media-sheets-completed"]
 
130
                cupsjobinfo["job_k_octets"] = cupsjobs[cupsjobinfo["job_id"]]["job-k-octets"]
 
131
                cupsjobinfo["job_priority"] = cupsjobs[cupsjobinfo["job_id"]]["job-priority"]
 
132
                cupsjobinfo["time_at_processing"] = int(cupsjobs[cupsjobinfo["job_id"]]["time-at-processing"])
 
133
                cupsjobinfo["job_thumb_file"] = os.path.join("%s/www/thumbs" % (config["install_path"]), "%s.jpg" % (str(cupsjobinfo["job_id"])))
 
134
                # importing the analyzer to calculate number of pages in postscript file and append it to cups info list
 
135
                from pkpgpdls import analyzer
 
136
                cupsjobinfo["job_pages"]=analyzer.PDLAnalyzer(cupsjobinfo["job_data_file"]).getJobSize()
 
137
                # make jpeg file from postscript file
 
138
                cmd = "gs -sDEVICE=jpeg -dPARANOIDSAFER -dNOPAUSE -dBATCH -dQUIET -r72 -dJPEGQ=100 -sOutputFile=%s %s" % (cupsjobinfo["job_thumb_file"],cupsjobinfo["job_data_file"])
 
139
                os.system(cmd)
 
140
                # make thumbnail file from jpeg file
 
141
                try:
 
142
                        from PIL import Image
 
143
                        PILimg = Image.open(cupsjobinfo["job_thumb_file"])
 
144
                        PILnewimg = PILimg.resize([180,250],Image.ANTIALIAS)
 
145
                        PILnewimg.save(cupsjobinfo["job_thumb_file"])
 
146
                except:
 
147
                        pass
 
148
                # setting filepermissions for thumbnail file
 
149
                os.chmod(cupsjobinfo["job_thumb_file"],0644)
 
150
                # connect to sql database
 
151
                database = create_database("mysql://%s:%s@%s:%s/%s" % (config["sql_user"],config["sql_pass"],config["sql_host"],config["sql_port"],config["sql_database"]))
 
152
                store = Store(database)
 
153
                # checking if job already exists in database
 
154
                sqljobs = store.find(dbjob, dbjob.job_id == int(cupsjobinfo["job_id"]))
 
155
                #  if job doesn't exist add cups info to database
 
156
                if sqljobs.count()==0:
 
157
                        sqljob = dbjob()
 
158
                        sqljob.job_id = int(cupsjobinfo["job_id"])
 
159
                        sqljob.job_options = u"%s" % (cupsjobinfo["job_options"])
 
160
                        sqljob.job_name = u"%s" % (cupsjobinfo["job_name"])
 
161
                        sqljob.job_printer = u"%s" % (cupsjobinfo["job_printer"])
 
162
                        sqljob.job_priority = u"%s" % (cupsjobinfo["job_priority"])
 
163
                        sqljob.time_at_processing = cupsjobinfo["time_at_processing"]
 
164
                        sqljob.job_originating_user_name = u"%s" % (cupsjobinfo["job_originating_user_name"])
 
165
                        sqljob.job_k_octets = int(cupsjobinfo["job_k_octets"])
 
166
                        sqljob.job_media_sheets_completed  = int(cupsjobinfo["job_media_sheets_completed"])
 
167
                        sqljob.time_at_creation = cupsjobinfo["time_at_creation"]
 
168
                        sqljob.job_state = int(cupsjobinfo["job_state"])
 
169
                        sqljob.job_pages = int(cupsjobinfo["job_pages"])
 
170
                        sqljob.job_data_file = u"%s" % (cupsjobinfo["job_data_file"])
 
171
                        sqljob.job_thumb_file = u"%s" % (cupsjobinfo["job_thumb_file"])
 
172
                        store.add(sqljob)
 
173
                        store.commit()
 
174
                sys.exit(CUPS_BACKEND_OK)
 
175
main()