Skip to content

Commit

Permalink
Address review comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
mtyaka committed Sep 25, 2018
1 parent 5d59e2d commit 7540902
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""
from __future__ import print_function
import os
import shutil

from django.core.management.base import BaseCommand, CommandError
from opaque_keys import InvalidKeyError
Expand Down Expand Up @@ -31,10 +32,9 @@ def handle(self, *args, **options):
module_store = modulestore()
try:
library_key = CourseKey.from_string(options['library_id'])
assert isinstance(library_key, LibraryLocator)
except InvalidKeyError:
raise CommandError(u'Invalid library ID: "{0}".'.format(options['library_id']))
except AssertionError:
if not isinstance(library_key, LibraryLocator):
raise CommandError(u'Argument "{0}" is not a library key'.format(options['library_id']))

library = module_store.get_library(library_key)
Expand All @@ -51,14 +51,15 @@ def handle(self, *args, **options):
except Exception as e:
raise CommandError(u'Failed to export "{0}" with "{1}"'.format(library_key, e))
else:
# Save generated archive with keyed filename
prefix, suffix, n = str(library_key).replace(':', '+'), '.tar.gz', 0
while os.path.exists(prefix + suffix):
n += 1
prefix = u'{0}_{1}'.format(prefix.rsplit('_', 1)[0], n) if n > 1 else u'{}_1'.format(prefix)
filename = prefix + suffix
target = os.path.join(dest_path, filename)
tarball.file.seek(0)
with open(target, 'w') as f:
f.write(tarball.file.read())
print(u'Library "{0}" exported to "{1}"'.format(library.location.library_key, target))
with tarball:
# Save generated archive with keyed filename
prefix, suffix, n = str(library_key).replace(':', '+'), '.tar.gz', 0
while os.path.exists(prefix + suffix):
n += 1
prefix = u'{0}_{1}'.format(prefix.rsplit('_', 1)[0], n) if n > 1 else u'{}_1'.format(prefix)
filename = prefix + suffix
target = os.path.join(dest_path, filename)
tarball.file.seek(0)
with open(target, 'w') as f:
shutil.copyfileobj(tarball.file, f)
print(u'Library "{0}" exported to "{1}"'.format(library.location.library_key, target))
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from django.core.exceptions import SuspiciousOperation
from django.core.management.base import BaseCommand, CommandError
from lxml import etree
from opaque_keys.edx.keys import CourseKey
from opaque_keys.edx.locator import LibraryLocator
from path import Path
from xmodule.contentstore.django import contentstore
from xmodule.modulestore import ModuleStoreEnum
Expand Down Expand Up @@ -61,7 +61,9 @@ def handle(self, *args, **options):

# Gather library metadata from XML file
xml_root = etree.parse(abs_xml_path / 'library.xml').getroot()
assert xml_root.tag == 'library'
if xml_root.tag != 'library':
raise CommandError(u'Failed to import {0}: Not a library archive'.format(archive_path))

metadata = xml_root.attrib
org = metadata['org']
library = metadata['library']
Expand All @@ -73,8 +75,9 @@ def handle(self, *args, **options):

# Check if data would be overwritten
ans = ''
while not created and ans.lower() not in ['y', 'yes', 'n', 'no']:
ans = raw_input(u'Library "{0}" already exists, overwrite it? [y/n] '.format(courselike_key))
while not created and ans not in ['y', 'yes', 'n', 'no']:
inp = raw_input(u'Library "{0}" already exists, overwrite it? [y/n] '.format(courselike_key))
ans = inp.lower()
if ans.startswith('n'):
print(u'Aborting import of "{0}"'.format(courselike_key))
return
Expand All @@ -88,9 +91,9 @@ def handle(self, *args, **options):
static_content_store=contentstore(),
target_id=courselike_key
)
except Exception as e:
except Exception:
print(u'\n=== Failed to import library-v1:{0}+{1}'.format(org, library))
raise e
raise

print(u'Library "{0}" imported to "{1}"'.format(archive_path, courselike_key))

Expand All @@ -116,4 +119,4 @@ def _get_or_create_library(org, number, display_name, user):
return library.location.library_key, True
except DuplicateCourseError:
# Course exists, return its key
return CourseKey.from_string(u'library-v1:{0}+{1}'.format(org, number)), False
return LibraryLocator(org=org, library=number), False

0 comments on commit 7540902

Please sign in to comment.