File: printconf

package info (click to toggle)
foomatic-gui 0.7.7
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 748 kB
  • ctags: 296
  • sloc: python: 2,150; makefile: 76; ansic: 52; sh: 14
file content (199 lines) | stat: -rwxr-xr-x 6,748 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/python

#     printconf - silly script to set up every printer attached to the
#                 system with zero fuss.
#     Copyright (C) 2004-05 Chris Lawrence <lawrencc@debian.org>

#     This program is free software; you can redistribute it and/or modify
#     it under the terms of the GNU General Public License as published by
#     the Free Software Foundation; either version 2 of the License, or
#     (at your option) any later version.
#
#     This program is distributed in the hope that it will be useful,
#     but WITHOUT ANY WARRANTY; without even the implied warranty of
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#     GNU General Public License for more details.
#
#     You should have received a copy of the GNU General Public License
#     along with this program; if not, write to the Free Software
#     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# $Id: printconf,v 1.25 2005/08/22 05:02:58 lordsutch Exp $

import foomatic
import foomatic.foomatic
import foomatic.detect
import pprint
import re
import os
import textwrap
import commands
import sys

from optparse import OptionParser

try:
    r, c = commands.getoutput('stty size').split()
    rows, columns = int(r) or 24, (int(c) or 80)-1
except:
    rows, columns = 24, 79

parser = OptionParser(version='%prog '+foomatic.__version__)
parser.add_option('-n', '--no-action', '--dry-run', dest='dryrun',
                  action='store_true', help="don't configure any printers")
parser.add_option('-v', '--verbose', dest='verbose', action='store_true',
                  help="include more detail about what's happening")
options, args = parser.parse_args()

dryrun = options.dryrun
verbose = options.verbose

if os.geteuid() and not dryrun:
    print >> sys.stderr, "Please run printconf as root or specify the --dry-run option."
    sys.exit(1)

# regex for invalid characters in queue names to be filtered
_invalidre = re.compile(r'([^A-Za-z0-9_])')

def print_fill(text, *args):
    if args:
        text = text % args
    print textwrap.fill(text, columns)

def cleanup_name(model):
    model = model.lower().replace(' ', '_')
    newmod = _invalidre.sub('', model)
    return newmod

from xml.sax.saxutils import escape

def format_detectdata(data, device):
    dtype = 'general'
    if device.startswith('parallel'):
        dtype = 'parallel'
    elif device.startswith('usb'):
        dtype = 'usb'

    print '<autodetect>'
    print '  <%s>' % dtype
    for param in ('commandset', 'description', 'manufacturer', 'model'):
        d = data.get(param)
        if d:
            print '    <%s>%s</%s>' % (param, escape(d), param)
    print '  </%s>' % dtype
    print '</autodetect>'
    return

# CUPS needs a non-empty printers.conf before lpadmin will work properly
if not os.path.exists('/etc/cups/printers.conf'):
    f = file('/etc/cups/printers.conf', 'a')
    f.close()

if verbose:
    print 'Getting printer information...'

printdb = foomatic.foomatic.get_printer_db()
if not printdb:
    print_fill('Unable to read printer database.  Please ensure the "foomatic-db" package is installed properly.')
    sys.exit(1)

conns = foomatic.detect.printer_connections()

if verbose:
    print 'Autoconfiguring printers...'
# Ensure we don't overwrite any existing queues
queues = {}
existing = foomatic.foomatic.get_printer_queues()
if existing:
    for q in existing.queues:
        queues[q['name']] = True

for (device, detectdata, devdesc, detectdesc) in conns:
    # Skip everything we don't have autodetection data for
    if not detectdata:
        if verbose:
            print 'Skipping %s; no autodetection data available.' % device
        continue

    # Get the IEEE 1284 printer description
    desc = detectdata.get('description')
    pdbinfo = None
    if desc:
        pdbinfo = printdb.autodetect_ids.get(desc)

    if not pdbinfo:
        mfg = detectdata.get('manufacturer', '')
        model = detectdata.get('model', '')
        pdbinfo = printdb.autodetect_ids.get((mfg, model))

    if not pdbinfo:
        # Try using the manufacturer and model information to query the
        # database (this is an ad hoc approach, but may often work)
        mfg = detectdata.get('manufacturer', '').upper()
        model = detectdata.get('model', '').upper()
        pdbinfo = printdb.cmakes.get(mfg, {}).get(model)
        if pdbinfo:
            print_fill('Printer on %s was detected by Debian using the ad-hoc method.  Please submit the following information to foomatic-db@packages.debian.org:', device)
            format_detectdata(detectdata, device)
            print
    
    # Unknown printer; probably should print a message here asking
    # people to contribute this information to foomatic-db
    if not pdbinfo:
        if detectdata:
            print_fill('Printer on %s was not automatically configurable by Debian.  Please submit the following information to foomatic-db@packages.debian.org:', device)
            format_detectdata(detectdata, device)
            print
        continue

    make = pdbinfo.get('make')
    model = pdbinfo.get('model')

    prefdriver = pdbinfo.get('driver')
    printer = pdbinfo.get('id')
    # Unsupported printer; barf something here
    if not prefdriver or not printer:
        print_fill('%s %s on %s is not currently supported by Debian.',
                   make, model, devdesc)
        print
        continue

    drivers = pdbinfo.get('drivers', [])

    if prefdriver == 'gimp-print' and 'gimp-print-ijs' in drivers:
        prefdriver = 'gimp-print-ijs'

    if verbose:
        print 'Printer database data:'
        pprint.pprint(pdbinfo)

    # In case more than one printer of the same type is on the computer
    qname = cleanname = cleanup_name(model)
    i=1
    while queues.get(qname):
        qname = '%s_%d' % (cleanname, i)
        i += 1
    
    data = {'connect' : device, 'description' : desc, 'name' : qname,
            'location' : devdesc, 'driver' : prefdriver,
            'printer' : printer }

    print_fill(
        'Configuring %s %s on %s with %s driver as queue "%s".',
        make, model, device, prefdriver, qname)
    if dryrun:
        print '(dry run; skipping call to foomatic-configure)'
    else:
        foomatic.foomatic.setup_queue(data)
    # Queue X is now set up with name based on the IEEE model
    # some sort of message here would be nice
    queues[qname] = True
    print

# After loop, reinit CUPS
if not dryrun:
    os.system('/usr/sbin/invoke-rc.d cupsys force-reload')    

    print_fill('''If printconf was unable to install all of your printers,
please visit http://www.linuxprinting.org/ for printer information and support
from fellow users.''')