import os import shutil import sys from pathlib import Path from honcho import manager from invoke import Responder, task from ucast import tests from ucast.service import cover, util, youtube os.chdir(Path(__file__).absolute().parent) DIR_RUN = Path("_run").absolute() DIR_STATIC = DIR_RUN / "static" DIR_DOWNLOAD = DIR_RUN / "data" FILE_DB = DIR_RUN / "db.sqlite" @task def test(c): """Run unit tests""" c.run("pytest", pty=True) @task def lint(c): """Check for code quality and formatting""" c.run("pre-commit run -a", pty=True) @task def format(c): """Format the code with black""" c.run("pre-commit run black -a", pty=True) @task def makemigrations(c): """Create a new migration that applies the changes made to the data model""" c.run("python manage.py makemigrations ucast") @task def collectstatic(c): """Copy static files into a common folder""" c.run("python manage.py collectstatic --noinput") @task def migrate(c): """Migrate the database""" c.run("python manage.py migrate") @task def create_testuser(c): """Create a test user with the credentials admin:pass""" responder_pwd = Responder(pattern=r"Password.*: ", response="pass\n") responder_yes = Responder(pattern=r"Bypass password validation", response="y\n") c.run( "python manage.py createsuperuser --username admin --email admin@example.com", pty=True, watchers=[responder_pwd, responder_yes], ) @task def get_cover(c, vid=""): """ Download thumbnail image of the YouTube video with the id from the ``--vid`` parameter and create cover images from it. The images are stored in the ``ucast/tests/testfiles`` directory. """ vinfo = youtube.get_video_details(vid) title = vinfo.title channel_name = vinfo.channel_name channel_id = vinfo.channel_id channel_metadata = youtube.get_channel_metadata( youtube.channel_url_from_id(channel_id) ) ti = 1 while os.path.exists(tests.DIR_TESTFILES / "avatar" / f"a{ti}.jpg"): ti += 1 tn_file = tests.DIR_TESTFILES / "thumbnail" / f"t{ti}.webp" av_file = tests.DIR_TESTFILES / "avatar" / f"a{ti}.jpg" cv_file = tests.DIR_TESTFILES / "cover" / f"c{ti}_gradient.png" cv_blur_file = tests.DIR_TESTFILES / "cover" / f"c{ti}_blur.png" youtube.download_thumbnail(vinfo, tn_file) util.download_image_file(channel_metadata.avatar_url, av_file) cover.create_cover_file( tn_file, av_file, title, channel_name, cover.COVER_STYLE_GRADIENT, cv_file ) cover.create_cover_file( tn_file, av_file, title, channel_name, cover.COVER_STYLE_BLUR, cv_blur_file ) @task def build_devcontainer(c): c.run( "docker buildx build -t thetadev256/ucast-dev --push --platform amd64,arm64,armhf -f deploy/Devcontainer.Dockerfile deploy" ) @task def reset(c): if DIR_DOWNLOAD.exists(): shutil.rmtree(DIR_DOWNLOAD) if FILE_DB.exists(): os.remove(FILE_DB) os.makedirs(DIR_DOWNLOAD, exist_ok=True) migrate(c) create_testuser(c) collectstatic(c) @task def worker(c, n=2): m = manager.Manager() for i in range(n): m.add_process(f"worker_{i}", "python manage.py rqworker") m.add_process("scheduler", "python manage.py rqscheduler") m.loop() sys.exit(m.returncode) @task def optimize_svg(c): out_dir = Path("ucast/static/ucast") for icon in (Path("assets/icons/logo.svg"), Path("assets/icons/logo_dark.svg")): c.run( f"scour --indent=none --no-line-breaks --enable-comment-stripping {icon} {out_dir / icon.name}" ) @task def build_sass(c): c.run("npm run build") collectstatic(c)