Skip to content

Instantly share code, notes, and snippets.

@FreedomBen
Created September 22, 2014 20:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FreedomBen/52e4e2cce6e6958424fc to your computer and use it in GitHub Desktop.
Save FreedomBen/52e4e2cce6e6958424fc to your computer and use it in GitHub Desktop.
Add Debug printout for ruby programs
#!/usr/bin/python
import sys
import os
REMOVE_FLAG = '<remove>'
COMMENT_STRING = '# Added by debug script. To remove, rerun script with -c|--cleanup flag'
DEBUG_STATEMENT = """
Rails.logger.debug("In method:".blue + " - " +
"#{self.nil? ? String.new : self.class.to_s}".green +
"#{self.class.superclass.nil? ? String.new : (__method__.nil? ? String.new : (self.class.superclass.instance_methods(false).include?((__method__).to_sym) ? (' < ' + self.class.superclass.to_s) : String.new))}".cyan +
"#{self.class.superclass.superclass.nil? ? String.new : (__method__.nil? ? String.new : (self.class.superclass.superclass.instance_methods(false).include?((__method__).to_sym) ? (' < ' + self.class.superclass.superclass.to_s) : String.new))}".purple +
" :: " +
"#{__method__}".yellow)
""".replace("\n", " ")
def fixFile( fileName ):
# save to temp file so we don't get jacked up until we're done
with open( fileName, 'r' ) as inp:
with open( fileName + '.temp', 'w' ) as out:
for line in inp:
if cleanup():
if COMMENT_STRING not in line and REMOVE_FLAG not in line:
out.write( line )
else:
out.write( line )
if ' def ' in line:
out.write( '%s %s\n' % ( DEBUG_STATEMENT, COMMENT_STRING ) )
# now copy temp file into other file and delete temp
with open( fileName + '.temp', 'r' ) as inp:
with open( fileName, 'w' ) as out:
for line in inp:
out.write( line )
os.remove( fileName + '.temp' )
def cleanup():
return '-c' in sys.argv or '--cleanup' in sys.argv
def printUsage():
print 'Usage: ' + sys.argv[0] + '[-c|--cleanup] file1 [file2] ...'
sys.exit( 1 )
if __name__ == '__main__':
# go through the file and add a 'Rails.logger.debug(message) to each method
if( len( sys.argv ) < 2 ) or ( '-h' in sys.argv ) or ( '--help' in sys.argv ):
printUsage()
files = sys.argv[1:]
if( '-c' in files ):
files.remove( '-c' )
if( '--cleanup' in files ):
files.remove( '--cleanup' )
numFiles = 0
for f in files:
if not os.path.isdir(f):
fixFile( f )
numFiles += 1
print 'Done. Fixed ' + str( numFiles ) + ' files.'
sys.exit( 0 )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment