|
| 1 | +import click |
| 2 | +import elsa |
| 3 | + |
| 4 | +from naucse.utils.routes import forks_enabled, does_course_return_info |
| 5 | + |
| 6 | + |
| 7 | +def cli(app, *, base_url=None, freezer=None): |
| 8 | + """ Extends the elsa command line interface with a new command which prints all courses and runs |
| 9 | + which are present with basic info about them. |
| 10 | + """ |
| 11 | + elsa_group = elsa.cli(app, base_url=base_url, freezer=freezer, invoke_cli=False) |
| 12 | + |
| 13 | + @click.group() |
| 14 | + def naucse(): |
| 15 | + pass |
| 16 | + |
| 17 | + @naucse.command() |
| 18 | + @click.option("--forks-only", default=False, is_flag=True, |
| 19 | + help="Only list courses and runs from forks") |
| 20 | + def list_courses(forks_only): |
| 21 | + """ List all courses and runs and info about them. |
| 22 | +
|
| 23 | + Mainly useful for courses from forks, shows where do they sourced from and if |
| 24 | + they return even the most basic information and will therefore be included in |
| 25 | + list of courses/runs. |
| 26 | +
|
| 27 | + A practical benefit is that on Travis CI the docker images are pulled/built |
| 28 | + in this command and the freezing won't timeout after the 10 minute limit if things are taking particularly long. |
| 29 | + """ |
| 30 | + from naucse.routes import model |
| 31 | + |
| 32 | + def canonical(course, suffix=""): |
| 33 | + click.echo(f" {course.slug}: {course.title}{suffix}") |
| 34 | + |
| 35 | + def fork_invalid(course): |
| 36 | + click.echo(f" {course.slug}, from {course.repo}@{course.branch}: " |
| 37 | + f"Fork doesn't return basic info, will be ignored.") |
| 38 | + |
| 39 | + def fork_valid(course, suffix=""): |
| 40 | + click.echo(f" {course.slug}, from {course.repo}@{course.branch}: {course.title}{suffix}") |
| 41 | + |
| 42 | + click.echo(f"Courses:") |
| 43 | + |
| 44 | + for course in model.courses.values(): |
| 45 | + if forks_only and not course.is_link(): |
| 46 | + continue |
| 47 | + |
| 48 | + if not course.is_link(): |
| 49 | + canonical(course) |
| 50 | + elif forks_enabled(): |
| 51 | + if does_course_return_info(course, force_ignore=True): |
| 52 | + fork_valid(course) |
| 53 | + else: |
| 54 | + fork_invalid(course) |
| 55 | + |
| 56 | + click.echo(f"Runs:") |
| 57 | + |
| 58 | + for course in model.runs.values(): |
| 59 | + if forks_only and not course.is_link(): |
| 60 | + continue |
| 61 | + |
| 62 | + if not course.is_link(): |
| 63 | + canonical(course, suffix=f" ({course.start_date} - {course.end_date})") |
| 64 | + elif forks_enabled(): |
| 65 | + if does_course_return_info(course, ["start_date", "end_date"], force_ignore=True): |
| 66 | + fork_valid(course, suffix=f" ({course.start_date} - {course.end_date})") |
| 67 | + else: |
| 68 | + fork_invalid(course) |
| 69 | + |
| 70 | + cli = click.CommandCollection(sources=[naucse, elsa_group]) |
| 71 | + |
| 72 | + return cli() |
0 commit comments