LexManos

Merger

Mar 4th, 2012
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.95 KB | None | 0 0
  1. import sys
  2. import csv
  3. import logging
  4. import os
  5. import shutil
  6. import re
  7. from optparse import OptionParser
  8.  
  9. regexps = {
  10.     'methods': re.compile(r'(?P<type>func)_(?P<id>[0-9]+)_[a-zA-Z_]+'),
  11.     'fields':  re.compile(r'(?P<type>field)_(?P<id>[0-9]+)_[a-zA-Z_]+'),
  12.     'params':  re.compile(r'(?P<type>p)_(?P<id>[\w]+)_(?P<index>\d+)_'),
  13.     'ctrs':    re.compile(r'(?P<type>i)(?P<id>[0-9]+)'),
  14. }
  15. csv.register_dialect('csvstyle', delimiter=',', quoting=csv.QUOTE_MINIMAL)
  16. fCsv = []
  17. mCsv = []
  18. cCsv = []
  19.  
  20. def main():
  21.     parser = OptionParser(version='Blah')
  22.     parser.add_option('-p', '--path', dest='path', help='The Method file')
  23.     options, _ = parser.parse_args()
  24.     load_mapings('client_server.csv')
  25.     merge('fields.csv.bak', 'fields.csv', fCsv, regexps['fields'])
  26.     merge('methods.csv.bak', 'methods.csv', mCsv, regexps['methods'])
  27.     fixexec('client.exc.bak', 'client.exc', '0')
  28.     fixexec('server.exc.bak', 'server.exc', '1')
  29.     fixsrg('client.srg.bak', 'client.srg', '0')
  30.     fixsrg('server.srg.bak', 'server.srg', '1')
  31.     geninit('client.exc.bak', 'server.exc.bak', 'cs_constructors.csv')
  32.    
  33. def geninit(clientFile, serverFile, outFile):
  34.     with open('./conf/' + clientFile) as f:
  35.         client = f.readlines()
  36.     with open('./conf/' + serverFile) as f:
  37.         server = f.readlines()
  38.     ctrs = {'client': {}, 'server': {}}
  39.    
  40.     reg = re.compile(r'(?P<pre>[^\.]+)\.<init>(?P<post>[^\=]+)=[^\|]*\|(?P<args>.*)')
  41.     for line in client:
  42.         match = reg.match(line)
  43.         if match:
  44.             matchA = regexps['params'].match(match.group('args'))
  45.             if matchA:
  46.                 ctrs['client'][match.group('pre') + match.group('post')] = matchA.group('id')
  47.                
  48.     for line in server:
  49.         match = reg.match(line)
  50.         if match:
  51.             matchA = regexps['params'].match(match.group('args'))
  52.             if matchA:
  53.                 ctrs['server'][match.group('pre') + match.group('post')] = matchA.group('id')
  54.                
  55.     mapings = []
  56.     for key in ctrs['client']:
  57.         if key in ctrs['server']:
  58.             mapings.append({'client': ctrs['client'][key], 'server': ctrs['server'][key]})
  59.            
  60.     saveCSV(outFile, sorted(mapings, key=lambda ent: ent['client']), ['client', 'server'])
  61.    
  62. def fixsrg(baseFile, outFile, side):
  63.     with open('./conf/' + baseFile) as f:
  64.         base = f.read()
  65.    
  66.     def fixsrg_fields(match):
  67.         map = getMap(fCsv, side, match.group())
  68.         if map:
  69.             if side == '1':
  70.                 match = regexps['fields'].match(map)
  71.             return '%s_%s_b' % (match.group('type'), match.group('id'))
  72.         else:
  73.             return '%s_%s_%s' % (match.group('type'), match.group('id'), 'c' if side == '0' else 's')
  74.            
  75.     def fixsrg_methods(match):
  76.         map = getMap(mCsv, side, match.group())
  77.         if map:
  78.             if side == '1':
  79.                 match = regexps['methods'].match(map)
  80.             return '%s_%s_b' % (match.group('type'), match.group('id'))
  81.         else:
  82.             return '%s_%s_%s' % (match.group('type'), match.group('id'), 'c' if side == '0' else 's')
  83.            
  84.     base = regexps['fields'].sub(fixsrg_fields, base)
  85.     base = regexps['methods'].sub(fixsrg_methods, base)
  86.    
  87.     with open('./conf/' + outFile, 'w') as f:
  88.         f.write(base)
  89.        
  90. def fixexec(baseFile, outFile, side):
  91.     with open('./conf/' + baseFile) as f:
  92.         base = f.readlines()
  93.     output = []
  94.    
  95.     reg = re.compile(r'(?P<pre>[^\.]+)\.(?P<func>[^(]+)(?P<post>[^\|]+)\|(?P<args>.*)')
  96.    
  97.     for line in base:
  98.         if line[0] == '#':
  99.             output.append(line)
  100.             continue
  101.            
  102.         match = reg.match(line)
  103.         if match:
  104.             func = match.group('func')
  105.             matchF = regexps['methods'].match(func)
  106.            
  107.             if matchF:            
  108.                 map = getMap(mCsv, side, func)
  109.                 if map:
  110.                     if side == '1':
  111.                         matchF = regexps['methods'].match(map)
  112.                     func = '%s_%s_b' % (matchF.group('type'), matchF.group('id'))
  113.                 else:
  114.                     func = '%s_%s_%s' % (matchF.group('type'), matchF.group('id'), 'c' if side == '0' else 's')
  115.                    
  116.                 def exec_replace_args(match):
  117.                     if map:
  118.                         return 'p_%s_%s_b_' % (matchF.group('id'), match.group('index'))
  119.                     else:
  120.                         return 'p_%s_%s_%s_' % (matchF.group('id'), match.group('index'), 'c' if side == '0' else 's')
  121.                    
  122.                 args = regexps['params'].sub(exec_replace_args, match.group('args'))
  123.                
  124.                 output.append('%s.%s%s|%s' % (match.group('pre'), func, match.group('post'), args))
  125.            
  126.             elif func == '<init>':
  127.                 args = match.group('args')
  128.                 matchA = regexps['params'].match(args)
  129.                 if matchA:
  130.                     map = getMap(cCsv, side, matchA.group('id'))
  131.                    
  132.                     def exec_replace_args_init(match):
  133.                         if map:
  134.                             return 'p_%s_%s_b_' % (map if side == '1' else match.group('id'), match.group('index'))
  135.                         else:
  136.                             return 'p_%s_%s_%s_' % (match.group('id'), match.group('index'), 'c' if side == '0' else 's')
  137.                            
  138.                     args = regexps['params'].sub(exec_replace_args_init, args)
  139.                 output.append('%s.%s%s|%s' % (match.group('pre'), func, match.group('post'), args))
  140.             else:
  141.                 output.append(line)
  142.                
  143.         else:
  144.             print line
  145.        
  146.    
  147.     with open('./conf/' + outFile, 'w') as f:
  148.         for line in output:
  149.             f.write(line)
  150.            
  151. def merge(baseFile, outFile, mapings, reg):
  152.     bCsv = readCSV(baseFile)
  153.     oCsv = []
  154.     matched   = {'0': 0, '1': 0}
  155.     unmatched = {'0': 0, '1': 0}
  156.     added     = {'0': 0, '1': 0}
  157.     mapInfo   = {'0':{}, '1':{}}
  158.    
  159.     for row in bCsv['values']:
  160.         if row['side'] == '1':
  161.             mapInfo['0'][row['searge']] = row
  162.         else:
  163.             mapInfo['1'][row['searge']] = row
  164.            
  165.     for row in bCsv['values']:
  166.         map = getMap(mapings, row['side'], row['searge'])
  167.         match = reg.match(row['searge'])
  168.         if map:
  169.             matched[row['side']] += 1
  170.             match = reg.match(row['searge'])
  171.             if row['side'] == '1':
  172.                 match = reg.match(map)
  173.             row['searge'] = "%s_%s_b" % (match.group('type'), match.group('id'))
  174.             try:
  175.                 info = mapInfo[row['side']][map]
  176.                 if row['side'] == '1':
  177.                     row['name'] = info['name']
  178.                     row['desc'] = info['desc']
  179.             except KeyError:
  180.                 newRow = {'searge': row['searge'], 'name': row['name'], 'side': '1' if row['side'] == '0' else '0', 'desc': row['desc']}
  181.                 added[row['side']] += 1
  182.                 oCsv.append(newRow)
  183.         else:
  184.             row['searge'] = '%s_%s_%s' % (match.group('type'), match.group('id'), 'c' if row['side'] == '0' else 's')
  185.             unmatched[row['side']] += 1
  186.         oCsv.append(row)
  187.        
  188.     print "%s Matched: %d Unique: %d/%d Added: %d/%d" % (outFile, matched['0'], unmatched['0'], unmatched['1'], added['0'], added['1'])
  189.    
  190.     saveCSV(outFile, oCsv, bCsv['desc'])
  191.        
  192. def getMap(data, side, value):
  193.     a = {'0': 'client', '1': 'server'}
  194.     b = {'1': 'client', '0': 'server'}
  195.     for row in data:
  196.         if row[a[side]] == value:
  197.             return row[b[side]]
  198.  
  199. def load_mapings(baseFile):
  200.     global fCsv, mCsv, cCsv
  201.     bCsv = readCSV(baseFile)
  202.     fCsv = []
  203.     mCsv = []
  204.     cCsv = []
  205.    
  206.     for row in bCsv['values']:
  207.         for group in ['methods', 'fields', 'ctrs']:
  208.             matchC = regexps[group].match(row['client'])
  209.             matchS = regexps[group].match(row['server'])
  210.             if matchC and matchS:
  211.                 if matchC.group('type') == 'func':  mCsv.append(row)                    
  212.                 if matchC.group('type') == 'field': fCsv.append(row)                    
  213.                 if matchC.group('type') == 'i':     cCsv.append(row)
  214.                
  215.     saveCSV('cs_fields.csv',  sorted(fCsv, key=lambda ent: ent['client']), bCsv['desc'])
  216.     saveCSV('cs_methods.csv', sorted(mCsv, key=lambda ent: ent['client']), bCsv['desc'])
  217.     saveCSV('cs_ctrs.csv', sorted(cCsv, key=lambda ent: ent['client']), bCsv['desc'])
  218.     print 'Loaded Mapings: %d/%d/%d' % (len(fCsv), len(mCsv), len(cCsv))
  219.        
  220. def saveCSV(filename, data, header):
  221.     with open('./conf/' + filename, 'wb') as f:
  222.         writer = csv.DictWriter(f, header, dialect='csvstyle')
  223.         writer.writerow(dict(zip(writer.fieldnames, writer.fieldnames)))
  224.         writer.writerows(data)
  225.        
  226. def readCSV(filename):
  227.     tCsv = csv.DictReader(open('./conf/' + filename, 'rb'))
  228.     bCsv = {'desc': tCsv.fieldnames, 'values': []}
  229.     for row in tCsv:
  230.         bCsv['values'].append(row)
  231.     return bCsv
  232.                
  233. if __name__ == '__main__':
  234.     main()
Advertisement
Add Comment
Please, Sign In to add comment