<s> import re <EOL> import sys <EOL> from os . path import abspath <EOL> import yaml <EOL> PREABLE = '''<STR_LIT>''' <EOL> KEYS = [ <EOL> ( '<STR_LIT:name>' , True , str , '''<STR_LIT>''' ) , <EOL> ( '<STR_LIT:version>' , True , str , '''<STR_LIT>''' ) , <EOL> ( '<STR_LIT>' , False , list , '''<STR_LIT>''' ) , <EOL> ( '<STR_LIT>' , False , ( list , str ) , '''<STR_LIT>''' ) , <EOL> ( '<STR_LIT>' , False , list , '''<STR_LIT>''' ) , <EOL> ( '<STR_LIT>' , False , ( list , str ) , '''<STR_LIT>''' ) , <EOL> ( '<STR_LIT>' , False , bool , '''<STR_LIT>''' ) , <EOL> ( '<STR_LIT>' , False , list , '''<STR_LIT>''' ) , <EOL> ( '<STR_LIT>' , False , str , '''<STR_LIT>''' ) , <EOL> ( '<STR_LIT>' , False , str , '''<STR_LIT>''' ) , <EOL> ( '<STR_LIT>' , False , bool , '''<STR_LIT>''' ) , <EOL> ( '<STR_LIT>' , False , str , '''<STR_LIT>''' ) , <EOL> ( '<STR_LIT>' , False , str , '''<STR_LIT>''' ) , <EOL> ( '<STR_LIT>' , False , str , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , False , str , '''<STR_LIT>''' ) , <EOL> ( '<STR_LIT>' , False , str , '''<STR_LIT>''' ) , <EOL> ( '<STR_LIT>' , False , str , '''<STR_LIT>''' ) , <EOL> ( '<STR_LIT>' , False , str , '''<STR_LIT>''' ) , <EOL> ] <EOL> def ns_platform ( platform ) : <EOL> p = platform <EOL> return dict ( <EOL> linux = p . startswith ( '<STR_LIT>' ) , <EOL> linux32 = bool ( p == '<STR_LIT>' or p == '<STR_LIT>' ) , <EOL> linux64 = bool ( p == '<STR_LIT>' or p == '<STR_LIT>' ) , <EOL> armv7l = bool ( p == '<STR_LIT>' ) , <EOL> ppc64le = bool ( p == '<STR_LIT>' ) , <EOL> x86 = p . endswith ( ( '<STR_LIT>' , '<STR_LIT>' ) ) , <EOL> x86_64 = p . endswith ( '<STR_LIT>' ) , <EOL> osx = p . startswith ( '<STR_LIT>' ) , <EOL> unix = p . startswith ( ( '<STR_LIT>' , '<STR_LIT>' ) ) , <EOL> win = p . startswith ( '<STR_LIT>' ) , <EOL> win32 = bool ( p == '<STR_LIT>' ) , <EOL> win64 = bool ( p == '<STR_LIT>' ) , <EOL> ) <EOL> sel_pat = re . compile ( r'<STR_LIT>' ) <EOL> def select_lines ( data , namespace ) : <EOL> lines = [ ] <EOL> for line in data . splitlines ( ) : <EOL> line = line . rstrip ( ) <EOL> m = sel_pat . match ( line ) <EOL> if m : <EOL> cond = m . group ( <NUM_LIT:2> ) <EOL> if eval ( cond , namespace , { } ) : <EOL> lines . append ( m . group ( <NUM_LIT:1> ) ) <EOL> continue <EOL> lines . append ( line ) <EOL> return '<STR_LIT:\n>' . join ( lines ) + '<STR_LIT:\n>' <EOL> def parse ( path , platform ) : <EOL> try : <EOL> with open ( path ) as fi : <EOL> data = fi . read ( ) <EOL> except IOError : <EOL> sys . exit ( "<STR_LIT>" % path ) <EOL> res = yaml . load ( select_lines ( data , ns_platform ( platform ) ) ) <EOL> try : <EOL> res [ '<STR_LIT:version>' ] = str ( res [ '<STR_LIT:version>' ] ) <EOL> except KeyError : <EOL> pass <EOL> for key in list ( res ) : <EOL> if res [ key ] is None : <EOL> del res [ key ] <EOL> return res <EOL> def verify ( info ) : <EOL> types_key = { } <EOL> required_keys = set ( ) <EOL> for key , required , types , unused_descr in KEYS : <EOL> types_key [ key ] = types <EOL> if required : <EOL> required_keys . add ( key ) <EOL> for key in info : <EOL> if key not in types_key : <EOL> sys . exit ( "<STR_LIT>" % key ) <EOL> elt = info [ key ] <EOL> types = types_key [ key ] <EOL> if not isinstance ( elt , types ) : <EOL> sys . exit ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( key , type ( elt ) , types ) ) <EOL> for key in required_keys : <EOL> if key not in info : <EOL> sys . exit ( "<STR_LIT>" % <EOL> key ) <EOL> def generate_doc ( ) : <EOL> path = abspath ( '<STR_LIT>' % __file__ ) <EOL> print ( '<STR_LIT>' % path ) <EOL> with open ( path , '<STR_LIT:w>' ) as fo : <EOL> fo . write ( PREABLE ) <EOL> for key , required , unused_types , descr in KEYS : <EOL> descr = descr . strip ( ) <EOL> if descr == '<STR_LIT>' : <EOL> continue <EOL> fo . write ( """<STR_LIT>""" % ( key , <EOL> '<STR_LIT>' if required else '<STR_LIT>' , <EOL> descr ) ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> generate_doc ( ) </s>
