Back to index

system-config-printer  1.3.9+20120706
validate.py
Go to the documentation of this file.
00001 #!/usr/bin/python
00002 
00003 ## system-config-printer
00004 
00005 ## Copyright (C) 2010 Red Hat, Inc.
00006 ## Authors:
00007 ##  Tim Waugh <twaugh@redhat.com>
00008 
00009 ## This program is free software; you can redistribute it and/or modify
00010 ## it under the terms of the GNU General Public License as published by
00011 ## the Free Software Foundation; either version 2 of the License, or
00012 ## (at your option) any later version.
00013 
00014 ## This program is distributed in the hope that it will be useful,
00015 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 ## GNU General Public License for more details.
00018 
00019 ## You should have received a copy of the GNU General Public License
00020 ## along with this program; if not, write to the Free Software
00021 ## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00022 
00023 ## This program performs validation that cannot be performed using
00024 ## RELAX NG alone.
00025 
00026 import fnmatch
00027 import sys
00028 import xml.etree.ElementTree
00029 
00030 class Validator:
00031     def __init__ (self, filename):
00032         self._filename = filename
00033 
00034     def validate (self):
00035         filename = self._filename
00036         print "Validating %s" % filename
00037         preferreddrivers = xml.etree.ElementTree.XML (file (filename).read ())
00038         (drivertypes, preferenceorder) = preferreddrivers.getchildren ()
00039         validates = True
00040 
00041         names = set()
00042         for drivertype in drivertypes.getchildren ():
00043             name = drivertype.get ("name")
00044             names.add (name)
00045 
00046         for printer in preferenceorder.getchildren ():
00047             types = []
00048             drivers = printer.find ("drivers")
00049             if drivers != None:
00050                 types.extend (drivers.getchildren ())
00051 
00052             blacklist = printer.find ("blacklist")
00053             if blacklist != None:
00054                 types.extend (blacklist.getchildren ())
00055 
00056             for drivertype in types:
00057                 pattern = drivertype.text.strip ()
00058                 matches = fnmatch.filter (names, pattern)
00059                 names -= set (matches)
00060 
00061         for name in names:
00062             validates = False
00063             print >>sys.stderr, ("*** Driver type \"%s\" is never used" %
00064                                  name)
00065 
00066         return validates
00067 
00068 import getopt
00069 import os
00070 opts, args = getopt.getopt (sys.argv[1:], "")
00071 
00072 if len (args) < 1:
00073     dirname = os.path.dirname (sys.argv[0])
00074     args = [os.path.join (dirname, "preferreddrivers.xml")]
00075 
00076 exitcode = 0
00077 for filename in args:
00078     validator = Validator (filename)
00079     if not validator.validate ():
00080         exitcode = 1
00081 
00082 sys.exit (exitcode)