1#! /usr/bin/python 2 3# Copyright (c) 2014, Intel Corporation 4# All rights reserved. 5# 6# Redistribution and use in source and binary forms, with or without modification, 7# are permitted provided that the following conditions are met: 8# 9# 1. Redistributions of source code must retain the above copyright notice, this 10# list of conditions and the following disclaimer. 11# 12# 2. Redistributions in binary form must reproduce the above copyright notice, 13# this list of conditions and the following disclaimer in the documentation and/or 14# other materials provided with the distribution. 15# 16# 3. Neither the name of the copyright holder nor the names of its contributors 17# may be used to endorse or promote products derived from this software without 18# specific prior written permission. 19# 20# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 21# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 22# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 24# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 25# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 26# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 27# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 31from lxml import etree 32from os import path 33from os import walk 34from sys import argv 35 36class PrintColor(): 37 @staticmethod 38 def success(stringToPrint): 39 green=32 40 PrintColor._printColor(green, stringToPrint) 41 42 @staticmethod 43 def error(stringToPrint): 44 red=31 45 PrintColor._printColor(red, stringToPrint) 46 47 @staticmethod 48 def _printColor(color, stringToPrint): 49 """prints strings in color via ascii escape sequence""" 50 print("\033[%sm%s\033[0m" % (str(color), stringToPrint)) 51 52def getSchemaFilenameFromXmlFile(xmlFilePath): 53 """getSchemaFileNameFromXmlFile 54 55 The pfw considers that the .xsd file has the same name as the 56 root element name of the .xml. 57 With of this knowledge, we may easily find the 58 schema file we need. 59 60 Args: 61 xmlFilePath: the xml file. 62 63 Returns: 64 str: the corresponding .schema name 65 """ 66 xmlTree = etree.parse(xmlFilePath) 67 rootElement = xmlTree.getroot() 68 return rootElement.tag + '.xsd' 69 70def validateXmlWithSchema(xmlFilePath, schemaFilePath): 71 """validateXmlWithSchema 72 73 Validates an .xml file based on his corresponding schema. 74 75 Args: 76 xmlFilePath (str): the absolute path to the xml file. 77 schemaFilePath (str): the absolute path to the schema. 78 """ 79 baseXmlName = path.basename(xmlFilePath) 80 baseSchemaName = path.basename(schemaFilePath) 81 print 'Attempt to validate', baseXmlName, 'with', baseSchemaName 82 83 schemaContent = etree.parse(schemaFilePath) 84 schema = etree.XMLSchema(schemaContent) 85 xmlContent = etree.parse(xmlFilePath) 86 xmlContent.xinclude() 87 88 if schema.validate(xmlContent): 89 PrintColor.success('%s is valid' % str(baseXmlName)) 90 else: 91 PrintColor.error('Error: %s' % str(schema.error_log)) 92 93# handle main arguments 94if len(argv) != 3: 95 PrintColor.error('Error: usage %s xmlDirectory schemaDirectory' % str(argv[0])) 96 exit(1) 97 98xmlDirectory = argv[1] 99schemaDirectory = argv[2] 100 101print('[*] Validate xml files in %s with %s' % (xmlDirectory, schemaDirectory)) 102 103for rootPath, _, files in walk(xmlDirectory): 104 for filename in files: 105 if filename.endswith('.xml'): 106 xmlFilePath = path.join(rootPath, filename) 107 schemaFileName = getSchemaFilenameFromXmlFile(xmlFilePath) 108 schemaFilePath = path.join(schemaDirectory, schemaFileName) 109 validateXmlWithSchema(xmlFilePath, schemaFilePath) 110