Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import csv
- import logging
- import os
- import shutil
- import re
- from optparse import OptionParser
- regexps = {
- 'methods': re.compile(r'(?P<type>func)_(?P<id>[0-9]+)_[a-zA-Z_]+'),
- 'fields': re.compile(r'(?P<type>field)_(?P<id>[0-9]+)_[a-zA-Z_]+'),
- 'params': re.compile(r'(?P<type>p)_(?P<id>[\w]+)_(?P<index>\d+)_'),
- 'ctrs': re.compile(r'(?P<type>i)(?P<id>[0-9]+)'),
- }
- csv.register_dialect('csvstyle', delimiter=',', quoting=csv.QUOTE_MINIMAL)
- fCsv = []
- mCsv = []
- cCsv = []
- def main():
- parser = OptionParser(version='Blah')
- parser.add_option('-p', '--path', dest='path', help='The Method file')
- options, _ = parser.parse_args()
- load_mapings('client_server.csv')
- merge('fields.csv.bak', 'fields.csv', fCsv, regexps['fields'])
- merge('methods.csv.bak', 'methods.csv', mCsv, regexps['methods'])
- fixexec('client.exc.bak', 'client.exc', '0')
- fixexec('server.exc.bak', 'server.exc', '1')
- fixsrg('client.srg.bak', 'client.srg', '0')
- fixsrg('server.srg.bak', 'server.srg', '1')
- geninit('client.exc.bak', 'server.exc.bak', 'cs_constructors.csv')
- def geninit(clientFile, serverFile, outFile):
- with open('./conf/' + clientFile) as f:
- client = f.readlines()
- with open('./conf/' + serverFile) as f:
- server = f.readlines()
- ctrs = {'client': {}, 'server': {}}
- reg = re.compile(r'(?P<pre>[^\.]+)\.<init>(?P<post>[^\=]+)=[^\|]*\|(?P<args>.*)')
- for line in client:
- match = reg.match(line)
- if match:
- matchA = regexps['params'].match(match.group('args'))
- if matchA:
- ctrs['client'][match.group('pre') + match.group('post')] = matchA.group('id')
- for line in server:
- match = reg.match(line)
- if match:
- matchA = regexps['params'].match(match.group('args'))
- if matchA:
- ctrs['server'][match.group('pre') + match.group('post')] = matchA.group('id')
- mapings = []
- for key in ctrs['client']:
- if key in ctrs['server']:
- mapings.append({'client': ctrs['client'][key], 'server': ctrs['server'][key]})
- saveCSV(outFile, sorted(mapings, key=lambda ent: ent['client']), ['client', 'server'])
- def fixsrg(baseFile, outFile, side):
- with open('./conf/' + baseFile) as f:
- base = f.read()
- def fixsrg_fields(match):
- map = getMap(fCsv, side, match.group())
- if map:
- if side == '1':
- match = regexps['fields'].match(map)
- return '%s_%s_b' % (match.group('type'), match.group('id'))
- else:
- return '%s_%s_%s' % (match.group('type'), match.group('id'), 'c' if side == '0' else 's')
- def fixsrg_methods(match):
- map = getMap(mCsv, side, match.group())
- if map:
- if side == '1':
- match = regexps['methods'].match(map)
- return '%s_%s_b' % (match.group('type'), match.group('id'))
- else:
- return '%s_%s_%s' % (match.group('type'), match.group('id'), 'c' if side == '0' else 's')
- base = regexps['fields'].sub(fixsrg_fields, base)
- base = regexps['methods'].sub(fixsrg_methods, base)
- with open('./conf/' + outFile, 'w') as f:
- f.write(base)
- def fixexec(baseFile, outFile, side):
- with open('./conf/' + baseFile) as f:
- base = f.readlines()
- output = []
- reg = re.compile(r'(?P<pre>[^\.]+)\.(?P<func>[^(]+)(?P<post>[^\|]+)\|(?P<args>.*)')
- for line in base:
- if line[0] == '#':
- output.append(line)
- continue
- match = reg.match(line)
- if match:
- func = match.group('func')
- matchF = regexps['methods'].match(func)
- if matchF:
- map = getMap(mCsv, side, func)
- if map:
- if side == '1':
- matchF = regexps['methods'].match(map)
- func = '%s_%s_b' % (matchF.group('type'), matchF.group('id'))
- else:
- func = '%s_%s_%s' % (matchF.group('type'), matchF.group('id'), 'c' if side == '0' else 's')
- def exec_replace_args(match):
- if map:
- return 'p_%s_%s_b_' % (matchF.group('id'), match.group('index'))
- else:
- return 'p_%s_%s_%s_' % (matchF.group('id'), match.group('index'), 'c' if side == '0' else 's')
- args = regexps['params'].sub(exec_replace_args, match.group('args'))
- output.append('%s.%s%s|%s' % (match.group('pre'), func, match.group('post'), args))
- elif func == '<init>':
- args = match.group('args')
- matchA = regexps['params'].match(args)
- if matchA:
- map = getMap(cCsv, side, matchA.group('id'))
- def exec_replace_args_init(match):
- if map:
- return 'p_%s_%s_b_' % (map if side == '1' else match.group('id'), match.group('index'))
- else:
- return 'p_%s_%s_%s_' % (match.group('id'), match.group('index'), 'c' if side == '0' else 's')
- args = regexps['params'].sub(exec_replace_args_init, args)
- output.append('%s.%s%s|%s' % (match.group('pre'), func, match.group('post'), args))
- else:
- output.append(line)
- else:
- print line
- with open('./conf/' + outFile, 'w') as f:
- for line in output:
- f.write(line)
- def merge(baseFile, outFile, mapings, reg):
- bCsv = readCSV(baseFile)
- oCsv = []
- matched = {'0': 0, '1': 0}
- unmatched = {'0': 0, '1': 0}
- added = {'0': 0, '1': 0}
- mapInfo = {'0':{}, '1':{}}
- for row in bCsv['values']:
- if row['side'] == '1':
- mapInfo['0'][row['searge']] = row
- else:
- mapInfo['1'][row['searge']] = row
- for row in bCsv['values']:
- map = getMap(mapings, row['side'], row['searge'])
- match = reg.match(row['searge'])
- if map:
- matched[row['side']] += 1
- match = reg.match(row['searge'])
- if row['side'] == '1':
- match = reg.match(map)
- row['searge'] = "%s_%s_b" % (match.group('type'), match.group('id'))
- try:
- info = mapInfo[row['side']][map]
- if row['side'] == '1':
- row['name'] = info['name']
- row['desc'] = info['desc']
- except KeyError:
- newRow = {'searge': row['searge'], 'name': row['name'], 'side': '1' if row['side'] == '0' else '0', 'desc': row['desc']}
- added[row['side']] += 1
- oCsv.append(newRow)
- else:
- row['searge'] = '%s_%s_%s' % (match.group('type'), match.group('id'), 'c' if row['side'] == '0' else 's')
- unmatched[row['side']] += 1
- oCsv.append(row)
- print "%s Matched: %d Unique: %d/%d Added: %d/%d" % (outFile, matched['0'], unmatched['0'], unmatched['1'], added['0'], added['1'])
- saveCSV(outFile, oCsv, bCsv['desc'])
- def getMap(data, side, value):
- a = {'0': 'client', '1': 'server'}
- b = {'1': 'client', '0': 'server'}
- for row in data:
- if row[a[side]] == value:
- return row[b[side]]
- def load_mapings(baseFile):
- global fCsv, mCsv, cCsv
- bCsv = readCSV(baseFile)
- fCsv = []
- mCsv = []
- cCsv = []
- for row in bCsv['values']:
- for group in ['methods', 'fields', 'ctrs']:
- matchC = regexps[group].match(row['client'])
- matchS = regexps[group].match(row['server'])
- if matchC and matchS:
- if matchC.group('type') == 'func': mCsv.append(row)
- if matchC.group('type') == 'field': fCsv.append(row)
- if matchC.group('type') == 'i': cCsv.append(row)
- saveCSV('cs_fields.csv', sorted(fCsv, key=lambda ent: ent['client']), bCsv['desc'])
- saveCSV('cs_methods.csv', sorted(mCsv, key=lambda ent: ent['client']), bCsv['desc'])
- saveCSV('cs_ctrs.csv', sorted(cCsv, key=lambda ent: ent['client']), bCsv['desc'])
- print 'Loaded Mapings: %d/%d/%d' % (len(fCsv), len(mCsv), len(cCsv))
- def saveCSV(filename, data, header):
- with open('./conf/' + filename, 'wb') as f:
- writer = csv.DictWriter(f, header, dialect='csvstyle')
- writer.writerow(dict(zip(writer.fieldnames, writer.fieldnames)))
- writer.writerows(data)
- def readCSV(filename):
- tCsv = csv.DictReader(open('./conf/' + filename, 'rb'))
- bCsv = {'desc': tCsv.fieldnames, 'values': []}
- for row in tCsv:
- bCsv['values'].append(row)
- return bCsv
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment