Guest User

Untitled

a guest
May 5th, 2012
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  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"])
Advertisement
Add Comment
Please, Sign In to add comment