Ticket #4924: compression.2.diff
File compression.2.diff, 6.8 KB (added by , 17 years ago) |
---|
-
django/core/management.py
6 6 from optparse import OptionParser 7 7 from django.utils import termcolors 8 8 import os, re, shutil, sys, textwrap 9 import bz2, gzip 9 10 10 11 try: 11 12 set … … 1384 1385 transaction.enter_transaction_management() 1385 1386 transaction.managed(True) 1386 1387 1388 compression_types = { 1389 None: file, 1390 'bz2': bz2.BZ2File, 1391 'gz': gzip.GzipFile, 1392 } 1393 1387 1394 app_fixtures = [os.path.join(os.path.dirname(app.__file__),'fixtures') for app in get_apps()] 1388 1395 for fixture_label in fixture_labels: 1389 1396 parts = fixture_label.split('.') 1397 1398 if len(parts) > 1 and parts[-1] in compression_types: 1399 compression_formats = [parts[-1]] 1400 parts = parts[:-1] 1401 else: 1402 compression_formats = compression_types.keys() 1403 1390 1404 if len(parts) == 1: 1391 1405 fixture_name = fixture_label 1392 1406 formats = serializers.get_serializer_formats() … … 1413 1427 if verbosity > 1: 1414 1428 print "Trying %s for %s fixture '%s'..." % \ 1415 1429 (humanize(fixture_dir), format, fixture_name) 1416 try: 1417 full_path = os.path.join(fixture_dir, '.'.join([fixture_name, format])) 1418 fixture = open(full_path, 'r') 1419 if label_found: 1420 fixture.close() 1421 print style.ERROR("Multiple fixtures named '%s' in %s. Aborting." % 1422 (fixture_name, humanize(fixture_dir))) 1423 transaction.rollback() 1424 transaction.leave_transaction_management() 1425 return 1426 else: 1427 count[1] += 1 1428 if verbosity > 0: 1429 print "Installing %s fixture '%s' from %s." % \ 1430 (format, fixture_name, humanize(fixture_dir)) 1431 try: 1432 objects = serializers.deserialize(format, fixture) 1433 for obj in objects: 1434 count[0] += 1 1435 models.add(obj.object.__class__) 1436 obj.save() 1437 label_found = True 1438 except Exception, e: 1430 1431 for compression_format in compression_formats: 1432 try: 1433 if compression_format: 1434 full_path = os.path.join(fixture_dir, 1435 '.'.join([fixture_name, format, 1436 compression_format])) 1437 else: 1438 full_path = os.path.join(fixture_dir, 1439 '.'.join([fixture_name, format])) 1440 1441 open_method = compression_types[compression_format] 1442 fixture = open_method(full_path, 'r') 1443 if label_found: 1439 1444 fixture.close() 1440 sys.stderr.write( 1441 style.ERROR("Problem installing fixture '%s': %s\n" % 1442 (full_path, str(e)))) 1445 print style.ERROR("Multiple fixtures named '%s' in %s. Aborting." % 1446 (fixture_name, humanize(fixture_dir))) 1443 1447 transaction.rollback() 1444 1448 transaction.leave_transaction_management() 1445 1449 return 1446 fixture.close() 1447 except: 1448 if verbosity > 1: 1449 print "No %s fixture '%s' in %s." % \ 1450 (format, fixture_name, humanize(fixture_dir)) 1450 else: 1451 count[1] += 1 1452 if verbosity > 0: 1453 print "Installing %s fixture '%s' from %s." % \ 1454 (format, fixture_name, humanize(fixture_dir)) 1455 try: 1456 objects = serializers.deserialize(format, fixture) 1457 for obj in objects: 1458 count[0] += 1 1459 models.add(obj.object.__class__) 1460 obj.save() 1461 label_found = True 1462 except Exception, e: 1463 fixture.close() 1464 sys.stderr.write( 1465 style.ERROR("Problem installing fixture '%s': %s\n" % 1466 (full_path, str(e)))) 1467 transaction.rollback() 1468 transaction.leave_transaction_management() 1469 return 1470 fixture.close() 1471 except: 1472 if verbosity > 1: 1473 print "No %s fixture '%s' in %s." % \ 1474 (format, fixture_name, humanize(fixture_dir)) 1451 1475 1452 1476 if count[0] > 0: 1453 1477 sequence_sql = backend.get_sql_sequence_reset(style, models) -
tests/modeltests/fixtures/models.py
59 59 >>> Article.objects.all() 60 60 [<Article: Python program becomes self aware>] 61 61 62 # Load fixture 4 (compressed), using format discovery 63 >>> management.load_data(['fixture4'], verbosity=0) 64 >>> Article.objects.all() 65 [<Article: End of the world nigh>, <Article: Relationship trouble? Blame your mother>, <Article: Python program becomes self aware>] 66 62 67 # Load fixture 1 again, using format discovery 68 >>> management.flush(verbosity=0, interactive=False) 63 69 >>> management.load_data(['fixture1'], verbosity=0) 64 70 >>> Article.objects.all() 65 71 [<Article: Time to reform copyright>, <Article: Poker has no place on ESPN>, <Article: Python program becomes self aware>] … … 69 75 >>> management.load_data(['fixture2'], verbosity=0) # doctest: +ELLIPSIS 70 76 Multiple fixtures named 'fixture2' in '...fixtures'. Aborting. 71 77 78 # Try to load fixture 5 using format discovery; this will fail 79 # because there are two fixture5's, one compressed with bzip. 80 >>> management.load_data(['fixture5'], verbosity=0) # doctest: +ELLIPSIS 81 Multiple fixtures named 'fixture5' in '...fixtures'. Aborting. 82 72 83 >>> Article.objects.all() 73 84 [<Article: Time to reform copyright>, <Article: Poker has no place on ESPN>, <Article: Python program becomes self aware>] 74 85