export.py 1.31 KB
Newer Older
David Baumgold committed
1 2 3
"""
Script for exporting courseware from Mongo to a tar.gz file
"""
Chris Dodge committed
4 5 6 7 8
import os

from django.core.management.base import BaseCommand, CommandError
from xmodule.modulestore.xml_exporter import export_to_xml
from xmodule.modulestore.django import modulestore
9
from opaque_keys.edx.keys import CourseKey
Chris Dodge committed
10
from xmodule.contentstore.django import contentstore
11
from opaque_keys import InvalidKeyError
12
from opaque_keys.edx.locations import SlashSeparatedCourseKey
Chris Dodge committed
13 14 15


class Command(BaseCommand):
David Baumgold committed
16 17 18
    """
    Export the specified data directory into the default ModuleStore
    """
19
    help = 'Export the specified data directory into the default ModuleStore'
Chris Dodge committed
20 21

    def handle(self, *args, **options):
David Baumgold committed
22
        "Execute the command"
Chris Dodge committed
23
        if len(args) != 2:
24
            raise CommandError("export requires two arguments: <course id> <output path>")
Chris Dodge committed
25

26 27 28 29 30
        try:
            course_key = CourseKey.from_string(args[0])
        except InvalidKeyError:
            course_key = SlashSeparatedCourseKey.from_deprecated_string(args[0])

Chris Dodge committed
31 32
        output_path = args[1]

33
        print("Exporting course id = {0} to {1}".format(course_key, output_path))
Chris Dodge committed
34 35 36 37

        root_dir = os.path.dirname(output_path)
        course_dir = os.path.splitext(os.path.basename(output_path))[0]

38
        export_to_xml(modulestore(), contentstore(), course_key, root_dir, course_dir)