Variable Declaration Type Error Scanner [Python]
Hi Folks,
Seems I am kinda lost these days from blogging. However I am happy to add another post into blog today. Hope you will like it. I am not sure till how far this python script is going to help people but I hope somehow you can make changes based on your requirements and do the needful. But at the same time I would be very much thankful if you can give me some credits as well.
The script is written to parse any source code and will return the result if any variable declaration is done in a wrong way. Well basically the script can be used to parse ‘.es’ ECMAScript which has a specific type of variable declaration method. Even though it won’t parse all the source codes i.e. C/C++/Java/Tcl/Ruby but if you want then you can make some changes based on your requirements. If still you want any help from me then don’t hesitate to buzz me. I would be extremely happy to help you, if I can.
You can find the source code here or you can wget to here. Sample ‘.es’ code can be wget’ed from here.
Script usage:
$sudo chmod a+x parser.py
$./parser.py [FileName.es]
Parsing Logic::
- Read the file which is passed into the command line argument as the 2nd argument.
- Read the file first time and do these stuffs:
a. Trim all the lines.
b. Find all the strings which are basically used to for saving data (I am considering it as improper method initially)
inside variables, creating objects etc etc.
e.g. test = new ObjectName();
Now it will save the name 'test' inside a list. (Object Name: fObj)
c. Find all the strings which are basically used to for proper variable initializations.
e.g. var proper = ;
var proper2 = ;
Now it will save the name 'proper' and 'proper2' inside another list (Object Name: fObj2)
- Extend the list and append it into the existing object eventually.
- Create the subset of the objects and through difference() find the difference between proper and improper list.
- And at last print the output in the STDOUT.
Below is the python code if you just want to have a look.
import string, sys, re, os
print '###########################################################'
print '[*]Variable Declaration Type Error Scanner #'
print '[*]Author: Sujit Ghosal #'
print '[*]Mail: x13.x37@gmail.com #'
print '[*]Web: http://wikisecure.net #'
print '[*]All rights reserved. #'
print '###########################################################\n'
def variableScanner(file):
# Parse the variable names which are initialized in their in-correct methods.
try:
file = sys.argv[1]
ext = os.path.splitext(file)
if ext[1] == '.es':
print '[+]Parsing variables inside the program code.'
elif not ext[1] == '.es':
print '[+]Wrong filename passed to the parameters list.'
print '[+]Usage: python parser.py script-name.es'
sys.exit(0)
fObj = open(file, 'r')
incorrect = []
for lines in fObj:
lines = lines.strip()
incFilter = re.findall('^\w+\s+=\s+', lines)
for variables in incFilter:
if variables:
items = string.replace(variables, ' = ', '')
incorrect.extend(items.split())
#print str(incorrect)
elif not variables:
print '[+]No variables found.'
sys.exit(0)
elif len(variables) <= 0:
print '[+]Incorrect variable declaration not found.'
sys.exit(0)
fObj.close()
except IOError:
print '[+]File not found.'
print '[+]Please input the file name correctly!'
# Parse the variable names which are initialized in their correct methods.
try:
fObj2 = open(file, 'r')
correct = []
for varLines in fObj2:
varLines = varLines.strip()
varFilters = re.findall('var\ \w+', varLines)
for patterns in varFilters:
if patterns:
varItems = string.replace(patterns, 'var ', '')
correct.extend(varItems.split())
#print str(correct)
elif not patterns:
print '[+]No variable initialization keyword found.'
sys.exit(0)
fObj2.close()
except IOError:
print '[+]No var built-in keyword usage found in the code.'
print '[+]Number of correct variables found :', int(len(correct))
print '[+]Number of in-correct variables found :', int(len(incorrect))
print '\n[+]Correct Variables Name List:'
for corData in correct:
print ' ', corData
print '[+]Incorrect Variables Name List:'
for incData in incorrect:
print ' ', incData
print '\n<< << << PARSING RESULT >> >> >>'
print '.......................................'
diffResult = list(set(incorrect).difference(correct))
if len(diffResult) > 0:
print '[+]Probably these are the incorrect variables names list:'
for unInit in diffResult:
print ' ', str(unInit)
else:
print '[+]No variable initialization errors found in this script.\n\n'
def main():
if not len(sys.argv) == 2:
print '[+]Wrong number of arguments passed.'
print '[+]Usage: python parser.py script-name.es'
sys.exit(0)
if __name__ == "__main__":
main()
call = variableScanner(file)
If you want to test a basic functionality of how this is working then you can download this sample code and test its feature. Currently I am having a problem if someone is issuing only 1 argument. Well thats a very minor issue. I will fix that up soon.
NOTE: You can change the extension parsing restriction also if you want. You will find the code in the above portion of the script. You can either keep it or remove it (if required).
If you have any feedback or suggestions then please post them.
Enjoy!
XyluX
Hey Pav,
. But I had planned to just make a post now and fix it tonight at home. But anyways thanks a lot for helping me out.
I have made the changes now.
Actually that was in my mind to change
– XyluX
def main():
if not len(sys.argv) == 2:
print ‘[+]Wrong number of arguments passed.’
print ‘[+]Usage: python parser.py script-name.es’
sys.exit(0)
if __name__ == “__main__”:
main()
call = variableScanner(file)
“”" This would fix your argument problem “”"