Advertisement
stream13

DeBash - debugging Shell Scripts

Feb 26th, 2013
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2.  
  3. import sys
  4. import os
  5.  
  6. filename = "FILE0"
  7. funcname = "FUNC0"
  8. start_writren = True
  9.  
  10. def debash(filename):
  11.         inputFile = open(filename, "r" )
  12.         sourceArray = []
  13.         for line in inputFile:
  14.             sourceArray.append( line )
  15.         inputFile.close()
  16.         print "DEBASH: [%s][Read done]." % (filename)    
  17.  
  18.         if (len(sys.argv) == 4 and sys.argv[2] == "-d"):
  19.             destFile = open(sys.argv[3], "w")
  20.         else:  
  21.             destFile = open(sys.argv[1], "w")
  22.  
  23.         sourceArray[0] = "%s %s" % (sourceArray[0], 'echo "Entered[%s]" >> /tmp/wisc.log; #DEBASH\n' % (filename))
  24.         start_writren = True
  25.         for line in sourceArray:
  26.             # hard
  27.             if(line.find("()") != -1):
  28.                 funcname = line[0:line.find("()")+2]
  29.                 start_writren = False
  30.  
  31.  
  32.             if(line.find("{") == -1 and line.find("}") == -1):
  33.                 destFile.write(line)
  34.             else:
  35.                 if(line.find("{") != -1 and line.find("}") == -1 and start_writren == False): # found only for the first time{
  36.                     destFile.write(line)
  37.                     destFile.write('\techo "[%s]:[%s]:[%s]" >> /tmp/wisc.log #DEBASH\n' % (filename, funcname, "start") )
  38.                     start_writren = True
  39.                 elif(line.find("{") == -1 and line.find("}") != -1 and line == '}\n' ): # found only }
  40.                     destFile.write('\techo "[%s]:[%s]:[%s]" >> /tmp/wisc.log #DEBASH\n' % (filename, funcname, "end") )
  41.                     destFile.write(line)
  42.                 else:
  43.                     destFile.write(line)
  44.             #   hard done
  45.         destFile.write('echo "Exited[%s]" >> /tmp/wisc.log #DEBASH\n' % (filename))
  46.         destFile.close()
  47.         print "DEBASH: [%s][Write done]." % (filename)
  48.  
  49. def undebash(filename):
  50.     targetfile =  open(filename, "r" )
  51.     sourceArray = []
  52.     for line in targetfile:
  53.         sourceArray.append( line )
  54.     targetfile.close()
  55.     destFile = open(filename, "w" )
  56.     for line in sourceArray:
  57.         if(line.find("#DEBASH") == -1):
  58.             destFile.write(line)
  59. def debash_help():
  60.     print "debash.py [SOURCE] {-u | -d [DESTFILE]}"
  61. def debash_descr():
  62.     debash_help()
  63.     print "\t[SOURCE] - file to debash"
  64.     print "\t-u - causes UNDEBASH"
  65.     print "\t-d - to determine another destination file.\n\t     If you want to keep debashed file with different name."
  66.  
  67. if (len(sys.argv) > 1 ):
  68.     if(os.path.isfile(sys.argv[1]) == True and os.path.islink(sys.argv[1]) == False):
  69.         isdebashed = False
  70.         tmpfile = open(sys.argv[1], 'r')
  71.         content = tmpfile.read()
  72.         if(content.find("#DEBASH") != -1):
  73.             isdebashed = True
  74.  
  75.         if(len(sys.argv) > 1):
  76.             if(len(sys.argv) == 3):
  77.                 if(sys.argv[2] == "-u"):
  78.                     if(isdebashed == True):
  79.                         undebash(sys.argv[1])
  80.                     else:
  81.                         print"DEBASH: File [%s] is undebashed. Nothing done." % (sys.argv[1])
  82.             else:
  83.                 if(isdebashed == False):
  84.                     debash(sys.argv[1])
  85.                 else:
  86.                     print"DEBASH: File [%s] is already debashed. Nothing done." % (sys.argv[1])
  87.     else:
  88.         if(sys.argv[1] == "-v"):
  89.             print "DEBASH v0.1"
  90.         elif(sys.argv[1] == "-h"):
  91.             debash_descr()
  92. else:
  93.     if(len(sys.argv) < 2):
  94.         print "DEBASH: ERROR! NO FILENAME DIVEN!"
  95.     elif(os.path.isfile(sys.argv[1]) == False):
  96.         print "DEBASH: ERROR! NOT A FILE!"
  97.     elif(os.path.islink(sys.argv[1]) == False):
  98.         print "DEBASH: FILE IS A LINK!"
  99.     else:
  100.         print "DEBASH: UNKNOWN ERROR LOL xD"
  101.     debash_help()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement