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

Revision 24814, 3.0 KB checked in by geofft, 14 years ago (diff)
lpq: subprocess.Popen().wait() is subprocess.call()
  • 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 system == common.SYSTEM_CUPS and args == []:
77        # CUPS clients before 1.4 and CUPS servers at least 1.4 don't
78        # communicate well about lpq stuff, so just implement RFC 1179 lpq
79        # stuff, so just implement lpq ourselves since that works
80        if cups_version_is_below_1_4():
81            try:
82                s = socket.socket()
83                s.settimeout(10)
84                s.connect((server, 515))
85                s.send("\x03" + queue + "\n")
86                print s.makefile().read()
87                return 0
88            except (socket.error, socket.timeout):
89                # Oh well.
90                pass
91       
92    args.insert(0, '%s%s' % (queue_opt, queue))
93    if server:
94        os.environ['CUPS_SERVER'] = server
95
96    common.dispatch_command(system, 'lpq', args)
97
98
99def main():
100    sys.exit(_main(sys.argv)) # pragma: nocover
101
102
103if __name__ == '__main__':
104    main() # pragma: nocover
Note: See TracBrowser for help on using the repository browser.