~ubuntu-branches/ubuntu/vivid/system-config-printer/vivid

« back to all changes in this revision

Viewing changes to test/test-cups-driver.py

  • Committer: Package Import Robot
  • Author(s): Till Kamppeter
  • Date: 2014-07-06 09:41:43 UTC
  • mfrom: (1.1.81)
  • Revision ID: package-import@ubuntu.com-20140706094143-yvp3kzo7ti1ghih8
Tags: 1.4.5+20140706-0ubuntu1
* New upstream release
   o GIT 1.4.x snapshot from 6 July 2014
   o Some codec fixes
   o Traceback fixes
   o IPv6 address entry fix
   o Auth info saving improvement
   o Some loop/hang bug fixes
   o Use LockButton for fewer auth dialogs
* 30_newprinter-driver-download-override-false-error-alarm.patch,
  33_dont-use-hp-makeuri-with-non-hp-printers.patch:
  Removed, included upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
# -*- python -*-
 
3
 
 
4
## Copyright (C) 2008 Red Hat, Inc.
 
5
## Copyright (C) 2008 Tim Waugh <twaugh@redhat.com>
 
6
 
 
7
## This program is free software; you can redistribute it and/or modify
 
8
## it under the terms of the GNU General Public License as published by
 
9
## the Free Software Foundation; either version 2 of the License, or
 
10
## (at your option) any later version.
 
11
 
 
12
## This program is distributed in the hope that it will be useful,
 
13
## but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
## GNU General Public License for more details.
 
16
 
 
17
## You should have received a copy of the GNU General Public License
 
18
## along with this program; if not, write to the Free Software
 
19
## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
20
 
 
21
import sys
 
22
 
 
23
import cups
 
24
try:
 
25
    from cupshelpers import missingPackagesAndExecutables
 
26
except ImportError:
 
27
    sys.path.append ('..')
 
28
    from cupshelpers import missingPackagesAndExecutables
 
29
 
 
30
from getopt import getopt
 
31
import os
 
32
import posix
 
33
import re
 
34
import shlex
 
35
import signal
 
36
import subprocess
 
37
import tempfile
 
38
 
 
39
class TimedOut(Exception):
 
40
    def __init__ (self):
 
41
        Exception.__init__ (self, "Timed out")
 
42
 
 
43
class MissingExecutables(Exception):
 
44
    def __init__ (self):
 
45
        Exception.__init__ (self, "Missing executables")
 
46
 
 
47
class Driver:
 
48
    def __init__ (self, driver):
 
49
        self.exe = "/usr/lib/cups/driver/%s" % driver
 
50
        self.ppds = None
 
51
        self.files = {}
 
52
        signal.signal (signal.SIGALRM, self._alarm)
 
53
 
 
54
    def _alarm (self, sig, stack):
 
55
        raise TimedOut
 
56
 
 
57
    def list (self):
 
58
        if self.ppds:
 
59
                return self.ppds
 
60
 
 
61
        signal.alarm (60)
 
62
        p = subprocess.Popen ([self.exe, "list"],
 
63
                              stdout=subprocess.PIPE,
 
64
                              stderr=subprocess.PIPE)
 
65
        try:
 
66
            (stdout, stderr) = p.communicate ()
 
67
            signal.alarm (0)
 
68
        except TimedOut:
 
69
            posix.kill (p.pid, signal.SIGKILL)
 
70
            raise
 
71
 
 
72
        if stderr:
 
73
                print >> sys.stderr, stderr
 
74
 
 
75
        ppds = []
 
76
        lines = stdout.split ('\n')
 
77
        for line in lines:
 
78
                l = shlex.split (line)
 
79
                if len (l) < 1:
 
80
                        continue
 
81
                ppds.append (l[0])
 
82
 
 
83
        self.ppds = ppds
 
84
        return ppds
 
85
 
 
86
    def cat (self, name):
 
87
        try:
 
88
            return self.files[name]
 
89
        except KeyError:
 
90
            signal.alarm (10)
 
91
            p = subprocess.Popen ([self.exe, "cat", name],
 
92
                                  stdout=subprocess.PIPE,
 
93
                                  stderr=subprocess.PIPE)
 
94
            try:
 
95
                (stdout, stderr) = p.communicate ()
 
96
                signal.alarm (0)
 
97
            except TimedOut:
 
98
                posix.kill (p.pid, signal.SIGKILL)
 
99
                raise
 
100
 
 
101
            if stderr:
 
102
                print >> sys.stderr, stderr
 
103
 
 
104
            self.files[name] = stdout
 
105
            return stdout
 
106
 
 
107
opts, args = getopt (sys.argv[1:], "m:")
 
108
if len (args) != 1:
 
109
    print "Syntax: test-cups-driver [-m REGEXP] DRIVER"
 
110
    sys.exit (1)
 
111
 
 
112
match = None
 
113
for opt, arg in opts:
 
114
    if opt == '-m':
 
115
        match = arg
 
116
        break
 
117
 
 
118
bad = []
 
119
ids = set()
 
120
d = Driver (args[0])
 
121
list = d.list ()
 
122
 
 
123
if match:
 
124
    exp = re.compile (match)
 
125
    list = filter (lambda x: exp.match (x), list)
 
126
 
 
127
n = len (list)
 
128
i = 0
 
129
for name in list:
 
130
    i += 1
 
131
    try:
 
132
        ppd = d.cat (name)
 
133
        (fd, fname) = tempfile.mkstemp ()
 
134
        f = os.fdopen (fd, "w")
 
135
        f.write (ppd)
 
136
        del f
 
137
        try:
 
138
            PPD = cups.PPD (fname)
 
139
        except:
 
140
            os.unlink (fname)
 
141
            raise
 
142
        os.unlink (fname)
 
143
 
 
144
        (pkgs, exes) = missingPackagesAndExecutables (PPD)
 
145
        if pkgs or exes:
 
146
            raise MissingExecutables
 
147
 
 
148
        attr = PPD.findAttr ('1284DeviceID')
 
149
        if attr:
 
150
            pieces = attr.value.split (';')
 
151
            mfg = mdl = None
 
152
            for piece in pieces:
 
153
                s = piece.split (':', 1)
 
154
                if len (s) < 2:
 
155
                    continue
 
156
                key, value = s
 
157
                key = key.upper ()
 
158
                if key in ["MFG", "MANUFACTURER"]:
 
159
                    mfg = value
 
160
                elif key in ["MDL", "MODEL"]:
 
161
                    mdl = value
 
162
            if mfg and mdl:
 
163
                id = "MFG:%s;MDL:%s;" % (mfg, mdl)
 
164
                ids.add (id)
 
165
        sys.stderr.write ("%3d%%\r" % (100 * i / n))
 
166
        sys.stderr.flush ()
 
167
    except KeyboardInterrupt:
 
168
        print "Keyboard interrupt\n"
 
169
        break
 
170
    except TimedOut as e:
 
171
        bad.append ((name, e))
 
172
        print "Timed out fetching %s" % name
 
173
    except Exception as e:
 
174
        bad.append ((name, e))
 
175
        print "Exception fetching %s: %s" % (name, e)
 
176
 
 
177
    sys.stdout.flush ()
 
178
 
 
179
if len (bad) > 0:
 
180
    print "Bad PPDs:"
 
181
    for each in bad:
 
182
        print "  %s (%s)" % each
 
183
    print
 
184
 
 
185
if len (ids) > 0:
 
186
    print "IEEE 1284 Device IDs:"
 
187
    for each in ids:
 
188
        print "  %s" % each
 
189
    print