[24763] | 1 | #!/usr/bin/python |
---|
| 2 | """Debathena lpq wrapper script. |
---|
| 3 | |
---|
| 4 | A script that intelligently determines whether a command was intended |
---|
| 5 | for CUPS or LPRng and sends it off in the right direction |
---|
| 6 | """ |
---|
| 7 | |
---|
| 8 | |
---|
[24813] | 9 | import os |
---|
| 10 | import socket |
---|
| 11 | import subprocess |
---|
[24763] | 12 | import sys |
---|
| 13 | |
---|
| 14 | from debathena.printing import common |
---|
| 15 | from debathena.printing import simple |
---|
| 16 | |
---|
| 17 | |
---|
| 18 | opts = ( |
---|
| 19 | (common.SYSTEM_CUPS, 'EU:h:P:al'), |
---|
| 20 | (common.SYSTEM_LPRNG, 'aAlLVcvP:st:D:'), |
---|
| 21 | ) |
---|
| 22 | |
---|
| 23 | |
---|
| 24 | queue_opt = '-P' |
---|
| 25 | |
---|
| 26 | |
---|
[24813] | 27 | def cups_version_is_below_1_4(): |
---|
| 28 | """ |
---|
| 29 | On Debian-based systems, return whether cupsys-bsd is older than 1.4. |
---|
| 30 | |
---|
| 31 | On non-Debian-based systems, return False unconditionally. |
---|
| 32 | """ |
---|
| 33 | try: |
---|
| 34 | version = subprocess.Popen( |
---|
| 35 | ["dpkg-query", "-W", "-f", "${Version}", |
---|
| 36 | "cups-bsd"], stdout=subprocess.PIPE).communicate()[0] |
---|
[24814] | 37 | compare = subprocess.call( |
---|
[24813] | 38 | ["dpkg", "--compare-versions", |
---|
| 39 | version, |
---|
| 40 | "lt-nl", "1.4"]) |
---|
[24814] | 41 | return compare == 0 |
---|
[24813] | 42 | except OSError: |
---|
| 43 | # Assume the current version of CUPS is fine |
---|
| 44 | return False |
---|
| 45 | |
---|
| 46 | |
---|
[24763] | 47 | def _main(args): |
---|
[24813] | 48 | args.pop(0) |
---|
[24763] | 49 | |
---|
[24813] | 50 | queue = common.get_default_printer() |
---|
| 51 | try: |
---|
| 52 | argstyle, options, arguments = common.parse_args(args, opts) |
---|
[24763] | 53 | |
---|
[24813] | 54 | # Find the last queue specified in the arguments |
---|
| 55 | queue_args, options = common.extract_opt(options, queue_opt) |
---|
| 56 | if queue_args: |
---|
| 57 | queue = queue_args[-1][-1] |
---|
| 58 | |
---|
| 59 | # Now that we've sliced up the arguments, put them back |
---|
| 60 | # together |
---|
| 61 | args = [o + a for o, a in options] + arguments |
---|
| 62 | except ValueError: |
---|
| 63 | # parse_args returned None, so we learned nothing. We'll just |
---|
| 64 | # go with the default queue |
---|
| 65 | pass |
---|
| 66 | |
---|
| 67 | if not queue: |
---|
| 68 | # We tried and couldn't figure it out, so not our problem |
---|
| 69 | common.error(2, ("\n" |
---|
| 70 | "No default printer configured. Specify a %s option, or configure a\n" |
---|
| 71 | "default printer via e.g. System | Administration | Printing.\n" |
---|
| 72 | "\n" % queue_opt)) |
---|
| 73 | |
---|
| 74 | system, server, queue = common.find_queue(queue) |
---|
| 75 | |
---|
[25318] | 76 | if server == None and common.get_cups_uri(queue) == None: |
---|
| 77 | # if there's no Hesiod server and no local queue, |
---|
| 78 | # tell the user they're wrong |
---|
| 79 | # But let it fall through in case the user is doing |
---|
| 80 | # stupid things with -h |
---|
| 81 | sys.stderr.write(("\nWARNING: The print queue '%s' does not appear to exist.\n" |
---|
| 82 | "If you're trying to print to a cluster or dorm printer,\n" |
---|
| 83 | "you should now be using the 'mitprint' queue instead.\n" |
---|
| 84 | "See http://mit.edu/printing/pharos for more information.\n\n" % queue)) |
---|
| 85 | |
---|
[25354] | 86 | if server and system == common.SYSTEM_CUPS and args == []: |
---|
[24813] | 87 | # CUPS clients before 1.4 and CUPS servers at least 1.4 don't |
---|
| 88 | # communicate well about lpq stuff, so just implement RFC 1179 lpq |
---|
[25143] | 89 | # ourselves since that works |
---|
[24813] | 90 | if cups_version_is_below_1_4(): |
---|
| 91 | try: |
---|
| 92 | s = socket.socket() |
---|
| 93 | s.settimeout(10) |
---|
| 94 | s.connect((server, 515)) |
---|
| 95 | s.send("\x03" + queue + "\n") |
---|
| 96 | print s.makefile().read() |
---|
| 97 | return 0 |
---|
| 98 | except (socket.error, socket.timeout): |
---|
| 99 | # Oh well. |
---|
| 100 | pass |
---|
| 101 | |
---|
| 102 | args.insert(0, '%s%s' % (queue_opt, queue)) |
---|
| 103 | if server: |
---|
| 104 | os.environ['CUPS_SERVER'] = server |
---|
| 105 | |
---|
| 106 | common.dispatch_command(system, 'lpq', args) |
---|
| 107 | |
---|
| 108 | |
---|
[24763] | 109 | def main(): |
---|
[24774] | 110 | sys.exit(_main(sys.argv)) # pragma: nocover |
---|
[24763] | 111 | |
---|
| 112 | |
---|
| 113 | if __name__ == '__main__': |
---|
[24774] | 114 | main() # pragma: nocover |
---|