- Author:
- madhav.bnk
- Posted:
- November 29, 2009
- Language:
- Python
- Version:
- 1.1
- Score:
- 3 (after 3 ratings)
I was using flup to run django in fcgi mode and encountered the dreaded "Unhandled Exception" page quite frequently. So apart from all the precautions about handling this except, I wrote the above code snippet, which checks the import across your ENTIRE project. Ofcourse this can be used on any python project, but I have written it for my favorite framework django. It is now written as a Django command extension, an can be run as: python manage.py imports_checker
This is a generic command, it does not check the settings.INSTALLED_APPS setting for cleaning. But can be improved to do the same.
Public Clone Url: git://gist.github.com/242451.git
Update: Now it supports checking imports, just only at the app level also usage: python manage.py imports_checker <appname>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | from django.core.management.base import BaseCommand
class Command(BaseCommand):
option_list = BaseCommand.option_list
help = "Scans through the given app for faulty imports, or the entire project directory if no apps are specified"
args = '[appname ...]'
requires_model_validation = False
def import_statement_extractor(self, directory_path, python_file):
python_file = '%s/%s' % (directory_path, python_file)
file_content = open(python_file).readlines()
line_number = 0
for line in file_content:
line_number += 1
line = line.strip()
if not line.startswith('#') or not line.startswith("'''"):
if line.startswith('from ') or line.startswith('import '):
try:
exec(line)
except ImportError, e:
print '%s(line:%s) Reason:%s' % (python_file, line_number, e.__str__())
except Exception, e:
print '%s(line:%s) Reason:%s' % (python_file, line_number, e.__str__())
def directory_py_files(self, parent_directory):
import os
directory_generator = os.walk(parent_directory)
directory_info = directory_generator.next()
for file_name in directory_info[2]:
if file_name.endswith('py'):
self.import_statement_extractor(directory_info[0], file_name)
for directory in directory_info[1]:
if not directory.startswith('.'):
self.directory_py_files('%s/%s' % (parent_directory, directory))
def handle(self, *app_labels, **options):
from django.conf import settings
import sys
if hasattr(settings, 'ROOT_PATH'):
ROOT_PATH = settings.ROOT_PATH
else:
import os
ROOT_PATH = os.getcwd()
if not app_labels:
self.directory_py_files(ROOT_PATH)
sys.exit()
for app_label in app_labels:
if app_label not in settings.INSTALLED_APPS:
sys.exit("Supplied app '%s' is not part of this project. Please mention a proper app name" % app_label)
for app_label in app_labels:
self.directory_py_files(settings.ROOT_PATH + "/" + app_label)
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 8 months, 4 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 9 months ago
- Serializer factory with Django Rest Framework by julio 1 year, 3 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 4 months ago
- Help text hyperlinks by sa2812 1 year, 5 months ago
Comments
I have modified the snippet, so that it checks the imports only at the <app> level also.
usage: python manage.py imports_checker <appname>
#
Please login first before commenting.