mirror of
https://github.com/django/django.git
synced 2025-10-24 14:16:09 +00:00
Fixed #4924: added support for loading compressed fixtures. Thanks to Lars Yencken and Jeremy Dunck.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@9527 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
@@ -3,6 +3,7 @@ from django.core.management.color import no_style
|
||||
from optparse import make_option
|
||||
import sys
|
||||
import os
|
||||
import bz2, gzip, zipfile
|
||||
|
||||
try:
|
||||
set
|
||||
@@ -51,11 +52,33 @@ class Command(BaseCommand):
|
||||
transaction.enter_transaction_management()
|
||||
transaction.managed(True)
|
||||
|
||||
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,
|
||||
'bz2': bz2.BZ2File,
|
||||
'gz': gzip.GzipFile,
|
||||
'zip': SingleZipReader
|
||||
}
|
||||
|
||||
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('.')
|
||||
|
||||
if len(parts) > 1 and parts[-1] in compression_types:
|
||||
compression_formats = [parts[-1]]
|
||||
parts = parts[:-1]
|
||||
else:
|
||||
compression_formats = compression_types.keys()
|
||||
|
||||
if len(parts) == 1:
|
||||
fixture_name = fixture_label
|
||||
fixture_name = parts[0]
|
||||
formats = serializers.get_public_serializer_formats()
|
||||
else:
|
||||
fixture_name, format = '.'.join(parts[:-1]), parts[-1]
|
||||
@@ -86,64 +109,63 @@ class Command(BaseCommand):
|
||||
|
||||
label_found = False
|
||||
for format in formats:
|
||||
serializer = serializers.get_serializer(format)
|
||||
if verbosity > 1:
|
||||
print "Trying %s for %s fixture '%s'..." % \
|
||||
(humanize(fixture_dir), format, fixture_name)
|
||||
try:
|
||||
full_path = os.path.join(fixture_dir, '.'.join([fixture_name, format]))
|
||||
fixture = open(full_path, 'r')
|
||||
if label_found:
|
||||
fixture.close()
|
||||
print self.style.ERROR("Multiple fixtures named '%s' in %s. Aborting." %
|
||||
(fixture_name, humanize(fixture_dir)))
|
||||
transaction.rollback()
|
||||
transaction.leave_transaction_management()
|
||||
return
|
||||
else:
|
||||
fixture_count += 1
|
||||
objects_in_fixture = 0
|
||||
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:
|
||||
objects_in_fixture += 1
|
||||
models.add(obj.object.__class__)
|
||||
obj.save()
|
||||
object_count += objects_in_fixture
|
||||
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()
|
||||
|
||||
# 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
|
||||
except:
|
||||
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 "No %s fixture '%s' in %s." % \
|
||||
(format, fixture_name, humanize(fixture_dir))
|
||||
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:
|
||||
fixture.close()
|
||||
print self.style.ERROR("Multiple fixtures named '%s' in %s. Aborting." %
|
||||
(fixture_name, humanize(fixture_dir)))
|
||||
transaction.rollback()
|
||||
transaction.leave_transaction_management()
|
||||
return
|
||||
else:
|
||||
fixture_count += 1
|
||||
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:
|
||||
object_count += 1
|
||||
models.add(obj.object.__class__)
|
||||
obj.save()
|
||||
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()
|
||||
except Exception, e:
|
||||
if verbosity > 1:
|
||||
print "No %s fixture '%s' in %s." % \
|
||||
(format, fixture_name, humanize(fixture_dir))
|
||||
print e
|
||||
transaction.rollback()
|
||||
transaction.leave_transaction_management()
|
||||
return
|
||||
|
||||
# If we found even one object in a fixture, we need to reset the
|
||||
# database sequences.
|
||||
|
||||
Reference in New Issue
Block a user