1 | #!/usr/bin/python |
---|
2 | |
---|
3 | # lpq.debathena |
---|
4 | # |
---|
5 | # Wrapper script that intelligently determines whether a command was |
---|
6 | # intended for CUPS or LPRng and sends it off in the right direction |
---|
7 | |
---|
8 | import os |
---|
9 | import sys |
---|
10 | |
---|
11 | from debathena import printing |
---|
12 | |
---|
13 | opts = { |
---|
14 | printing.SYSTEM_CUPS: { |
---|
15 | 'lp': 'EU:cd:h:mn:o:q:st:H:P:i:', |
---|
16 | 'lpq': 'EU:h:P:al', |
---|
17 | 'lprm': 'EU:h:P:', |
---|
18 | }, |
---|
19 | printing.SYSTEM_LPRNG: { |
---|
20 | 'lp': 'ckmprswBGYd:D:f:n:q:t:', |
---|
21 | 'lpq': 'aAlLVcvP:st:D:', |
---|
22 | 'lprm': 'aAD:P:VU:', |
---|
23 | }, |
---|
24 | } |
---|
25 | |
---|
26 | def queue_opt(command): |
---|
27 | return '-P' if command != 'lp' else '-d' |
---|
28 | |
---|
29 | def error(code, message): |
---|
30 | """Exit out with an error |
---|
31 | """ |
---|
32 | sys.stderr.write(message) |
---|
33 | sys.exit(code) |
---|
34 | |
---|
35 | def execCups(command, queue, args): |
---|
36 | """Pass the command and arguments on to the CUPS versions of the command |
---|
37 | """ |
---|
38 | new_command = '/usr/bin/cups-%s' % command |
---|
39 | new_args = [command, '%s%s' % (queue_opt(command), queue)] + args |
---|
40 | os.execv(new_command, new_args) |
---|
41 | |
---|
42 | def execLprng(command, queue, args): |
---|
43 | """Pass the command and arguments on to the LPRng versions of the command |
---|
44 | """ |
---|
45 | new_command = '/usr/bin/mit-%s' % command |
---|
46 | new_args = [command, '%s%s' % (queue_opt(command), queue)] + args |
---|
47 | os.execv(new_command, new_args) |
---|
48 | |
---|
49 | def main(): |
---|
50 | # Remove the command name from the arguments when we extract it |
---|
51 | command = os.path.basename(sys.argv.pop(0)) |
---|
52 | # Strip .debathena suffix if it's there |
---|
53 | if command[-10:] == '.debathena': |
---|
54 | command = command[:-10] |
---|
55 | |
---|
56 | args = sys.argv |
---|
57 | |
---|
58 | try: |
---|
59 | optinfos = [(s, opts[s][command]) for s in printing.SYSTEMS] |
---|
60 | except KeyError: |
---|
61 | error(1, ("\n" |
---|
62 | "Error: this script was called as %s, when it must be called as\n" |
---|
63 | "one of lpq, lprm, or lp\n" |
---|
64 | "\n" % command)) |
---|
65 | |
---|
66 | queue = printing.get_default_printer() |
---|
67 | argstyle = None |
---|
68 | try: |
---|
69 | argstyle, options, arguments = printing.parse_args(args, optinfos) |
---|
70 | |
---|
71 | # Find the queue specified in the arguments |
---|
72 | queue_args, options = printing.extract_opt(options, queue_opt(command)) |
---|
73 | if queue_args: |
---|
74 | # The last queue specified wins |
---|
75 | queue = queue_args[-1][-1] |
---|
76 | |
---|
77 | # Now that we've sliced up the arguments, put them back |
---|
78 | # together |
---|
79 | args = [o + a for o, a in options] + arguments |
---|
80 | except ValueError: |
---|
81 | # parse_args returned None, so we learned nothing. We'll just |
---|
82 | # go with the default queue |
---|
83 | pass |
---|
84 | |
---|
85 | if not queue: |
---|
86 | # We tried and couldn't figure it out, so not our problem |
---|
87 | error(2, ("\n" |
---|
88 | "No default printer configured. Specify a -P option, or configure a\n" |
---|
89 | "default printer via e.g. System | Administration | Printing.\n" |
---|
90 | "\n")) |
---|
91 | |
---|
92 | system, server, queue = printing.find_queue(queue) |
---|
93 | if system == printing.SYSTEM_CUPS: |
---|
94 | if server: |
---|
95 | os.environ['CUPS_SERVER'] = server |
---|
96 | execCups(command, queue, args) |
---|
97 | elif system == printing.SYSTEM_LPRNG: |
---|
98 | execLprng(command, queue, args) |
---|
99 | else: |
---|
100 | error(1, ("\n" |
---|
101 | "Error: the print queue %s uses an unknown printing infrastructure\n" |
---|
102 | "\n" % queue)) |
---|
103 | |
---|
104 | if __name__ == '__main__': |
---|
105 | main() |
---|