feat(cli): add final-exam and sync-public commands
This commit is contained in:
5 files changed
+206
-9
No files matched your search
@@ -53,21 +53,23 @@ Sync `public/` to an SSH host without transferring video files. Provide
|
||||
your destination at runtime and preview the changes first:
|
||||
|
||||
```bash
|
||||
./sync-public.sh --dry-run \
|
||||
ravin sync-public --dry-run \
|
||||
--destination 'user@example-host:/path/to/project/public/'
|
||||
```
|
||||
|
||||
Then perform the sync:
|
||||
|
||||
```bash
|
||||
./sync-public.sh \
|
||||
ravin sync-public \
|
||||
--destination 'user@example-host:/path/to/project/public/'
|
||||
```
|
||||
|
||||
You can alternatively set `SYNC_PUBLIC_DESTINATION` in your local shell. The
|
||||
script does not remove remote files by default; pass `--delete` when an exact
|
||||
command does not remove remote files by default; pass `--delete` when an exact
|
||||
mirror of non-video files is required.
|
||||
|
||||
The original `./sync-public.sh` entry point remains available for compatibility.
|
||||
|
||||
`scan` replaces the former separate course-listing, file-listing, and library-generation commands. It reads the LMS structure, reconciles everything already present on disk, and writes the JSON manifests consumed by the static site.
|
||||
|
||||
Scan only selected courses or print machine-readable results:
|
||||
@@ -224,6 +226,12 @@ Start the interactive import wizard:
|
||||
ravin questions
|
||||
```
|
||||
|
||||
For a declared course-level final exam, use the dedicated shortcut:
|
||||
|
||||
```bash
|
||||
ravin final-exam
|
||||
```
|
||||
|
||||
The wizard:
|
||||
|
||||
1. Lists local courses that contain exams.
|
||||
@@ -437,9 +445,11 @@ ravin download COURSE_ID [--overwrite] [--retries N] [--json] [--public PATH]
|
||||
ravin transcribe [COURSE_ID ...] [--model MODEL] [--device DEVICE] [--language LANGUAGE]
|
||||
ravin summarize [COURSE_ID ...] [--model MODEL] [--retries N] [--timeout SECONDS]
|
||||
ravin questions [COURSE_ID] [ACTIVITY_OR_ASSESSMENT_ID] [QUESTIONS.md] [--file ATTACHMENT ...]
|
||||
ravin final-exam [COURSE_ID] [QUESTIONS.md] [--file ATTACHMENT ...]
|
||||
ravin recording [COURSE_ID] [ACTIVITY_ID] [VIDEO]
|
||||
ravin export [--output ARCHIVE.zip] [--include-videos] [--public PATH]
|
||||
ravin import URL [--timeout SECONDS] [--json] [--public PATH]
|
||||
ravin sync-public [--destination USER@HOST:PATH] [--dry-run] [--delete] [--public PATH]
|
||||
ravin serve [--host ADDRESS] [--port PORT] [--open] [--public PATH]
|
||||
```
|
||||
|
||||
|
||||
+60
-6
@@ -26,6 +26,7 @@ from .recordings import format_recording_result, import_recording, recording_wiz
|
||||
from .scan import format_scan, scan_offline, scan_output, scan_remote, update_download_state
|
||||
from .server import _serve_library
|
||||
from .summarize import SummaryOptions, format_summary_result, summarize_courses
|
||||
from .sync_public import sync_public
|
||||
from .transcribe import TranscriptionOptions, format_transcription_result, transcribe_courses
|
||||
|
||||
|
||||
@@ -167,6 +168,32 @@ def build_parser() -> argparse.ArgumentParser:
|
||||
questions_parser.add_argument("--public", type=Path, default=Path("public"), help="public web root")
|
||||
questions_parser.add_argument("--json", action="store_true", help="print imported paths as JSON")
|
||||
|
||||
final_exam_parser = subparsers.add_parser(
|
||||
"final-exam",
|
||||
help="import questions and attachments for a declared final exam",
|
||||
description=(
|
||||
"Import questions and optional attachments for the course assessment named final-exam; "
|
||||
"all positional values are optional."
|
||||
),
|
||||
)
|
||||
final_exam_parser.add_argument("course_id", nargs="?", type=int, help="course containing the final exam")
|
||||
final_exam_parser.add_argument(
|
||||
"questions",
|
||||
nargs="?",
|
||||
type=Path,
|
||||
help="UTF-8 Markdown final-exam questions and answers",
|
||||
)
|
||||
final_exam_parser.add_argument(
|
||||
"--file",
|
||||
dest="files",
|
||||
type=Path,
|
||||
action="append",
|
||||
default=[],
|
||||
help="optional original exam attachment; repeat for multiple files",
|
||||
)
|
||||
final_exam_parser.add_argument("--public", type=Path, default=Path("public"), help="public web root")
|
||||
final_exam_parser.add_argument("--json", action="store_true", help="print imported paths as JSON")
|
||||
|
||||
recording_parser = subparsers.add_parser(
|
||||
"recording",
|
||||
aliases=["recordings"],
|
||||
@@ -212,6 +239,24 @@ def build_parser() -> argparse.ArgumentParser:
|
||||
)
|
||||
import_parser.add_argument("--json", action="store_true", help="print import and scan details as JSON")
|
||||
|
||||
sync_parser = subparsers.add_parser(
|
||||
"sync-public",
|
||||
help="sync the public library to an SSH destination without videos",
|
||||
description="Sync public course data with rsync while excluding current and partial video files.",
|
||||
)
|
||||
sync_parser.add_argument(
|
||||
"-d",
|
||||
"--destination",
|
||||
help="rsync SSH destination (USER@HOST:PATH); defaults to SYNC_PUBLIC_DESTINATION",
|
||||
)
|
||||
sync_parser.add_argument("--public", type=Path, default=Path("public"), help="public web root")
|
||||
sync_parser.add_argument("-n", "--dry-run", action="store_true", help="show changes without transferring files")
|
||||
sync_parser.add_argument(
|
||||
"--delete",
|
||||
action="store_true",
|
||||
help="delete remote non-video files that are missing locally",
|
||||
)
|
||||
|
||||
serve_parser = subparsers.add_parser("serve", help="serve the public learning library locally")
|
||||
serve_parser.add_argument("--public", type=Path, default=Path("public"), help="public web root")
|
||||
serve_parser.add_argument("--host", default="127.0.0.1", help="local bind address")
|
||||
@@ -258,10 +303,18 @@ def main(argv: list[str] | None = None) -> int:
|
||||
_serve_library(args.public, args.host, args.port, args.open)
|
||||
return 0
|
||||
|
||||
if args.command == "sync-public":
|
||||
return sync_public(
|
||||
args.public,
|
||||
args.destination,
|
||||
dry_run=args.dry_run,
|
||||
delete=args.delete,
|
||||
)
|
||||
|
||||
public = getattr(args, "public", Path("public")).expanduser().resolve()
|
||||
if args.command in {
|
||||
"scan", "download", "transcribe", "summarize", "questions", "recording", "recordings", "export",
|
||||
"import",
|
||||
"scan", "download", "transcribe", "summarize", "questions", "final-exam", "recording",
|
||||
"recordings", "export", "import",
|
||||
}:
|
||||
_maybe_migrate(public)
|
||||
|
||||
@@ -353,18 +406,19 @@ def main(argv: list[str] | None = None) -> int:
|
||||
)
|
||||
return result.exit_code
|
||||
|
||||
if args.command == "questions":
|
||||
interactive = args.course_id is None or args.activity_id is None or args.questions is None
|
||||
if args.command in {"questions", "final-exam"}:
|
||||
activity_id = "final-exam" if args.command == "final-exam" else args.activity_id
|
||||
interactive = args.course_id is None or activity_id is None or args.questions is None
|
||||
course_id, activity_id, questions_path, attachment_paths = questions_wizard(
|
||||
public,
|
||||
args.course_id,
|
||||
args.activity_id,
|
||||
activity_id,
|
||||
args.questions,
|
||||
tuple(args.files),
|
||||
prompt_for_attachment=interactive,
|
||||
) if interactive else (
|
||||
args.course_id,
|
||||
args.activity_id,
|
||||
activity_id,
|
||||
args.questions,
|
||||
tuple(args.files),
|
||||
)
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
"""Synchronize the public learning library to an SSH destination."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import shutil
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
|
||||
from .models import MoodleError
|
||||
|
||||
|
||||
VIDEO_PATTERNS = (
|
||||
"*.[mM][pP]4", "*.[mM][pP]4.part",
|
||||
"*.[mM]4[vV]", "*.[mM]4[vV].part",
|
||||
"*.[mM][kK][vV]", "*.[mM][kK][vV].part",
|
||||
"*.[mM][oO][vV]", "*.[mM][oO][vV].part",
|
||||
"*.[aA][vV][iI]", "*.[aA][vV][iI].part",
|
||||
"*.[wW][eE][bB][mM]", "*.[wW][eE][bB][mM].part",
|
||||
"*.[fF][lL][vV]", "*.[fF][lL][vV].part",
|
||||
"*.[wW][mM][vV]", "*.[wW][mM][vV].part",
|
||||
"*.[mM][pP][eE][gG]", "*.[mM][pP][eE][gG].part",
|
||||
"*.[mM][pP][gG]", "*.[mM][pP][gG].part",
|
||||
"*.[3][gG][pP]", "*.[3][gG][pP].part",
|
||||
"*.[oO][gG][vV]", "*.[oO][gG][vV].part",
|
||||
)
|
||||
|
||||
|
||||
def sync_public(
|
||||
public: Path,
|
||||
destination: str | None,
|
||||
*,
|
||||
dry_run: bool = False,
|
||||
delete: bool = False,
|
||||
) -> int:
|
||||
"""Run rsync for non-video public files and return its exit code."""
|
||||
resolved_destination = destination or os.getenv("SYNC_PUBLIC_DESTINATION")
|
||||
if not resolved_destination:
|
||||
raise MoodleError(
|
||||
"no sync destination provided; use --destination USER@HOST:PATH "
|
||||
"or set SYNC_PUBLIC_DESTINATION"
|
||||
)
|
||||
source = public.expanduser().resolve()
|
||||
if not source.is_dir():
|
||||
raise MoodleError(f"public web root does not exist: {source}")
|
||||
rsync = shutil.which("rsync")
|
||||
if rsync is None:
|
||||
raise MoodleError("rsync is not installed locally")
|
||||
|
||||
command = [
|
||||
rsync,
|
||||
"--archive",
|
||||
"--compress",
|
||||
"--human-readable",
|
||||
"--partial",
|
||||
"--progress",
|
||||
"--itemize-changes",
|
||||
]
|
||||
command.extend(f"--exclude={pattern}" for pattern in VIDEO_PATTERNS)
|
||||
if dry_run:
|
||||
command.append("--dry-run")
|
||||
if delete:
|
||||
command.append("--delete")
|
||||
command.extend(("--", f"{source}/", resolved_destination))
|
||||
|
||||
print(f"Syncing {source}/ to {resolved_destination}")
|
||||
if dry_run:
|
||||
print("Dry run: no files will be changed.")
|
||||
if delete:
|
||||
print("Mirror mode: stale remote non-video files will be deleted.")
|
||||
return subprocess.run(command, check=False).returncode
|
||||
@@ -111,6 +111,12 @@ class ParserTests(unittest.TestCase):
|
||||
self.assertIsNone(questions_wizard.course_id)
|
||||
self.assertIsNone(questions_wizard.activity_id)
|
||||
self.assertIsNone(questions_wizard.questions)
|
||||
final_exam = build_parser().parse_args(
|
||||
["final-exam", "44", "final-exam.md", "--file", "final-exam.pdf"]
|
||||
)
|
||||
self.assertEqual(final_exam.course_id, 44)
|
||||
self.assertEqual(final_exam.questions, Path("final-exam.md"))
|
||||
self.assertEqual(final_exam.files, [Path("final-exam.pdf")])
|
||||
recording = build_parser().parse_args(["recording", "44", "5097", "class.mp4"])
|
||||
self.assertEqual(recording.course_id, 44)
|
||||
self.assertEqual(recording.activity_id, 5097)
|
||||
@@ -132,6 +138,12 @@ class ParserTests(unittest.TestCase):
|
||||
)
|
||||
self.assertEqual(import_archive.url, "https://mirror.example/exports/latest.zip")
|
||||
self.assertEqual(import_archive.timeout, 120)
|
||||
sync_public = build_parser().parse_args(
|
||||
["sync-public", "--destination", "user@example:/srv/public/", "--dry-run", "--delete"]
|
||||
)
|
||||
self.assertEqual(sync_public.destination, "user@example:/srv/public/")
|
||||
self.assertTrue(sync_public.dry_run)
|
||||
self.assertTrue(sync_public.delete)
|
||||
serve = build_parser().parse_args(["serve", "--port", "9000"])
|
||||
self.assertEqual(serve.port, 9000)
|
||||
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src"))
|
||||
|
||||
from ravin.models import MoodleError
|
||||
from ravin.sync_public import VIDEO_PATTERNS, sync_public
|
||||
|
||||
|
||||
class SyncPublicTests(unittest.TestCase):
|
||||
def test_builds_video_excluding_rsync_command(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
public = Path(directory) / "public"
|
||||
public.mkdir()
|
||||
completed = subprocess.CompletedProcess([], 0)
|
||||
with (
|
||||
patch("ravin.sync_public.shutil.which", return_value="/usr/bin/rsync"),
|
||||
patch("ravin.sync_public.subprocess.run", return_value=completed) as run,
|
||||
):
|
||||
exit_code = sync_public(
|
||||
public,
|
||||
"user@example:/srv/public/",
|
||||
dry_run=True,
|
||||
delete=True,
|
||||
)
|
||||
|
||||
self.assertEqual(exit_code, 0)
|
||||
command = run.call_args.args[0]
|
||||
self.assertIn("--dry-run", command)
|
||||
self.assertIn("--delete", command)
|
||||
self.assertEqual(
|
||||
[argument for argument in command if argument.startswith("--exclude=")],
|
||||
[f"--exclude={pattern}" for pattern in VIDEO_PATTERNS],
|
||||
)
|
||||
self.assertEqual(command[-3:], ["--", f"{public.resolve()}/", "user@example:/srv/public/"])
|
||||
|
||||
def test_requires_a_destination(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as directory, patch.dict("os.environ", {}, clear=True):
|
||||
with self.assertRaisesRegex(MoodleError, "no sync destination"):
|
||||
sync_public(Path(directory), None)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user