From 694bc0f22bdb30a472de6fec05a44adf5c0f1f2c Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Sun, 11 Dec 2011 10:03:09 +0000 Subject: [PATCH] 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 --- django/core/management/base.py | 15 ++++++++++++--- django/core/management/commands/loaddata.py | 2 +- django/core/management/commands/syncdb.py | 2 +- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/django/core/management/base.py b/django/core/management/base.py index e8f461e354..23ff7d6194 100644 --- a/django/core/management/base.py +++ b/django/core/management/base.py @@ -7,6 +7,7 @@ be executed through ``django-admin.py`` or ``manage.py``). import os import sys from optparse import make_option, OptionParser +import traceback import django from django.core.exceptions import ImproperlyConfigured @@ -197,8 +198,9 @@ class BaseCommand(object): ``self.requires_model_validation``). If the command raises a ``CommandError``, intercept it and print it sensibly to stderr. - """ + show_traceback = options.get('traceback', False) + # Switch to English, because django-admin.py creates database content # like permissions, and those shouldn't contain any translations. # But only do this if we can assume we have a working settings file, @@ -212,8 +214,12 @@ class BaseCommand(object): except ImportError, e: # If settings should be available, but aren't, # 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) + try: self.stdout = options.get('stdout', sys.stdout) self.stderr = options.get('stderr', sys.stderr) @@ -232,7 +238,10 @@ class BaseCommand(object): if self.output_transaction: self.stdout.write('\n' + self.style.SQL_KEYWORD("COMMIT;") + '\n') 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) if saved_lang is not None: translation.activate(saved_lang) diff --git a/django/core/management/commands/loaddata.py b/django/core/management/commands/loaddata.py index bd867796c2..d96421f73c 100644 --- a/django/core/management/commands/loaddata.py +++ b/django/core/management/commands/loaddata.py @@ -7,6 +7,7 @@ import os import gzip import zipfile from optparse import make_option +import traceback from django.conf import settings from django.core import serializers @@ -211,7 +212,6 @@ class Command(BaseCommand): except (SystemExit, KeyboardInterrupt): raise except Exception: - import traceback fixture.close() if commit: transaction.rollback(using=using) diff --git a/django/core/management/commands/syncdb.py b/django/core/management/commands/syncdb.py index a9d803a8e4..852fa15410 100644 --- a/django/core/management/commands/syncdb.py +++ b/django/core/management/commands/syncdb.py @@ -1,5 +1,6 @@ from optparse import make_option import sys +import traceback from django.conf import settings 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" % \ (app_name, model._meta.object_name, e)) if show_traceback: - import traceback traceback.print_exc() transaction.rollback_unless_managed(using=db) else: