2007-08-16 14:06:55 +08:00
|
|
|
from django.core.management.base import BaseCommand
|
|
|
|
from django.core.management.color import no_style
|
2007-09-10 05:57:59 +08:00
|
|
|
from optparse import make_option
|
2007-08-16 14:06:55 +08:00
|
|
|
import sys
|
|
|
|
import os
|
2008-11-25 04:42:09 +08:00
|
|
|
import bz2, gzip, zipfile
|
2007-08-16 14:06:55 +08:00
|
|
|
|
|
|
|
try:
|
|
|
|
set
|
|
|
|
except NameError:
|
|
|
|
from sets import Set as set # Python 2.3 fallback
|
|
|
|
|
2008-12-01 10:20:59 +08:00
|
|
|
try:
|
|
|
|
import bz2
|
|
|
|
has_bz2 = True
|
|
|
|
except ImportError:
|
|
|
|
has_bz2 = False
|
|
|
|
|
2007-08-16 14:06:55 +08:00
|
|
|
class Command(BaseCommand):
|
|
|
|
help = 'Installs the named fixture(s) in the database.'
|
2007-09-10 05:57:59 +08:00
|
|
|
args = "fixture [fixture ...]"
|
2007-08-16 14:06:55 +08:00
|
|
|
|
|
|
|
def handle(self, *fixture_labels, **options):
|
|
|
|
from django.db.models import get_apps
|
|
|
|
from django.core import serializers
|
2007-08-20 08:21:10 +08:00
|
|
|
from django.db import connection, transaction
|
2007-08-16 14:06:55 +08:00
|
|
|
from django.conf import settings
|
|
|
|
|
|
|
|
self.style = no_style()
|
|
|
|
|
2007-08-17 08:11:56 +08:00
|
|
|
verbosity = int(options.get('verbosity', 1))
|
2007-12-17 19:09:32 +08:00
|
|
|
show_traceback = options.get('traceback', False)
|
2008-10-02 20:57:13 +08:00
|
|
|
|
|
|
|
# commit is a stealth option - it isn't really useful as
|
2008-08-13 22:36:40 +08:00
|
|
|
# a command line option, but it can be useful when invoking
|
2008-10-02 20:57:13 +08:00
|
|
|
# loaddata from within another script.
|
2008-08-13 22:36:40 +08:00
|
|
|
# If commit=True, loaddata will use its own transaction;
|
|
|
|
# if commit=False, the data load SQL will become part of
|
|
|
|
# the transaction in place when loaddata was invoked.
|
|
|
|
commit = options.get('commit', True)
|
2008-10-02 20:57:13 +08:00
|
|
|
|
2007-08-16 14:06:55 +08:00
|
|
|
# Keep a count of the installed objects and fixtures
|
2008-02-22 20:50:10 +08:00
|
|
|
fixture_count = 0
|
|
|
|
object_count = 0
|
2007-08-16 14:06:55 +08:00
|
|
|
models = set()
|
|
|
|
|
|
|
|
humanize = lambda dirname: dirname and "'%s'" % dirname or 'absolute path'
|
|
|
|
|
|
|
|
# Get a cursor (even though we don't need one yet). This has
|
|
|
|
# the side effect of initializing the test database (if
|
|
|
|
# it isn't already initialized).
|
|
|
|
cursor = connection.cursor()
|
|
|
|
|
|
|
|
# Start transaction management. All fixtures are installed in a
|
|
|
|
# single transaction to ensure that all references are resolved.
|
2008-08-13 22:36:40 +08:00
|
|
|
if commit:
|
|
|
|
transaction.commit_unless_managed()
|
|
|
|
transaction.enter_transaction_management()
|
|
|
|
transaction.managed(True)
|
2007-08-16 14:06:55 +08:00
|
|
|
|
2008-11-25 04:42:09 +08:00
|
|
|
class SingleZipReader(zipfile.ZipFile):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
zipfile.ZipFile.__init__(self, *args, **kwargs)
|
|
|
|
if settings.DEBUG:
|
|
|
|
assert len(self.namelist()) == 1, "Zip-compressed fixtures must contain only one file."
|
|
|
|
def read(self):
|
|
|
|
return zipfile.ZipFile.read(self, self.namelist()[0])
|
|
|
|
|
|
|
|
compression_types = {
|
|
|
|
None: file,
|
|
|
|
'gz': gzip.GzipFile,
|
|
|
|
'zip': SingleZipReader
|
|
|
|
}
|
2008-12-01 10:20:59 +08:00
|
|
|
if has_bz2:
|
|
|
|
compression_types['bz2'] = bz2.BZ2File
|
2008-11-25 04:42:09 +08:00
|
|
|
|
2007-08-16 14:06:55 +08:00
|
|
|
app_fixtures = [os.path.join(os.path.dirname(app.__file__), 'fixtures') for app in get_apps()]
|
|
|
|
for fixture_label in fixture_labels:
|
|
|
|
parts = fixture_label.split('.')
|
2008-11-25 04:42:09 +08:00
|
|
|
|
|
|
|
if len(parts) > 1 and parts[-1] in compression_types:
|
|
|
|
compression_formats = [parts[-1]]
|
|
|
|
parts = parts[:-1]
|
|
|
|
else:
|
|
|
|
compression_formats = compression_types.keys()
|
|
|
|
|
2007-08-16 14:06:55 +08:00
|
|
|
if len(parts) == 1:
|
2008-11-25 04:42:09 +08:00
|
|
|
fixture_name = parts[0]
|
2007-12-17 14:53:15 +08:00
|
|
|
formats = serializers.get_public_serializer_formats()
|
2007-08-16 14:06:55 +08:00
|
|
|
else:
|
|
|
|
fixture_name, format = '.'.join(parts[:-1]), parts[-1]
|
2007-12-17 14:53:15 +08:00
|
|
|
if format in serializers.get_public_serializer_formats():
|
2007-08-16 14:06:55 +08:00
|
|
|
formats = [format]
|
|
|
|
else:
|
|
|
|
formats = []
|
|
|
|
|
2008-06-08 16:21:18 +08:00
|
|
|
if formats:
|
|
|
|
if verbosity > 1:
|
2007-08-16 14:06:55 +08:00
|
|
|
print "Loading '%s' fixtures..." % fixture_name
|
2008-06-08 16:21:18 +08:00
|
|
|
else:
|
|
|
|
sys.stderr.write(
|
|
|
|
self.style.ERROR("Problem installing fixture '%s': %s is not a known serialization format." %
|
|
|
|
(fixture_name, format)))
|
|
|
|
transaction.rollback()
|
|
|
|
transaction.leave_transaction_management()
|
|
|
|
return
|
2007-08-16 14:06:55 +08:00
|
|
|
|
2008-02-22 20:50:10 +08:00
|
|
|
if os.path.isabs(fixture_name):
|
|
|
|
fixture_dirs = [fixture_name]
|
|
|
|
else:
|
|
|
|
fixture_dirs = app_fixtures + list(settings.FIXTURE_DIRS) + ['']
|
|
|
|
|
|
|
|
for fixture_dir in fixture_dirs:
|
2007-08-16 14:06:55 +08:00
|
|
|
if verbosity > 1:
|
|
|
|
print "Checking %s for fixtures..." % humanize(fixture_dir)
|
|
|
|
|
|
|
|
label_found = False
|
|
|
|
for format in formats:
|
2008-11-25 04:42:09 +08:00
|
|
|
for compression_format in compression_formats:
|
|
|
|
if compression_format:
|
|
|
|
file_name = '.'.join([fixture_name, format,
|
|
|
|
compression_format])
|
|
|
|
else:
|
|
|
|
file_name = '.'.join([fixture_name, format])
|
|
|
|
|
|
|
|
if verbosity > 1:
|
|
|
|
print "Trying %s for %s fixture '%s'..." % \
|
|
|
|
(humanize(fixture_dir), file_name, fixture_name)
|
|
|
|
full_path = os.path.join(fixture_dir, file_name)
|
|
|
|
open_method = compression_types[compression_format]
|
|
|
|
try:
|
|
|
|
fixture = open_method(full_path, 'r')
|
|
|
|
if label_found:
|
2007-08-16 14:06:55 +08:00
|
|
|
fixture.close()
|
2008-11-25 04:42:09 +08:00
|
|
|
print self.style.ERROR("Multiple fixtures named '%s' in %s. Aborting." %
|
|
|
|
(fixture_name, humanize(fixture_dir)))
|
2007-12-17 19:09:32 +08:00
|
|
|
transaction.rollback()
|
|
|
|
transaction.leave_transaction_management()
|
2007-08-16 14:06:55 +08:00
|
|
|
return
|
2008-11-25 04:42:09 +08:00
|
|
|
else:
|
|
|
|
fixture_count += 1
|
2008-11-25 05:13:33 +08:00
|
|
|
objects_in_fixture = 0
|
2008-11-25 04:42:09 +08:00
|
|
|
if verbosity > 0:
|
|
|
|
print "Installing %s fixture '%s' from %s." % \
|
|
|
|
(format, fixture_name, humanize(fixture_dir))
|
|
|
|
try:
|
|
|
|
objects = serializers.deserialize(format, fixture)
|
|
|
|
for obj in objects:
|
2008-11-25 05:13:33 +08:00
|
|
|
objects_in_fixture += 1
|
2008-11-25 04:42:09 +08:00
|
|
|
models.add(obj.object.__class__)
|
|
|
|
obj.save()
|
2008-11-25 05:13:33 +08:00
|
|
|
object_count += objects_in_fixture
|
2008-11-25 04:42:09 +08:00
|
|
|
label_found = True
|
|
|
|
except (SystemExit, KeyboardInterrupt):
|
|
|
|
raise
|
|
|
|
except Exception:
|
|
|
|
import traceback
|
|
|
|
fixture.close()
|
|
|
|
transaction.rollback()
|
|
|
|
transaction.leave_transaction_management()
|
|
|
|
if show_traceback:
|
|
|
|
import traceback
|
|
|
|
traceback.print_exc()
|
|
|
|
else:
|
|
|
|
sys.stderr.write(
|
|
|
|
self.style.ERROR("Problem installing fixture '%s': %s\n" %
|
|
|
|
(full_path, traceback.format_exc())))
|
|
|
|
return
|
|
|
|
fixture.close()
|
2008-11-25 06:33:18 +08:00
|
|
|
|
2008-11-25 05:13:33 +08:00
|
|
|
# If the fixture we loaded contains 0 objects, assume that an
|
|
|
|
# error was encountered during fixture loading.
|
|
|
|
if objects_in_fixture == 0:
|
|
|
|
sys.stderr.write(
|
|
|
|
self.style.ERROR("No fixture data found for '%s'. (File format may be invalid.)" %
|
|
|
|
(fixture_name)))
|
|
|
|
transaction.rollback()
|
|
|
|
transaction.leave_transaction_management()
|
|
|
|
return
|
2008-11-25 06:33:18 +08:00
|
|
|
|
2008-11-25 04:42:09 +08:00
|
|
|
except Exception, e:
|
|
|
|
if verbosity > 1:
|
|
|
|
print "No %s fixture '%s' in %s." % \
|
|
|
|
(format, fixture_name, humanize(fixture_dir))
|
|
|
|
print e
|
2008-11-06 19:19:13 +08:00
|
|
|
transaction.rollback()
|
|
|
|
transaction.leave_transaction_management()
|
|
|
|
return
|
2007-08-16 14:06:55 +08:00
|
|
|
|
2008-08-13 22:36:40 +08:00
|
|
|
# If we found even one object in a fixture, we need to reset the
|
2008-06-08 16:21:18 +08:00
|
|
|
# database sequences.
|
2008-02-22 20:50:10 +08:00
|
|
|
if object_count > 0:
|
2007-08-20 08:21:10 +08:00
|
|
|
sequence_sql = connection.ops.sequence_reset_sql(self.style, models)
|
2007-08-16 14:06:55 +08:00
|
|
|
if sequence_sql:
|
|
|
|
if verbosity > 1:
|
|
|
|
print "Resetting sequences"
|
|
|
|
for line in sequence_sql:
|
|
|
|
cursor.execute(line)
|
2008-10-02 20:57:13 +08:00
|
|
|
|
2008-08-13 22:36:40 +08:00
|
|
|
if commit:
|
|
|
|
transaction.commit()
|
|
|
|
transaction.leave_transaction_management()
|
2007-08-16 14:06:55 +08:00
|
|
|
|
2008-02-22 20:50:10 +08:00
|
|
|
if object_count == 0:
|
2008-06-08 16:21:18 +08:00
|
|
|
if verbosity > 1:
|
2007-08-16 14:06:55 +08:00
|
|
|
print "No fixtures found."
|
|
|
|
else:
|
|
|
|
if verbosity > 0:
|
2008-02-22 20:50:10 +08:00
|
|
|
print "Installed %d object(s) from %d fixture(s)" % (object_count, fixture_count)
|
2008-10-02 20:57:13 +08:00
|
|
|
|
2008-06-30 20:18:29 +08:00
|
|
|
# Close the DB connection. This is required as a workaround for an
|
|
|
|
# edge case in MySQL: if the same connection is used to
|
|
|
|
# create tables, load data, and query, the query can return
|
|
|
|
# incorrect results. See Django #7572, MySQL #37735.
|
2008-08-13 22:36:40 +08:00
|
|
|
if commit:
|
|
|
|
connection.close()
|