Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 5th, 2012  |  syntax: None  |  size: 0.84 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #! /usr/bin/env python
  2.  
  3. """
  4. Usage: cat [infile] | python deanon.py > [outfile]
  5.  
  6. Anonymous functions will be renamed to anon_[lineno]_*
  7. where * is a number that gets incremented for each functionon the line
  8. """
  9.  
  10. from sys import stdin, stdout, stderr, argv
  11. import re
  12.  
  13. if len(argv) > 1:
  14.   lines = open(argv[1]).readlines()
  15.   out = open(argv[1], 'w')
  16. else:
  17.   lines = stdin.readlines()
  18.   out = stdout
  19.  
  20. x = {}
  21. x["anon_count"] = 0
  22.  
  23. def make_repl_func(lineno):
  24.   x["anon_line_count"] = 0
  25.   def create_id(match):
  26.     unique_id = str(lineno + 1) + "_" + str(x["anon_line_count"])
  27.     x["anon_line_count"] += 1
  28.     x["anon_count"] += 1
  29.     return "function anon_%s(" % unique_id
  30.   return create_id
  31.  
  32. for lineno, line in enumerate(lines):
  33.   out.write(re.sub("function *\(", make_repl_func(lineno), line))
  34.  
  35. stderr.write("De-anonymized %d functions.\n" % x["anon_count"])