From 0e3b0da2e3ecee05e8b5eee763f444c94280dbcb Mon Sep 17 00:00:00 2001 From: Paolo Melchiorre Date: Fri, 15 May 2020 09:40:42 +0200 Subject: [PATCH] Fixed #31552 -- Added support for LZMA and XZ fixtures to loaddata. --- django/core/management/commands/loaddata.py | 9 +++++++++ docs/ref/django-admin.txt | 13 +++++++++---- docs/releases/3.2.txt | 3 ++- tests/fixtures/fixtures/fixture5.json.lzma | Bin 0 -> 157 bytes tests/fixtures/fixtures/fixture5.json.xz | Bin 0 -> 200 bytes tests/fixtures/tests.py | 20 ++++++++++++++++++++ 6 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 tests/fixtures/fixtures/fixture5.json.lzma create mode 100644 tests/fixtures/fixtures/fixture5.json.xz diff --git a/django/core/management/commands/loaddata.py b/django/core/management/commands/loaddata.py index 29f7630ff7..eda3d068af 100644 --- a/django/core/management/commands/loaddata.py +++ b/django/core/management/commands/loaddata.py @@ -26,6 +26,12 @@ try: except ImportError: has_bz2 = False +try: + import lzma + has_lzma = True +except ImportError: + has_lzma = False + READ_STDIN = '-' @@ -97,6 +103,9 @@ class Command(BaseCommand): } if has_bz2: self.compression_formats['bz2'] = (bz2.BZ2File, 'r') + if has_lzma: + self.compression_formats['lzma'] = (lzma.LZMAFile, 'r') + self.compression_formats['xz'] = (lzma.LZMAFile, 'r') # Django's test suite repeatedly tries to load initial_data fixtures # from apps that don't have any fixtures. Because disabling constraint diff --git a/docs/ref/django-admin.txt b/docs/ref/django-admin.txt index 329cb72215..4cd477b7a8 100644 --- a/docs/ref/django-admin.txt +++ b/docs/ref/django-admin.txt @@ -599,13 +599,14 @@ The :djadmin:`dumpdata` command can be used to generate input for ``loaddata``. Compressed fixtures ~~~~~~~~~~~~~~~~~~~ -Fixtures may be compressed in ``zip``, ``gz``, or ``bz2`` format. For example:: +Fixtures may be compressed in ``zip``, ``gz``, ``bz2``, ``lzma``, or ``xz`` +format. For example:: django-admin loaddata mydata.json -would look for any of ``mydata.json``, ``mydata.json.zip``, -``mydata.json.gz``, or ``mydata.json.bz2``. The first file contained within a -zip-compressed archive is used. +would look for any of ``mydata.json``, ``mydata.json.zip``, ``mydata.json.gz``, +``mydata.json.bz2``, ``mydata.json.lzma``, or ``mydata.json.xz``. The first +file contained within a compressed archive is used. Note that if two fixtures with the same name but different fixture type are discovered (for example, if ``mydata.json`` and @@ -619,6 +620,10 @@ installation will be aborted, and any data installed in the call to constraints, so if you use MyISAM, you won't get validation of fixture data, or a rollback if multiple transaction files are found. +.. versionchanged:: 3.2 + + Support for XZ archives (``.xz``) and LZMA archives (``.lzma``) was added. + Database-specific fixtures ~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/docs/releases/3.2.txt b/docs/releases/3.2.txt index 18b1df2836..4ba20c4c09 100644 --- a/docs/releases/3.2.txt +++ b/docs/releases/3.2.txt @@ -154,7 +154,8 @@ Logging Management Commands ~~~~~~~~~~~~~~~~~~~ -* ... +* :djadmin:`loaddata` now supports fixtures stored in XZ archives (``.xz``) and + LZMA archives (``.lzma``). Migrations ~~~~~~~~~~ diff --git a/tests/fixtures/fixtures/fixture5.json.lzma b/tests/fixtures/fixtures/fixture5.json.lzma new file mode 100644 index 0000000000000000000000000000000000000000..a41fdaa82fa37ca5ddbd06f0dea6ff30bac91315 GIT binary patch literal 157 zcmV;O0Al}L004jh|NsC0|NsC004;)m86hqf@n9wTq?Hr)3s|8+PT!XOLKR?-?82^F z(C6j(+ zAtwkS2yDeC(6O(@U~A&+Hdo;INJgyw8bl)8fR#WYp!~Y~m=kveDfEgh!7V%&u`lOW; z_6t~{K~CS6{X!LBkL<#(ThQ-Dr<3>6bc)Y^)d&sd9ut{p|Jb9E0sL~JKvtDXCcX#G z1DJqetZxO8F}85JeCe1a^C2e)AqZ^6C(yC4#b9gV>^4{6_((>q{u)Fg+<=upA)x%a z`MD~(XYq+IiZm4E=I8q`mP=dG0h0i*%f0RRAkMft|D#Ao{g000001X)_3 CXj#4h literal 0 HcmV?d00001 diff --git a/tests/fixtures/tests.py b/tests/fixtures/tests.py index d46bf65c97..ac96c48734 100644 --- a/tests/fixtures/tests.py +++ b/tests/fixtures/tests.py @@ -27,6 +27,12 @@ try: except ImportError: HAS_BZ2 = False +try: + import lzma # NOQA + HAS_LZMA = True +except ImportError: + HAS_LZMA = False + class TestCaseFixtureLoadingTests(TestCase): fixtures = ['fixture1.json', 'fixture2.json'] @@ -558,6 +564,20 @@ class FixtureLoadingTests(DumpDataAssertMixin, TestCase): '', ]) + @unittest.skipUnless(HAS_LZMA, 'No lzma library detected.') + def test_compressed_loading_lzma(self): + management.call_command('loaddata', 'fixture5.json.lzma', verbosity=0) + self.assertQuerysetEqual(Article.objects.all(), [ + '', + ]) + + @unittest.skipUnless(HAS_LZMA, 'No lzma library detected.') + def test_compressed_loading_xz(self): + management.call_command('loaddata', 'fixture5.json.xz', verbosity=0) + self.assertQuerysetEqual(Article.objects.all(), [ + '', + ]) + def test_ambiguous_compressed_fixture(self): # The name "fixture5" is ambiguous, so loading raises an error. msg = "Multiple fixtures named 'fixture5'"