| 1 |
#!/usr/bin/python3 |
| 2 |
|
| 3 |
## system-config-printer |
| 4 |
|
| 5 |
## Copyright (C) 2010 Red Hat, Inc. |
| 6 |
## Authors: |
| 7 |
## Tim Waugh <[email protected]> |
| 8 |
|
| 9 |
## This program is free software; you can redistribute it and/or modify |
| 10 |
## it under the terms of the GNU General Public License as published by |
| 11 |
## the Free Software Foundation; either version 2 of the License, or |
| 12 |
## (at your option) any later version. |
| 13 |
|
| 14 |
## This program is distributed in the hope that it will be useful, |
| 15 |
## but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 |
## GNU General Public License for more details. |
| 18 |
|
| 19 |
## You should have received a copy of the GNU General Public License |
| 20 |
## along with this program; if not, write to the Free Software |
| 21 |
## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 22 |
|
| 23 |
## This program performs validation that cannot be performed using |
| 24 |
## RELAX NG alone. |
| 25 |
|
| 26 |
import fnmatch |
| 27 |
import sys |
| 28 |
import xml.etree.ElementTree |
| 29 |
|
| 30 |
class Validator: |
| 31 |
def __init__ (self, filename): |
| 32 |
self._filename = filename |
| 33 |
|
| 34 |
def validate (self): |
| 35 |
filename = self._filename |
| 36 |
print ("Validating %s" % filename) |
| 37 |
preferreddrivers = xml.etree.ElementTree.XML (open (filename).read ()) |
| 38 |
(drivertypes, preferenceorder) = list(preferreddrivers) |
| 39 |
validates = True |
| 40 |
|
| 41 |
names = set() |
| 42 |
for drivertype in list(drivertypes): |
| 43 |
name = drivertype.get ("name") |
| 44 |
names.add (name) |
| 45 |
|
| 46 |
for printer in list(preferenceorder): |
| 47 |
types = [] |
| 48 |
drivers = printer.find ("drivers") |
| 49 |
if drivers is not None: |
| 50 |
types.extend (list(drivers)) |
| 51 |
|
| 52 |
blacklist = printer.find ("blacklist") |
| 53 |
if blacklist is not None: |
| 54 |
types.extend (list(blacklist)) |
| 55 |
|
| 56 |
for drivertype in types: |
| 57 |
pattern = drivertype.text.strip () |
| 58 |
matches = fnmatch.filter (names, pattern) |
| 59 |
names -= set (matches) |
| 60 |
|
| 61 |
for name in names: |
| 62 |
validates = False |
| 63 |
print(("*** Driver type \"%s\" is never used" % |
| 64 |
name), file=sys.stderr) |
| 65 |
|
| 66 |
return validates |
| 67 |
|
| 68 |
import getopt |
| 69 |
import os |
| 70 |
opts, args = getopt.getopt (sys.argv[1:], "") |
| 71 |
|
| 72 |
if len (args) < 1: |
| 73 |
dirname = os.path.dirname (sys.argv[0]) |
| 74 |
args = [os.path.join (dirname, "preferreddrivers.xml")] |
| 75 |
|
| 76 |
exitcode = 0 |
| 77 |
for filename in args: |
| 78 |
validator = Validator (filename) |
| 79 |
if not validator.validate (): |
| 80 |
exitcode = 1 |
| 81 |
|
| 82 |
sys.exit (exitcode) |