1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
|
#! /usr/bin/env python
#############################################################
# #
# Author: Bertrand Neron #
# Organization:'Biological Software and Databases' Group, #
# Institut Pasteur, Paris. #
# Distributed under GPLv2 Licence. Please refer to the #
# COPYING.LIB document. #
# #
#############################################################
import os , sys
import time
from lxml import etree
MOBYLEHOME = None
if os.environ.has_key('MOBYLEHOME'):
MOBYLEHOME = os.environ['MOBYLEHOME']
if not MOBYLEHOME:
sys.exit('MOBYLEHOME must be defined in your environment')
if ( MOBYLEHOME ) not in sys.path:
sys.path.append( MOBYLEHOME )
if ( os.path.join( MOBYLEHOME , 'Src' ) ) not in sys.path:
sys.path.append( os.path.join( MOBYLEHOME , 'Src' ) )
from Mobyle.Classes.DataType import DataTypeFactory
import Mobyle.Parser
import Mobyle.Classes.Core
import Local.CustomClasses
from Mobyle.MobyleError import ParserError
class XmlDataTypeRepository:
TAIL = '//'
SEP = '|'
COMMENT = '#'
XML_HEADER = "%-30.30s %s %-15.15s %s %s" % ( " xml class" , SEP , "python class", SEP , "description" )
PYTHON_HEADER = " Mobyle python class used "
BIOTYPE_HEADER = " biotypes used "
def __init__(self, path):
self._dataTypeFactory = DataTypeFactory()
self.descriptions = {}
self.biotypes = []
self.xmlTypes = [] # [ xmlType, python Type ]
self.pythonTypes = []
self.parse( path )
def parse(self, xmlPaths):
self.name = 'xml list'
errors = []
biotypes = {}
for xmlPath in xmlPaths:
doc = etree.parse( "file://%s" % os.path.abspath( xmlPath ) )
parameters = doc.xpath( './/parameter' )
for parameter in parameters:
try:
typeNode = parameter.xpath( 'type' )[0]
mt = Mobyle.Parser.parseType( typeNode , self._dataTypeFactory )
for biotype in mt.getBioTypes():
biotypes[ biotype ] = None
except IndexError:
parameterName = parameter.xpath( 'name/text()')[0]
errors.append( "%s/%s haven't any type" % (xmlPath , parameterName ) )
except ParserError , err:
parameterName = parameter.xpath( 'name/text()')[0]
errors.append( "%s/%s : %s" % (xmlPath , parameterName , err ) )
if errors:
for error in errors:
print >> sys.stderr , "ERROR ", error
sys.exit(1)
for dtName in self._dataTypeFactory.definedDataTypes.keys():
ancestors = self._dataTypeFactory.definedDataTypes[ dtName ].ancestors
mobyleClasses = dir( Mobyle.Classes )
localCustomClasses = dir( Local.CustomClasses )
if dtName in mobyleClasses:
self.pythonTypes.append( dtName )
elif dtName in localCustomClasses:
self.pythonTypes.append( dtName )
else:
self.xmlTypes.append( ( ancestors[0] , ancestors[1] ) )
self.biotypes = biotypes.keys()
self.biotypes.sort()
self.pythonTypes.sort()
self.xmlTypes.sort()
def mistakeDetector(self):
warnings = []
xmlCaseError = {}
for xmlType , pythonType in self.xmlTypes:
upXmlType = xmlType.upper()
if xmlCaseError.has_key( upXmlType ):
warnings.append( "found to similar xml datatype %s and %s" % ( xmlType , xmlCaseError[ upXmlType ] ))
else:
xmlCaseError[ upXmlType ] = xmlType
biotypeCaseError = {}
for biotype in self.biotypes:
upBiotype = biotype.upper()
if biotypeCaseError.has_key( upBiotype ):
warnings.append( "found to similar biotype %s and %s" % (biotype , biotypeCaseError[ upBiotype] ) )
else:
biotypeCaseError[ upBiotype ] = biotype
return warnings
def __str__(self):
repr = ''
comment = '# mobyle datatype repositotry %s #' % time.strftime( '%x %X' , time.localtime() )
pythonBody = '\n'.join( [ "%-25.25s %s " % ( pythonType , self.SEP ) for pythonType in self.pythonTypes ] )
repr = repr + "%(comment)s\n%(diese)s\n%(header)s\n%(diese)s\n%(body)s\n%(tail)s\n" % {"comment": comment ,
"diese" : '#'*26 ,
"header" : self.PYTHON_HEADER ,
"body" : pythonBody ,
"tail" : self.TAIL
}
xmlBody = '\n'.join( [ "%-30.30s %s %-15.15s %s " % ( xmlType , self.SEP , pythonType , self.SEP ) for xmlType , pythonType in self.xmlTypes ] )
repr = repr + "\n%(diese)s\n%(header)s\n%(diese)s\n%(body)s\n%(tail)s\n" % {"diese" : '#'*64 ,
"header" : self.XML_HEADER ,
"body" : xmlBody ,
"tail" : self.TAIL
}
biotypesBody = '\n'.join([ '%-15.15s %s' % ( biotype , self.SEP ) for biotype in self.biotypes ] )
repr = repr + "\n%(diese)s\n%(header)s\n%(diese)s\n%(body)s\n%(tail)s\n" % {"diese" : '#'*15 ,
"header" : self.BIOTYPE_HEADER ,
"body" : biotypesBody ,
"tail" : self.TAIL
}
return repr
def diff(self , repository ):
import difflib
sm = difflib.SequenceMatcher()
pythonDiff = ''
xmlDiff = ''
bioDiff = ''
#sm.set_seqs( self.pythonTypes , repository.pythonTypes )
sm.set_seqs( repository.pythonTypes , self.pythonTypes )
blocks = sm.get_opcodes()
for block in blocks:
if block[0] == 'equal' :
continue
elif block[0] == 'insert' :
pythonDiff += '\n'.join( [ '+ '+ str( Type ) for Type in self.pythonTypes[ block[3] : block[4] ] ] )
pythonDiff += '\n'
elif block[0] == 'delete':
pythonDiff += '\n'.join( [ '- '+ str( Type ) for Type in repository.pythonTypes[ block[1] : block[2] ] ])
pythonDiff += '\n'
if pythonDiff:
pythonDiff = "Mobyle python diff\n%s\n%s\n" % ( "="*20 , pythonDiff )
sm.set_seqs( repository.xmlTypes , self.xmlTypes )
blocks = sm.get_opcodes()
for block in blocks:
if block[0] == 'equal' :
continue
elif block[0] == 'insert' :
xmlDiff += '\n'.join( ["+ %-15.15s %s %-30.30s %s" % ( Type[0] , self.SEP , Type[1] , self.SEP ) for Type in self.xmlTypes[ block[3] : block[4] ] ] )
xmlDiff += '\n'
elif block[0] == 'delete':
xmlDiff += '\n'.join( ["- %-15.15s %s %-30.30s %s" % ( Type[0] , self.SEP , Type[1] , self.SEP ) for Type in repository.xmlTypes[ block[1] : block[2] ] ] )
xmlDiff += '\n'
if xmlDiff:
xmlDiff = "xml class diff\n%s\n%s\n" % ( "="*15 , xmlDiff)
sm.set_seqs( repository.biotypes , self.biotypes )
blocks = sm.get_opcodes()
for block in blocks:
if block[0] == 'equal' :
continue
elif block[0] == 'insert' :
bioDiff += '\n'.join( [ '+ '+ str( Type ) for Type in self.biotypes[ block[3] : block[4] ] ] )
pythonDiff += '\n'
elif block[0] == 'delete':
bioDiff += '\n'.join( [ '- '+ str( Type ) for Type in repository.biotypes[ block[1] : block[2] ] ])
bioDiff += '\n'
if bioDiff:
bioDiff = "biotypes diff\n%s\n%s\n" % ( "="*14 , bioDiff )
if pythonDiff or xmlDiff or bioDiff:
return "--- %s\n+++ %s\n\n %s%s%s" % ( repository.name , self.name , pythonDiff , xmlDiff , bioDiff )
class DataTypeReference( XmlDataTypeRepository ):
def parse(self , repositoryPath ):
self.name = repositoryPath
f = file( repositoryPath , 'r' )
header_found = False
tail_found = False
for line in f:
line = line.strip()
if line.startswith( self.COMMENT ):
continue
elif line == self.PYTHON_HEADER.strip():
header_found = True
self.pythonTypes = self._parsePythonType( f )
elif line == self.XML_HEADER.strip():
header_found = True
self.xmlTypes , self.descriptions = self._parseXmlType( f )
elif line == self.BIOTYPE_HEADER.strip():
header_found = True
self.biotypes = self._parseBiotype( f )
elif line.startswith( self.TAIL ):
tail_found = True
break
else:
if not header_found:
print >> sys.stderr , "ERROR bad file format for DataType Reference %s : No header found" % f.name
sys.exit(2)
def _parsePythonType(self , refFile ):
tail_found = False
pythonTypes = {}
for line in refFile:
if line.startswith( self.COMMENT ):
continue
elif line.startswith( self.TAIL ):
tail_found = True
break
else:
fields = line.split( self.SEP )
fields = [ item.strip() for item in fields ]
if fields == ['']: # skip blank line
continue
else:
pythonType = fields[0]
if pythonTypes.has_key( pythonType ):
print >> sys.stderr , "the reference %s has 2 %s entries" % ( ref.name , pythonType )
sys.exit(2)
else:
pythonTypes[ pythonType ] = None
if not tail_found:
print >> sys.stderr , "ERROR bad file format for DataType Reference %s : Unexpected end of file " % refFile.name
sys.exit(2)
pythonTypes = pythonTypes.keys()
pythonTypes.sort()
return pythonTypes
def _parseXmlType(self, refFile ):
tail_found = False
descriptions = {}
for line in refFile:
if line.startswith( self.COMMENT ):
continue
elif line.startswith( self.TAIL ):
tail_found = True
break
else:
fields = line.split( self.SEP )
fields = [ item.strip() for item in fields ]
if fields == ['']: # skip blank line
continue
else:
xmlKlass , pythonKlass , description = fields
dataType = self._dataTypeFactory.newDataType( pythonKlass , xmlName = xmlKlass )
descriptions[ xmlKlass ] = description
if not tail_found:
print >> sys.stderr , "ERROR bad file format for DataType Reference %s : Unexpected end of file " % refFile.name
sys.exit(2)
xmlTypes = []
for dtName in self._dataTypeFactory.definedDataTypes.keys():
ancestors = self._dataTypeFactory.definedDataTypes[ dtName ].ancestors
mobyleClasses = dir( Mobyle.Classes )
localCustomClasses = dir( Local.CustomClasses )
if dtName in mobyleClasses:
pass
elif dtName in localCustomClasses:
pass
else:
xmlTypes.append( ( ancestors[0] , ancestors[1] ) )
xmlTypes.sort()
return xmlTypes , descriptions
def _parseBiotype(self, refFile ):
biotypes = {}
tail_found = False
for line in refFile:
if line.startswith( self.COMMENT ):
continue
elif line.startswith( self.TAIL ):
tail_found = True
break
else:
fields = line.split( self.SEP )
fields = [ item.strip() for item in fields ]
if fields == ['']: # skip blank line
continue
else:
biotype = fields[0]
if biotypes.has_key( biotype ):
print >> sys.stderr , "the reference %s has 2 %s entries" % ( ref.name , biotype )
sys.exit(2)
else:
biotypes[ biotype ] = None
if not tail_found:
print >> sys.stderr , "ERROR bad file format for DataType Reference %s : Unexpected end of file " % refFile.name
sys.exit(2)
biotypes = biotypes.keys()
biotypes.sort()
return biotypes
if __name__ == '__main__':
from getopt import gnu_getopt , GetoptError
def usage():
print """
generate on the standart output a mobyle types repository ( python , xml and biotype ).
if -ref is specified do a diff between the ref and the xml
usage: mobtypes < --ref path to a datatype repository > < xmlpaths to analyze >
if no paths are provide as arguments, use the xmlpaths installed (local and imported) for this portal.
options:
-h or --help ... Print this message and exit.
-r or --ref ... the path to a datatype repository.
"""
try:
opts, xmlPaths = gnu_getopt( sys.argv[1:], "hr:", [ "help" , "ref=" ] )
refPath = None
for option , value in opts:
if option in ( "-h","--help" ):
usage()
sys.exit( 0 )
elif option in ( "-r" , "--ref=" ):
refPath = value
if not os.path.exists( value ):
print >> sys.stderr , "ERROR : %s %s the No such file" % ( option , value )
sys.exit(1)
if not xmlPaths:
from Mobyle.Registry import registry
xmlPaths = [ p.path for p in registry.programs ]
except GetoptError:
print usage()
sys.exit( 1 )
xmlRepository = XmlDataTypeRepository( xmlPaths )
if refPath:
ref = DataTypeReference( refPath )
print ref.diff( xmlRepository )
else:
print xmlRepository
w = xmlRepository.mistakeDetector()
if w:
print >> sys.stderr , " -- WARNINGS -- "
print >> sys.stderr , '\n'.join( w )
|