Commit b797e34d by Carlos Andrés Rocha

Allow the export_course command to dump results to stdout

parent 7d76f360
""" """
A Django command that exports a course to a tar.gz file. A Django command that exports a course to a tar.gz file.
If <filename> is '-', it pipes the file to stdout
""" """
import os
import shutil import shutil
import tarfile import tarfile
from tempfile import mkdtemp from tempfile import mktemp, mkdtemp
from textwrap import dedent from textwrap import dedent
from path import path from path import path
...@@ -25,13 +29,37 @@ class Command(BaseCommand): ...@@ -25,13 +29,37 @@ class Command(BaseCommand):
help = dedent(__doc__).strip() help = dedent(__doc__).strip()
def handle(self, *args, **options): def handle(self, *args, **options):
course_id, filename, pipe_results = self._parse_arguments(args)
export_course_to_tarfile(course_id, filename)
results = self._get_results(filename) if pipe_results else None
return results
def _parse_arguments(self, args):
"""Parse command line arguments"""
try: try:
course_id = args[0] course_id = args[0]
filename = args[1] filename = args[1]
except IndexError: except IndexError:
raise CommandError("Insufficient arguments") raise CommandError("Insufficient arguments")
export_course_to_tarfile(course_id, filename) # If filename is '-' save to a temp file
pipe_results = False
if filename == '-':
filename = mktemp()
pipe_results = True
return course_id, filename, pipe_results
def _get_results(self, filename):
"""Load results from file"""
results = None
with open(filename) as f:
results = f.read()
os.remove(filename)
return results
def export_course_to_tarfile(course_id, filename): def export_course_to_tarfile(course_id, filename):
......
...@@ -96,10 +96,15 @@ class CommandsTestBase(object): ...@@ -96,10 +96,15 @@ class CommandsTestBase(object):
finally: finally:
shutil.rmtree(tmp_dir) shutil.rmtree(tmp_dir)
def test_export_course_stdout(self):
output = self.run_export_course('-')
with tarfile.open(fileobj=StringIO(output)) as tar_file:
self.check_export_file(tar_file)
def run_export_course(self, filename): # pylint: disable=missing-docstring def run_export_course(self, filename): # pylint: disable=missing-docstring
args = ['edX/simple/2012_Fall', filename] args = ['edX/simple/2012_Fall', filename]
kwargs = {'modulestore': 'default'} kwargs = {'modulestore': 'default'}
self.call_command('export_course', *args, **kwargs) return self.call_command('export_course', *args, **kwargs)
def check_export_file(self, tar_file): # pylint: disable=missing-docstring def check_export_file(self, tar_file): # pylint: disable=missing-docstring
names = tar_file.getnames() names = tar_file.getnames()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment