Ticket #4924: compression.diff
File compression.diff, 6.5 KB (added by , 17 years ago) |
---|
-
django/core/management.py
1402 1402 transaction.enter_transaction_management() 1403 1403 transaction.managed(True) 1404 1404 1405 import bz2, gzip 1406 compressionTypes = { 1407 'bz2': bz2.BZ2File, 1408 'gz': gzip.GzipFile, 1409 } 1410 1405 1411 app_fixtures = [os.path.join(os.path.dirname(app.__file__),'fixtures') for app in get_apps()] 1406 1412 for fixture_label in fixture_labels: 1407 1413 parts = fixture_label.split('.') 1414 1415 openMethod = open 1416 if len(parts) > 1 and parts[-1] in compressionTypes: 1417 openMethod = compressionTypes[parts[-1]] 1418 parts = parts[:-1] 1419 1408 1420 if len(parts) == 1: 1409 1421 fixture_name = fixture_label 1410 1422 formats = serializers.get_serializer_formats() … … 1431 1443 if verbosity > 1: 1432 1444 print "Trying %s for %s fixture '%s'..." % \ 1433 1445 (humanize(fixture_dir), format, fixture_name) 1434 try: 1435 full_path = os.path.join(fixture_dir, '.'.join([fixture_name, format])) 1436 fixture = open(full_path, 'r') 1437 if label_found: 1438 fixture.close() 1439 print style.ERROR("Multiple fixtures named '%s' in %s. Aborting." % 1440 (fixture_name, humanize(fixture_dir))) 1441 transaction.rollback() 1442 transaction.leave_transaction_management() 1443 return 1444 else: 1445 count[1] += 1 1446 if verbosity > 0: 1447 print "Installing %s fixture '%s' from %s." % \ 1448 (format, fixture_name, humanize(fixture_dir)) 1449 try: 1450 objects = serializers.deserialize(format, fixture) 1451 for obj in objects: 1452 count[0] += 1 1453 models.add(obj.object.__class__) 1454 obj.save() 1455 label_found = True 1456 except Exception, e: 1446 1447 for extension, openMethod in compressionTypes.iteritems(): 1448 try: 1449 if extension: 1450 full_path = os.path.join(fixture_dir, 1451 '.'.join([fixture_name, format, extension])) 1452 else: 1453 full_path = os.path.join(fixture_dir, 1454 '.'.join([fixture_name, format])) 1455 1456 fixture = openMethod(full_path, 'r') 1457 if label_found: 1457 1458 fixture.close() 1458 sys.stderr.write( 1459 style.ERROR("Problem installing fixture '%s': %s\n" % 1460 (full_path, str(e)))) 1459 print style.ERROR("Multiple fixtures named '%s' in %s. Aborting." % 1460 (fixture_name, humanize(fixture_dir))) 1461 1461 transaction.rollback() 1462 1462 transaction.leave_transaction_management() 1463 1463 return 1464 fixture.close() 1465 except: 1466 if verbosity > 1: 1467 print "No %s fixture '%s' in %s." % \ 1468 (format, fixture_name, humanize(fixture_dir)) 1464 else: 1465 count[1] += 1 1466 if verbosity > 0: 1467 print "Installing %s fixture '%s' from %s." % \ 1468 (format, fixture_name, humanize(fixture_dir)) 1469 try: 1470 objects = serializers.deserialize(format, fixture) 1471 for obj in objects: 1472 count[0] += 1 1473 models.add(obj.object.__class__) 1474 obj.save() 1475 label_found = True 1476 except Exception, e: 1477 fixture.close() 1478 sys.stderr.write( 1479 style.ERROR("Problem installing fixture '%s': %s\n" % 1480 (full_path, str(e)))) 1481 transaction.rollback() 1482 transaction.leave_transaction_management() 1483 return 1484 fixture.close() 1485 except: 1486 if verbosity > 1: 1487 print "No %s fixture '%s' in %s." % \ 1488 (format, fixture_name, humanize(fixture_dir)) 1469 1489 1470 1490 if count[0] > 0: 1471 1491 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>] 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