def convert_file(mac_file, outfile):
 re_compiles = ['<BAD COMPILE>']
 parse_depth = 27
 string_idx = 5
 for i in range(1, parse_depth):
  s = (('^(0x[0-9a-fA-F]+):\\s{' + ('%d' % string_idx)) + '}(\\w+)\\s')
  re_compiles.append(re.compile(s))
  string_idx = (string_idx + 4)
 at_re = re.compile('^\\s+(\\w+)\\((.+)')
 level = 0
 dontbreak = 0
 for line in mac_file.readlines():
  if (len(line) < 2):
   outfile.write('\n')
   level = 0
   continue
  if (line.find('-------------') != (-1)):
   level = 0
   continue
  if (line.find('File:') != (-1)):
   level = 0
   continue
  if (line.find('.debug_info') != (-1)):
   level = 0
   continue
  if (line.find('Compile Unit:') != (-1)):
   level = 0
   continue
  if (line.find('TAG_compile_unit') != (-1)):
   outfile.write('<1><999999999999999><TAG_compile_unit> ')
   level = 1
   continue
  if (level == 0):
   line_wrote = False
   for check_idx in range(1, parse_depth):
    re_check = re_compiles[check_idx]
    matchobj = re_check.match(line)
    if matchobj:
     (id, name) = matchobj.groups()
     id = ('%d' % int(id, 16))
     level = 1
     write_line(outfile, check_idx, id, name)
     line_wrote = True
     break
   if (not line_wrote):
    print ('State machine broken! level 0! %s' % line)
    sys.exit(1)
  elif (level == 1):
   m = re_compiles[2].match(line)
   a = at_re.match(line)
   if m:
    (id, name) = m.groups()
    id = ('%d' % int(id, 16))
    level = 2
    outfile.write(('<%s><%s><%s> ' % (level, id, name)))
   elif a:
    (name, val) = a.groups()
    val = val[:(-2)]
    if (val[0] == ' '):
     val = val[1:]
    if (name == 'AT_name'):
     val = val[1:(-1)]
    if (name == 'AT_const_value'):
     ents = val.split()
     if (len(ents) > 1):
      ents = ents[1:]
      try:
       val = ('%d' % int(('0x' + ''.join([x for x in ents])), 16))
      except:
       val = 'Bad const list val'
     else:
      try:
       val = ('%d' % int(val, 16))
      except:
       val = 'Bad const value'
    if (name in ['AT_byte_size', 'AT_bit_offset', 'AT_bit_size', 'AT_upper_bound']):
     val = ('%d' % int(val, 16))
    if (name == 'AT_data_member_location'):
     val = val[1:]
    if (name == 'AT_type'):
     val = val.split()[0]
     val = val[1:(-1)]
     val = ('<%d>' % int(val, 16))
    outfile.write(('%s<%s> ' % (name, val)))
    outfile.flush()

def convert_file ( mac_file , outfile ) : re_compiles = [ '<BAD COMPILE>' ] parse_depth = 27 string_idx = 5 for i in range ( 1 , parse_depth ) : s = ( ( '^(0x[0-9a-fA-F]+):\\s{' + ( '%d' % string_idx ) ) + '}(\\w+)\\s' ) re_compiles . append ( re . compile ( s ) ) string_idx = ( string_idx + 4 ) at_re = re . compile ( '^\\s+(\\w+)\\((.+)' ) level = 0 dontbreak = 0 for line in mac_file . readlines ( ) : if ( len ( line ) < 2 ) : outfile . write ( '\n' ) level = 0 continue if ( line . find ( '-------------' ) != ( - 1 ) ) : level = 0 continue if ( line . find ( 'File:' ) != ( - 1 ) ) : level = 0 continue if ( line . find ( '.debug_info' ) != ( - 1 ) ) : level = 0 continue if ( line . find ( 'Compile Unit:' ) != ( - 1 ) ) : level = 0 continue if ( line . find ( 'TAG_compile_unit' ) != ( - 1 ) ) : outfile . write ( '<1><999999999999999><TAG_compile_unit> ' ) level = 1 continue if ( level == 0 ) : line_wrote = False for check_idx in range ( 1 , parse_depth ) : re_check = re_compiles [ check_idx ] matchobj = re_check . match ( line ) if matchobj : ( id , name ) = matchobj . groups ( ) id = ( '%d' % int ( id , 16 ) ) level = 1 write_line ( outfile , check_idx , id , name ) line_wrote = True break if ( not line_wrote ) : print ( 'State machine broken! level 0! %s' % line ) sys . exit ( 1 ) elif ( level == 1 ) : m = re_compiles [ 2 ] . match ( line ) a = at_re . match ( line ) if m : ( id , name ) = m . groups ( ) id = ( '%d' % int ( id , 16 ) ) level = 2 outfile . write ( ( '<%s><%s><%s> ' % ( level , id , name ) ) ) elif a : ( name , val ) = a . groups ( ) val = val [ : ( - 2 ) ] if ( val [ 0 ] == ' ' ) : val = val [ 1 : ] if ( name == 'AT_name' ) : val = val [ 1 : ( - 1 ) ] if ( name == 'AT_const_value' ) : ents = val . split ( ) if ( len ( ents ) > 1 ) : ents = ents [ 1 : ] try : val = ( '%d' % int ( ( '0x' + '' . join ( [ x for x in ents ] ) ) , 16 ) ) except : val = 'Bad const list val' else : try : val = ( '%d' % int ( val , 16 ) ) except : val = 'Bad const value' if ( name in [ 'AT_byte_size' , 'AT_bit_offset' , 'AT_bit_size' , 'AT_upper_bound' ] ) : val = ( '%d' % int ( val , 16 ) ) if ( name == 'AT_data_member_location' ) : val = val [ 1 : ] if ( name == 'AT_type' ) : val = val . split ( ) [ 0 ] val = val [ 1 : ( - 1 ) ] val = ( '<%d>' % int ( val , 16 ) ) outfile . write ( ( '%s<%s> ' % ( name , val ) ) ) outfile . flush ( )
function for converting from an imageset batch cpickle file into a flat binary with a choice of keys .