Fixed #16397 -- Respected the --traceback flag in BaseCommand. This should make import loops easier to debug. Refs #11667, #17369.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17197 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Aymeric Augustin 2011-12-11 10:03:09 +00:00
parent affca1369c
commit 694bc0f22b
3 changed files with 14 additions and 5 deletions

View File

@ -7,6 +7,7 @@ be executed through ``django-admin.py`` or ``manage.py``).
import os import os
import sys import sys
from optparse import make_option, OptionParser from optparse import make_option, OptionParser
import traceback
import django import django
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
@ -197,8 +198,9 @@ class BaseCommand(object):
``self.requires_model_validation``). If the command raises a ``self.requires_model_validation``). If the command raises a
``CommandError``, intercept it and print it sensibly to ``CommandError``, intercept it and print it sensibly to
stderr. stderr.
""" """
show_traceback = options.get('traceback', False)
# Switch to English, because django-admin.py creates database content # Switch to English, because django-admin.py creates database content
# like permissions, and those shouldn't contain any translations. # like permissions, and those shouldn't contain any translations.
# But only do this if we can assume we have a working settings file, # But only do this if we can assume we have a working settings file,
@ -212,8 +214,12 @@ class BaseCommand(object):
except ImportError, e: except ImportError, e:
# If settings should be available, but aren't, # If settings should be available, but aren't,
# raise the error and quit. # raise the error and quit.
sys.stderr.write(smart_str(self.style.ERROR('Error: %s\n' % e))) if show_traceback:
traceback.print_exc()
else:
sys.stderr.write(smart_str(self.style.ERROR('Error: %s\n' % e)))
sys.exit(1) sys.exit(1)
try: try:
self.stdout = options.get('stdout', sys.stdout) self.stdout = options.get('stdout', sys.stdout)
self.stderr = options.get('stderr', sys.stderr) self.stderr = options.get('stderr', sys.stderr)
@ -232,7 +238,10 @@ class BaseCommand(object):
if self.output_transaction: if self.output_transaction:
self.stdout.write('\n' + self.style.SQL_KEYWORD("COMMIT;") + '\n') self.stdout.write('\n' + self.style.SQL_KEYWORD("COMMIT;") + '\n')
except CommandError, e: except CommandError, e:
self.stderr.write(smart_str(self.style.ERROR('Error: %s\n' % e))) if show_traceback:
traceback.print_exc()
else:
self.stderr.write(smart_str(self.style.ERROR('Error: %s\n' % e)))
sys.exit(1) sys.exit(1)
if saved_lang is not None: if saved_lang is not None:
translation.activate(saved_lang) translation.activate(saved_lang)

View File

@ -7,6 +7,7 @@ import os
import gzip import gzip
import zipfile import zipfile
from optparse import make_option from optparse import make_option
import traceback
from django.conf import settings from django.conf import settings
from django.core import serializers from django.core import serializers
@ -211,7 +212,6 @@ class Command(BaseCommand):
except (SystemExit, KeyboardInterrupt): except (SystemExit, KeyboardInterrupt):
raise raise
except Exception: except Exception:
import traceback
fixture.close() fixture.close()
if commit: if commit:
transaction.rollback(using=using) transaction.rollback(using=using)

View File

@ -1,5 +1,6 @@
from optparse import make_option from optparse import make_option
import sys import sys
import traceback
from django.conf import settings from django.conf import settings
from django.core.management.base import NoArgsCommand from django.core.management.base import NoArgsCommand
@ -129,7 +130,6 @@ class Command(NoArgsCommand):
sys.stderr.write("Failed to install custom SQL for %s.%s model: %s\n" % \ sys.stderr.write("Failed to install custom SQL for %s.%s model: %s\n" % \
(app_name, model._meta.object_name, e)) (app_name, model._meta.object_name, e))
if show_traceback: if show_traceback:
import traceback
traceback.print_exc() traceback.print_exc()
transaction.rollback_unless_managed(using=db) transaction.rollback_unless_managed(using=db)
else: else: