Wednesday, February 11, 2009

Simple Django/python utility to check all the import statements in your project

As I mentioned in my previous posting, I was stuck with Flup exceptions and I am trying to implement what Maxx had suggested. But one interesting thing I did, just before MAXX suggested, to check all the import statements in my entire project codebase. Ofcourse this is not a Django level thing, but at the python level. I started writing one and made that a Django command extension, run as python manage.py imports_checker. It helped me in identifying many faulty imports which could be coined as one of the culprits for the "flup's dreaded exception". The code is mentioned below:
  1. class Command(NoArgsCommand):  
  2.     option_list = NoArgsCommand.option_list  
  3.     help = "Scans through the entire project directory and collects all the stale/obsolete import statements"  
  4.     requires_model_validation = True  
  5.   
  6. def import_statement_extractor(self,directory_path,python_file):  
  7.     python_file = '%s/%s' % (directory_path,python_file)  
  8.     file_content = open(python_file).readlines()  
  9.     for line in file_content:  
  10.         line = line.strip()  
  11.         if line.startswith('from'or line.startswith('import'):  
  12.             try:  
  13.                 exec(line)  
  14. #                    print '%s:==>:%s:Pass' % (python_file,line)  
  15.             except ImportError:  
  16.                 print '%s:XXX:%s:Fail' % (python_file,line)  
  17.   
  18. def directory_py_files(self,parent_directory):  
  19.    import os  
  20.    directory_generator = os.walk(parent_directory)  
  21.    directory_info = directory_generator.next()  
  22.    for file in directory_info[2]:  
  23.        if file.endswith('py'):  
  24.           self.import_statement_extractor(directory_info[0],file)  
  25.    for directory in directory_info[1]:  
  26.        if not directory.startswith('.'):  
  27.           print '\n'  
  28.           self.directory_py_files('%s/%s' % (parent_directory,directory))  
  29.      
  30. def handle_noargs(self, **options):  
  31.     from django.conf import settings  
  32.     self.directory_py_files(settings.ROOT_PATH)  
This is a generic command, it does not check the settings.INSTALLED_APPS setting for cleaning up imports but it does a decent job of identifying all stale or obsolete imports.

No comments:

Post a Comment