Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/bin/env python
# reorder_args.py for Python 2.x
# written by Chikuzen
import sys
import os
def usage():
print "usage : reorder_args.py <func name> <num of args> <new order> <filename>\n"
print 'example: reorder_args.py "harpderp()" 5 "0,3,2,1,4" "hogehoge.c"\n'
print " before : herpderp( foo, bar, fizz, buzz, bleh )\n"
print " after : herpderp( foo, buzz, fizz, bar, bleh )\n"
sys.exit()
argvs = sys.argv
if len(argvs) != 5:
usage()
funcname = argvs[1].rstrip(')')
num = int(argvs[2])
order = [int(i) for i in argvs[3].split(',') if 0 <= int(i) < num]
if num != len(order):
print "error: '%s' and '%s' didn't suit.\n\n" % (argvs[2], argvs[3])
usage()
after = "%s_tmp" % argvs[4]
file1 = open(argvs[4], 'r')
file2 = open(after, 'w')
count = 0
for line in file1.readlines():
count += 1
try:
if line.find(funcname) != -1:
tmp1 = line.split(funcname)
for i in xrange(len(tmp1) - 1):
tmp2 = tmp1[i + 1].split(')')
tmp3 = [j.strip() for j in tmp2[0].split(',')]
tmp2[0] = ""
for k in xrange(num):
tmp2[0] += " %s," % tmp3[order[k]]
tmp2[0].rstrip(',')
tmp1[i + 1] = ' )'.join(tmp2)
line = funcname.join(tmp1)
except:
print "warning: catch exception line %i in %s\n" % (count, argvs[4])
file2.write(line)
file1.close()
file2.close()
os.remove(argvs[4])
os.rename(after, argvs[4])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment