source: trunk/debathena/config/printing-config/debathena/printing/lpq.py @ 25354

Revision 25354, 3.5 KB checked in by jdreed, 13 years ago (diff)
In printing-config: * Don't trigger the sketchy lpd code for local queues
  • Property svn:executable set to *
Line 
1#!/usr/bin/python
2"""Debathena lpq wrapper script.
3
4A script that intelligently determines whether a command was intended
5for CUPS or LPRng and sends it off in the right direction
6"""
7
8
9import os
10import socket
11import subprocess
12import sys
13
14from debathena.printing import common
15from debathena.printing import simple
16
17
18opts = (
19    (common.SYSTEM_CUPS, 'EU:h:P:al'),
20    (common.SYSTEM_LPRNG, 'aAlLVcvP:st:D:'),
21)
22
23
24queue_opt = '-P'
25
26
27def 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]
37        compare = subprocess.call(
38                      ["dpkg", "--compare-versions",
39                       version,
40                       "lt-nl", "1.4"])
41        return compare == 0
42    except OSError:
43        # Assume the current version of CUPS is fine
44        return False
45
46 
47def _main(args):
48    args.pop(0)
49
50    queue = common.get_default_printer()
51    try:
52        argstyle, options, arguments = common.parse_args(args, opts)
53
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
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
86    if server and system == common.SYSTEM_CUPS and args == []:
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
89        # ourselves since that works
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
109def main():
110    sys.exit(_main(sys.argv)) # pragma: nocover
111
112
113if __name__ == '__main__':
114    main() # pragma: nocover
Note: See TracBrowser for help on using the repository browser.