| 182 | def extractDatasFromCups(self) : |
| 183 | """Extract datas from CUPS IPP message or page_log file.""" |
| 184 | # tries to extract job-originating-host-name and other information |
| 185 | self.regainPriv() |
| 186 | cupsdconf = self.getCupsConfigDirectives(["PageLog", "RequestRoot"]) |
| 187 | requestroot = cupsdconf.get("requestroot", "/var/spool/cups") |
| 188 | if (len(self.jobid) < 5) and self.jobid.isdigit() : |
| 189 | ippmessagefile = "c%05i" % int(self.jobid) |
| 190 | else : |
| 191 | ippmessagefile = "c%s" % self.jobid |
| 192 | ippmessagefile = os.path.join(requestroot, ippmessagefile) |
| 193 | ippmessage = {} |
| 194 | try : |
| 195 | ippdatafile = open(ippmessagefile) |
| 196 | except : |
| 197 | self.printInfo("Unable to open IPP message file %s" % ippmessagefile, "warn") |
| 198 | else : |
| 199 | self.logdebug("Parsing of IPP message file %s begins." % ippmessagefile) |
| 200 | try : |
| 201 | ippmessage = IPPRequest(ippdatafile.read()) |
| 202 | ippmessage.parse() |
| 203 | except IPPError, msg : |
| 204 | self.printInfo("Error while parsing %s : %s" % (ippmessagefile, msg), "warn") |
| 205 | else : |
| 206 | self.logdebug("Parsing of IPP message file %s ends." % ippmessagefile) |
| 207 | ippdatafile.close() |
| 208 | self.dropPriv() |
| 209 | |
| 210 | try : |
| 211 | (chtype, clienthost) = ippmessage.operation_attributes.get("job-originating-host-name", \ |
| 212 | ippmessage.job_attributes.get("job-originating-host-name", (None, None))) |
| 213 | (jbtype, bcode) = ippmessage.job_attributes.get("job-billing", (None, None)) |
| 214 | except AttributeError : |
| 215 | clienthost = None |
| 216 | bcode = None |
| 217 | if clienthost is None : |
| 218 | # TODO : in case the job ticket is overwritten later, self.username is not the correct one. |
| 219 | # TODO : doesn't matter much, since this code is only used as a last resort. |
| 220 | (bcode, clienthost) = self.getJobInfosFromPageLog(cupsdconf, self.printername, self.username, self.jobid) |
| 221 | self.logdebug("Client Hostname : %s" % (clienthost or "Unknown")) |
| 222 | self.clientHostname = clienthost |
| 223 | self.initialBillingCode = bcode |
| 224 | os.environ["PYKOTAJOBORIGINATINGHOSTNAME"] = str(self.clientHostname or "") |
| 225 | |
193 | | # tries to extract job-originating-host-name and other information |
194 | | self.regainPriv() |
195 | | cupsdconf = self.getCupsConfigDirectives(["PageLog", "RequestRoot"]) |
196 | | requestroot = cupsdconf.get("requestroot", "/var/spool/cups") |
197 | | if (len(self.jobid) < 5) and self.jobid.isdigit() : |
198 | | ippmessagefile = "c%05i" % int(self.jobid) |
199 | | else : |
200 | | ippmessagefile = "c%s" % self.jobid |
201 | | ippmessagefile = os.path.join(requestroot, ippmessagefile) |
202 | | ippmessage = {} |
203 | | try : |
204 | | ippdatafile = open(ippmessagefile) |
205 | | except : |
206 | | self.printInfo("Unable to open IPP message file %s" % ippmessagefile, "warn") |
207 | | else : |
208 | | self.logdebug("Parsing of IPP message file %s begins." % ippmessagefile) |
209 | | try : |
210 | | ippmessage = IPPRequest(ippdatafile.read()) |
211 | | ippmessage.parse() |
212 | | except IPPError, msg : |
213 | | self.printInfo("Error while parsing %s : %s" % (ippmessagefile, msg), "warn") |
214 | | else : |
215 | | self.logdebug("Parsing of IPP message file %s ends." % ippmessagefile) |
216 | | ippdatafile.close() |
217 | | self.dropPriv() |
218 | | |
219 | | try : |
220 | | (chtype, clienthost) = ippmessage.operation_attributes.get("job-originating-host-name", \ |
221 | | ippmessage.job_attributes.get("job-originating-host-name", (None, None))) |
222 | | (jbtype, bcode) = ippmessage.job_attributes.get("job-billing", (None, None)) |
223 | | except AttributeError : |
224 | | clienthost = None |
225 | | bcode = None |
226 | | if clienthost is None : |
227 | | (bcode, clienthost) = self.getJobInfosFromPageLog(cupsdconf, printer.Name, user.Name, self.jobid) |
228 | | self.logdebug("Client Hostname : %s" % (clienthost or "Unknown")) |
229 | | |
230 | | bcode = self.overwrittenBillingCode or bcode |
| 237 | bcode = self.overwrittenBillingCode or self.initialBillingCode |