From 1497e3e679dbd437c66823eb3badb26407ab5558 Mon Sep 17 00:00:00 2001 From: Peder Bergebakken Sundt Date: Sat, 27 Aug 2022 22:27:55 +0200 Subject: [PATCH] brrr --- .envrc | 15 +++++++- README.md | 2 +- flake.lock | 92 ++++++++++++++++++++++++++++++++++++++++++++ flake.nix | 55 ++++++++++++++++++++++++++ grzegorz/__init__.py | 9 ++++- grzegorz/mpv.py | 5 ++- 6 files changed, 172 insertions(+), 6 deletions(-) create mode 100644 flake.lock create mode 100644 flake.nix diff --git a/.envrc b/.envrc index 2ca12fb..b6c6c0e 100644 --- a/.envrc +++ b/.envrc @@ -2,7 +2,20 @@ # This file is loaded with `direnv`. # It enters you into the poetry venv, removing the need for `poetry run`. -poetry run true # create venv if it doesn't exist +if command -v nix >/dev/null; then + use nix -p poetry +fi + +# Instead of using the flake, we use poetry to manage a development venv +# We only use poetry2nix for deployment + +# create venv if it doesn't exist +poetry run true +# enter venv export VIRTUAL_ENV=$(poetry env info --path) export POETRY_ACTIVE=1 PATH_add "$VIRTUAL_ENV/bin" + +if ! command -v sanic >/dev/null; then + poetry install +fi diff --git a/README.md b/README.md index 04c7f4b..87dd286 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ The API is described and can be tested at `http:/localhost:8080/swagger` while t Gregorz manages a MPV process, meaning you need to have MPV installed on your system. Look for it in your package manager. sudo pip install git+https://github.com/Programvareverkstedet/grzegorz#master - sanic grzegorz.app --host :: --port 80 --fast + sanic grzegorz.app --host :: --port 8080 Details are over [here](https://sanic.dev/en/guide/deployment/running.html#running-via-command). diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..57c9216 --- /dev/null +++ b/flake.lock @@ -0,0 +1,92 @@ +{ + "nodes": { + "flake-utils": { + "locked": { + "lastModified": 1659877975, + "narHash": "sha256-zllb8aq3YO3h8B/U0/J1WBgAL8EX5yWf5pMj3G0NAmc=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "c0e246b9b83f637f4681389ecabcb2681b4f3af0", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "flake-utils_2": { + "locked": { + "lastModified": 1659877975, + "narHash": "sha256-zllb8aq3YO3h8B/U0/J1WBgAL8EX5yWf5pMj3G0NAmc=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "c0e246b9b83f637f4681389ecabcb2681b4f3af0", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1661621241, + "narHash": "sha256-qUsCgbZNM9b8yOV2XWB//0/qUduDAnbB9WFXr8XzWB0=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "f214afa5fb6452900e27776735db21f5092261b8", + "type": "github" + }, + "original": { + "owner": "NixOS", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs_2": { + "locked": { + "lastModified": 1661621241, + "narHash": "sha256-qUsCgbZNM9b8yOV2XWB//0/qUduDAnbB9WFXr8XzWB0=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "f214afa5fb6452900e27776735db21f5092261b8", + "type": "github" + }, + "original": { + "owner": "NixOS", + "repo": "nixpkgs", + "type": "github" + } + }, + "poetry2nix": { + "inputs": { + "flake-utils": "flake-utils_2", + "nixpkgs": "nixpkgs_2" + }, + "locked": { + "lastModified": 1661298951, + "narHash": "sha256-R4zRiXIYic14aNJGuWvBZBblyt6I0GNzgBEQAXhchf4=", + "owner": "nix-community", + "repo": "poetry2nix", + "rev": "e1e95bbf61be2d150613f5e0560d70c4b316ba4a", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "poetry2nix", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs", + "poetry2nix": "poetry2nix" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..18b1938 --- /dev/null +++ b/flake.nix @@ -0,0 +1,55 @@ +{ + description = "A REST API for managing a MPV instance over via a RPC socket"; + + inputs.flake-utils.url = "github:numtide/flake-utils"; + inputs.nixpkgs.url = "github:NixOS/nixpkgs"; + inputs.poetry2nix.url = "github:nix-community/poetry2nix"; + + outputs = { self, nixpkgs, flake-utils, poetry2nix }: + { + # Nixpkgs overlay providing the application + overlay = nixpkgs.lib.composeManyExtensions [ + poetry2nix.overlay + (final: prev: { + # The application + grzegorz = prev.poetry2nix.mkPoetryApplication { + projectDir = ./.; + }; + }) + ]; + } // (flake-utils.lib.eachDefaultSystem (system: + let + pkgs = import nixpkgs { + inherit system; + overlays = [ self.overlay ]; + }; + entrypoint = pkgs.writeShellApplication { + name = "grzegorz-run"; + runtimeInputs = [ + pkgs.grzegorz + pkgs.mpv + ]; + text = '' + SPIS_MEG=() + if test -z "$*"; then + >&2 echo "DEBUG: No args provided, running with defaults...." + SPIS_MEG=("--host" "::" "--port" "8080") + fi + ( + set -x + sanic grzegorz.app "''${SPIS_MEG[@]}" "$@" + ) + ''; + }; + in { + packages = { + inherit (pkgs) grzegorz; + grzegorz-run = entrypoint; + }; + + apps = { + default.type = "app"; + default.program = "${entrypoint}/bin/grzegorz-run"; + }; + })); +} diff --git a/grzegorz/__init__.py b/grzegorz/__init__.py index faa4d5c..8c6f8c5 100644 --- a/grzegorz/__init__.py +++ b/grzegorz/__init__.py @@ -1,6 +1,7 @@ from sanic import Sanic from sanic_openapi import swagger_blueprint#, openapi2_blueprint from pathlib import Path +import traceback from . import mpv from . import api @@ -31,18 +32,22 @@ app.blueprint(swagger_blueprint) # mpv: -app.config["mpv_control"] = mpv.MPVControl() async def runMPVControl(): + app.config["mpv_control"] = mpv.MPVControl() try: await app.config["mpv_control"].run() + except: + traceback.print_exc() finally: - app.stop() + pass #app.stop() app.add_task(runMPVControl()) # populate playlist async def ensure_splash(): here = Path(__file__).parent.resolve() + while not "mpv_control" in app.config: + await None mpv_control: mpv.MPVControl = app.config["mpv_control"] playlist = await mpv_control.playlist_get() if len(playlist) == 0: diff --git a/grzegorz/mpv.py b/grzegorz/mpv.py index 5307212..5e914dc 100644 --- a/grzegorz/mpv.py +++ b/grzegorz/mpv.py @@ -13,7 +13,7 @@ from . import nyasync class MPV: # TODO: move this to /tmp or /var/run ? # TODO: make it configurable with an env variable? - _ipc_endpoint = Path("mpv_ipc.socket") + _ipc_endpoint = Path(f"mpv_ipc.socket") def __init__(self): self.requests = nyasync.Queue() @@ -48,6 +48,7 @@ class MPV: ) async def connect(self, *, timeout=10): + await asyncio.sleep(0.5) t = time.time() while self.is_running and time.time() - t < timeout: try: @@ -154,7 +155,7 @@ class MPVControl: "force", "on" ) - code = await process.wait() + code = await p.wait() #Shorthand command requests: