diff --git a/README.claude.md b/README.claude.md index f6f8cc4..7873df4 100644 --- a/README.claude.md +++ b/README.claude.md @@ -12,8 +12,10 @@ they come as separate streams). * [`yt-dlp`](https://pypi.org/project/yt-dlp/) and `ffmpeg` available on your `PATH` (`ffmpeg` is a system package, not a Python one — install it with your OS package manager, e.g. `apt install ffmpeg` / - `brew install ffmpeg`). -* Python packages from `requirements.txt`: + `brew install ffmpeg`). Needed regardless of how you run the app, + including the prebuilt installers below, since it's invoked as an + external command, never imported as a Python library. +* Python packages from `requirements.txt` (pins `textual==8.2.8`): ```bash pip install -r requirements.txt @@ -32,6 +34,15 @@ python main.py viewer. Works even while the Url/Name input is focused (it overrides `Input`'s own "select all" binding on that key). +## Installers + +For distributing the app without asking someone to clone the repo and +`pip install`, see [`packaging/README.md`](packaging/README.md): +`packaging/build_pyinstaller.sh` builds a single native executable with +Python bundled in, and `packaging/build_pex.sh` builds a small `.pex` +zipapp that needs a system `python3` but no `pip install` step. Both +still require `yt-dlp` and `ffmpeg` on `PATH` at runtime. + ## Tests ```bash @@ -62,6 +73,9 @@ pytest main.py Entry point; wires up i18n, then starts the app app/ tui_app.py The Textual App: layout + all screen wiring + runtime_paths.py Resolves data-file locations the same way for a + plain checkout, a PyInstaller build, and a pex + zipapp (see packaging/README.md) styles.tcss External stylesheet for the app and every modal screen (loaded via DownloadManagerApp.CSS_PATH) models.py FormatEntry / VideoInfo + the filtering logic @@ -81,6 +95,8 @@ i18n/en/, i18n/de/ This application's own translation data (strings.json + help.md), as expected by the library at /i18n// tests/ pytest suite (see "Tests" below) +packaging/ PyInstaller spec + build scripts for both + installers (see "Installers" above) ``` The i18n *library* is kept in `lib/i18n` (added to `sys.path` by diff --git a/app/runtime_paths.py b/app/runtime_paths.py new file mode 100644 index 0000000..72fb4ba --- /dev/null +++ b/app/runtime_paths.py @@ -0,0 +1,45 @@ +"""Resolve where the application's own files live at runtime. + +Three ways this app can run, all needing the *same* relative layout +(``i18n//...`` and ``app/styles.tcss`` next to ``main.py``) to be +discoverable as real files on disk: + +* **Plain source checkout** — ``python main.py``. Everything is exactly + where it looks in this repository. +* **PyInstaller onefile build** — the interpreter, bundled packages and + ``--add-data`` files are unpacked into a temporary directory at startup, + exposed as ``sys._MEIPASS``. Pure-Python modules (like this one) run + from a synthetic, non-existent path, but explicitly added data files + (``i18n/`` and ``app/styles.tcss``) are real files under ``_MEIPASS``. +* **pex zipapp** — pex extracts its bundled sources to a cache directory + on first run and imports them from there, so ``__file__``-based + resolution behaves exactly like the plain source checkout. + +:func:`bundle_root` returns the single directory to resolve both cases +against, so callers don't need to know which one they're running under. +""" + +from __future__ import annotations + +import sys +from pathlib import Path + + +def is_frozen() -> bool: + """True inside a PyInstaller-built executable.""" + return bool(getattr(sys, "frozen", False)) + + +def bundle_root() -> Path: + """Directory that contains ``i18n/`` and ``app/`` at runtime.""" + if is_frozen(): + meipass = getattr(sys, "_MEIPASS", None) + if meipass: + return Path(meipass) + # Onedir builds without _MEIPASS (shouldn't normally happen with + # the bundled spec, but fall back to the executable's directory). + return Path(sys.executable).resolve().parent + + # Plain source checkout or a pex zipapp: this file lives at + # /app/runtime_paths.py. + return Path(__file__).resolve().parent.parent diff --git a/app/tui_app.py b/app/tui_app.py index 0c4de6a..5a06b60 100644 --- a/app/tui_app.py +++ b/app/tui_app.py @@ -14,6 +14,7 @@ from i18n import I18n from . import ffmpeg_client, ytdlp_client from .models import FormatEntry, VideoInfo +from .runtime_paths import bundle_root from .screens import ErrorScreen, FormatSelectScreen, HelpScreen, LanguageSelectScreen @@ -22,7 +23,11 @@ class DownloadManagerApp(App[None]): # All widget styling (this screen plus every modal screen) lives in one # external stylesheet rather than scattered DEFAULT_CSS/CSS blocks. - CSS_PATH = "styles.tcss" + # Resolved via bundle_root() rather than a plain relative path so it + # keeps working from a PyInstaller build, where this module's __file__ + # is a synthetic path Textual can't open() directly — see + # runtime_paths.py. + CSS_PATH = str(bundle_root() / "app" / "styles.tcss") # `priority=True` makes these global: they fire even while an Input (or # any other widget with its own key bindings, e.g. Input's built-in diff --git a/main.py b/main.py index 187e612..54e803b 100644 --- a/main.py +++ b/main.py @@ -4,6 +4,10 @@ Sets up the bundled i18n library (see ``lib/i18n``) with the application's own translation data (in ``i18n//strings.json``) before starting the Textual application. + +Works unmodified from a plain source checkout, from a PyInstaller build, or +from a pex zipapp — see ``app/runtime_paths.py`` for how the base directory +is resolved in each case. """ from __future__ import annotations @@ -11,11 +15,18 @@ from __future__ import annotations import sys from pathlib import Path -PROJECT_ROOT = Path(__file__).resolve().parent +# main.py's own directory is already on sys.path (Python does this for the +# script it runs), so the local `app` package is importable immediately. +from app.runtime_paths import bundle_root + +PROJECT_ROOT = bundle_root() # The i18n library package lives in lib/, kept separate from the i18n/ # *data* directory (project_root/i18n//strings.json) that the library -# itself expects to find at the project root. +# itself expects to find at the project root. In a PyInstaller build this +# path doesn't exist on disk (the library's code is embedded directly in +# the bundle and importable regardless), so inserting it is a harmless +# no-op there. sys.path.insert(0, str(PROJECT_ROOT / "lib")) from i18n import I18n # noqa: E402 (import must follow sys.path setup) diff --git a/packaging/README.md b/packaging/README.md new file mode 100644 index 0000000..1582f26 --- /dev/null +++ b/packaging/README.md @@ -0,0 +1,81 @@ +# Installers + +Two ways to build a distributable bundle, both producing a single file +under `dist/`. Both bundle the app's own code, the i18n library, the i18n +translation data, and `app/styles.tcss`. Neither bundles `yt-dlp` or +`ffmpeg` — the app runs them as external command-line tools via +`subprocess`, never imports them as Python libraries, so **both still need +to be installed separately and be on `PATH`** on the machine that runs the +installer's output, regardless of which installer you use. + +| | `build_pyinstaller.sh` | `build_pex.sh` | +|---|---|---| +| Bundles a Python interpreter | Yes | No — needs a compatible `python3` already on the target machine | +| Bundles Textual 8.2.8 + deps | Yes | Yes | +| Output | `dist/ytdlp-tui` (native executable) | `dist/ytdlp-tui.pex` (zipapp) | +| Run it with | `./dist/ytdlp-tui` | `python3 dist/ytdlp-tui.pex` | +| Typical size | ~35-40 MB | ~3-4 MB | + +Both scripts create their own throwaway build `venv` (`.build-venv-*`, +already git-ignored) so they don't touch whatever environment you're +already using, and both read dependency versions from +`requirements-installer.txt` — the subset of `requirements.txt` the app +actually imports (just `textual==8.2.8`). `yt-dlp` is deliberately *not* +pip-installed into either bundle: even if it were, that would only add the +`yt_dlp` Python package inside the bundle, not put a `yt-dlp` executable on +the target machine's `PATH` — which is what the app's subprocess calls +actually need. + +## Build + +```bash +# Bundled-Python installer (PyInstaller) +packaging/build_pyinstaller.sh +./dist/ytdlp-tui + +# No-bundled-Python zipapp (pex) +packaging/build_pex.sh +python3 dist/ytdlp-tui.pex +``` + +Run either from the project root. Both are Bash scripts (Linux/macOS); on +Windows use WSL or adapt them to PowerShell — the underlying `pyinstaller`/ +`pex` invocations are unchanged. + +## How the data files travel + +`app/tui_app.py`'s `CSS_PATH` and `main.py`'s `PROJECT_ROOT` are both +resolved through `app/runtime_paths.bundle_root()` rather than a plain +`Path(__file__).parent`, because a frozen PyInstaller build's Python +modules run from inside an embedded archive with no real file on disk — +`__file__`-relative resolution would silently point at a path that +doesn't exist. `bundle_root()` returns: + +* `sys._MEIPASS` when running under a PyInstaller build (where + `--add-data` files are actually extracted to), or +* the directory containing `main.py` otherwise — true for a plain source + checkout, and also for a pex zipapp, since pex extracts its bundled + sources to a cache directory on first run and imports them from there + like a normal checkout. + +The PyInstaller spec (`packaging/pyinstaller/ytdlp-tui.spec`) explicitly +adds `i18n/` and `app/styles.tcss` as `datas` so they land next to +`_MEIPASS` at the same relative paths `bundle_root()` expects. It also +runs `collect_all("textual")` — without it, the frozen build fails the +first time a lazily-loaded widget (e.g. `MarkdownViewer`, opened via +Ctrl+A) is used, since Textual imports some of its own widgets on demand +through `__getattr__`, which PyInstaller's static analysis can't see. + +The pex build doesn't need any of this special-casing: `-D ` +bundles a plain copy of `main.py`, `app/`, `lib/`, `i18n/` verbatim +(code *and* data files alike), preserving the exact same relative layout +as a normal checkout. + +## Verifying a build + +Both scripts were smoke-tested end to end (not just "it starts"): run +under a pty, the full main screen renders with translated labels and the +external stylesheet applied, and `Ctrl+A` correctly opens the +`MarkdownViewer` showing `i18n/en/help.md`'s actual content — confirming +both the bundled stylesheet and the bundled translation/help data resolve +correctly in each packaging mode. diff --git a/packaging/build_pex.sh b/packaging/build_pex.sh new file mode 100755 index 0000000..02dc804 --- /dev/null +++ b/packaging/build_pex.sh @@ -0,0 +1,47 @@ +#!/usr/bin/env bash +# Build a self-contained Python zipapp (.pex) with Textual 8.2.8 and its +# other dependencies bundled, but WITHOUT a bundled Python interpreter — the +# target machine needs a compatible `python3` already on PATH. +# +# Usage: packaging/build_pex.sh +# Output: dist/ytdlp-tui.pex +# Run it with: python3 dist/ytdlp-tui.pex (or ./dist/ytdlp-tui.pex directly, +# since the shebang points at /usr/bin/env python3 on Unix) +set -euo pipefail + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" +cd "$PROJECT_ROOT" + +# pex bundles whatever's under the -D directory verbatim (code AND +# non-Python data files alike), so stage only the runtime files the app +# actually needs at runtime — not tests/, README.md, packaging/, etc. +STAGE="$(mktemp -d)" +trap 'rm -rf "$STAGE"' EXIT + +cp -r main.py app lib i18n "$STAGE"/ +find "$STAGE" -name "__pycache__" -exec rm -rf {} + 2>/dev/null || true + +VENV_DIR=".build-venv-pex" +python3 -m venv "$VENV_DIR" +# shellcheck disable=SC1091 +source "$VENV_DIR/bin/activate" + +pip install --upgrade pip -q +pip install "pex>=2.3" -q + +mkdir -p dist + +pex \ + -o dist/ytdlp-tui.pex \ + -D "$STAGE" \ + -r requirements-installer.txt \ + -e main:main \ + --python-shebang '/usr/bin/env python3' + +deactivate + +echo +echo "Built: dist/ytdlp-tui.pex" +echo "Run with: python3 dist/ytdlp-tui.pex" +echo "Note: needs a compatible python3 (and yt-dlp/ffmpeg on PATH) on the target machine — see packaging/README.md." diff --git a/packaging/build_pyinstaller.sh b/packaging/build_pyinstaller.sh new file mode 100755 index 0000000..3e4f80d --- /dev/null +++ b/packaging/build_pyinstaller.sh @@ -0,0 +1,28 @@ +#!/usr/bin/env bash +# Build a single-file, self-contained executable (bundled Python interpreter +# + Textual 8.2.8 + all deps + i18n data) using PyInstaller. +# +# Usage: packaging/build_pyinstaller.sh +# Output: dist/ytdlp-tui (or dist/ytdlp-tui.exe on Windows) +set -euo pipefail + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" +cd "$PROJECT_ROOT" + +VENV_DIR=".build-venv-pyinstaller" +python3 -m venv "$VENV_DIR" +# shellcheck disable=SC1091 +source "$VENV_DIR/bin/activate" + +pip install --upgrade pip -q +pip install -r requirements-installer.txt -q +pip install "pyinstaller>=6.0" -q + +pyinstaller --noconfirm --clean packaging/pyinstaller/ytdlp-tui.spec + +deactivate + +echo +echo "Built: dist/ytdlp-tui" +echo "Note: yt-dlp and ffmpeg are still required on PATH at runtime (see packaging/README.md)." diff --git a/pyproject.toml b/pyproject.toml index bc3fdb2..43867e6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -3,6 +3,8 @@ name = "ytdlp-tui" version = "0.1.0" requires-python = ">=3.12" dependencies = [ - "textual>=0.60", + "pytest>=8.0", + "pytest-asyncio>=0.24", + "textual==8.2.8", "yt-dlp>=2024.1.1", ] diff --git a/requirements-installer.txt b/requirements-installer.txt new file mode 100644 index 0000000..b2b23cb --- /dev/null +++ b/requirements-installer.txt @@ -0,0 +1,10 @@ +# Only the Python packages this app actually imports, used when building +# the PyInstaller/pex installers. yt-dlp and ffmpeg are invoked as external +# command-line tools via subprocess (never imported as Python libraries), so +# bundling the yt-dlp *pip package* here wouldn't put a `yt-dlp` binary on +# the target machine's PATH anyway — both still need to be installed +# separately regardless of which installer is used. See requirements.txt +# for the full dev-install set (which does include the yt-dlp pip package, +# purely as a convenient way to also get its console-script on PATH in a +# plain venv). +textual==8.2.8 diff --git a/requirements.txt b/requirements.txt index 40003a1..2dd5d3c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,2 @@ -textual>=0.60 +textual==8.2.8 yt-dlp>=2024.1.1 diff --git a/uv.lock b/uv.lock index e94929e..76dd700 100644 --- a/uv.lock +++ b/uv.lock @@ -2,6 +2,24 @@ version = 1 revision = 3 requires-python = ">=3.12" +[[package]] +name = "colorama" +version = "0.4.6" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/d8/53/6f443c9a4a8358a93a6792e2acffb9d9d5cb0a5cfd8802644b7b1c9a02e4/colorama-0.4.6.tar.gz", hash = "sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44", size = 27697, upload-time = "2022-10-25T02:36:22.414Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/d1/d6/3965ed04c63042e047cb6a3e6ed1a63a35087b6a609aa3a15ed8ac56c221/colorama-0.4.6-py2.py3-none-any.whl", hash = "sha256:4f1d9991f5acc0ca119f9d443620b77f9d6b33703e51011c16baf57afb285fc6", size = 25335, upload-time = "2022-10-25T02:36:20.889Z" }, +] + +[[package]] +name = "iniconfig" +version = "2.3.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/72/34/14ca021ce8e5dfedc35312d08ba8bf51fdd999c576889fc2c24cb97f4f10/iniconfig-2.3.0.tar.gz", hash = "sha256:c76315c77db068650d49c5b56314774a7804df16fee4402c1f19d6d15d8c4730", size = 20503, upload-time = "2025-10-18T21:55:43.219Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/cb/b1/3846dd7f199d53cb17f49cba7e651e9ce294d8497c8c150530ed11865bb8/iniconfig-2.3.0-py3-none-any.whl", hash = "sha256:f631c04d2c48c52b84d0d0549c99ff3859c98df65b3101406327ecc7d53fbf12", size = 7484, upload-time = "2025-10-18T21:55:41.639Z" }, +] + [[package]] name = "linkify-it-py" version = "2.2.0" @@ -49,6 +67,15 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/b3/38/89ba8ad64ae25be8de66a6d463314cf1eb366222074cfda9ee839c56a4b4/mdurl-0.1.2-py3-none-any.whl", hash = "sha256:84008a41e51615a49fc9966191ff91509e3c40b939176e643fd50a5c2196b8f8", size = 9979, upload-time = "2022-08-14T12:40:09.779Z" }, ] +[[package]] +name = "packaging" +version = "26.3" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/7d/fa/3944b40b07da9ce895c0e6303a5ab7d53da063554f534556b134a54d6093/packaging-26.3.tar.gz", hash = "sha256:94edc256424af38762eb31306eed28beb9f0efc50a8837492c9d6fd6004aed79", size = 313412, upload-time = "2026-08-04T18:15:28.737Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/63/34/ba1c580383c9eada3711951fef0795c80b829a078d72188184bcab9dd527/packaging-26.3-py3-none-any.whl", hash = "sha256:d7193f7c8e4e93f444fde0262bf90af30e16fa0ad0ad44cb553c87339b23cd1c", size = 129956, upload-time = "2026-08-04T18:15:27.159Z" }, +] + [[package]] name = "platformdirs" version = "4.11.11" @@ -58,6 +85,15 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/39/be/3ae887e84c38c4dd1c549cd5825adc93f48069a94fe1cb8ac52a9da16f15/platformdirs-4.11.11-py3-none-any.whl", hash = "sha256:972ea6b2b387155a536226a0750e46923d731d459a387c4a255c583ed2f64547", size = 24992, upload-time = "2026-09-19T01:18:45.825Z" }, ] +[[package]] +name = "pluggy" +version = "1.6.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/f9/e2/3e91f31a7d2b083fe6ef3fa267035b518369d9511ffab804f839851d2779/pluggy-1.6.0.tar.gz", hash = "sha256:7dcc130b76258d33b90f61b658791dede3486c3e6bfb003ee5c9bfb396dd22f3", size = 69412, upload-time = "2025-05-15T12:30:07.975Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/54/20/4d324d65cc6d9205fabedc306948156824eb9f0ee1633355a8f7ec5c66bf/pluggy-1.6.0-py3-none-any.whl", hash = "sha256:e920276dd6813095e9377c0bc5566d94c932c33b27a3e3945d8389c374dd4746", size = 20538, upload-time = "2025-05-15T12:30:06.134Z" }, +] + [[package]] name = "pygments" version = "2.21.0" @@ -67,6 +103,35 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/71/46/17f022dd3e953bf20a04a028a21ec746d942f8d2af30fa0f124fa0e6a684/pygments-2.21.0-py3-none-any.whl", hash = "sha256:2363c69b61c4a97c838da3b130dcd6468f4848992b21a82f2a63ec34377137d9", size = 1250147, upload-time = "2026-08-17T08:02:44.912Z" }, ] +[[package]] +name = "pytest" +version = "9.1.1" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "colorama", marker = "sys_platform == 'win32'" }, + { name = "iniconfig" }, + { name = "packaging" }, + { name = "pluggy" }, + { name = "pygments" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/e4/47/b9efed96c114afcfa3c9d3fe98a76a1d14c74a9e266d397cf6eb64be5e01/pytest-9.1.1.tar.gz", hash = "sha256:1088fbde8f2b49d95a549a195707afa7a76a3ce9bcadc26b6d71f0ffda5fe313", size = 1636369, upload-time = "2026-06-19T10:58:32.857Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/24/25/1de2678b631f5a49215c6c96fff41ba892b0a34df68d6d80292b1b48aa7f/pytest-9.1.1-py3-none-any.whl", hash = "sha256:37a86b45efb9a47a61a36449063e8e18d0cab3161329fc099eb21783169c4f0c", size = 386536, upload-time = "2026-06-19T10:58:31.347Z" }, +] + +[[package]] +name = "pytest-asyncio" +version = "1.4.0" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "pytest" }, + { name = "typing-extensions", marker = "python_full_version < '3.13'" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/43/7c/d36d04db312ecf4298932ef77e6e4a9e8ad017906e24e34f0b0c361a2473/pytest_asyncio-1.4.0.tar.gz", hash = "sha256:c6c0d2259945122819f171a32ecea2c349ead889ee28176caaf492143424be42", size = 58514, upload-time = "2026-05-26T09:56:04.083Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/03/e2/08a497ef684b88559c9cc5f4ad53a37e7b99e727094a86d6ea32536d5d3c/pytest_asyncio-1.4.0-py3-none-any.whl", hash = "sha256:933ca923a23075a87fb7070c0ec272a6848489824d887c85c812670932835aa1", size = 16930, upload-time = "2026-05-26T09:56:02.576Z" }, +] + [[package]] name = "rich" version = "15.0.0" @@ -120,12 +185,16 @@ name = "ytdlp-tui" version = "0.1.0" source = { virtual = "." } dependencies = [ + { name = "pytest" }, + { name = "pytest-asyncio" }, { name = "textual" }, { name = "yt-dlp" }, ] [package.metadata] requires-dist = [ - { name = "textual", specifier = ">=0.60" }, + { name = "pytest", specifier = ">=8.0" }, + { name = "pytest-asyncio", specifier = ">=0.24" }, + { name = "textual", specifier = "==8.2.8" }, { name = "yt-dlp", specifier = ">=2024.1.1" }, ]