201 | | def fakePrint(self) : |
202 | | """Fakes to print the job.""" |
203 | | if os.environ.has_key("CUPS_SERVERROOT") and os.path.isdir(os.environ.get("CUPS_SERVERROOT", "")) : |
204 | | # Either the job's datas are on our stdin, or in a file |
205 | | if len(sys.argv) == 7 : |
206 | | self.InputFile = sys.argv[6] |
207 | | else : |
208 | | self.InputFile = None |
209 | | |
210 | | # check that the DEVICE_URI environment variable's value is |
211 | | # prefixed with "tea4cups:" otherwise don't touch it. |
212 | | # If this is the case, we have to remove the prefix from |
213 | | # the environment before launching the real backend in cupspykota |
214 | | device_uri = os.environ.get("DEVICE_URI", "") |
215 | | if device_uri.startswith("cupspykota:") : |
216 | | fulldevice_uri = device_uri[:] |
217 | | device_uri = fulldevice_uri[len("cupspykota:"):] |
218 | | if device_uri.startswith("//") : # lpd (at least) |
219 | | device_uri = device_uri[2:] |
220 | | os.environ["DEVICE_URI"] = device_uri # TODO : side effect ! |
221 | | # TODO : check this for more complex urls than ipp://myprinter.dot.com:631/printers/lp |
222 | | try : |
223 | | (backend, destination) = device_uri.split(":", 1) |
224 | | except ValueError : |
225 | | raise PyKotaToolError, "Invalid DEVICE_URI : %s\n" % device_uri |
226 | | while destination.startswith("/") : |
227 | | destination = destination[1:] |
228 | | checkauth = destination.split("@", 1) |
229 | | if len(checkauth) == 2 : |
230 | | destination = checkauth[1] |
231 | | printerhostname = destination.split("/")[0].split(":")[0] |
232 | | return ("CUPS", \ |
233 | | printerhostname, \ |
234 | | os.environ.get("PRINTER"), \ |
235 | | sys.argv[2].strip(), \ |
236 | | sys.argv[1].strip(), \ |
237 | | inputfile, \ |
238 | | int(sys.argv[4].strip()), \ |
239 | | sys.argv[3], \ |
240 | | sys.argv[5], \ |
241 | | backend) |
| 203 | def initBackend(self) : |
| 204 | """Initializes the backend's attributes.""" |
| 205 | # check that the DEVICE_URI environment variable's value is |
| 206 | # prefixed with self.myname otherwise don't touch it. |
| 207 | # If this is the case, we have to remove the prefix from |
| 208 | # the environment before launching the real backend |
| 209 | muststartwith = "%s:" % self.myname |
| 210 | device_uri = os.environ.get("DEVICE_URI", "") |
| 211 | if device_uri.startswith(muststartwith) : |
| 212 | fulldevice_uri = device_uri[:] |
| 213 | device_uri = fulldevice_uri[len(muststartwith):] |
| 214 | if device_uri.startswith("//") : |
| 215 | device_uri = device_uri[2:] |
| 216 | try : |
| 217 | (backend, destination) = device_uri.split(":", 1) |
| 218 | except ValueError : |
| 219 | raise TeeError, "Invalid DEVICE_URI : %s\n" % device_uri |
| 220 | |
| 221 | self.JobId = sys.argv[1].strip() |
| 222 | self.UserName = sys.argv[2].strip() |
| 223 | self.Title = sys.argv[3].strip() |
| 224 | self.Copies = int(sys.argv[4].strip()) |
| 225 | self.Options = sys.argv[5].strip() |
| 226 | if len(sys.argv) == 7 : |
| 227 | self.InputFile = sys.argv[6] # read job's datas from file |
| 228 | else : |
| 229 | self.InputFile = None # read job's datas from stdin |
| 230 | |
| 231 | self.RealBackend = backend |
| 232 | self.DeviceURI = device_uri |
| 233 | self.PrinterName = os.environ.get("PRINTER", "") |
| 234 | self.Directory = self.getPrintQueueOption(self.PrinterName, "directory") |
| 235 | self.DataFile = os.path.join(self.Directory, "%s-%s-%s" % (self.myname, self.PrinterName, self.JobId)) |
| 236 | |
| 237 | def exportAttributes(self) : |
| 238 | """Exports our backend's attributes to the environment.""" |
| 239 | os.environ["DEVICE_URI"] = self.DeviceURI # WARNING ! |
| 240 | os.environ["TEAPRINTERNAME"] = self.PrinterName |
| 241 | os.environ["TEADIRECTORY"] = self.Directory |
| 242 | os.environ["TEADATAFILE"] = self.DataFile |
| 243 | os.environ["TEAJOBSIZE"] = self.JobSize |
| 244 | os.environ["TEAMD5SUM"] = self.JobMD5Sum |
| 245 | os.environ["TEAJOBID"] = self.JobId |
| 246 | os.environ["TEAUSERNAME"] = self.UserName |
| 247 | os.environ["TEATITLE"] = self.Title |
| 248 | os.environ["TEACOPIES"] = str(self.Copies) |
| 249 | os.environ["TEAOPTIONS"] = self.Options |
| 250 | os.environ["TEAINPUTFILE"] = self.InputFile or "" |
| 251 | |
| 252 | def saveDatasAndCheckSum(self) : |
| 253 | """Saves the input datas into a static file.""" |
| 254 | self.logDebug("Duplicating data stream to %s" % self.DataFile) |
| 255 | mustclose = 0 |
| 256 | if self.InputFile is not None : |
| 257 | infile = open(self.InputFile, "rb") |
| 258 | mustclose = 1 |
| 259 | else : |
| 260 | infile = sys.stdin |
| 261 | CHUNK = 64*1024 # read 64 Kb at a time |
| 262 | dummy = 0 |
| 263 | sizeread = 0 |
| 264 | checksum = md5.new() |
| 265 | outfile = open(self.DataFile, "wb") |
| 266 | while 1 : |
| 267 | data = infile.read(CHUNK) |
| 268 | if not data : |
| 269 | break |
| 270 | sizeread += len(data) |
| 271 | outfile.write(data) |
| 272 | checksum.update(data) |
| 273 | if not (dummy % 32) : # Only display every 2 Mb |
| 274 | self.logDebug("%s bytes saved..." % sizeread) |
| 275 | dummy += 1 |
| 276 | outfile.close() |
| 277 | if mustclose : |
| 278 | self.infile.close() |
| 279 | self.JobSize = sizeread |
| 280 | self.JobMD5Sum = checksum.hexdigest() |
| 281 | self.logDebug("Job %s is %s bytes long." % (self.JobId, self.JobSize)) |
| 282 | self.logDebug("Job %s MD5 sum is %s" % (self.JobId, self.JobMD5Sum)) |