Hello everyone...
Its been a long time since I have posted in the blog. This time I will be writing about a python program which help to check for errors in the python scripts. Python is a very powerful high level language and I am pretty much passionate about programming in python.
Its been a long time since I have posted in the blog. This time I will be writing about a python program which help to check for errors in the python scripts. Python is a very powerful high level language and I am pretty much passionate about programming in python.
There are a lot of libraries in Python which we can use to develop useful scripts according to the nature of requirement. In this post, we will be using 'pyflakes'. PyFlakes is one of the opensource projects formerly from divmod.org. As per documentation given by divmod, pyflakes is a fast tool to report defects in Python such as unnecessary or forgotten imports, two different modules/functions with same name, etc. You can visit the documentation for PyFlakes and even contribute at github. To install PyFlakes using pip, run the following command:
pip install --upgrade pyflakes
The name of our program is PyError Utiltity. The aim of this utility is to find out the errors or defects if present in a python script. The user must provide a complete filename or the name of a directory in which the scripts are saved. There are two user defined functions/methods, I have created.
1. pathChecker(filepath_complete) - This method is used to check for the validity of the file/directory path. The 'filepath_complete' is an argument passed, over which the function operates. The return statement is a string which contains either '*invalid*' or '*valid*'.
########################################################
def pathChecker(filepath_complete): if os.path.exists(filepath_complete): res = str(filepath_complete + ' is a *valid* path... ') else: res = str(filepath_complete + ' is an *invalid* path... ') print '\n\t\tMessage: ' + res return res
########################################################
2. errorChecker(filepath_complete) - This method is used after the pathChecker method. If it is confirmed that the following filename exist, this method will check for the errors and print the errors in the terminal window. If the program does not find any errors, it will show a message for no errors found.
Coming to the testing of the PyError Utility, I have created a directory as '/PyError-Related' and dumped test1.py. test1.py script changes file permissions for passwd, shadow and group files in /etc, creates .acl of old permissions in /, and log files in /tmp. There are some errors in test1.py.
So, I checked for the errors using PyError Utility.
Then I removed the errors in test1.py, copied the code in test2.py and again executed the PyError Utility but this time with directory name provided.
To get the complete code for 'PyError Utility', go to this link of github.
########################################################
def errorChecker(filepath_complete): cmd = 'pyflakes ' + filepath_complete run = os.popen(cmd) result = ''.join(run.readlines()) if len(result) > 0: print '\n\t\tError in: ' + result else: print '\n\t\tNo Errors found in: ' + filepath_complete
########################################################
Coming to the testing of the PyError Utility, I have created a directory as '/PyError-Related' and dumped test1.py. test1.py script changes file permissions for passwd, shadow and group files in /etc, creates .acl of old permissions in /, and log files in /tmp. There are some errors in test1.py.
########################################################
# this is my test1.py file # modifying the file permissions for owner, groups, others
import os, datetime, sys, time, re odat = (time.strftime("%d-%m-%Y")) dat = datetime.datetime.now() if os.path.exists('/tmp')==False: print "/tmp directory is not there, so creating /tmp" os.makedirs('/tmp') file_log = '/tmp/test1' + str(odat) + '.log' f = open(file_log,'a+') saveout = sys.stdout sys.stdout = f print "***************Log Started for test1*******************" os.system("getfacl -p /etc/group > /group.acl") os.system("getfacl -p /etc/passwd > /passwd.acl") os.system("getfacl -p /etc/shadow > /shadow.acl") print "Backup permissions files are created as '/group.acl','/passwd.acl','/shadow.acl' " os.system("sudo chmod 644 /etc/passwd") os.system("sudo chmod 600 /etc/shadow") os.system("sudo chmod 644 /etc/group") print '\n'+str(dat)+'\n' print 'Updated access permissions:\n' p1 = os.popen("ls -l /etc/passwd") print p1.readline() p2 = os.popen("ls -l /etc/shadow") print p2.readline() p3 = os.popen("ls -l /etc/group") print p3.readline() print p4.readline() print "***************Log Ended for test1*******************" sys.stdout = saveout f.close() print "Check the log file at: ", file_log
#######################################################
So, I checked for the errors using PyError Utility.
Then I removed the errors in test1.py, copied the code in test2.py and again executed the PyError Utility but this time with directory name provided.
To get the complete code for 'PyError Utility', go to this link of github.
The visibility of PyFlakes in finding errors is fine. Certainly, there are some limitations of PyFlakes therefore it is not possible to find out every single possible error with PyFlakes.
0 comments: