From 4a30c4ab35cb8915f33dc53d476e2dc728e4cc6a Mon Sep 17 00:00:00 2001 From: "Kyle D. McCormick" Date: Wed, 10 Jan 2024 11:46:29 -0500 Subject: [PATCH] feat: enhance importdemocourse ; add importdemolibraries --- ...0240110_101228_kyle_importnewdemocourse.md | 7 ++ tests/commands/test_jobs.py | 27 ++++++- tutor/commands/jobs.py | 76 ++++++++++++++++++- 3 files changed, 105 insertions(+), 5 deletions(-) create mode 100644 changelog.d/20240110_101228_kyle_importnewdemocourse.md diff --git a/changelog.d/20240110_101228_kyle_importnewdemocourse.md b/changelog.d/20240110_101228_kyle_importnewdemocourse.md new file mode 100644 index 0000000000..79a7d44b70 --- /dev/null +++ b/changelog.d/20240110_101228_kyle_importnewdemocourse.md @@ -0,0 +1,7 @@ +- [Feature] Several enhancements to the Demo Course (by @kdmccormick): + - The [Open edX Demo Course](https://github.com/openedx/openedx-demo-course) has been re-built from scratch with up-to-date instruction-focused content. Its directory structure has changed. + - In order to support both the old and new structures of the Demo Course's repository, the command `tutor local do importdemocourse` will now auto-determine the course root based on the location of `course.xml`. Use the `--repo-dir` argument to override this behavior. + - The new command `tutor local do importdemolibraries` will import any content libraries defined within the Demo Course repository. At the moment, that is just the "Respiratory System Question Bank", which is an optional but helpful extension to the new Demo Course. + - To try out the new Demo Course now, run: `tutor local do importdemocourse --version master`. + - To try out the demo Respiratory System Question Bank now, run: `tutor local do importdemolibraries --version master`. + - To revert back to an older Demo Course version at any point, run: `tutor local do importdemocourse --version open-release/quince.2`, replacing `quince.2` with your preferred course version. diff --git a/tests/commands/test_jobs.py b/tests/commands/test_jobs.py index 793931e0db..2ab388773b 100644 --- a/tests/commands/test_jobs.py +++ b/tests/commands/test_jobs.py @@ -38,7 +38,32 @@ def test_import_demo_course(self) -> None: self.assertEqual(0, result.exit_code) self.assertIn("cms-job", dc_args) self.assertIn( - "git clone https://github.com/openedx/edx-demo-course", dc_args[-1] + "git clone https://github.com/openedx/openedx-demo-course", dc_args[-1] + ) + + def test_import_demo_libraries(self) -> None: + with temporary_root() as root: + self.invoke_in_root(root, ["config", "save"]) + with patch("tutor.utils.docker_compose") as mock_docker_compose: + result = self.invoke_in_root( + root, + [ + "local", + "do", + "importdemolibraries", + "admin", + ], + ) + dc_args, _dc_kwargs = mock_docker_compose.call_args + self.assertIsNone(result.exception) + self.assertEqual(0, result.exit_code) + self.assertIn("cms-job", dc_args) + self.assertIn( + "git clone https://github.com/openedx/openedx-demo-course", dc_args[-1] + ) + self.assertIn( + "./manage.py cms import_content_library /tmp/library.tar.gz admin", + dc_args[-1], ) def test_set_theme(self) -> None: diff --git a/tutor/commands/jobs.py b/tutor/commands/jobs.py index 562e78d454..f6795fefcd 100644 --- a/tutor/commands/jobs.py +++ b/tutor/commands/jobs.py @@ -124,7 +124,7 @@ def create_user_template( @click.option( "-r", "--repo", - default="https://github.com/openedx/edx-demo-course", + default="https://github.com/openedx/openedx-demo-course", show_default=True, help="Git repository that contains the course to be imported", ) @@ -133,7 +133,7 @@ def create_user_template( "--repo-dir", default="", show_default=True, - help="Git relative subdirectory to import data from", + help="Git relative subdirectory to import data from. If unspecified, will default to the directory containing course.xml", ) @click.option( "-v", @@ -145,15 +145,82 @@ def importdemocourse( ) -> t.Iterable[tuple[str, str]]: version = version or "{{ OPENEDX_COMMON_VERSION }}" template = f""" -# Import demo course +# Clone the repo git clone {repo} --branch {version} --depth 1 /tmp/course -python ./manage.py cms import ../data /tmp/course/{repo_dir} + +# Determine root directory for course import. If one is provided, use that. +# Otherwise, use the directory containing course.xml, failing if there isn't exactly one. +if [ -n "{repo_dir}" ] ; then + course_root=/tmp/course/{repo_dir} +else + course_xml_first="$(find /tmp/course -name course.xml | head -n 1)" + course_xml_extra="$(find /tmp/course -name course.xml | tail -n +2)" + echo "INFO: Found course.xml files(s): $course_xml_first $course_xml_extra" + if [ -z "$course_xml_first" ] ; then + echo "ERROR: Could not find course.xml. Are you sure this is the right repository?" + exit 1 + fi + if [ -n "$course_xml_extra" ] ; then + echo "ERROR: Found multiple course.xml files--course root is ambiguous!" + echo " Please specify a course root dir (relative to repo root) using --repo-dir." + exit 1 + fi + course_root="$(dirname "$course_xml_first")" +fi +echo "INFO: Will import course data at: $course_root" && echo + +# Import into CMS +python ./manage.py cms import ../data "$course_root" # Re-index courses ./manage.py cms reindex_course --all --setup""" yield ("cms", template) +@click.command(help="Import the demo content libraries") +@click.argument("owner_username") +@click.option( + "-r", + "--repo", + default="https://github.com/openedx/openedx-demo-course", + show_default=True, + help="Git repository that contains the library/libraries to be imported", +) +@click.option( + "-v", + "--version", + help="Git branch, tag or sha1 identifier. If unspecified, will default to the value of the OPENEDX_COMMON_VERSION setting.", +) +def importdemolibraries( + owner_username: str, repo: str, version: t.Optional[str] +) -> t.Iterable[tuple[str, str]]: + version = version or "{{ OPENEDX_COMMON_VERSION }}" + template = f""" +# Clone the repo +git clone {repo} --branch {version} --depth 1 /tmp/library + +# Fail loudly if: +# * there no library.xml files, or +# * any library.xml is not within a directory named "library/" (upstream edx-platform expectation). +if ! find /tmp/library -name library.xml | grep -q "." ; then + echo "ERROR: No library.xml files found in repository. Are you sure this is the right repository and version?" + exit 1 +fi + +# For every library.xml file, create a tar of its parent directory, and import into CMS. +for lib_root in $(find /tmp/library -name library.xml | xargs dirname) ; do + echo "INFO: Will import library at $lib_root" + if [ "$(basename "$lib_root")" != "library" ] ; then + echo "ERROR: can only import library.xml files that are within a directory named 'library'" + exit 1 + fi + rm -rf /tmp/library.tar.gz + ( cd "$(dirname "$lib_root")" && tar czvf /tmp/library.tar.gz library ) + yes | ./manage.py cms import_content_library /tmp/library.tar.gz {owner_username} +done""" + yield ("cms", template) + + @click.command( name="print-edx-platform-setting", help="Print the value of an edx-platform Django setting.", @@ -324,6 +391,7 @@ def do_callback(service_commands: t.Iterable[tuple[str, str]]) -> None: [ createuser, importdemocourse, + importdemolibraries, initialise, print_edx_platform_setting, settheme,