Skip to content

Commit

Permalink
Merge pull request #12059 from thesujai/manifest-only
Browse files Browse the repository at this point in the history
Add --manifest-only option to exportcontent command
  • Loading branch information
rtibbles authored Apr 12, 2024
2 parents 421626f + 42be9a0 commit 9d8ea58
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 16 deletions.
39 changes: 23 additions & 16 deletions kolibri/core/content/management/commands/exportcontent.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ def add_arguments(self, parser):

parser.add_argument("channel_id", type=str)
parser.add_argument("destination", type=str)
parser.add_argument(
"--manifest-only",
action="store_true",
default=False,
help="Generate only the manifest.json file",
)

def update_job_metadata(self, total_bytes_to_transfer, total_resource_count):
job = get_current_job()
Expand All @@ -75,9 +81,6 @@ def handle_async(self, *args, **options):
data_dir = os.path.realpath(options["destination"])
node_ids = options["node_ids"]
exclude_node_ids = options["exclude_node_ids"]
logger.info(
"Exporting content for channel id {} to {}".format(channel_id, data_dir)
)

channel_metadata = ChannelMetadata.objects.get(id=channel_id)

Expand All @@ -91,19 +94,11 @@ def handle_async(self, *args, **options):

self.update_job_metadata(total_bytes_to_transfer, total_resource_count)

exported_files = []

with self.start_progress(
total=total_bytes_to_transfer
) as overall_progress_update:
for f in files:

if self.is_cancelled():
break

dest = self.export_file(f, data_dir, overall_progress_update)
if dest:
exported_files.append(dest)
# dont copy files if we are only exporting the manifest
if not options["manifest_only"]:
self.copy_content_files(
channel_id, data_dir, files, total_bytes_to_transfer
)

# Reraise any cancellation
self.check_for_cancel()
Expand All @@ -120,6 +115,18 @@ def handle_async(self, *args, **options):
)
content_manifest.write(manifest_path)

def copy_content_files(self, channel_id, data_dir, files, total_bytes_to_transfer):
logger.info(
"Exporting content for channel id {} to {}".format(channel_id, data_dir)
)
with self.start_progress(
total=total_bytes_to_transfer
) as overall_progress_update:
for f in files:
if self.is_cancelled():
break
self.export_file(f, data_dir, overall_progress_update)

def export_file(self, f, data_dir, overall_progress_update):
filename = get_content_file_name(f)
try:
Expand Down
27 changes: 27 additions & 0 deletions kolibri/core/content/test/test_import_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -2254,6 +2254,33 @@ def test_local_cancel_during_transfer(
)
cancel_mock.assert_called_with()

@patch(
"kolibri.core.content.management.commands.exportcontent.Command.copy_content_files"
)
def test_manifest_only(
self,
copy_content_files_mock,
ContentManifestMock,
get_content_nodes_data_mock,
get_import_export_nodes_mock,
):
get_content_nodes_data_mock.return_value = (
1,
[LocalFile.objects.values("id", "file_size", "extension").first()],
10,
)

# run with manifest-only option
call_command(
"exportcontent", self.the_channel_id, tempfile.mkdtemp(), manifest_only=True
)

copy_content_files_mock.assert_not_called()

ContentManifestMock.return_value.write.assert_called_once()

# Shall be enough mock assertions for now ?


class TestFilesToTransfer(TestCase):

Expand Down

0 comments on commit 9d8ea58

Please sign in to comment.