3
exec -a "$0" python -- "$@" <(tail -n +4 -- "$0") || exit 0 # -*- python -*-
5
## Copyright (C) 2009 Red Hat, Inc.
6
## Author: Tim Waugh <twaugh@redhat.com>
8
## This program is free software; you can redistribute it and/or modify
9
## it under the terms of the GNU General Public License as published by
10
## the Free Software Foundation; either version 2 of the License, or
11
## (at your option) any later version.
13
## This program is distributed in the hope that it will be useful,
14
## but WITHOUT ANY WARRANTY; without even the implied warranty of
15
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
## GNU General Public License for more details.
18
## You should have received a copy of the GNU General Public License
19
## along with this program; if not, write to the Free Software
20
## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28
CAN_EXAMINE_PPDS=False
30
from getopt import getopt
41
class TimedOut(Exception):
43
Exception.__init__ (self, "Timed out")
56
for a in [' ', '(', ')']:
57
r = r.replace (a, '_')
61
for mfg, mdlset in self.ids.iteritems ():
62
mfgsquash = squash (mfg)
64
mdlsquash = squash (mdl)
65
ret.append ("postscriptdriver(%s;%s;)" % (mfgsquash,
70
def __add__ (self, other):
71
if isinstance(other, DeviceIDs):
72
for omfg, omdlset in other.ids.iteritems ():
74
mdlset = self.ids[omfg]
77
self.ids[omfg] = mdlset
79
mdlset.update (omdlset)
83
pieces = other.split (';')
91
if key in ["MFG", "MANUFACTURER"]:
93
elif key in ["MDL", "MODEL"]:
98
mdlset = self.ids[mfg]
101
self.ids[mfg] = mdlset
109
self.ids = DeviceIDs()
114
class PPDDriver(Driver):
115
def __init__ (self, pathname=None):
116
Driver.__init__ (self)
117
self.pathname = pathname
120
if self.pathname != None:
121
self.examine_file (self.pathname)
123
return Driver.list (self)
125
def examine_file (self, path):
127
ppd = cups.PPD (path)
129
# Not a PPD file. Perhaps it's a drv file.
130
drv = DrvDriver (path)
131
self.ids += drv.list ()
134
attr = ppd.findAttr ('1284DeviceID')
136
self.ids += attr.value
138
class DynamicDriver(Driver):
139
def __init__ (self, driver):
140
Driver.__init__ (self)
142
signal.signal (signal.SIGALRM, self._alarm)
144
def _alarm (self, sig, stack):
149
p = subprocess.Popen ([self.driver, "list"],
150
stdout=subprocess.PIPE,
151
stderr=subprocess.PIPE)
153
(stdout, stderr) = p.communicate ()
156
posix.kill (p.pid, signal.SIGKILL)
160
print >> sys.stderr, stderr
163
lines = stdout.split ('\n')
165
l = shlex.split (line)
170
return Driver.list (self)
172
class DrvDriver(PPDDriver):
173
def __init__ (self, pathname):
174
PPDDriver.__init__ (self)
177
def _alarm (self, sig, stack):
181
tmpdir = os.environ.get ("TMPDIR", "/tmp") + os.path.sep
182
outputdir = tempfile.mkdtemp (dir=tmpdir)
186
"-I", "/usr/share/cups/ppdc",
190
p = subprocess.Popen (argv,
191
stdout=subprocess.PIPE,
192
stderr=subprocess.PIPE)
194
(stdout, stderr) = p.communicate ()
197
posix.kill (p.pid, signal.SIGKILL)
200
os.path.walk (outputdir, self.examine_directory, None)
202
return Driver.list (self)
204
def examine_directory (self, unused, dirname, fnames):
206
path = dirname + os.path.sep + fname
207
self.examine_file (path)
211
def __init__ (self, filelist=None):
215
paths = map (lambda x: x.rstrip (), filelist.readlines ())
216
self.ids = DeviceIDs ()
219
if path.find ("/usr/lib/cups/driver/") != -1:
221
self.ids += DynamicDriver (path).list ()
228
for searchpath in ["/usr/share/cups/model/",
230
"/usr/share/cups/drv/"]:
232
if path.find (searchpath) != -1:
234
if stat.S_ISLNK (st.st_mode):
236
elif stat.S_ISREG (st.st_mode):
237
candidates.add (path)
239
# Now check for symlinks (just one level)
240
for each in symlinks:
241
target = os.path.realpath (each)
242
if target in candidates:
246
st = os.lstat (target)
250
if stat.S_ISREG (st.st_mode):
251
candidates.add (target)
252
elif stat.S_ISDIR (st.st_mode):
253
if not target.endswith (os.path.sep):
254
target += os.path.sep
257
if path.find (target) != -1:
259
if stat.S_ISREG (st.st_mode):
260
candidates.add (path)
262
for path in candidates:
264
self.ids += PPDDriver (path).list ()
269
return self.ids.get_tags ()
271
if __name__ == "__main__":
272
builder = TagBuilder ()
273
tags = builder.get_tags ()