#!/usr/bin/python # -*- coding: utf-8 -*- """ Copyright(C) 2007 INL Written by Damien Boucard <damien.boucard AT inl.fr> 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, version 3 of the License. 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, see <http://www.gnu.org/licenses/>. --- checkdesc is a program which check the validity of a given desc.xml file. """ __revision__ = '0.1' __author__ = 'Damien Boucard' __copyright__ = 'Copyright 2006, INL' from sys import exit, argv, stderr from optparse import OptionParser from warnings import simplefilter from checkdesc.desc_warnings import TypeWarning, NameWarning, \ VersionWarning, UselessWarning from checkdesc.unicity import UnicityError, IncreasingError from checkdesc.descxml import Loader, OmittedPropertyError def parse_command_line(): usage = "usage: %prog [options] desc_file.xml" parser = OptionParser(usage, version = '%prog '+ __revision__) # defining expected options parser.add_option('-W', '--warn', metavar='CATEGORY', action='append', help="Filter to display warnings or not (type -Whelp for more details)") parser.add_option('--check-version', metavar='VERSION', type='str', default=None, help="Reject different version than VERSION") parser.add_option('--debug', action='store_true', help="Filter to display warnings or not (type -Whelp for more details)") # parsing command line (options, args) = parser.parse_args(argv) # checking options if options.warn is None: options.warn = ['all',] warn_available = { 'type': TypeWarning, 'name': NameWarning, 'version': VersionWarning, 'useless': UselessWarning, } prefix_to_action = {"always":"always", "no":"ignore", "once":"once", "error":"error"} cat_dict = dict([(cat, 'ignore') for cat in warn_available]) set_no = False action = "always" for value in options.warn: if value == "help": print """ Usage: -WCATEGORY, --warn=CATEGORY Filter to display warnings or not This option can be called several times in the same command line. The order of option values has effect on interpretation (see examples). If no -W or --warn option is set, "always-all" is used by default. If no prefix is given, "always" is used. If a prefix is given without category, "all" is used (do put the trailing '-' in this case, see examples). Warning categories: all Enable all categories (DEFAULT). id Enable or disable warnings about "id" properties. useless Enable or disable warnings about unexpected properties. type Enable or disable warnings about "type" properties. name Enable or disable warnings about "name" properties. queue Enable or disable warnings about "queue" properties. address Enable or disable warnings about properties containing an IP address. Each warning category can be prefixed by: always- Print each time a warning occurs (DEFAULT). no- Ignore all warning of a given category. once- Print only once each warning of a same category. error- Raise an exception which stops the program. Examples: * Only enable warnings about IP address and type: -Waddress -Wtype * Disable all warning: -Wno * Enable all warnings excepted those about id: -Wall -Wno-id * Enables all warnings but just one time those about id: -Wall -Wonce-id * All warnings raise an exception excepted those unexpected: -Werror -Wno-useless """ exit(0) n = value.find('-') if n != -1: vprefix = value[:n] vcat = value[n+1:] if vprefix not in prefix_to_action or vcat not in (["all",] + warn_available.keys()): raise ValueError("Invalid option value: \"%s\"" %(value)) else: if value in prefix_to_action: vprefix = value vcat = "all" elif value == "all" or value in warn_available: vprefix = "always" vcat = value else: raise ValueError("Invalid option value: \"%s\"" %(value)) if vprefix == "no": action = "ignore" else: action = vprefix if vcat == "all": for cat in cat_dict: cat_dict[cat] = action else: cat_dict[vcat] = action for cat,action in cat_dict.iteritems(): simplefilter(action, category=warn_available[cat]) # checking arguments if len(args) != 2: print >>stderr, "Bad number of arguments." parser.print_help() exit(1) try: desc_xml = open(args[1], 'r') except IOError, e: print >>stderr, "%s: '%s'" %(e.strerror, e.filename) parser.print_help() exit(1) return options, desc_xml def loadFile(filename, options): loader = Loader(filename) if options.check_version: version = options.check_version else: version = None desc = loader.build(version) desc.check_integrity() def main(): ERRORS = (OmittedPropertyError, UnicityError, IncreasingError, ValueError, KeyError) options, desc_xml = parse_command_line() if not options.debug: try: loadFile(desc_xml, options) except ERRORS, err: print "ERROR: %s" % err exit(1) else: loadFile(desc_xml, options) if __name__ == "__main__": main()